| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Startup Compute Generators for:
- // 1. 128^3 Tileable Perlin-Worley + Worley FBM Base Cloud Volume
- // 2. 32^3 Tileable High-Frequency Worley Detail Cloud Volume
- // 3. 256^2 Tileable Winter Overcast Weather Map
- @group(0) @binding(0) var<storage, read_write> out_base: array<u32>;
- @group(0) @binding(1) var<storage, read_write> out_detail: array<u32>;
- @group(0) @binding(2) var out_weather: texture_storage_2d<rgba8unorm, write>;
- fn pack_rgba8(v: vec4<f32>) -> u32 {
- let c = vec4<u32>(clamp(v, vec4<f32>(0.0), vec4<f32>(1.0)) * 255.0 + 0.5);
- return c.r | (c.g << 8u) | (c.b << 16u) | (c.a << 24u);
- }
- fn hash33(p: vec3<f32>) -> vec3<f32> {
- var p3 = fract(p * vec3<f32>(0.1031, 0.1030, 0.0973));
- p3 += dot(p3, p3.yxz + 33.33);
- return fract((p3.xxy + p3.yxx) * p3.zyx);
- }
- fn wrap_cell(c: vec3<i32>, p_i: i32) -> vec3<f32> {
- return vec3<f32>(((c % p_i) + p_i) % p_i);
- }
- fn tile_worley3(uvw: vec3<f32>, period: f32) -> f32 {
- let p = uvw * period;
- let ip = vec3<i32>(floor(p));
- let fp = fract(p);
- let p_i = max(i32(period), 1);
- var min_d = 1.0;
- for (var idx = 0; idx < 27; idx = idx + 1) {
- let x = (idx % 3) - 1;
- let y = ((idx / 3) % 3) - 1;
- let z = (idx / 9) - 1;
- let off_i = vec3<i32>(x, y, z);
- let cell = wrap_cell(ip + off_i, p_i);
- let feat = hash33(cell);
- let d = length(vec3<f32>(off_i) + feat - fp);
- min_d = min(min_d, d);
- }
- return clamp(1.0 - min_d, 0.0, 1.0);
- }
- fn perlin_grad(ip: vec3<i32>, p_i: i32, off: vec3<i32>, fp: vec3<f32>) -> f32 {
- let g = hash33(wrap_cell(ip + off, p_i)) * 2.0 - 1.0;
- return dot(g, fp - vec3<f32>(off));
- }
- fn tile_perlin3(uvw: vec3<f32>, period: f32) -> f32 {
- let p = uvw * period;
- let ip = vec3<i32>(floor(p));
- let fp = fract(p);
- let u = fp * fp * (3.0 - 2.0 * fp);
- let p_i = max(i32(period), 1);
- let c000 = perlin_grad(ip, p_i, vec3<i32>(0, 0, 0), fp);
- let c100 = perlin_grad(ip, p_i, vec3<i32>(1, 0, 0), fp);
- let c010 = perlin_grad(ip, p_i, vec3<i32>(0, 1, 0), fp);
- let c110 = perlin_grad(ip, p_i, vec3<i32>(1, 1, 0), fp);
- let c001 = perlin_grad(ip, p_i, vec3<i32>(0, 0, 1), fp);
- let c101 = perlin_grad(ip, p_i, vec3<i32>(1, 0, 1), fp);
- let c011 = perlin_grad(ip, p_i, vec3<i32>(0, 1, 1), fp);
- let c111 = perlin_grad(ip, p_i, vec3<i32>(1, 1, 1), fp);
- let x00 = mix(c000, c100, u.x);
- let x10 = mix(c010, c110, u.x);
- let x01 = mix(c001, c101, u.x);
- let x11 = mix(c011, c111, u.x);
- let y0 = mix(x00, x10, u.y);
- let y1 = mix(x01, x11, u.y);
- return clamp(mix(y0, y1, u.z) * 0.65 + 0.5, 0.0, 1.0);
- }
- fn perlin_fbm(uvw: vec3<f32>) -> f32 {
- return 0.52 * tile_perlin3(uvw, 4.0)
- + 0.28 * tile_perlin3(uvw, 8.0)
- + 0.14 * tile_perlin3(uvw, 16.0)
- + 0.06 * tile_perlin3(uvw, 32.0);
- }
- fn worley_fbm(uvw: vec3<f32>, base_freq: f32) -> f32 {
- return 0.625 * tile_worley3(uvw, base_freq)
- + 0.25 * tile_worley3(uvw, base_freq * 2.0)
- + 0.125 * tile_worley3(uvw, base_freq * 4.0);
- }
- @compute @workgroup_size(4, 4, 4)
- fn gen_base(@builtin(global_invocation_id) gid: vec3<u32>) {
- if (gid.x >= 128u || gid.y >= 128u || gid.z >= 128u) {
- return;
- }
- let uvw = (vec3<f32>(gid) + 0.5) / 128.0;
- let p = perlin_fbm(uvw);
- let o4 = tile_worley3(uvw, 4.0);
- let o8 = tile_worley3(uvw, 8.0);
- let o16 = tile_worley3(uvw, 16.0);
- let o32 = tile_worley3(uvw, 32.0);
- let o64 = tile_worley3(uvw, 64.0);
- let w0 = 0.625 * o4 + 0.25 * o8 + 0.125 * o16;
- let w1 = 0.625 * o8 + 0.25 * o16 + 0.125 * o32;
- let w2 = 0.625 * o16 + 0.25 * o32 + 0.125 * o64;
- let pw = clamp(mix(p, w0, 0.42) * 1.15 - 0.08, 0.0, 1.0);
- let idx = (gid.z * 128u + gid.y) * 128u + gid.x;
- out_base[idx] = pack_rgba8(vec4<f32>(pw, w0, w1, w2));
- }
- @compute @workgroup_size(4, 4, 4)
- fn gen_detail(@builtin(global_invocation_id) gid: vec3<u32>) {
- if (gid.x >= 32u || gid.y >= 32u || gid.z >= 32u) {
- return;
- }
- let uvw = (vec3<f32>(gid) + 0.5) / 32.0;
- let d0 = worley_fbm(uvw, 4.0);
- let d1 = worley_fbm(uvw, 8.0);
- let d2 = worley_fbm(uvw, 12.0);
- let idx = (gid.z * 32u + gid.y) * 64u + gid.x;
- out_detail[idx] = pack_rgba8(vec4<f32>(d0, d1, d2, 1.0));
- }
- @compute @workgroup_size(8, 8, 1)
- fn gen_weather(@builtin(global_invocation_id) gid: vec3<u32>) {
- let dims = textureDimensions(out_weather);
- if (gid.x >= dims.x || gid.y >= dims.y) {
- return;
- }
- let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims);
- let uvw = vec3<f32>(uv.x, uv.y, 0.25);
- let cov = clamp(0.65 * tile_perlin3(uvw, 3.0) + 0.35 * tile_worley3(uvw, 5.0), 0.0, 1.0);
- let ctype = clamp(tile_perlin3(uvw + vec3<f32>(0.4, 0.1, 0.6), 4.0), 0.0, 1.0);
- let rain = clamp(tile_worley3(uvw + vec3<f32>(0.2, 0.7, 0.3), 6.0), 0.0, 1.0);
- textureStore(out_weather, vec2<i32>(gid.xy), vec4<f32>(cov, ctype, rain, 1.0));
- }
|