Skip to content

Commit 17304ce

Browse files
authored
Merge pull request #6503 from perminder-17/bluring
Solves issue #6502
2 parents c934fb9 + 2a50776 commit 17304ce

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

src/image/pixels.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ p5.prototype.filter = function(...args) {
614614
// dest coords
615615
-this.width/2, -this.height/2, this.width, this.height
616616
);
617-
617+
//clearing the main canvas
618+
this._renderer.clear();
618619
// filter it with shaders
619620
this.filterGraphicsLayer.filter(operation, value);
620621

src/webgl/p5.RendererGL.js

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ const defaultShaders = {
7676
lineDefs + readFileSync(join(__dirname, '/shaders/line.frag'), 'utf-8'),
7777
pointVert: readFileSync(join(__dirname, '/shaders/point.vert'), 'utf-8'),
7878
pointFrag: readFileSync(join(__dirname, '/shaders/point.frag'), 'utf-8'),
79-
imageLightVert : readFileSync(join(__dirname, '/shaders/imageLight.vert'), 'utf-8'),
80-
imageLightDiffusedFrag : readFileSync(join(__dirname, '/shaders/imageLightDiffused.frag'), 'utf-8'),
81-
imageLightSpecularFrag : readFileSync(join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8')
79+
imageLightVert: readFileSync(join(__dirname, '/shaders/imageLight.vert'), 'utf-8'),
80+
imageLightDiffusedFrag: readFileSync(join(__dirname, '/shaders/imageLightDiffused.frag'), 'utf-8'),
81+
imageLightSpecularFrag: readFileSync(join(__dirname, '/shaders/imageLightSpecular.frag'), 'utf-8')
8282
};
8383
for (const key in defaultShaders) {
8484
defaultShaders[key] = webgl2CompatibilityShader + defaultShaders[key];
@@ -538,6 +538,10 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
538538
this._curCamera._computeCameraDefaultSettings();
539539
this._curCamera._setDefaultCamera();
540540

541+
// FilterCamera
542+
this.filterCamera = new p5.Camera(this);
543+
this.filterCamera._computeCameraDefaultSettings();
544+
this.filterCamera._setDefaultCamera();
541545
// Information about the previous frame's touch object
542546
// for executing orbitControl()
543547
this.prevTouches = [];
@@ -1078,7 +1082,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
10781082
// Create and store shader for constants once on initial filter call.
10791083
// Need to store multiple in case user calls different filters,
10801084
// eg. filter(BLUR) then filter(GRAY)
1081-
if ( !(operation in this.defaultFilterShaders) ) {
1085+
if (!(operation in this.defaultFilterShaders)) {
10821086
this.defaultFilterShaders[operation] = new p5.Shader(
10831087
pg._renderer,
10841088
filterShaderVert,
@@ -1109,7 +1113,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
11091113
this._pInst.noStroke();
11101114

11111115
// draw main to temp buffer
1112-
tmp.image(this, -this.width/2, -this.height/2);
1116+
tmp.image(this, -this.width / 2, -this.height / 2);
11131117

11141118
pg.shader(this.filterShader);
11151119
this.filterShader.setUniform('texelSize', texelSize);
@@ -1119,15 +1123,18 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
11191123
// horiz pass
11201124
this.filterShader.setUniform('direction', [1, 0]);
11211125
this.filterShader.setUniform('tex0', tmp);
1122-
pg.rect(-this.width/2, -this.height/2, this.width, this.height);
1126+
pg.clear();
1127+
pg.rect(-this.width / 2, -this.height / 2, this.width, this.height);
11231128

11241129
// read back to temp buffer
1125-
tmp.image(pg, -this.width/2, -this.height/2);
1130+
tmp.clear();
1131+
tmp.image(pg, -this.width / 2, -this.height / 2);
11261132

11271133
// vert pass
11281134
this.filterShader.setUniform('direction', [0, 1]);
11291135
this.filterShader.setUniform('tex0', tmp);
1130-
pg.rect(-this.width/2, -this.height/2, this.width, this.height);
1136+
pg.clear();
1137+
pg.rect(-this.width / 2, -this.height / 2, this.width, this.height);
11311138

11321139
this._pInst.pop();
11331140
}
@@ -1140,15 +1147,24 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
11401147
// filterParameter uniform only used for POSTERIZE, and THRESHOLD
11411148
// but shouldn't hurt to always set
11421149
this.filterShader.setUniform('filterParameter', filterParameter);
1143-
pg.rect(-this.width/2, -this.height/2, this.width, this.height);
1150+
pg.rect(-this.width / 2, -this.height / 2, this.width, this.height);
11441151

11451152
}
11461153
// draw pg contents onto main renderer
11471154
this._pInst.push();
1155+
pg._pInst.resetMatrix();
11481156
this._pInst.noStroke();
1149-
this._pInst.image(pg, -this.width/2, -this.height/2,
1157+
pg._pInst.imageMode(constants.CORNER);
1158+
pg._pInst.blendMode(constants.BLEND);
1159+
this.clear();
1160+
this._pInst.push();
1161+
this.filterCamera._resize();
1162+
this._pInst.setCamera(this.filterCamera);
1163+
this._pInst.resetMatrix();
1164+
this._pInst.image(pg, -this.width / 2, -this.height / 2,
11501165
this.width, this.height);
11511166
this._pInst.pop();
1167+
this._pInst.pop();
11521168
}
11531169

11541170
blendMode(mode) {
@@ -1765,17 +1781,17 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
17651781
this._defaultLightShader = new p5.Shader(
17661782
this,
17671783
this._webGL2CompatibilityPrefix('vert', 'highp') +
1768-
defaultShaders.phongVert,
1784+
defaultShaders.phongVert,
17691785
this._webGL2CompatibilityPrefix('frag', 'highp') +
1770-
defaultShaders.phongFrag
1786+
defaultShaders.phongFrag
17711787
);
17721788
} else {
17731789
this._defaultLightShader = new p5.Shader(
17741790
this,
17751791
this._webGL2CompatibilityPrefix('vert', 'highp') +
1776-
defaultShaders.lightVert,
1792+
defaultShaders.lightVert,
17771793
this._webGL2CompatibilityPrefix('frag', 'highp') +
1778-
defaultShaders.lightTextureFrag
1794+
defaultShaders.lightTextureFrag
17791795
);
17801796
}
17811797
}
@@ -1788,9 +1804,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
17881804
this._defaultImmediateModeShader = new p5.Shader(
17891805
this,
17901806
this._webGL2CompatibilityPrefix('vert', 'mediump') +
1791-
defaultShaders.immediateVert,
1807+
defaultShaders.immediateVert,
17921808
this._webGL2CompatibilityPrefix('frag', 'mediump') +
1793-
defaultShaders.vertexColorFrag
1809+
defaultShaders.vertexColorFrag
17941810
);
17951811
}
17961812

@@ -1802,9 +1818,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
18021818
this._defaultNormalShader = new p5.Shader(
18031819
this,
18041820
this._webGL2CompatibilityPrefix('vert', 'mediump') +
1805-
defaultShaders.normalVert,
1821+
defaultShaders.normalVert,
18061822
this._webGL2CompatibilityPrefix('frag', 'mediump') +
1807-
defaultShaders.normalFrag
1823+
defaultShaders.normalFrag
18081824
);
18091825
}
18101826

