Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1const FFT_RESOLUTION: u32 = 256u;
2const LOD_COUNT: u32 = 5u;
3const ATTENUATION_BINS: u32 = 4u;
5struct CascadeParams {
6 center: vec2<f32>,
7 scale: f32,
8 texture_res: f32,
9 inv_texture_res: f32,
10 texel_width: f32,
11 weight: f32,
12 max_wavelength: f32,
14struct CascadeLayout {
15 cascades: array<CascadeParams, 6>,
16 center: vec4<f32>,
17 // XY bed-map first-texel world origin, ZW inverse world extent.
18 bed_transform: vec4<f32>,
19 // X height minimum, Y height span (negative = no bed map), Z sea level.
20 bed_range: vec4<f32>,
22struct FftUniform {
23 cascade_layout: CascadeLayout,
24 params: vec4<f32>,
25 // x: active attenuation-bin count (1 or ATTENUATION_BINS).
26 mode: vec4<f32>,
29// Decoded "no bed data" depth: matches a cleared full-depth capture texel.
30const NO_BED_DEPTH: f32 = 256.0;
32/// Water depth under the sea level from the game's bed height map. Outside
33/// the mapped area (or with no map at all) the sample keeps the deep default,
34/// mirroring a full-depth capture texel. Nearest-texel read: the shoaling
35/// profile is smooth enough that one bed texel per cascade texel suffices.
36fn bed_water_depth(world_xz: vec2<f32>) -> f32 {
37 let range = fft.cascade_layout.bed_range;
38 if range.y < 0.0 {
39 return NO_BED_DEPTH;
40 }
41 let uv = (world_xz - fft.cascade_layout.bed_transform.xy)
42 * fft.cascade_layout.bed_transform.zw;
43 if any(uv < vec2(0.0)) || any(uv > vec2(1.0)) {
44 return NO_BED_DEPTH;
45 }
46 let dimensions = vec2<u32>(textureDimensions(bed_height));
47 let maximum = vec2<i32>(dimensions - vec2<u32>(1u));
48 let texel = clamp(vec2<i32>(round(uv * vec2<f32>(maximum))), vec2(i32(0)), maximum);
49 let height = f32(textureLoad(bed_height, texel, 0).r) * range.y + range.x;
50 return max(range.z - height, 0.0);
53@group(0) @binding(0) var height_x: texture_2d_array<f32>;
54@group(0) @binding(1) var z_field: texture_2d_array<f32>;
55@group(0) @binding(2) var bed_height: texture_2d<f32>;
56@group(0) @binding(3) var output: texture_storage_2d_array<rgba16float, write>;
57@group(0) @binding(4) var<uniform> fft: FftUniform;
59fn shoaling_weights(depth: f32, wave_number: f32) -> vec2<f32> {
60 let relative_depth = clamp(depth * wave_number / 3.141592653589793, 0.0, 1.0);
61 let vertical_base = smoothstep(0.0, 1.0, relative_depth);
62 let breaker = 4.0 * vertical_base * (1.0 - vertical_base);
63 let vertical = vertical_base * (1.0 + 0.18 * breaker);
64 let chop_ratio = mix(0.55, 1.0, smoothstep(0.15, 0.85, vertical_base));
65 return vec2(vertical * chop_ratio, vertical);
68@compute @workgroup_size(8, 8, 1)
69fn resolve(@builtin(global_invocation_id) id: vec3<u32>) {
70 if any(id.xy >= vec2<u32>(FFT_RESOLUTION)) || id.z >= LOD_COUNT { return; }
71 let bins = u32(fft.mode.x);
72 let normalization = 1.0 / f32(FFT_RESOLUTION * FFT_RESOLUTION);
73 let cascade = fft.cascade_layout.cascades[id.z];
74 var displacement = vec3(0.0);
75 if bins == 1u {
76 // Deep-water fast path: shoaling is exactly vec2(1.0) for every bin,
77 // so the four-bin sum reduces to this one layer with no depth read.
78 let packed = textureLoad(height_x, vec2<i32>(id.xy), i32(id.z), 0);
79 let z = textureLoad(z_field, vec2<i32>(id.xy), i32(id.z), 0).x;
80 displacement = vec3(packed.z, packed.x, z) * normalization;
81 } else {
82 let coverage = cascade.texel_width * cascade.texture_res;
83 let uv = (vec2<f32>(id.xy) + vec2(0.5)) / vec2<f32>(cascade.texture_res);
84 let depth = bed_water_depth(coverage * (uv - vec2(0.5)) + cascade.center);
85 for (var bin = 0u; bin < bins; bin += 1u) {
86 let layer = id.z * bins + bin;
87 let packed = textureLoad(height_x, vec2<i32>(id.xy), i32(layer), 0);
88 let z = textureLoad(z_field, vec2<i32>(id.xy), i32(layer), 0).x;
89 let octave_fraction = (f32(bin) + 0.5) / f32(bins);
90 let representative_wavelength = 0.5 * cascade.max_wavelength
91 * exp2(octave_fraction);
92 let wave_number = 2.0 * 3.141592653589793 / representative_wavelength;
93 let shoaling = shoaling_weights(depth, wave_number);
94 let attenuation = mix(vec2(1.0), shoaling, fft.params.y);
95 displacement += vec3(
96 packed.z * attenuation.x,
97 packed.x * attenuation.y,
98 z * attenuation.x,
99 ) * normalization;
100 }
101 }
102 textureStore(output, vec2<i32>(id.xy), i32(id.z), vec4(displacement, 0.0));
Contents