Skip to content

Commit a0abc41

Browse files
committed
Add methods to retrieve noise parameters for WebGL shaders
1 parent 095f220 commit a0abc41

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/math/noise.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ function noise(p5, fn){
397397
}
398398
};
399399

400+
/**
401+
* @private
402+
* Returns the current number of octaves used by noise().
403+
*/
404+
fn._getNoiseOctaves = function() {
405+
return perlin_octaves;
406+
};
407+
408+
/**
409+
* @private
410+
* Returns the current falloff factor used by noise().
411+
*/
412+
fn._getNoiseAmpFalloff = function() {
413+
return perlin_amp_falloff;
414+
};
415+
400416
/**
401417
* Sets the seed value for the <a href="#/p5/noise">noise()</a> function.
402418
*

src/strands/strands_api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
121121
return originalNoise.apply(this, args); // fallback to regular p5.js noise
122122
}
123123

124+
const hasNoiseUniforms = strandsContext.uniforms.some(u => u.name === 'uNoiseOctaves');
125+
if (!hasNoiseUniforms) {
126+
strandsContext.uniforms.push({
127+
name: 'uNoiseOctaves',
128+
typeInfo: DataType.int1,
129+
defaultValue: () => p5._getNoiseOctaves()
130+
});
131+
strandsContext.uniforms.push({
132+
name: 'uNoiseAmpFalloff',
133+
typeInfo: DataType.float1,
134+
defaultValue: () => p5._getNoiseAmpFalloff()
135+
});
136+
}
137+
124138
strandsContext.vertexDeclarations.add(noiseGLSL);
125139
strandsContext.fragmentDeclarations.add(noiseGLSL);
126140
// Handle noise(x, y) as noise(vec2)

src/webgl/shaders/functions/noise3DGLSL.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ float noise(vec3 st) {
9898
float amplitude = 1.0;
9999
float frequency = 1.0;
100100

101-
for (int i = 0; i < 4; i++) {
101+
for (int i = 0; i < 8; i++) {
102+
if (i >= uNoiseOctaves) break;
102103
result += amplitude * baseNoise(st * frequency);
103104
frequency *= 2.0;
104-
amplitude *= 0.5;
105+
amplitude *= uNoiseAmpFalloff;
105106
}
106107

107108
return result;

0 commit comments

Comments
 (0)