Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/math/noise.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ function noise(p5, fn){
}
};

/**
* @private
* Returns the current number of octaves used by noise().
*/
fn._getNoiseOctaves = function() {
return perlin_octaves;
};

/**
* @private
* Returns the current falloff factor used by noise().
*/
fn._getNoiseAmpFalloff = function() {
return perlin_amp_falloff;
};

/**
* Sets the seed value for the <a href="#/p5/noise">noise()</a> function.
*
Expand Down
22 changes: 18 additions & 4 deletions src/strands/strands_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,27 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
}
// Add GLSL noise. TODO: Replace this with a backend-agnostic implementation
const originalNoise = fn.noise;
const originalNoiseDetail = fn.noiseDetail;

strandsContext._noiseOctaves = null;
strandsContext._noiseAmpFalloff = null;

fn.noiseDetail = function (lod, falloff) {
if (!strandsContext.active) {
return originalNoiseDetail.apply(this, arguments);
}

strandsContext._noiseOctaves = lod;
strandsContext._noiseAmpFalloff = falloff;
};

fn.noise = function (...args) {
if (!strandsContext.active) {
return originalNoise.apply(this, args); // fallback to regular p5.js noise
return originalNoise.apply(this, args);
}
strandsContext.vertexDeclarations.add(noiseGLSL);
strandsContext.fragmentDeclarations.add(noiseGLSL);
// Handle noise(x, y) as noise(vec2)

let nodeArgs;
if (args.length === 3) {
nodeArgs = [fn.vec3(args[0], args[1], args[2])];
Expand All @@ -121,8 +135,8 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
}
const { id, dimension } = build.functionCallNode(strandsContext, 'noise', nodeArgs, {
overloads: [{
params: [DataType.float3],
returnType: DataType.float1,
params: [DataType.float3, DataType.int1, DataType.float1],
returnType: DataType.float1
}]
});
return createStrandsNode(id, dimension, strandsContext);
Expand Down
7 changes: 4 additions & 3 deletions src/webgl/shaders/functions/noise3DGLSL.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ float baseNoise(vec3 v)
dot(p2,x2), dot(p3,x3) ) );
}

float noise(vec3 st) {
float noise(vec3 st, int octaves, float ampFalloff) {
float result = 0.0;
float amplitude = 1.0;
float frequency = 1.0;

for (int i = 0; i < 4; i++) {
for (int i = 0; i < 8; i++) {
if (i >= octaves) break;
result += amplitude * baseNoise(st * frequency);
frequency *= 2.0;
amplitude *= 0.5;
amplitude *= ampFalloff;
}

return result;
Expand Down
Loading