@@ -1816,9 +1832,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
18161832
this._defaultColorShader = new p5.Shader(
18171833
this,
18181834
this._webGL2CompatibilityPrefix('vert', 'mediump') +
1819-
defaultShaders.normalVert,
1835+
defaultShaders.normalVert,
18201836
this._webGL2CompatibilityPrefix('frag', 'mediump') +
1821-
defaultShaders.basicFrag
1837+
defaultShaders.basicFrag
18221838
);
18231839
}
18241840

@@ -1830,9 +1846,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
18301846
this._defaultPointShader = new p5.Shader(
18311847
this,
18321848
this._webGL2CompatibilityPrefix('vert', 'mediump') +
1833-
defaultShaders.pointVert,
1849+
defaultShaders.pointVert,
18341850
this._webGL2CompatibilityPrefix('frag', 'mediump') +
1835-
defaultShaders.pointFrag
1851+
defaultShaders.pointFrag
18361852
);
18371853
}
18381854
return this._defaultPointShader;
@@ -1843,9 +1859,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
18431859
this._defaultLineShader = new p5.Shader(
18441860
this,
18451861
this._webGL2CompatibilityPrefix('vert', 'mediump') +
1846-
defaultShaders.lineVert,
1862+
defaultShaders.lineVert,
18471863
this._webGL2CompatibilityPrefix('frag', 'mediump') +
1848-
defaultShaders.lineFrag
1864+
defaultShaders.lineFrag
18491865
);
18501866
}
18511867

