Skip to content

Commit 77f803a

Browse files
authored
Merge pull request #8227 from processing/fix/noise-vector-input
Fix handling of vector arguments to noise() in p5.strands
2 parents e131ff4 + 6898da2 commit 77f803a

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

src/strands/strands_api.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,26 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
110110
}
111111
strandsContext.vertexDeclarations.add(noiseGLSL);
112112
strandsContext.fragmentDeclarations.add(noiseGLSL);
113-
// Handle noise(x, y) as noise(vec2)
113+
114+
// Make each input into a strands node so that we can check their dimensions
115+
const strandsArgs = args.map(arg => p5.strandsNode(arg));
114116
let nodeArgs;
115-
if (args.length === 3) {
116-
nodeArgs = [fn.vec3(args[0], args[1], args[2])];
117-
} else if (args.length === 2) {
118-
nodeArgs = [fn.vec3(args[0], args[1], 0)];
117+
if (strandsArgs.length === 3) {
118+
nodeArgs = [fn.vec3(strandsArgs[0], strandsArgs[1], strandsArgs[2])];
119+
} else if (strandsArgs.length === 2) {
120+
nodeArgs = [fn.vec3(strandsArgs[0], strandsArgs[1], 0)];
121+
} else if (strandsArgs.length === 1 && strandsArgs[0].dimension <= 3) {
122+
if (strandsArgs[0].dimension === 3) {
123+
nodeArgs = strandsArgs;
124+
} else if (strandsArgs[0].dimension === 2) {
125+
nodeArgs = [fn.vec3(strandsArgs[0], 0)];
126+
} else {
127+
nodeArgs = [fn.vec3(strandsArgs[0], 0, 0)];
128+
}
119129
} else {
120-
nodeArgs = args;
130+
p5._friendlyError(
131+
`It looks like you've called noise() with ${args.length} arguments. It only supports 1D to 3D input.`
132+
);
121133
}
122134
const { id, dimension } = build.functionCallNode(strandsContext, 'noise', nodeArgs, {
123135
overloads: [{

test/unit/webgl/p5.Shader.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,5 +1337,67 @@ suite('p5.Shader', function() {
13371337
assert.isNumber(pixelColor[2]);
13381338
});
13391339
});
1340+
1341+
suite('noise()', () => {
1342+
for (let i = 1; i <= 3; i++) {
1343+
test(`works with ${i}D vectors`, () => {
1344+
expect(() => {
1345+
myp5.createCanvas(50, 50, myp5.WEBGL);
1346+
const input = new Array(i).fill(10);
1347+
const testShader = myp5.baseFilterShader().modify(() => {
1348+
myp5.getColor(() => {
1349+
return [myp5.noise(input), 0, 0, 1];
1350+
});
1351+
}, { myp5, input });
1352+
myp5.shader(testShader);
1353+
myp5.plane(10, 10);
1354+
}).not.toThrowError();
1355+
});
1356+
1357+
test(`works with ${i}D positional arguments`, () => {
1358+
expect(() => {
1359+
myp5.createCanvas(50, 50, myp5.WEBGL);
1360+
const input = new Array(i).fill(10);
1361+
const testShader = myp5.baseFilterShader().modify(() => {
1362+
myp5.getColor(() => {
1363+
return [myp5.noise(...input), 0, 0, 1];
1364+
});
1365+
}, { myp5, input });
1366+
myp5.shader(testShader);
1367+
myp5.plane(10, 10);
1368+
}).not.toThrowError();
1369+
});
1370+
}
1371+
1372+
for (const i of [0, 4]) {
1373+
test(`Does not work in ${i}D`, () => {
1374+
expect(() => {
1375+
myp5.createCanvas(50, 50, myp5.WEBGL);
1376+
const input = new Array(i).fill(10);
1377+
const testShader = myp5.baseFilterShader().modify(() => {
1378+
myp5.getColor(() => {
1379+
return [myp5.noise(input), 0, 0, 1];
1380+
});
1381+
}, { myp5, input });
1382+
myp5.shader(testShader);
1383+
myp5.plane(10, 10);
1384+
}).toThrowError();
1385+
});
1386+
1387+
test(`Does not work in ${i}D with positional arguments`, () => {
1388+
expect(() => {
1389+
myp5.createCanvas(50, 50, myp5.WEBGL);
1390+
const input = new Array(i).fill(10);
1391+
const testShader = myp5.baseFilterShader().modify(() => {
1392+
myp5.getColor(() => {
1393+
return [myp5.noise(...input), 0, 0, 1];
1394+
});
1395+
}, { myp5, input });
1396+
myp5.shader(testShader);
1397+
myp5.plane(10, 10);
1398+
}).toThrowError();
1399+
});
1400+
}
1401+
});
13401402
});
13411403
});

0 commit comments

Comments
 (0)