@@ -480,10 +480,10 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
480
480
this . activeImageLight = null ;
481
481
// If activeImageLight property is Null, diffusedTextures,
482
482
// specularTextures are Empty.
483
- // Else, it maps a p5.Image used by imageLight() to a p5.Graphics .
484
- // p5.Graphics for this are calculated in getDiffusedTexture function
483
+ // Else, it maps a p5.Image used by imageLight() to a p5.framebuffer .
484
+ // p5.framebuffer for this are calculated in getDiffusedTexture function
485
485
this . diffusedTextures = new Map ( ) ;
486
- // p5.Graphics for this are calculated in getSpecularTexture function
486
+ // p5.framebuffer for this are calculated in getSpecularTexture function
487
487
this . specularTextures = new Map ( ) ;
488
488
489
489
this . drawMode = constants . FILL ;
@@ -553,6 +553,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
553
553
this . executeZoom = false ;
554
554
this . executeRotateAndMove = false ;
555
555
556
+ this . specularShader = undefined ;
557
+ this . diffusedShader = undefined ;
556
558
this . _defaultLightShader = undefined ;
557
559
this . _defaultImmediateModeShader = undefined ;
558
560
this . _defaultNormalShader = undefined ;
@@ -1917,33 +1919,40 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
1917
1919
* To create a blurry image from the input non blurry img, if it doesn't already exist
1918
1920
* Add it to the diffusedTexture map,
1919
1921
* Returns the blurry image
1920
- * maps a p5.Image used by imageLight() to a p5.Graphics
1922
+ * maps a p5.Image used by imageLight() to a p5.Framebuffer
1921
1923
*/
1922
1924
getDiffusedTexture ( input ) {
1923
1925
// if one already exists for a given input image
1924
1926
if ( this . diffusedTextures . get ( input ) != null ) {
1925
1927
return this . diffusedTextures . get ( input ) ;
1926
1928
}
1927
1929
// if not, only then create one
1928
- let newGraphic ; // maybe switch to framebuffer
1930
+ let newFramebuffer ;
1929
1931
// hardcoded to 200px, because it's going to be blurry and smooth
1930
1932
let smallWidth = 200 ;
1931
1933
let width = smallWidth ;
1932
1934
let height = Math . floor ( smallWidth * ( input . height / input . width ) ) ;
1933
- newGraphic = this . _pInst . createGraphics ( width , height , constants . WEBGL ) ;
1934
- // create graphics is like making a new sketch, all functions on main
1935
- // sketch it would be available on graphics
1936
- let irradiance = newGraphic . createShader (
1937
- defaultShaders . imageLightVert ,
1938
- defaultShaders . imageLightDiffusedFrag
1939
- ) ;
1940
- newGraphic . shader ( irradiance ) ;
1941
- irradiance . setUniform ( 'environmentMap' , input ) ;
1942
- newGraphic . noStroke ( ) ;
1943
- newGraphic . rectMode ( newGraphic . CENTER ) ;
1944
- newGraphic . rect ( 0 , 0 , newGraphic . width , newGraphic . height ) ;
1945
- this . diffusedTextures . set ( input , newGraphic ) ;
1946
- return newGraphic ;
1935
+ newFramebuffer = this . _pInst . createFramebuffer ( {
1936
+ width, height, density : 1
1937
+ } ) ;
1938
+ // create framebuffer is like making a new sketch, all functions on main
1939
+ // sketch it would be available on framebuffer
1940
+ if ( ! this . diffusedShader ) {
1941
+ this . diffusedShader = this . _pInst . createShader (
1942
+ defaultShaders . imageLightVert ,
1943
+ defaultShaders . imageLightDiffusedFrag
1944
+ ) ;
1945
+ }
1946
+ newFramebuffer . draw ( ( ) => {
1947
+ this . _pInst . shader ( this . diffusedShader ) ;
1948
+ this . diffusedShader . setUniform ( 'environmentMap' , input ) ;
1949
+ this . _pInst . noStroke ( ) ;
1950
+ this . _pInst . rectMode ( constants . CENTER ) ;
1951
+ this . _pInst . noLights ( ) ;
1952
+ this . _pInst . rect ( 0 , 0 , width , height ) ;
1953
+ } ) ;
1954
+ this . diffusedTextures . set ( input , newFramebuffer ) ;
1955
+ return newFramebuffer ;
1947
1956
}
1948
1957
1949
1958
/*
@@ -1965,31 +1974,38 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
1965
1974
const size = 512 ;
1966
1975
let tex ;
1967
1976
const levels = [ ] ;
1968
- const graphic = this . _pInst . createGraphics ( size , size , constants . WEBGL ) ;
1977
+ const framebuffer = this . _pInst . createFramebuffer ( {
1978
+ width : size , height : size , density : 1
1979
+ } ) ;
1969
1980
let count = Math . log ( size ) / Math . log ( 2 ) ;
1970
- graphic . pixelDensity ( 1 ) ;
1981
+ if ( ! this . specularShader ) {
1982
+ this . specularShader = this . _pInst . createShader (
1983
+ defaultShaders . imageLightVert ,
1984
+ defaultShaders . imageLightSpecularFrag
1985
+ ) ;
1986
+ }
1971
1987
// currently only 8 levels
1972
- // This loop calculates 8 graphics of varying size of canvas
1988
+ // This loop calculates 8 framebuffers of varying size of canvas
1973
1989
// and corresponding different roughness levels.
1974
1990
// Roughness increases with the decrease in canvas size,
1975
1991
// because rougher surfaces have less detailed/more blurry reflections.
1976
1992
for ( let w = size ; w >= 1 ; w /= 2 ) {
1977
- graphic . resizeCanvas ( w , w ) ;
1993
+ framebuffer . resize ( w , w ) ;
1978
1994
let currCount = Math . log ( w ) / Math . log ( 2 ) ;
1979
1995
let roughness = 1 - currCount / count ;
1980
- let myShader = graphic . createShader (
1981
- defaultShaders . imageLightVert ,
1982
- defaultShaders . imageLightSpecularFrag
1983
- ) ;
1984
- graphic . shader ( myShader ) ;
1985
- graphic . clear ( ) ;
1986
- myShader . setUniform ( 'environmentMap' , input ) ;
1987
- myShader . setUniform ( 'roughness' , roughness ) ;
1988
- graphic . noStroke ( ) ;
1989
- graphic . plane ( w , w ) ;
1990
- levels . push ( graphic . get ( ) . drawingContext . getImageData ( 0 , 0 , w , w ) ) ;
1991
- }
1992
- graphic . remove ( ) ;
1996
+ framebuffer . draw ( ( ) => {
1997
+ this . _pInst . shader ( this . specularShader ) ;
1998
+ this . _pInst . clear ( ) ;
1999
+ this . specularShader . setUniform ( 'environmentMap' , input ) ;
2000
+ this . specularShader . setUniform ( 'roughness' , roughness ) ;
2001
+ this . _pInst . noStroke ( ) ;
2002
+ this . _pInst . noLights ( ) ;
2003
+ this . _pInst . plane ( w , w ) ;
2004
+ } ) ;
2005
+ levels . push ( framebuffer . get ( ) . drawingContext . getImageData ( 0 , 0 , w , w ) ) ;
2006
+ }
2007
+ // Free the Framebuffer
2008
+ framebuffer . remove ( ) ;
1993
2009
tex = new MipmapTexture ( this , levels , { } ) ;
1994
2010
this . specularTextures . set ( input , tex ) ;
1995
2011
return tex ;
0 commit comments