Skip to content

Commit

Permalink
Handle default parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Dec 17, 2024
1 parent 61754a0 commit e4e020e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/image/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as constants from '../core/constants';
export const filterParamDefaults = {
[constants.BLUR]: 3,
[constants.POSTERIZE]: 4,
[constants.THRESHOLD]: 0.5,
};
29 changes: 24 additions & 5 deletions src/image/filterRenderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import filterOpaqueFrag from '../webgl/shaders/filters/opaque.frag';
import filterInvertFrag from '../webgl/shaders/filters/invert.frag';
import filterThresholdFrag from '../webgl/shaders/filters/threshold.frag';
import filterShaderVert from '../webgl/shaders/filters/default.vert';
import { filterParamDefaults } from "./const";

class FilterRenderer2D {
/**
Expand Down Expand Up @@ -99,6 +100,12 @@ class FilterRenderer2D {
setOperation(operation, filterParameter, customShader = null) {
this.operation = operation;
this.filterParameter = filterParameter;

let useDefaultParam = operation in filterParamDefaults && filterParameter === undefined;
if (useDefaultParam) {
this.filterParameter = filterParamDefaults[operation];
}

this.customShader = customShader;
this._initializeShader();
}
Expand Down Expand Up @@ -148,6 +155,13 @@ class FilterRenderer2D {
gl.bufferData(target, values, gl.STATIC_DRAW);
}

get canvasTexture() {
if (!this._canvasTexture) {
this._canvasTexture = new Texture(this._renderer, this.pInst.wrappedElt);
}
return this._canvasTexture;
}

/**
* Prepares and runs the full-screen quad draw call.
*/
Expand All @@ -161,7 +175,7 @@ class FilterRenderer2D {
1 / (this.pInst.height * pixelDensity)
];

const canvasTexture = new Texture(this._renderer, this.pInst.wrappedElt);
const canvasTexture = this.canvasTexture;

// Set uniforms for the shader
this._shader.setUniform('tex0', canvasTexture);
Expand All @@ -186,15 +200,17 @@ class FilterRenderer2D {
// Bind and enable vertex attributes
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
this._shader.enableAttrib(this._shader.attributes.aPosition, 2);

gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer);
this._shader.enableAttrib(this._shader.attributes.aTexCoord, 2);


this._shader.bindTextures();
this._shader.disableRemainingAttributes();

// Draw the quad
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
// Unbind the shader
this._shader.unbindShader();

}

/**
Expand All @@ -206,6 +222,8 @@ class FilterRenderer2D {
console.error("Cannot apply filter: shader not initialized.");
return;
}
this.pInst.push();
this.pInst.resetMatrix();
// For blur, we typically do two passes: one horizontal, one vertical.
if (this.operation === constants.BLUR && !this.customShader) {
// Horizontal pass
Expand All @@ -230,9 +248,10 @@ class FilterRenderer2D {
// con
this.pInst.blendMode(constants.BLEND);


this.pInst.drawingContext.drawImage(this.canvas, 0, 0, this.pInst.width, this.pInst.height);
}
this.pInst.pop();
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/image/pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,18 +749,16 @@ function pixels(p5, fn){
if (this._renderer.isP3D) {
this._renderer.filter(operation, value);
}

// when this is P2D renderer, create/use hidden webgl renderer
else {

this._renderer.resetMatrix();
// when this is P2D renderer, create/use hidden webgl renderer
else {

if (shader) {
this._renderer.filterRenderer.setOperation(operation, value, shader);
} else {
this._renderer.filterRenderer.setOperation(operation, value);
}

this._renderer.filterRenderer.applyFilter();
}
};
Expand Down Expand Up @@ -1163,4 +1161,4 @@ export default pixels;

if(typeof p5 !== 'undefined'){
pixels(p5, p5.prototype);
}
}
19 changes: 7 additions & 12 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Graphics } from "../core/p5.Graphics";
import { Element } from "../dom/p5.Element";
import { ShapeBuilder } from "./ShapeBuilder";
import { GeometryBufferCache } from "./GeometryBufferCache";
import { filterParamDefaults } from '../image/const';

import lightingShader from "./shaders/lighting.glsl";
import webgl2CompatibilityShader from "./shaders/webgl2Compatibility.glsl";
Expand Down Expand Up @@ -1150,13 +1151,8 @@ class RendererGL extends Renderer {
let operation = undefined;
if (typeof args[0] === "string") {
operation = args[0];
let defaults = {
[constants.BLUR]: 3,
[constants.POSTERIZE]: 4,
[constants.THRESHOLD]: 0.5,
};
let useDefaultParam = operation in defaults && args[1] === undefined;
filterParameter = useDefaultParam ? defaults[operation] : args[1];
let useDefaultParam = operation in filterParamDefaults && args[1] === undefined;
filterParameter = useDefaultParam ? filterParamDefaults[operation] : args[1];

// Create and store shader for constants once on initial filter call.
// Need to store multiple in case user calls different filters,
Expand Down Expand Up @@ -1189,11 +1185,6 @@ class RendererGL extends Renderer {
1 / (target.height * target.pixelDensity()),
];

this.blendMode(constants.BLEND);
this.states.rectMode = constants.CORNER;
this.states.imageMode = constants.CORNER;


// apply blur shader with multiple passes.
if (operation === constants.BLUR) {
// Treating 'tmp' as a framebuffer.
Expand All @@ -1203,6 +1194,7 @@ class RendererGL extends Renderer {
// setup
this.push();
this.states.strokeColor = null;
this.blendMode(constants.BLEND);

// draw main to temp buffer
this.shader(this.states.filterShader);
Expand Down Expand Up @@ -1242,6 +1234,7 @@ class RendererGL extends Renderer {
else {
fbo.draw(() => {
this.states.strokeColor = null;
this.blendMode(constants.BLEND);
this.shader(this.states.filterShader);
this.states.filterShader.setUniform("tex0", target);
this.states.filterShader.setUniform("texelSize", texelSize);
Expand All @@ -1261,6 +1254,8 @@ class RendererGL extends Renderer {
this.states.strokeColor = null;
this.clear();
this.push();
this.states.imageMode = constants.CORNER;
this.blendMode(constants.BLEND);
target.filterCamera._resize();
this.setCamera(target.filterCamera);
this.resetMatrix();
Expand Down
2 changes: 1 addition & 1 deletion src/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Texture {
// flag for update in a future frame.
// if we don't do this, a paused video, for example, may not
// send the first frame to texture memory.
data.setModified(true);
data.setModified && data.setModified(true);
}
} else if (this.isSrcP5Image) {
// for an image, we only update if the modified field has been set,
Expand Down

0 comments on commit e4e020e

Please sign in to comment.