Skip to content

Commit 522d2c2

Browse files
authored
Merge pull request #6599 from perminder-17/image-framebuffer
Image light performance improvements by implementing Framebuffers instead of Graphics.
2 parents 07d5b98 + 12d67f0 commit 522d2c2

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

src/webgl/light.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ p5.prototype.noLights = function(...args) {
11441144
this._assert3d('noLights');
11451145
p5._validateParameters('noLights', args);
11461146

1147+
this._renderer.activeImageLight = null;
11471148
this._renderer._enableLighting = false;
11481149

11491150
this._renderer.ambientLightColors.length = 0;

src/webgl/p5.RendererGL.js

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
480480
this.activeImageLight = null;
481481
// If activeImageLight property is Null, diffusedTextures,
482482
// 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
485485
this.diffusedTextures = new Map();
486-
// p5.Graphics for this are calculated in getSpecularTexture function
486+
// p5.framebuffer for this are calculated in getSpecularTexture function
487487
this.specularTextures = new Map();
488488

489489
this.drawMode = constants.FILL;
@@ -553,6 +553,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
553553
this.executeZoom = false;
554554
this.executeRotateAndMove = false;
555555

556+
this.specularShader = undefined;
557+
this.diffusedShader = undefined;
556558
this._defaultLightShader = undefined;
557559
this._defaultImmediateModeShader = undefined;
558560
this._defaultNormalShader = undefined;
@@ -1917,33 +1919,40 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19171919
* To create a blurry image from the input non blurry img, if it doesn't already exist
19181920
* Add it to the diffusedTexture map,
19191921
* 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
19211923
*/
19221924
getDiffusedTexture(input) {
19231925
// if one already exists for a given input image
19241926
if (this.diffusedTextures.get(input) != null) {
19251927
return this.diffusedTextures.get(input);
19261928
}
19271929
// if not, only then create one
1928-
let newGraphic; // maybe switch to framebuffer
1930+
let newFramebuffer;
19291931
// hardcoded to 200px, because it's going to be blurry and smooth
19301932
let smallWidth = 200;
19311933
let width = smallWidth;
19321934
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;
19471956
}
19481957

19491958
/*
@@ -1965,31 +1974,38 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19651974
const size = 512;
19661975
let tex;
19671976
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+
});
19691980
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+
}
19711987
// 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
19731989
// and corresponding different roughness levels.
19741990
// Roughness increases with the decrease in canvas size,
19751991
// because rougher surfaces have less detailed/more blurry reflections.
19761992
for (let w = size; w >= 1; w /= 2) {
1977-
graphic.resizeCanvas(w, w);
1993+
framebuffer.resize(w, w);
19781994
let currCount = Math.log(w) / Math.log(2);
19791995
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();
19932009
tex = new MipmapTexture(this, levels, {});
19942010
this.specularTextures.set(input, tex);
19952011
return tex;

0 commit comments

Comments
 (0)