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