Skip to content

Commit d6310a6

Browse files
Add 3D noise(vec3) support to p5.strands
1 parent 6d4540c commit d6310a6

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { parse } from 'acorn';
88
import { ancestor } from 'acorn-walk';
99
import escodegen from 'escodegen';
1010
import noiseGLSL from './shaders/functions/noiseGLSL.glsl';
11+
import noise3DGLSL from './shaders/functions/noise3DGLSL.glsl';
1112

1213
function shadergenerator(p5, fn) {
1314

@@ -1663,23 +1664,34 @@ function shadergenerator(p5, fn) {
16631664
const originalNoise = fn.noise;
16641665
fn.noise = function (...args) {
16651666
if (!GLOBAL_SHADER?.isGenerating) {
1666-
return originalNoise.apply(this, args); // fallback to regular p5.js noise
1667+
return originalNoise.apply(this, args); // fallback to p5.js noise
16671668
}
1668-
1669-
GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
1670-
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);
1671-
// Handle noise(x, y) as noise(vec2)
1669+
16721670
let nodeArgs;
1673-
if (args.length === 2) {
1671+
1672+
if (args.length === 3) {
1673+
// GLSL vec3 noise(x, y, z)
1674+
nodeArgs = [fn.vec3(args[0], args[1], args[2])];
1675+
GLOBAL_SHADER.output.vertexDeclarations.add(noise3DGLSL);
1676+
GLOBAL_SHADER.output.fragmentDeclarations.add(noise3DGLSL);
1677+
1678+
return fnNodeConstructor('noise', nodeArgs, {
1679+
args: ['vec3'],
1680+
returnType: 'float',
1681+
});
1682+
} else if (args.length === 2) {
1683+
// GLSL vec2 noise(x, y)
16741684
nodeArgs = [fn.vec2(args[0], args[1])];
1675-
} else {
1676-
nodeArgs = args;
1685+
GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
1686+
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);
1687+
1688+
return fnNodeConstructor('noise', nodeArgs, {
1689+
args: ['vec2'],
1690+
returnType: 'float',
1691+
});
16771692
}
16781693

1679-
return fnNodeConstructor('noise', nodeArgs, {
1680-
args: ['vec2'],
1681-
returnType: 'float'
1682-
});
1694+
return originalNoise.apply(this, args); // fallback again if unexpected
16831695
};
16841696

16851697
}

0 commit comments

Comments
 (0)