Skip to content

Commit 79d2fe2

Browse files
committed
Fix noise() getting overridden; add tests
1 parent 5229bc2 commit 79d2fe2

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ function shadergenerator(p5, fn) {
3333
const transpiledSource = escodegen.generate(ast);
3434
const scopeKeys = Object.keys(scope);
3535
const internalGeneratorFunction = new Function(
36-
'p5',
36+
// Create a parameter called __p5, not just p5, because users of instance mode
37+
// may pass in a variable called p5 as a scope variable. If we rely on a variable called
38+
// p5, then the scope variable called p5 might accidentally override internal function
39+
// calls to p5 static methods.
40+
'__p5',
3741
...scopeKeys,
3842
transpiledSource
3943
.slice(
@@ -100,7 +104,7 @@ function shadergenerator(p5, fn) {
100104
node.type = 'CallExpression'
101105
node.callee = {
102106
type: 'Identifier',
103-
name: 'p5.unaryNode',
107+
name: '__p5.unaryNode',
104108
}
105109
node.arguments = [node.argument, signNode]
106110
}
@@ -123,7 +127,7 @@ function shadergenerator(p5, fn) {
123127
type: 'CallExpression',
124128
callee: {
125129
type: 'Identifier',
126-
name: 'p5.unaryNode'
130+
name: '__p5.unaryNode'
127131
},
128132
arguments: [node.argument.object, signNode],
129133
};
@@ -188,7 +192,7 @@ function shadergenerator(p5, fn) {
188192
node.type = 'CallExpression';
189193
node.callee = {
190194
type: 'Identifier',
191-
name: 'p5.dynamicNode',
195+
name: '__p5.dynamicNode',
192196
};
193197
node.arguments = [original];
194198
},
@@ -242,7 +246,7 @@ function shadergenerator(p5, fn) {
242246
type: 'CallExpression',
243247
callee: {
244248
type: 'Identifier',
245-
name: 'p5.dynamicNode',
249+
name: '__p5.dynamicNode',
246250
},
247251
arguments: [node.left]
248252
}
@@ -1608,7 +1612,6 @@ function shadergenerator(p5, fn) {
16081612
],
16091613
'sqrt': { args: ['genType'], returnType: 'genType', isp5Function: true},
16101614
'step': { args: ['genType', 'genType'], returnType: 'genType', isp5Function: false},
1611-
'noise': { args: ['vec2'], returnType: 'float', isp5Function: false },
16121615
'trunc': { args: ['genType'], returnType: 'genType', isp5Function: false},
16131616

16141617
////////// Vector //////////
@@ -1675,7 +1678,7 @@ function shadergenerator(p5, fn) {
16751678
} else {
16761679
nodeArgs = args;
16771680
}
1678-
1681+
16791682
return fnNodeConstructor('noise', nodeArgs, {
16801683
args: ['vec2'],
16811684
returnType: 'float'

test/unit/visual/cases/noise.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { visualSuite, visualTest } from '../visualTest';
2+
3+
visualSuite('Noise', function() {
4+
visualTest('Drawn as values', function(p5, screenshot) {
5+
p5.createCanvas(50, 50);
6+
p5.background(255);
7+
p5.strokeWeight(2);
8+
p5.noFill();
9+
p5.noiseSeed(0);
10+
p5.beginShape();
11+
for (let x = 0; x <= p5.width; x += 2) {
12+
p5.vertex(x, p5.noise(x * 0.1) * p5.height);
13+
}
14+
p5.endShape();
15+
screenshot();
16+
});
17+
18+
visualTest('Drawn in a shader', function(p5, screenshot) {
19+
p5.createCanvas(50, 50, p5.WEBGL);
20+
const shader = p5.baseFilterShader().modify(() => {
21+
p5.getColor((inputs) => {
22+
const value = p5.clamp(p5.noise(inputs.texCoord * 4) * 2, 0, 1);
23+
return [value, value, value, 1];
24+
});
25+
}, { p5 });
26+
p5.filter(shader);
27+
screenshot();
28+
});
29+
});
681 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3.25 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)