Skip to content

Commit eb3448c

Browse files
committed
add suggested changes
1 parent a0abc41 commit eb3448c

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/strands/strands_api.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,28 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
116116

117117
// Add GLSL noise. TODO: Replace this with a backend-agnostic implementation
118118
const originalNoise = fn.noise;
119-
fn.noise = function (...args) {
119+
const originalNoiseDetail = fn.noiseDetail;
120+
121+
strandsContext._noiseOctaves = null;
122+
strandsContext._noiseAmpFalloff = null;
123+
124+
fn.noiseDetail = function (lod, falloff) {
120125
if (!strandsContext.active) {
121-
return originalNoise.apply(this, args); // fallback to regular p5.js noise
126+
return originalNoiseDetail.apply(this, arguments);
122127
}
123128

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-
});
129+
strandsContext._noiseOctaves = lod;
130+
strandsContext._noiseAmpFalloff = falloff;
131+
};
132+
133+
fn.noise = function (...args) {
134+
if (!strandsContext.active) {
135+
return originalNoise.apply(this, args);
136136
}
137137

138138
strandsContext.vertexDeclarations.add(noiseGLSL);
139139
strandsContext.fragmentDeclarations.add(noiseGLSL);
140-
// Handle noise(x, y) as noise(vec2)
140+
141141
let nodeArgs;
142142
if (args.length === 3) {
143143
nodeArgs = [fn.vec3(args[0], args[1], args[2])];
@@ -147,10 +147,20 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
147147
nodeArgs = args;
148148
}
149149

150+
const octaves = strandsContext._noiseOctaves !== null
151+
? strandsContext._noiseOctaves
152+
: p5._getNoiseOctaves();
153+
const falloff = strandsContext._noiseAmpFalloff !== null
154+
? strandsContext._noiseAmpFalloff
155+
: p5._getNoiseAmpFalloff();
156+
157+
nodeArgs.push(octaves);
158+
nodeArgs.push(falloff);
159+
150160
const { id, dimension } = build.functionCallNode(strandsContext, 'noise', nodeArgs, {
151161
overloads: [{
152-
params: [DataType.float3],
153-
returnType: DataType.float1,
162+
params: [DataType.float3, DataType.int1, DataType.float1],
163+
returnType: DataType.float1
154164
}]
155165
});
156166
return createStrandsNode(id, dimension, strandsContext);

src/webgl/shaders/functions/noise3DGLSL.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +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

101101
for (int i = 0; i < 8; i++) {
102-
if (i >= uNoiseOctaves) break;
102+
if (i >= octaves) break;
103103
result += amplitude * baseNoise(st * frequency);
104104
frequency *= 2.0;
105-
amplitude *= uNoiseAmpFalloff;
105+
amplitude *= ampFalloff;
106106
}
107107

108108
return result;

0 commit comments

Comments
 (0)