Skip to content

Commit 2936e42

Browse files
committed
Add noiseDetail() support for p5.strands
1 parent 60ef55e commit 2936e42

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
104104
}
105105
// Add GLSL noise. TODO: Replace this with a backend-agnostic implementation
106106
const originalNoise = fn.noise;
107+
const originalNoiseDetail = fn.noiseDetail;
108+
109+
strandsContext._noiseOctaves = null;
110+
strandsContext._noiseAmpFalloff = null;
111+
112+
fn.noiseDetail = function (lod, falloff) {
113+
if (!strandsContext.active) {
114+
return originalNoiseDetail.apply(this, arguments);
115+
}
116+
117+
strandsContext._noiseOctaves = lod;
118+
strandsContext._noiseAmpFalloff = falloff;
119+
};
120+
107121
fn.noise = function (...args) {
108122
if (!strandsContext.active) {
109123
return originalNoise.apply(this, args); // fallback to regular p5.js noise
@@ -131,9 +145,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
131145
`It looks like you've called noise() with ${args.length} arguments. It only supports 1D to 3D input.`
132146
);
133147
}
148+
149+
const octaves = strandsContext._noiseOctaves !== null
150+
? strandsContext._noiseOctaves
151+
: p5._getNoiseOctaves();
152+
const falloff = strandsContext._noiseAmpFalloff !== null
153+
? strandsContext._noiseAmpFalloff
154+
: p5._getNoiseAmpFalloff();
155+
156+
nodeArgs.push(octaves);
157+
nodeArgs.push(falloff);
158+
134159
const { id, dimension } = build.functionCallNode(strandsContext, 'noise', nodeArgs, {
135160
overloads: [{
136-
params: [DataType.float3],
161+
params: [DataType.float3, DataType.int1, DataType.float1],
137162
returnType: DataType.float1,
138163
}]
139164
});

src/webgl/shaders/functions/noise3DGLSL.glsl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@ float baseNoise(vec3 v)
9393
dot(p2,x2), dot(p3,x3) ) );
9494
}
9595

96-
float noise(vec3 st) {
96+
float noise(vec3 st, int octaves, float ampFalloff) {
9797
float result = 0.0;
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 >= octaves) break;
102103
result += amplitude * baseNoise(st * frequency);
103104
frequency *= 2.0;
104-
amplitude *= 0.5;
105+
amplitude *= ampFalloff;
105106
}
106107

107108
return result;

0 commit comments

Comments
 (0)