Skip to content

Commit ced12cd

Browse files
authored
Merge branch 'dev-2.0' into transform/Clip
2 parents 0552008 + 090ae9f commit ced12cd

9 files changed

Lines changed: 338 additions & 130 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test/**/*.js": "eslint",
2020
"utils/**/*.{js,mjs}": "eslint"
2121
},
22-
"version": "2.2.0-rc.3",
22+
"version": "2.2.0-rc.4",
2323
"dependencies": {
2424
"@davepagurek/bezier-path": "^0.0.2",
2525
"@japont/unicode-range": "^1.0.0",

src/core/filterShaders.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import * as constants from './constants';
22

33
/*
4-
* Creates p5.strands filter shaders for cross-platform compatibility
4+
* Creates p5.strands filter shaders for cross-platform compatibility.
5+
*
6+
* NOTE: These work a little differently than p5.js web editor shaders work!
7+
* Firstly, it uses instance mode, so we have to explicitly pass in context
8+
* variables in an argument to your callback and as a second argument to `modify`.
9+
* Secondly, always manually specify uniform names, as variable names will change
10+
* in minified builds.
511
*/
612
export function makeFilterShader(renderer, operation, p5) {
713
switch (operation) {
814
case constants.GRAY:
9-
return renderer.baseFilterShader().modify(() => {
15+
return renderer.baseFilterShader().modify(({ p5 }) => {
1016
p5.getColor((inputs, canvasContent) => {
1117
const tex = p5.getTexture(canvasContent, inputs.texCoord);
1218
// weighted grayscale with luminance values
@@ -16,7 +22,7 @@ export function makeFilterShader(renderer, operation, p5) {
1622
}, { p5 });
1723

1824
case constants.INVERT:
19-
return renderer.baseFilterShader().modify(() => {
25+
return renderer.baseFilterShader().modify(({ p5 }) => {
2026
p5.getColor((inputs, canvasContent) => {
2127
const color = p5.getTexture(canvasContent, inputs.texCoord);
2228
const invertedColor = p5.vec3(1.0) - color.rgb;
@@ -25,8 +31,8 @@ export function makeFilterShader(renderer, operation, p5) {
2531
}, { p5 });
2632

2733
case constants.THRESHOLD:
28-
return renderer.baseFilterShader().modify(() => {
29-
const filterParameter = p5.uniformFloat();
34+
return renderer.baseFilterShader().modify(({ p5 }) => {
35+
const filterParameter = p5.uniformFloat('filterParameter');
3036
p5.getColor((inputs, canvasContent) => {
3137
const color = p5.getTexture(canvasContent, inputs.texCoord);
3238
// weighted grayscale with luminance values
@@ -38,8 +44,8 @@ export function makeFilterShader(renderer, operation, p5) {
3844
}, { p5 });
3945

4046
case constants.POSTERIZE:
41-
return renderer.baseFilterShader().modify(() => {
42-
const filterParameter = p5.uniformFloat();
47+
return renderer.baseFilterShader().modify(({ p5 }) => {
48+
const filterParameter = p5.uniformFloat('filterParameter');
4349
const quantize = (color, n) => {
4450
// restrict values to N options/bins
4551
// and floor each channel to nearest value
@@ -60,9 +66,9 @@ export function makeFilterShader(renderer, operation, p5) {
6066
}, { p5 });
6167

6268
case constants.BLUR:
63-
return renderer.baseFilterShader().modify(() => {
64-
const radius = p5.uniformFloat();
65-
const direction = p5.uniformVec2();
69+
return renderer.baseFilterShader().modify(({ p5 }) => {
70+
const radius = p5.uniformFloat('radius');
71+
const direction = p5.uniformVec2('direction');
6672

6773
// This isn't a real Gaussian weight, it's a quadratic weight
6874
const quadWeight = (x, e) => {
@@ -120,7 +126,7 @@ export function makeFilterShader(renderer, operation, p5) {
120126
}, { p5 });
121127

122128
case constants.ERODE:
123-
return renderer.baseFilterShader().modify(() => {
129+
return renderer.baseFilterShader().modify(({ p5 }) => {
124130
const luma = (color) => {
125131
return p5.dot(color.rgb, p5.vec3(0.2126, 0.7152, 0.0722));
126132
};
@@ -150,7 +156,7 @@ export function makeFilterShader(renderer, operation, p5) {
150156
}, { p5 });
151157

152158
case constants.DILATE:
153-
return renderer.baseFilterShader().modify(() => {
159+
return renderer.baseFilterShader().modify(({ p5 }) => {
154160
const luma = (color) => {
155161
return p5.dot(color.rgb, p5.vec3(0.2126, 0.7152, 0.0722));
156162
};
@@ -180,7 +186,7 @@ export function makeFilterShader(renderer, operation, p5) {
180186
}, { p5 });
181187

182188
case constants.OPAQUE:
183-
return renderer.baseFilterShader().modify(() => {
189+
return renderer.baseFilterShader().modify(({ p5 }) => {
184190
p5.getColor((inputs, canvasContent) => {
185191
const color = p5.getTexture(canvasContent, inputs.texCoord);
186192
return p5.vec4(color.rgb, 1.0);

src/core/p5.Renderer3D.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,14 +1911,14 @@ export class Renderer3D extends Renderer {
19111911
_getSphereMapping(img) {
19121912
if (!this.sphereMapping) {
19131913
const p5 = this._pInst;
1914-
this.sphereMapping = this.baseFilterShader().modify(() => {
1915-
const uEnvMap = p5.uniformTexture();
1916-
const uFovY = p5.uniformFloat();
1917-
const uAspect = p5.uniformFloat();
1914+
this.sphereMapping = this.baseFilterShader().modify(({ p5 }) => {
1915+
const uEnvMap = p5.uniformTexture('uEnvMap');
1916+
const uFovY = p5.uniformFloat('uFovY');
1917+
const uAspect = p5.uniformFloat('uAspect');
19181918
// Hack: we don't have matrix uniforms yet; use three vectors
1919-
const uN1 = p5.uniformVec3();
1920-
const uN2 = p5.uniformVec3();
1921-
const uN3 = p5.uniformVec3();
1919+
const uN1 = p5.uniformVec3('uN1');
1920+
const uN2 = p5.uniformVec3('uN2');
1921+
const uN3 = p5.uniformVec3('uN3');
19221922
p5.getColor((inputs) => {
19231923
const uFovX = uFovY * uAspect;
19241924
const angleY = p5.mix(uFovY/2.0, -uFovY/2.0, inputs.texCoord.y);

src/strands/p5.strands.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function strands(p5, fn) {
7575

7676
p5.Shader.prototype.modify = function (shaderModifier, scope = {}) {
7777
try {
78-
if (shaderModifier instanceof Function) {
78+
if (shaderModifier instanceof Function || typeof shaderModifier === 'string') {
7979
// Reset the context object every time modify is called;
8080
// const backend = glslBackend;
8181
initStrandsContext(strandsContext, this._renderer.strandsBackend, {
@@ -92,7 +92,9 @@ function strands(p5, fn) {
9292
if (options.parser) {
9393
// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
9494
// https://github.com/acornjs/acorn/issues/1385
95-
const sourceString = `(${shaderModifier.toString()})`;
95+
const sourceString = typeof shaderModifier === 'string'
96+
? `(${shaderModifier})`
97+
: `(${shaderModifier.toString()})`;
9698
strandsCallback = transpileStrandsToJS(
9799
p5,
98100
sourceString,

src/strands/strands_for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class StrandsFor {
359359
CFG.popBlock(cfg);
360360

361361
const loopVarNode = createStrandsNode(phiNode.id, phiNode.dimension, this.strandsContext);
362-
this.bodyResults = this.bodyCb(loopVarNode, phiVars);
362+
this.bodyResults = this.bodyCb(loopVarNode, phiVars) || {};
363363
for (const key in this.bodyResults) {
364364
this.bodyResults[key] = this.strandsContext.p5.strandsNode(this.bodyResults[key]);
365365
}

0 commit comments

Comments
 (0)