Skip to content

Commit

Permalink
Change perlinNoise to simpleNoise
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubidumdu committed Mar 23, 2024
1 parent 4dbeb62 commit 195dde5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 85 deletions.
3 changes: 2 additions & 1 deletion pages/visualizer/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ const init = async () => {
);
camera.attachControl(canvas, true);

const PARTICLE_NUMS = 200_000;
const PARTICLE_NUMS = 100_000;
const RADIUS = 80;

const mesh = MeshBuilder.CreatePolyhedron('oct', { type: 3, size: 1 }, scene);

mesh.forcedInstanceCount = PARTICLE_NUMS;

const particleMeshMaterial = new ShaderMaterial(
Expand Down
109 changes: 30 additions & 79 deletions pages/visualizer/shaders/compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,96 +33,47 @@ const core = vec3(0., 0., 0.);
let rotateYMatrix = mat3x3(vec3(cos(speed), 0, -sin(speed)), vec3(0., 1., 0.), vec3(sin(speed), 0, cos(speed)));
let radius = uniforms.radius;
let position = particles[index].position;
let noise = max(fbm3d((position.xyz * (.4 + .1 * high) + vec3(0, time, 0)) * .2), .1);
let velocity = (rotateYMatrix * (normalize(position - core) * radius) - position) * .05;
let noise = fbm3d((position.xyz * (.4 + .2 * high) + vec3(0, time, 0)) * .3);
let nPosition = normalize(position - core);
let velocity = (rotateYMatrix * (nPosition * radius) - position) * .05;

particles[index].noise = noise;
particles[index].position += velocity;
}

fn permute4(x: vec4f) -> vec4f { return ((x * 34. + 1.) * x) % vec4f(289.); }
fn taylorInvSqrt4(r: vec4f) -> vec4f { return 1.79284291400159 - 0.85373472095314 * r; }
fn fade3(t: vec3f) -> vec3f { return t * t * t * (t * (t * 6. - 15.) + 10.); }

