Skip to content

Add 3D noise(vec3) support to p5.strands #7978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev-2.0
Choose a base branch
from
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
36 changes: 24 additions & 12 deletions src/webgl/ShaderGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parse } from 'acorn';
import { ancestor } from 'acorn-walk';
import escodegen from 'escodegen';
import noiseGLSL from './shaders/functions/noiseGLSL.glsl';
import noise3DGLSL from './shaders/functions/noise3DGLSL.glsl';

function shadergenerator(p5, fn) {

Expand Down Expand Up @@ -1663,23 +1664,34 @@ function shadergenerator(p5, fn) {
const originalNoise = fn.noise;
fn.noise = function (...args) {
if (!GLOBAL_SHADER?.isGenerating) {
return originalNoise.apply(this, args); // fallback to regular p5.js noise
return originalNoise.apply(this, args); // fallback to p5.js noise
}

GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);
// Handle noise(x, y) as noise(vec2)

let nodeArgs;
if (args.length === 2) {

if (args.length === 3) {
// GLSL vec3 noise(x, y, z)
nodeArgs = [fn.vec3(args[0], args[1], args[2])];
GLOBAL_SHADER.output.vertexDeclarations.add(noise3DGLSL);
GLOBAL_SHADER.output.fragmentDeclarations.add(noise3DGLSL);

return fnNodeConstructor('noise', nodeArgs, {
args: ['vec3'],
returnType: 'float',
});
} else if (args.length === 2) {
// GLSL vec2 noise(x, y)
nodeArgs = [fn.vec2(args[0], args[1])];
} else {
nodeArgs = args;
GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL);
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL);

return fnNodeConstructor('noise', nodeArgs, {
args: ['vec2'],
returnType: 'float',
});
}

return fnNodeConstructor('noise', nodeArgs, {
args: ['vec2'],
returnType: 'float'
});
return originalNoise.apply(this, args); // fallback again if unexpected
};

}
Expand Down
52 changes: 52 additions & 0 deletions src/webgl/shaders/functions/noise3DGLSL.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Based on https://thebookofshaders.com/13/ and https://github.com/patriciogonzalezvivo/lygia
// MIT licensed, adapted for p5.strands

vec3 random3(vec3 p) {
return fract(sin(vec3(
dot(p, vec3(127.1, 311.7, 74.7)),
dot(p, vec3(269.5, 183.3, 246.1)),
dot(p, vec3(113.5, 271.9, 124.6))
)) * 43758.5453123);
}

float baseNoise(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);

// Compute corner vectors
vec3 a = random3(i);
vec3 b = random3(i + vec3(1, 0, 0));
vec3 c = random3(i + vec3(0, 1, 0));
vec3 d = random3(i + vec3(1, 1, 0));
vec3 e = random3(i + vec3(0, 0, 1));
vec3 f1 = random3(i + vec3(1, 0, 1));
vec3 g = random3(i + vec3(0, 1, 1));
vec3 h = random3(i + vec3(1, 1, 1));

vec3 u = f * f * (3.0 - 2.0 * f);

return mix(
mix(
mix(dot(a, f - vec3(0, 0, 0)), dot(b, f - vec3(1, 0, 0)), u.x),
mix(dot(c, f - vec3(0, 1, 0)), dot(d, f - vec3(1, 1, 0)), u.x), u.y
),
mix(
mix(dot(e, f - vec3(0, 0, 1)), dot(f1, f - vec3(1, 0, 1)), u.x),
mix(dot(g, f - vec3(0, 1, 1)), dot(h, f - vec3(1, 1, 1)), u.x), u.y
),
u.z
);
}

float noise(vec3 p) {
float result = 0.0;
float amplitude = 1.0;
float frequency = 1.0;

for (int i = 0; i < 4; i++) {
result += amplitude * baseNoise(p * frequency);
frequency *= 2.0;
amplitude *= 0.5;
}
return result;
}
Loading