gen_cloud_noise.wgsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Startup Compute Generators for:
  2. // 1. 128^3 Tileable Perlin-Worley + Worley FBM Base Cloud Volume
  3. // 2. 32^3 Tileable High-Frequency Worley Detail Cloud Volume
  4. // 3. 256^2 Tileable Winter Overcast Weather Map
  5. @group(0) @binding(0) var<storage, read_write> out_base: array<u32>;
  6. @group(0) @binding(1) var<storage, read_write> out_detail: array<u32>;
  7. @group(0) @binding(2) var out_weather: texture_storage_2d<rgba8unorm, write>;
  8. fn pack_rgba8(v: vec4<f32>) -> u32 {
  9. let c = vec4<u32>(clamp(v, vec4<f32>(0.0), vec4<f32>(1.0)) * 255.0 + 0.5);
  10. return c.r | (c.g << 8u) | (c.b << 16u) | (c.a << 24u);
  11. }
  12. fn hash33(p: vec3<f32>) -> vec3<f32> {
  13. var p3 = fract(p * vec3<f32>(0.1031, 0.1030, 0.0973));
  14. p3 += dot(p3, p3.yxz + 33.33);
  15. return fract((p3.xxy + p3.yxx) * p3.zyx);
  16. }
  17. fn wrap_cell(c: vec3<i32>, p_i: i32) -> vec3<f32> {
  18. return vec3<f32>(((c % p_i) + p_i) % p_i);
  19. }
  20. fn tile_worley3(uvw: vec3<f32>, period: f32) -> f32 {
  21. let p = uvw * period;
  22. let ip = vec3<i32>(floor(p));
  23. let fp = fract(p);
  24. let p_i = max(i32(period), 1);
  25. var min_d = 1.0;
  26. for (var idx = 0; idx < 27; idx = idx + 1) {
  27. let x = (idx % 3) - 1;
  28. let y = ((idx / 3) % 3) - 1;
  29. let z = (idx / 9) - 1;
  30. let off_i = vec3<i32>(x, y, z);
  31. let cell = wrap_cell(ip + off_i, p_i);
  32. let feat = hash33(cell);
  33. let d = length(vec3<f32>(off_i) + feat - fp);
  34. min_d = min(min_d, d);
  35. }
  36. return clamp(1.0 - min_d, 0.0, 1.0);
  37. }
  38. fn perlin_grad(ip: vec3<i32>, p_i: i32, off: vec3<i32>, fp: vec3<f32>) -> f32 {
  39. let g = hash33(wrap_cell(ip + off, p_i)) * 2.0 - 1.0;
  40. return dot(g, fp - vec3<f32>(off));
  41. }
  42. fn tile_perlin3(uvw: vec3<f32>, period: f32) -> f32 {
  43. let p = uvw * period;
  44. let ip = vec3<i32>(floor(p));
  45. let fp = fract(p);
  46. let u = fp * fp * (3.0 - 2.0 * fp);
  47. let p_i = max(i32(period), 1);
  48. let c000 = perlin_grad(ip, p_i, vec3<i32>(0, 0, 0), fp);
  49. let c100 = perlin_grad(ip, p_i, vec3<i32>(1, 0, 0), fp);
  50. let c010 = perlin_grad(ip, p_i, vec3<i32>(0, 1, 0), fp);
  51. let c110 = perlin_grad(ip, p_i, vec3<i32>(1, 1, 0), fp);
  52. let c001 = perlin_grad(ip, p_i, vec3<i32>(0, 0, 1), fp);
  53. let c101 = perlin_grad(ip, p_i, vec3<i32>(1, 0, 1), fp);
  54. let c011 = perlin_grad(ip, p_i, vec3<i32>(0, 1, 1), fp);
  55. let c111 = perlin_grad(ip, p_i, vec3<i32>(1, 1, 1), fp);
  56. let x00 = mix(c000, c100, u.x);
  57. let x10 = mix(c010, c110, u.x);
  58. let x01 = mix(c001, c101, u.x);
  59. let x11 = mix(c011, c111, u.x);
  60. let y0 = mix(x00, x10, u.y);
  61. let y1 = mix(x01, x11, u.y);
  62. return clamp(mix(y0, y1, u.z) * 0.65 + 0.5, 0.0, 1.0);
  63. }
  64. fn perlin_fbm(uvw: vec3<f32>) -> f32 {
  65. return 0.52 * tile_perlin3(uvw, 4.0)
  66. + 0.28 * tile_perlin3(uvw, 8.0)
  67. + 0.14 * tile_perlin3(uvw, 16.0)
  68. + 0.06 * tile_perlin3(uvw, 32.0);
  69. }
  70. fn worley_fbm(uvw: vec3<f32>, base_freq: f32) -> f32 {
  71. return 0.625 * tile_worley3(uvw, base_freq)
  72. + 0.25 * tile_worley3(uvw, base_freq * 2.0)
  73. + 0.125 * tile_worley3(uvw, base_freq * 4.0);
  74. }
  75. @compute @workgroup_size(4, 4, 4)
  76. fn gen_base(@builtin(global_invocation_id) gid: vec3<u32>) {
  77. if (gid.x >= 128u || gid.y >= 128u || gid.z >= 128u) {
  78. return;
  79. }
  80. let uvw = (vec3<f32>(gid) + 0.5) / 128.0;
  81. let p = perlin_fbm(uvw);
  82. let o4 = tile_worley3(uvw, 4.0);
  83. let o8 = tile_worley3(uvw, 8.0);
  84. let o16 = tile_worley3(uvw, 16.0);
  85. let o32 = tile_worley3(uvw, 32.0);
  86. let o64 = tile_worley3(uvw, 64.0);
  87. let w0 = 0.625 * o4 + 0.25 * o8 + 0.125 * o16;
  88. let w1 = 0.625 * o8 + 0.25 * o16 + 0.125 * o32;
  89. let w2 = 0.625 * o16 + 0.25 * o32 + 0.125 * o64;
  90. let pw = clamp(mix(p, w0, 0.42) * 1.15 - 0.08, 0.0, 1.0);
  91. let idx = (gid.z * 128u + gid.y) * 128u + gid.x;
  92. out_base[idx] = pack_rgba8(vec4<f32>(pw, w0, w1, w2));
  93. }
  94. @compute @workgroup_size(4, 4, 4)
  95. fn gen_detail(@builtin(global_invocation_id) gid: vec3<u32>) {
  96. if (gid.x >= 32u || gid.y >= 32u || gid.z >= 32u) {
  97. return;
  98. }
  99. let uvw = (vec3<f32>(gid) + 0.5) / 32.0;
  100. let d0 = worley_fbm(uvw, 4.0);
  101. let d1 = worley_fbm(uvw, 8.0);
  102. let d2 = worley_fbm(uvw, 12.0);
  103. let idx = (gid.z * 32u + gid.y) * 64u + gid.x;
  104. out_detail[idx] = pack_rgba8(vec4<f32>(d0, d1, d2, 1.0));
  105. }
  106. @compute @workgroup_size(8, 8, 1)
  107. fn gen_weather(@builtin(global_invocation_id) gid: vec3<u32>) {
  108. let dims = textureDimensions(out_weather);
  109. if (gid.x >= dims.x || gid.y >= dims.y) {
  110. return;
  111. }
  112. let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims);
  113. let uvw = vec3<f32>(uv.x, uv.y, 0.25);
  114. let cov = clamp(0.65 * tile_perlin3(uvw, 3.0) + 0.35 * tile_worley3(uvw, 5.0), 0.0, 1.0);
  115. let ctype = clamp(tile_perlin3(uvw + vec3<f32>(0.4, 0.1, 0.6), 4.0), 0.0, 1.0);
  116. let rain = clamp(tile_worley3(uvw + vec3<f32>(0.2, 0.7, 0.3), 6.0), 0.0, 1.0);
  117. textureStore(out_weather, vec2<i32>(gid.xy), vec4<f32>(cov, ctype, rain, 1.0));
  118. }