@@ -1919,9 +1935,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19191935
* Returns the blurry image
19201936
* maps a p5.Image used by imageLight() to a p5.Graphics
19211937
*/
1922-
getDiffusedTexture(input){
1938+
getDiffusedTexture(input) {
19231939
// if one already exists for a given input image
1924-
if(this.diffusedTextures.get(input)!=null){
1940+
if (this.diffusedTextures.get(input) != null) {
19251941
return this.diffusedTextures.get(input);
19261942
}
19271943
// if not, only then create one
@@ -1955,18 +1971,18 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19551971
* Storing the texture for input image in map called `specularTextures`
19561972
* maps the input p5.Image to a p5.MipmapTexture
19571973
*/
1958-
getSpecularTexture(input){
1974+
getSpecularTexture(input) {
19591975
// check if already exits (there are tex of diff resolution so which one to check)
19601976
// currently doing the whole array
1961-
if(this.specularTextures.get(input)!=null){
1977+
if (this.specularTextures.get(input) != null) {
19621978
return this.specularTextures.get(input);
19631979
}
19641980
// Hardcoded size
19651981
const size = 512;
19661982
let tex;
19671983
const levels = [];
19681984
const graphic = this._pInst.createGraphics(size, size, constants.WEBGL);
1969-
let count = Math.log(size)/Math.log(2);
1985+
let count = Math.log(size) / Math.log(2);
19701986
graphic.pixelDensity(1);
19711987
// currently only 8 levels
19721988
// This loop calculates 8 graphics of varying size of canvas
@@ -1975,8 +1991,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19751991
// because rougher surfaces have less detailed/more blurry reflections.
19761992
for (let w = size; w >= 1; w /= 2) {
19771993
graphic.resizeCanvas(w, w);
1978-
let currCount = Math.log(w)/Math.log(2);
1979-
let roughness = 1-currCount/count;
1994+
let currCount = Math.log(w) / Math.log(2);
1995+
let roughness = 1 - currCount / count;
19801996
let myShader = graphic.createShader(
19811997
defaultShaders.imageLightVert,
19821998
defaultShaders.imageLightSpecularFrag
@@ -1991,7 +2007,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
19912007
}
19922008
graphic.remove();
19932009
tex = new MipmapTexture(this, levels, {});
1994-
this.specularTextures.set(input,tex);
2010+
this.specularTextures.set(input, tex);
19952011
return tex;
19962012
}
19972013

@@ -2093,9 +2109,9 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
20932109
}
20942110

20952111
// getting called from _setFillUniforms
2096-
_setImageLightUniforms(shader){
2112+
_setImageLightUniforms(shader) {
20972113
//set uniform values
2098-
shader.setUniform('uUseImageLight', this.activeImageLight != null );
2114+
shader.setUniform('uUseImageLight', this.activeImageLight != null);
20992115
// true
21002116
if (this.activeImageLight) {
21012117
// this.activeImageLight has image as a key
@@ -2109,8 +2125,8 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
21092125
// only calculated 8 different levels of roughness
21102126
// The factor of 20 is just to spread up this range so that,
21112127
// [1, max] of shininess is converted to [0,160] of roughness
2112-
let roughness = 20/this._useShininess;
2113-
shader.setUniform('levelOfDetail', roughness*8);
2128+
let roughness = 20 / this._useShininess;
2129+
shader.setUniform('levelOfDetail', roughness * 8);
21142130
shader.setUniform('environmentMapSpecular', specularLight);
21152131
}
21162132
}

src/webgl/shaders/filters/invert.frag

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ varying vec2 vTexCoord;
88
uniform sampler2D tex0;
99

1010
void main() {
11-
vec4 color = texture2D(tex0, vTexCoord);
12-
vec3 invertedColor = 1.0 - color.rgb;
13-
gl_FragColor = vec4(invertedColor, color.a);
11+
vec4 color = texture2D(tex0, vTexCoord);
12+
vec3 origColor = color.rgb / color.a;
13+
vec3 invertedColor = vec3(1.0) - origColor;
14+
gl_FragColor = vec4(invertedColor * color.a, color.a);
1415
}

0 commit comments

Comments
 (0)