@@ -1714,7 +1714,7 @@ p5.RendererGL = class RendererGL extends Renderer {
1714
1714
sphereMapping
1715
1715
) ;
1716
1716
}
1717
- this . uNMatrix . inverseTranspose ( this . uMVMatrix ) ;
1717
+ this . uNMatrix . inverseTranspose ( this . uViewMatrix ) ;
1718
1718
this . uNMatrix . invert3x3 ( this . uNMatrix ) ;
1719
1719
this . sphereMapping . setUniform ( 'uFovY' , this . _curCamera . cameraFOV ) ;
1720
1720
this . sphereMapping . setUniform ( 'uAspect' , this . _curCamera . aspectRatio ) ;
@@ -1785,6 +1785,15 @@ p5.RendererGL = class RendererGL extends Renderer {
1785
1785
return this . _getImmediateLineShader ( ) ;
1786
1786
}
1787
1787
1788
+ materialShader ( ) {
1789
+ if ( ! this . _pInst . _glAttributes . perPixelLighting ) {
1790
+ throw new Error (
1791
+ 'The material shader does not support hooks without perPixelLighting. Try turning it back on.'
1792
+ ) ;
1793
+ }
1794
+ return this . _getLightShader ( ) ;
1795
+ }
1796
+
1788
1797
_getLightShader ( ) {
1789
1798
if ( ! this . _defaultLightShader ) {
1790
1799
if ( this . _pInst . _glAttributes . perPixelLighting ) {
@@ -1793,7 +1802,34 @@ p5.RendererGL = class RendererGL extends Renderer {
1793
1802
this . _webGL2CompatibilityPrefix ( 'vert' , 'highp' ) +
1794
1803
defaultShaders . phongVert ,
1795
1804
this . _webGL2CompatibilityPrefix ( 'frag' , 'highp' ) +
1796
- defaultShaders . phongFrag
1805
+ defaultShaders . phongFrag ,
1806
+ {
1807
+ vertex : {
1808
+ 'void beforeVertex' : '() {}' ,
1809
+ 'vec3 getLocalPosition' : '(vec3 position) { return position; }' ,
1810
+ 'vec3 getWorldPosition' : '(vec3 position) { return position; }' ,
1811
+ 'vec3 getLocalNormal' : '(vec3 normal) { return normal; }' ,
1812
+ 'vec3 getWorldNormal' : '(vec3 normal) { return normal; }' ,
1813
+ 'vec2 getUV' : '(vec2 uv) { return uv; }' ,
1814
+ 'vec4 getVertexColor' : '(vec4 color) { return color; }' ,
1815
+ 'void afterVertex' : '() {}'
1816
+ } ,
1817
+ fragment : {
1818
+ 'void beforeFragment' : '() {}' ,
1819
+ 'Inputs getPixelInputs' : '(Inputs inputs) { return inputs; }' ,
1820
+ 'vec4 combineColors' : `(ColorComponents components) {
1821
+ vec4 color = vec4(0.);
1822
+ color.rgb += components.diffuse * components.baseColor;
1823
+ color.rgb += components.ambient * components.ambientColor;
1824
+ color.rgb += components.specular * components.specularColor;
1825
+ color.rgb += components.emissive;
1826
+ color.a = components.opacity;
1827
+ return color;
1828
+ }` ,
1829
+ 'vec4 getFinalColor' : '(vec4 color) { return color; }' ,
1830
+ 'void afterFragment' : '() {}'
1831
+ }
1832
+ }
1797
1833
) ;
1798
1834
} else {
1799
1835
this . _defaultLightShader = new p5 . Shader (
@@ -1823,55 +1859,163 @@ p5.RendererGL = class RendererGL extends Renderer {
1823
1859
return this . _defaultImmediateModeShader ;
1824
1860
}
1825
1861
1862
+ normalShader ( ) {
1863
+ return this . _getNormalShader ( ) ;
1864
+ }
1865
+
1826
1866
_getNormalShader ( ) {
1827
1867
if ( ! this . _defaultNormalShader ) {
1828
1868
this . _defaultNormalShader = new p5 . Shader (
1829
1869
this ,
1830
1870
this . _webGL2CompatibilityPrefix ( 'vert' , 'mediump' ) +
1831
1871
defaultShaders . normalVert ,
1832
1872
this . _webGL2CompatibilityPrefix ( 'frag' , 'mediump' ) +
1833
- defaultShaders . normalFrag
1873
+ defaultShaders . normalFrag ,
1874
+ {
1875
+ vertex : {
1876
+ 'void beforeVertex' : '() {}' ,
1877
+ 'vec3 getLocalPosition' : '(vec3 position) { return position; }' ,
1878
+ 'vec3 getWorldPosition' : '(vec3 position) { return position; }' ,
1879
+ 'vec3 getLocalNormal' : '(vec3 normal) { return normal; }' ,
1880
+ 'vec3 getWorldNormal' : '(vec3 normal) { return normal; }' ,
1881
+ 'vec2 getUV' : '(vec2 uv) { return uv; }' ,
1882
+ 'vec4 getVertexColor' : '(vec4 color) { return color; }' ,
1883
+ 'void afterVertex' : '() {}'
1884
+ } ,
1885
+ fragment : {
1886
+ 'void beforeFragment' : '() {}' ,
1887
+ 'vec4 getFinalColor' : '(vec4 color) { return color; }' ,
1888
+ 'void afterFragment' : '() {}'
1889
+ }
1890
+ }
1834
1891
) ;
1835
1892
}
1836
1893
1837
1894
return this . _defaultNormalShader ;
1838
1895
}
1839
1896
1897
+ colorShader ( ) {
1898
+ return this . _getColorShader ( ) ;
1899
+ }
1900
+
1840
1901
_getColorShader ( ) {
1841
1902
if ( ! this . _defaultColorShader ) {
1842
1903
this . _defaultColorShader = new p5 . Shader (
1843
1904
this ,
1844
1905
this . _webGL2CompatibilityPrefix ( 'vert' , 'mediump' ) +
1845
1906
defaultShaders . normalVert ,
1846
1907
this . _webGL2CompatibilityPrefix ( 'frag' , 'mediump' ) +
1847
- defaultShaders . basicFrag
1908
+ defaultShaders . basicFrag ,
1909
+ {
1910
+ vertex : {
1911
+ 'void beforeVertex' : '() {}' ,
1912
+ 'vec3 getLocalPosition' : '(vec3 position) { return position; }' ,
1913
+ 'vec3 getWorldPosition' : '(vec3 position) { return position; }' ,
1914
+ 'vec3 getLocalNormal' : '(vec3 normal) { return normal; }' ,
1915
+ 'vec3 getWorldNormal' : '(vec3 normal) { return normal; }' ,
1916
+ 'vec2 getUV' : '(vec2 uv) { return uv; }' ,
1917
+ 'vec4 getVertexColor' : '(vec4 color) { return color; }' ,
1918
+ 'void afterVertex' : '() {}'
1919
+ } ,
1920
+ fragment : {
1921
+ 'void beforeFragment' : '() {}' ,
1922
+ 'vec4 getFinalColor' : '(vec4 color) { return color; }' ,
1923
+ 'void afterFragment' : '() {}'
1924
+ }
1925
+ }
1848
1926
) ;
1849
1927
}
1850
1928
1851
1929
return this . _defaultColorShader ;
1852
1930
}
1853
1931
1932
+ /**
1933
+ * TODO(dave): un-private this when there is a way to actually override the
1934
+ * shader used for points
1935
+ *
1936
+ * Get the shader used when drawing points with <a href="#/p5/point">`point()`</a>.
1937
+ *
1938
+ * You can call <a href="#/p5.Shader/modify">`pointShader().modify()`</a>
1939
+ * and change any of the following hooks:
1940
+ * - `void beforeVertex`: Called at the start of the vertex shader.
1941
+ * - `vec3 getLocalPosition`: Update the position of vertices before transforms are applied. It takes in `vec3 position` and must return a modified version.
1942
+ * - `vec3 getWorldPosition`: Update the position of vertices after transforms are applied. It takes in `vec3 position` and pust return a modified version.
1943
+ * - `float getPointSize`: Update the size of the point. It takes in `float size` and must return a modified version.
1944
+ * - `void afterVertex`: Called at the end of the vertex shader.
1945
+ * - `void beforeFragment`: Called at the start of the fragment shader.
1946
+ * - `bool shouldDiscard`: Points are drawn inside a square, with the corners discarded in the fragment shader to create a circle. Use this to change this logic. It takes in a `bool willDiscard` and must return a modified version.
1947
+ * - `vec4 getFinalColor`: Update the final color after mixing. It takes in a `vec4 color` and must return a modified version.
1948
+ * - `void afterFragment`: Called at the end of the fragment shader.
1949
+ *
1950
+ * Call `pointShader().inspectHooks()` to see all the possible hooks and
1951
+ * their default implementations.
1952
+ *
1953
+ * @returns {p5.Shader } The `point()` shader
1954
+ * @private ()
1955
+ */
1956
+ pointShader ( ) {
1957
+ return this . _getPointShader ( ) ;
1958
+ }
1959
+
1854
1960
_getPointShader ( ) {
1855
1961
if ( ! this . _defaultPointShader ) {
1856
1962
this . _defaultPointShader = new p5 . Shader (
1857
1963
this ,
1858
1964
this . _webGL2CompatibilityPrefix ( 'vert' , 'mediump' ) +
1859
1965
defaultShaders . pointVert ,
1860
1966
this . _webGL2CompatibilityPrefix ( 'frag' , 'mediump' ) +
1861
- defaultShaders . pointFrag
1967
+ defaultShaders . pointFrag ,
1968
+ {
1969
+ vertex : {
1970
+ 'void beforeVertex' : '() {}' ,
1971
+ 'vec3 getLocalPosition' : '(vec3 position) { return position; }' ,
1972
+ 'vec3 getWorldPosition' : '(vec3 position) { return position; }' ,
1973
+ 'float getPointSize' : '(float size) { return size; }' ,
1974
+ 'void afterVertex' : '() {}'
1975
+ } ,
1976
+ fragment : {
1977
+ 'void beforeFragment' : '() {}' ,
1978
+ 'vec4 getFinalColor' : '(vec4 color) { return color; }' ,
1979
+ 'bool shouldDiscard' : '(bool outside) { return outside; }' ,
1980
+ 'void afterFragment' : '() {}'
1981
+ }
1982
+ }
1862
1983
) ;
1863
1984
}
1864
1985
return this . _defaultPointShader ;
1865
1986
}
1866
1987
1988
+ strokeShader ( ) {
1989
+ return this . _getLineShader ( ) ;
1990
+ }
1991
+
1867
1992
_getLineShader ( ) {
1868
1993
if ( ! this . _defaultLineShader ) {
1869
1994
this . _defaultLineShader = new p5 . Shader (
1870
1995
this ,
1871
1996
this . _webGL2CompatibilityPrefix ( 'vert' , 'mediump' ) +
1872
1997
defaultShaders . lineVert ,
1873
1998
this . _webGL2CompatibilityPrefix ( 'frag' , 'mediump' ) +
1874
- defaultShaders . lineFrag
1999
+ defaultShaders . lineFrag ,
2000
+ {
2001
+ vertex : {
2002
+ 'void beforeVertex' : '() {}' ,
2003
+ 'vec3 getLocalPosition' : '(vec3 position) { return position; }' ,
2004
+ 'vec3 getWorldPosition' : '(vec3 position) { return position; }' ,
2005
+ 'float getStrokeWeight' : '(float weight) { return weight; }' ,
2006
+ 'vec2 getLineCenter' : '(vec2 center) { return center; }' ,
2007
+ 'vec2 getLinePosition' : '(vec2 position) { return position; }' ,
2008
+ 'vec4 getVertexColor' : '(vec4 color) { return color; }' ,
2009
+ 'void afterVertex' : '() {}'
2010
+ } ,
2011
+ fragment : {
2012
+ 'void beforeFragment' : '() {}' ,
2013
+ 'Inputs getPixelInputs' : '(Inputs inputs) { return inputs; }' ,
2014
+ 'vec4 getFinalColor' : '(vec4 color) { return color; }' ,
2015
+ 'bool shouldDiscard' : '(bool outside) { return outside; }' ,
2016
+ 'void afterFragment' : '() {}'
2017
+ }
2018
+ }
1875
2019
) ;
1876
2020
}
1877
2021
@@ -2088,7 +2232,7 @@ p5.RendererGL = class RendererGL extends Renderer {
2088
2232
fillShader . setUniform ( 'uSpecular' , this . _useSpecularMaterial ) ;
2089
2233
fillShader . setUniform ( 'uEmissive' , this . _useEmissiveMaterial ) ;
2090
2234
fillShader . setUniform ( 'uShininess' , this . _useShininess ) ;
2091
- fillShader . setUniform ( 'metallic ' , this . _useMetalness ) ;
2235
+ fillShader . setUniform ( 'uMetallic ' , this . _useMetalness ) ;
2092
2236
2093
2237
this . _setImageLightUniforms ( fillShader ) ;
2094
2238
@@ -2161,14 +2305,7 @@ p5.RendererGL = class RendererGL extends Renderer {
2161
2305
let diffusedLight = this . getDiffusedTexture ( this . activeImageLight ) ;
2162
2306
shader . setUniform ( 'environmentMapDiffused' , diffusedLight ) ;
2163
2307
let specularLight = this . getSpecularTexture ( this . activeImageLight ) ;
2164
- // In p5js the range of shininess is >= 1,
2165
- // Therefore roughness range will be ([0,1]*8)*20 or [0, 160]
2166
- // The factor of 8 is because currently the getSpecularTexture
2167
- // only calculated 8 different levels of roughness
2168
- // The factor of 20 is just to spread up this range so that,
2169
- // [1, max] of shininess is converted to [0,160] of roughness
2170
- let roughness = 20 / this . _useShininess ;
2171
- shader . setUniform ( 'levelOfDetail' , roughness * 8 ) ;
2308
+
2172
2309
shader . setUniform ( 'environmentMapSpecular' , specularLight ) ;
2173
2310
}
2174
2311
}
0 commit comments