Skip to content

Add GLSL-based noise(vec2) function to p5.strands (#7897) #7921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/webgl/ShaderGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { parse } from 'acorn';
import { ancestor } from 'acorn-walk';
import escodegen from 'escodegen';
import noiseGLSL from './shaders/functions/noiseGLSL.glsl';

function shadergenerator(p5, fn) {

Expand Down Expand Up @@ -1578,6 +1579,7 @@ function shadergenerator(p5, fn) {
],
'sqrt': { args: ['genType'], returnType: 'genType', isp5Function: true},
'step': { args: ['genType', 'genType'], returnType: 'genType', isp5Function: false},
'noise': { args: ['vec2'], returnType: 'float', isp5Function: false },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the glsl code, we have a baseNoise() function returning float. So, either you have to rename at the js file or in the glsl file. Probably changing from baseNoise() to noise() in the glsl is what I would prefer.

'trunc': { args: ['genType'], returnType: 'genType', isp5Function: false},

////////// Vector //////////
Expand Down Expand Up @@ -1629,10 +1631,21 @@ function shadergenerator(p5, fn) {
return originalLerp.apply(this, args); // Fallback to normal p5.js lerp
}
};
fn.noise = function (...args) {
if (!GLOBAL_SHADER?.isGenerating) {
p5._friendlyError(
`It looks like you've called noise() outside of a shader's modify() function.`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noise is also a regular p5 function, so in this case we can call the original implementation, similar to how you did lerp.

);
return;
}

GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);
return fnNodeConstructor('noise', args, { args: ['vec2'], returnType: 'float' });
};
}



export default shadergenerator;

if (typeof p5 !== 'undefined') {
Expand Down
13 changes: 13 additions & 0 deletions src/webgl/shaders/functions/noiseGLSL.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
float baseNoise(vec2 st) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to have more such functions in the future like noise? I am not sure if we should create a functions directory for it? @davepagurek What's your thought on this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

off the top of my head I think we were considering at least random(), so I think we can keep this folder structure for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @LalitNarayanYadav while I tested it locally, I found that the things are little jittery and we want the noise to be smoother, maybe we just need a different basis for each octave.

e.g. something like the examples here, but, maybe finding where they came from to double check the licenses, or doing something similar but different https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83

return fract(sin(dot(st.xy ,vec2(12.9898,78.233))) * 43758.5453123);
}

float noise(vec2 st) {
float result = 0.0;
for (int i = 0; i < 3; i++) {
float freq = pow(2.0, float(i));
float amp = pow(0.5, float(i));
result += amp * baseNoise(st * freq);
}
return result;
}
Loading