Skip to content

Commit 08eb850

Browse files
committed
Merge branch 'dev-2.0' into webgpu
2 parents caffc71 + a6e0b46 commit 08eb850

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
@@ -125,6 +125,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
125125

126126
// Add noise function with backend-agnostic implementation
127127
const originalNoise = fn.noise;
128+
const originalNoiseDetail = fn.noiseDetail;
129+
130+
strandsContext._noiseOctaves = null;
131+
strandsContext._noiseAmpFalloff = null;
132+
133+
fn.noiseDetail = function (lod, falloff) {
134+
if (!strandsContext.active) {
135+
return originalNoiseDetail.apply(this, arguments);
136+
}
137+
138+
strandsContext._noiseOctaves = lod;
139+
strandsContext._noiseAmpFalloff = falloff;
140+
};
141+
128142
fn.noise = function (...args) {
129143
if (!strandsContext.active) {
130144
return originalNoise.apply(this, args); // fallback to regular p5.js noise
@@ -154,9 +168,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
154168
`It looks like you've called noise() with ${args.length} arguments. It only supports 1D to 3D input.`
155169
);
156170
}
171+
172+
const octaves = strandsContext._noiseOctaves !== null
173+
? strandsContext._noiseOctaves
174+
: fn._getNoiseOctaves();
175+
const falloff = strandsContext._noiseAmpFalloff !== null
176+
? strandsContext._noiseAmpFalloff
177+
: fn._getNoiseAmpFalloff();
178+
179+
nodeArgs.push(octaves);
180+
nodeArgs.push(falloff);
181+
157182
const { id, dimension } = build.functionCallNode(strandsContext, 'noise', nodeArgs, {
158183
overloads: [{
159-
params: [DataType.float3],
184+
params: [DataType.float3, DataType.int1, DataType.float1],
160185
returnType: DataType.float1,
161186
}]
162187
});

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)