|
| 1 | +import { Shader } from "../webgl/p5.Shader"; |
| 2 | +import { Texture } from "../webgl/p5.Texture"; |
| 3 | +import { Image } from "./p5.Image"; |
| 4 | +import * as constants from '../core/constants'; |
| 5 | + |
| 6 | +import filterGrayFrag from '../webgl/shaders/filters/gray.frag'; |
| 7 | +import filterErodeFrag from '../webgl/shaders/filters/erode.frag'; |
| 8 | +import filterDilateFrag from '../webgl/shaders/filters/dilate.frag'; |
| 9 | +import filterBlurFrag from '../webgl/shaders/filters/blur.frag'; |
| 10 | +import filterPosterizeFrag from '../webgl/shaders/filters/posterize.frag'; |
| 11 | +import filterOpaqueFrag from '../webgl/shaders/filters/opaque.frag'; |
| 12 | +import filterInvertFrag from '../webgl/shaders/filters/invert.frag'; |
| 13 | +import filterThresholdFrag from '../webgl/shaders/filters/threshold.frag'; |
| 14 | +import filterShaderVert from '../webgl/shaders/filters/default.vert'; |
| 15 | +import { filterParamDefaults } from "./const"; |
| 16 | + |
| 17 | +class FilterRenderer2D { |
| 18 | + /** |
| 19 | + * Creates a new FilterRenderer2D instance. |
| 20 | + * @param {p5} pInst - The p5.js instance. |
| 21 | + */ |
| 22 | + constructor(pInst) { |
| 23 | + this.pInst = pInst; |
| 24 | + // Create a canvas for applying WebGL-based filters |
| 25 | + this.canvas = document.createElement('canvas'); |
| 26 | + this.canvas.width = pInst.width; |
| 27 | + this.canvas.height = pInst.height; |
| 28 | + |
| 29 | + // Initialize the WebGL context |
| 30 | + this.gl = this.canvas.getContext('webgl'); |
| 31 | + if (!this.gl) { |
| 32 | + console.error("WebGL not supported, cannot apply filter."); |
| 33 | + return; |
| 34 | + } |
| 35 | + // Minimal renderer object required by p5.Shader and p5.Texture |
| 36 | + this._renderer = { |
| 37 | + GL: this.gl, |
| 38 | + registerEnabled: new Set(), |
| 39 | + _curShader: null, |
| 40 | + _emptyTexture: null, |
| 41 | + webglVersion: 'WEBGL', |
| 42 | + states: { |
| 43 | + textureWrapX: this.gl.CLAMP_TO_EDGE, |
| 44 | + textureWrapY: this.gl.CLAMP_TO_EDGE, |
| 45 | + }, |
| 46 | + _arraysEqual: (a, b) => JSON.stringify(a) === JSON.stringify(b), |
| 47 | + _getEmptyTexture: () => { |
| 48 | + if (!this._emptyTexture) { |
| 49 | + const im = new Image(1, 1); |
| 50 | + im.set(0, 0, 255); |
| 51 | + this._emptyTexture = new Texture(this._renderer, im); |
| 52 | + } |
| 53 | + return this._emptyTexture; |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + // Store the fragment shader sources |
| 58 | + this.filterShaderSources = { |
| 59 | + [constants.BLUR]: filterBlurFrag, |
| 60 | + [constants.INVERT]: filterInvertFrag, |
| 61 | + [constants.THRESHOLD]: filterThresholdFrag, |
| 62 | + [constants.ERODE]: filterErodeFrag, |
| 63 | + [constants.GRAY]: filterGrayFrag, |
| 64 | + [constants.DILATE]: filterDilateFrag, |
| 65 | + [constants.POSTERIZE]: filterPosterizeFrag, |
| 66 | + [constants.OPAQUE]: filterOpaqueFrag, |
| 67 | + }; |
| 68 | + |
| 69 | + // Store initialized shaders for each operation |
| 70 | + this.filterShaders = {}; |
| 71 | + |
| 72 | + // These will be set by setOperation |
| 73 | + this.operation = null; |
| 74 | + this.filterParameter = 1; |
| 75 | + this.customShader = null; |
| 76 | + this._shader = null; |
| 77 | + |
| 78 | + // Create buffers once |
| 79 | + this.vertexBuffer = this.gl.createBuffer(); |
| 80 | + this.texcoordBuffer = this.gl.createBuffer(); |
| 81 | + |
| 82 | + // Set up the vertices and texture coordinates for a full-screen quad |
| 83 | + this.vertices = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]); |
| 84 | + this.texcoords = new Float32Array([0, 1, 1, 1, 0, 0, 1, 0]); |
| 85 | + |
| 86 | + // Upload vertex data once |
| 87 | + this._bindBufferData(this.vertexBuffer, this.gl.ARRAY_BUFFER, this.vertices); |
| 88 | + |
| 89 | + // Upload texcoord data once |
| 90 | + this._bindBufferData(this.texcoordBuffer, this.gl.ARRAY_BUFFER, this.texcoords); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set the current filter operation and parameter. If a customShader is provided, |
| 95 | + * that overrides the operation-based shader. |
| 96 | + * @param {string} operation - The filter operation type (e.g., constants.BLUR). |
| 97 | + * @param {number} filterParameter - The strength of the filter. |
| 98 | + * @param {p5.Shader} customShader - Optional custom shader. |
| 99 | + */ |
| 100 | + setOperation(operation, filterParameter, customShader = null) { |
| 101 | + this.operation = operation; |
| 102 | + this.filterParameter = filterParameter; |
| 103 | + |
| 104 | + let useDefaultParam = operation in filterParamDefaults && filterParameter === undefined; |
| 105 | + if (useDefaultParam) { |
| 106 | + this.filterParameter = filterParamDefaults[operation]; |
| 107 | + } |
| 108 | + |
| 109 | + this.customShader = customShader; |
| 110 | + this._initializeShader(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Initializes or retrieves the shader program for the current operation. |
| 115 | + * If a customShader is provided, that is used. |
| 116 | + * Otherwise, returns a cached shader if available, or creates a new one, caches it, and sets it as current. |
| 117 | + */ |
| 118 | + _initializeShader() { |
| 119 | + if (this.customShader) { |
| 120 | + this._shader = this.customShader; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (!this.operation) { |
| 125 | + console.error("No operation set for FilterRenderer2D, cannot initialize shader."); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // If we already have a compiled shader for this operation, reuse it |
| 130 | + if (this.filterShaders[this.operation]) { |
| 131 | + this._shader = this.filterShaders[this.operation]; |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + const fragShaderSrc = this.filterShaderSources[this.operation]; |
| 136 | + if (!fragShaderSrc) { |
| 137 | + console.error("No shader available for this operation:", this.operation); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // Create and store the new shader |
| 142 | + const newShader = new Shader(this._renderer, filterShaderVert, fragShaderSrc); |
| 143 | + this.filterShaders[this.operation] = newShader; |
| 144 | + this._shader = newShader; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Binds a buffer to the drawing context |
| 149 | + * when passed more than two arguments it also updates or initializes |
| 150 | + * the data associated with the buffer |
| 151 | + */ |
| 152 | + _bindBufferData(buffer, target, values) { |
| 153 | + const gl = this.gl; |
| 154 | + gl.bindBuffer(target, buffer); |
| 155 | + gl.bufferData(target, values, gl.STATIC_DRAW); |
| 156 | + } |
| 157 | + |
| 158 | + get canvasTexture() { |
| 159 | + if (!this._canvasTexture) { |
| 160 | + this._canvasTexture = new Texture(this._renderer, this.pInst.wrappedElt); |
| 161 | + } |
| 162 | + return this._canvasTexture; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Prepares and runs the full-screen quad draw call. |
| 167 | + */ |
| 168 | + _renderPass() { |
| 169 | + const gl = this.gl; |
| 170 | + this._shader.bindShader(); |
| 171 | + const pixelDensity = this.pInst.pixelDensity ? this.pInst.pixelDensity() : 1; |
| 172 | + |
| 173 | + const texelSize = [ |
| 174 | + 1 / (this.pInst.width * pixelDensity), |
| 175 | + 1 / (this.pInst.height * pixelDensity) |
| 176 | + ]; |
| 177 | + |
| 178 | + const canvasTexture = this.canvasTexture; |
| 179 | + |
| 180 | + // Set uniforms for the shader |
| 181 | + this._shader.setUniform('tex0', canvasTexture); |
| 182 | + this._shader.setUniform('texelSize', texelSize); |
| 183 | + this._shader.setUniform('canvasSize', [this.pInst.width, this.pInst.height]); |
| 184 | + this._shader.setUniform('radius', Math.max(1, this.filterParameter)); |
| 185 | + this._shader.setUniform('filterParameter', this.filterParameter); |
| 186 | + |
| 187 | + this.pInst.states.rectMode = constants.CORNER; |
| 188 | + this.pInst.states.imageMode = constants.CORNER; |
| 189 | + this.pInst.blendMode(constants.BLEND); |
| 190 | + this.pInst.resetMatrix(); |
| 191 | + |
| 192 | + |
| 193 | + const identityMatrix = [1, 0, 0, 0, |
| 194 | + 0, 1, 0, 0, |
| 195 | + 0, 0, 1, 0, |
| 196 | + 0, 0, 0, 1]; |
| 197 | + this._shader.setUniform('uModelViewMatrix', identityMatrix); |
| 198 | + this._shader.setUniform('uProjectionMatrix', identityMatrix); |
| 199 | + |
| 200 | + // Bind and enable vertex attributes |
| 201 | + gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); |
| 202 | + this._shader.enableAttrib(this._shader.attributes.aPosition, 2); |
| 203 | + |
| 204 | + gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer); |
| 205 | + this._shader.enableAttrib(this._shader.attributes.aTexCoord, 2); |
| 206 | + |
| 207 | + this._shader.bindTextures(); |
| 208 | + this._shader.disableRemainingAttributes(); |
| 209 | + |
| 210 | + // Draw the quad |
| 211 | + gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); |
| 212 | + // Unbind the shader |
| 213 | + this._shader.unbindShader(); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Applies the current filter operation. If the filter requires multiple passes (e.g. blur), |
| 218 | + * it handles those internally. Make sure setOperation() has been called before applyFilter(). |
| 219 | + */ |
| 220 | + applyFilter() { |
| 221 | + if (!this._shader) { |
| 222 | + console.error("Cannot apply filter: shader not initialized."); |
| 223 | + return; |
| 224 | + } |
| 225 | + this.pInst.push(); |
| 226 | + this.pInst.resetMatrix(); |
| 227 | + // For blur, we typically do two passes: one horizontal, one vertical. |
| 228 | + if (this.operation === constants.BLUR && !this.customShader) { |
| 229 | + // Horizontal pass |
| 230 | + this._shader.setUniform('direction', [1, 0]); |
| 231 | + this._renderPass(); |
| 232 | + |
| 233 | + // Draw the result onto itself |
| 234 | + this.pInst.clear(); |
| 235 | + this.pInst.drawingContext.drawImage(this.canvas, 0, 0, this.pInst.width, this.pInst.height); |
| 236 | + |
| 237 | + // Vertical pass |
| 238 | + this._shader.setUniform('direction', [0, 1]); |
| 239 | + this._renderPass(); |
| 240 | + |
| 241 | + this.pInst.clear(); |
| 242 | + this.pInst.drawingContext.drawImage(this.canvas, 0, 0, this.pInst.width, this.pInst.height); |
| 243 | + } else { |
| 244 | + // Single-pass filters |
| 245 | + |
| 246 | + this._renderPass(); |
| 247 | + this.pInst.clear(); |
| 248 | + // con |
| 249 | + this.pInst.blendMode(constants.BLEND); |
| 250 | + |
| 251 | + |
| 252 | + this.pInst.drawingContext.drawImage(this.canvas, 0, 0, this.pInst.width, this.pInst.height); |
| 253 | + } |
| 254 | + this.pInst.pop(); |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +export default FilterRenderer2D; |
0 commit comments