Skip to content

Commit e2f123d

Browse files
committed
change public filter() signature
adding an opt-out parameter made things a little more complicated (handling two optional parameters now) using shaders behind a P2D renderer also adds complexity ^ still need to handle that case
1 parent 9f15a50 commit e2f123d

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

src/image/pixels.js

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,11 @@ p5.prototype._copyHelper = (
355355
* POSTERIZE, BLUR, ERODE, DILATE or BLUR.
356356
* See Filters.js for docs on
357357
* each available filter
358-
* @param {Number} [filterParam] an optional parameter unique
358+
* @param {Number} filterParam an optional parameter unique
359359
* to each filter, see above
360+
* @param {Boolean} [useWebGL] a flag to control whether to use fast
361+
* WebGL filters (GPU) or original image
362+
* filters (CPU); defaults to true
360363
*
361364
* @example
362365
* <div>
@@ -514,27 +517,75 @@ p5.prototype._copyHelper = (
514517
* gray square
515518
*/
516519

520+
/**
521+
* @method filter
522+
* @param {Constant} filterType
523+
* @param {Boolean} [useWebGL]
524+
*/
517525
/**
518526
* @method filter
519527
* @param {p5.Shader} shaderFilter A shader that's been loaded, with the
520528
* frag shader using a `tex0` uniform
521529
*/
522-
p5.prototype.filter = function(operation, value) {
530+
p5.prototype.filter = function(...args) {
523531
p5._validateParameters('filter', arguments);
524532

525-
// TODO: use shader filters always, and provide an opt out
526-
if (this._renderer.isP3D) {
527-
p5.RendererGL.prototype.filter.call(this._renderer, arguments);
533+
let { shader, operation, value, useWebGL } = parseFilterArgs(...args);
534+
535+
// when passed a shader, use it directly
536+
if (shader) {
537+
p5.RendererGL.prototype.filter.call(this._renderer, shader);
528538
return;
529539
}
530540

531-
if (this.canvas !== undefined) {
532-
Filters.apply(this.canvas, Filters[operation], value);
533-
} else {
534-
Filters.apply(this.elt, Filters[operation], value);
541+
// when opting out of webgl, use old pixels method
542+
if (!useWebGL) {
543+
if (this.canvas !== undefined) {
544+
Filters.apply(this.canvas, Filters[operation], value);
545+
} else {
546+
Filters.apply(this.elt, Filters[operation], value);
547+
}
548+
return;
549+
}
550+
551+
// when this is a webgl renderer, apply constant shader filter
552+
if (this._renderer.isP3D) {
553+
p5.RendererGL.prototype.filter.call(this._renderer, operation, value);
554+
}
555+
556+
// when this is P2D renderer, create/use hidden webgl renderer
557+
else {
558+
// TODO: create/use hidden webgl renderer and transfer contents to this p2d
559+
p5._friendlyError('webgl filter implementation in progress');
535560
}
536561
};
537562

563+
function parseFilterArgs(...args) {
564+
let result = {
565+
shader: undefined,
566+
operation: undefined,
567+
value: undefined,
568+
useWebGL: true
569+
};
570+
571+
if (args[0] instanceof p5.Shader) {
572+
result.shader = args[0];
573+
return result;
574+
}
575+
else {
576+
result.operation = args[0];
577+
}
578+
579+
if (args.length > 1 && typeof args[1] === 'number') {
580+
result.value = args[1];
581+
}
582+
583+
if (args[args.length-1] === false) {
584+
result.useWebGL = false;
585+
}
586+
return result;
587+
}
588+
538589
/**
539590
* Get a region of pixels, or a single pixel, from the canvas.
540591
*

0 commit comments

Comments
 (0)