Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 13 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,20 @@ function shadergenerator(p5, fn) {
return originalLerp.apply(this, args); // Fallback to normal p5.js lerp
}
};
fn.noise = function (...args) {
if (GLOBAL_SHADER?.isGenerating) {
Copy link
Collaborator

@perminder-17 perminder-17 Jun 28, 2025

Choose a reason for hiding this comment

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

Hi @LalitNarayanYadav a very small suggestion, we are on the last bits now.

What do you think about having an early return guard. Like rather than doing

if (GLOBAL_SHADER?.isGenerating) {
// your code ....
} else {
// friendly error message 
}

Use an early-return guard inside the function.

Something like :

fn.noise = function (...args) {
if (!GLOBAL_SHADER?.isGenerating) {
// friendly-error messages
return;
}
// Now your code here without else block:
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @perminder-17, great suggestion! I've refactored the function to use an early return guard as you described. Looks much cleaner now.

GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);
return fnNodeConstructor('noise', args, { args: ['vec2'], returnType: 'float' });
} else {
p5._friendlyError(
`It looks like you've called noise() outside of a shader's modify() function.`
);
}
};
}



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