fn perlinNoise3(P: vec3f) -> f32 {
var Pi0 : vec3f = floor(P); // Integer part for indexing
var Pi1 : vec3f = Pi0 + vec3f(1.); // Integer part + 1
Pi0 = Pi0 % vec3f(289.);
Pi1 = Pi1 % vec3f(289.);
let Pf0 = fract(P); // Fractional part for interpolation
let Pf1 = Pf0 - vec3f(1.); // Fractional part - 1.
let ix = vec4f(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
let iy = vec4f(Pi0.yy, Pi1.yy);
let iz0 = Pi0.zzzz;
let iz1 = Pi1.zzzz;

let ixy = permute4(permute4(ix) + iy);
let ixy0 = permute4(ixy + iz0);
let ixy1 = permute4(ixy + iz1);

var gx0: vec4f = ixy0 / 7.;
var gy0: vec4f = fract(floor(gx0) / 7.) - 0.5;
gx0 = fract(gx0);
var gz0: vec4f = vec4f(0.5) - abs(gx0) - abs(gy0);
var sz0: vec4f = step(gz0, vec4f(0.));
gx0 = gx0 + sz0 * (step(vec4f(0.), gx0) - 0.5);
gy0 = gy0 + sz0 * (step(vec4f(0.), gy0) - 0.5);

var gx1: vec4f = ixy1 / 7.;
var gy1: vec4f = fract(floor(gx1) / 7.) - 0.5;
gx1 = fract(gx1);
var gz1: vec4f = vec4f(0.5) - abs(gx1) - abs(gy1);
var sz1: vec4f = step(gz1, vec4f(0.));
gx1 = gx1 - sz1 * (step(vec4f(0.), gx1) - 0.5);
gy1 = gy1 - sz1 * (step(vec4f(0.), gy1) - 0.5);

var g000: vec3f = vec3f(gx0.x, gy0.x, gz0.x);
var g100: vec3f = vec3f(gx0.y, gy0.y, gz0.y);
var g010: vec3f = vec3f(gx0.z, gy0.z, gz0.z);
var g110: vec3f = vec3f(gx0.w, gy0.w, gz0.w);
var g001: vec3f = vec3f(gx1.x, gy1.x, gz1.x);
var g101: vec3f = vec3f(gx1.y, gy1.y, gz1.y);
var g011: vec3f = vec3f(gx1.z, gy1.z, gz1.z);
var g111: vec3f = vec3f(gx1.w, gy1.w, gz1.w);

let norm0 = taylorInvSqrt4(
vec4f(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
g000 = g000 * norm0.x;
g010 = g010 * norm0.y;
g100 = g100 * norm0.z;
g110 = g110 * norm0.w;
let norm1 = taylorInvSqrt4(
vec4f(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
g001 = g001 * norm1.x;
g011 = g011 * norm1.y;
g101 = g101 * norm1.z;
g111 = g111 * norm1.w;

let n000 = dot(g000, Pf0);
let n100 = dot(g100, vec3f(Pf1.x, Pf0.yz));
let n010 = dot(g010, vec3f(Pf0.x, Pf1.y, Pf0.z));
let n110 = dot(g110, vec3f(Pf1.xy, Pf0.z));
let n001 = dot(g001, vec3f(Pf0.xy, Pf1.z));
let n101 = dot(g101, vec3f(Pf1.x, Pf0.y, Pf1.z));
let n011 = dot(g011, vec3f(Pf0.x, Pf1.yz));
let n111 = dot(g111, Pf1);

var fade_xyz: vec3f = fade3(Pf0);
let temp = vec4f(f32(fade_xyz.z)); // simplify after chrome bug fix
let n_z = mix(vec4f(n000, n100, n010, n110), vec4f(n001, n101, n011, n111), temp);
let n_yz = mix(n_z.xy, n_z.zw, vec2f(f32(fade_xyz.y))); // simplify after chrome bug fix
let n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
return 2.2 * n_xyz;
fn mod289(x: vec4f) -> vec4f { return x - floor(x * (1. / 289.)) * 289.; }
fn perm4(x: vec4f) -> vec4f { return mod289(((x * 34.) + 1.) * x); }

fn noise3(p: vec3f) -> f32 {
let a = floor(p);
var d: vec3f = p - a;
d = d * d * (3. - 2. * d);

let b = a.xxyy + vec4f(0., 1., 0., 1.);
let k1 = perm4(b.xyxy);
let k2 = perm4(k1.xyxy + b.zzww);

let c = k2 + a.zzzz;
let k3 = perm4(c);
let k4 = perm4(c + 1.);

let o1 = fract(k3 * (1. / 41.));
let o2 = fract(k4 * (1. / 41.));

let o3 = o2 * d.z + o1 * (1. - d.z);
let o4 = o3.yw * d.x + o3.xz * (1. - d.x);

return o4.y * d.y + o4.x * (1. - d.y);
}

const m3: mat3x3f = mat3x3f(vec3f(0.8, 0.6, 0.4), vec3f(-0.6, 0.8, 0.4), vec3f(0.1, 0.2, 0.3));

fn fbm3d(_p: vec3f) -> f32 {
var f: f32 = 0.;
var p: vec3f = _p;
f = f + 0.5000 * perlinNoise3(p); p = m3 * p * 2.02;
f = f + 0.2500 * perlinNoise3(p); p = m3 * p * 2.03;
f = f + 0.1250 * perlinNoise3(p); p = m3 * p * 2.01;
f = f + 0.0625 * perlinNoise3(p);
f = f + 0.5000 * noise3(p); p = m3 * p * 2.02;
f = f + 0.2500 * noise3(p); p = m3 * p * 2.03;
f = f + 0.1250 * noise3(p); p = m3 * p * 2.01;
f = f + 0.0625 * noise3(p);
return f / 0.9375;
}
8 changes: 3 additions & 5 deletions pages/visualizer/shaders/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ out vec3 vNormal;
out vec3 vOuter;

void main() {
vec3 position = (p_position + (noise * .6) * v_position);
vec3 outer = normalize(p_position - vec3(0.));
vPosition = mat3(world)*p_position;
vNormal = mat3(world)*normal;
vOuter = mat3(world)*outer;
vec3 position = (p_position + (noise * .3) * v_position);
vec3 outer = normalize(p_position - vec3(0.));
float nMid = mid / 255.;
float nLow = low / 255.;
float nHigh = high / 255.;
gl_Position = worldViewProjection * vec4(((.8 + .2 * nLow ) * position + (2. + nMid * 20. + nHigh * 20.) * noise * outer), 1.);
gl_Position = worldViewProjection * vec4(((.8 + .2 * nLow ) * position + (4. + nMid * 20. + nHigh * 20.) * noise * outer), 1.);
}

0 comments on commit 195dde5

Please sign in to comment.