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.
1// Closed-form river-wave synthesis shared by every GPU consumer: the cascade
2// material's vertex path, motion prepass, and wave-query compute pass.
4#define_import_path bevy_aqua_core::river
6/// Analytic sum-of-sines displacement for river bodies.
7///
8/// Small bodies use the closed-form path: three
9/// components travelling along the baked local current, deep-water
10/// dispersion omega = sqrt(g k), amplitude scaled by channel width and
11/// bank proximity. Visible deformation, motion vectors, and WaveQuery import
12/// this one implementation.
13const RIVER_WAVELENGTHS: vec3<f32> = vec3(13.0, 5.5, 2.3);
14const RIVER_BASE_AMPLITUDES: vec3<f32> = vec3(0.16, 0.085, 0.04);
16fn river_analytic_displacement(world_xz: vec2<f32>, flow_sample: vec4<f32>, time: f32) -> vec3<f32> {
17 let speed = length(flow_sample.xy);
18 if speed < 1e-4 {
19 return vec3(0.0);
20 }
21 let direction = flow_sample.xy / speed;
22 let bank_fade = clamp(flow_sample.z / max(flow_sample.w, 1e-3), 0.0, 1.0);
23 let width_scale = clamp(flow_sample.w * 0.0 + flow_sample.z * 0.06, 0.5, 1.4);
24 let speed_scale = clamp(0.5 + 0.35 * speed, 0.5, 1.5);
25 let amplitude_scale = bank_fade * width_scale * speed_scale;
27 var height = 0.0;
28 var slope_x = 0.0;
29 var slope_z = 0.0;
30 for (var index = 0u; index < 3u; index += 1u) {
31 let wavelength = RIVER_WAVELENGTHS[index];
32 let k = 6.283185307179586 / wavelength;
33 let omega = sqrt(9.80665 * k);
34 let amplitude = RIVER_BASE_AMPLITUDES[index] * amplitude_scale;
35 // Advect the pattern with the current so waves travel downstream.
36 let advected = world_xz - flow_sample.xy * time;
37 let phase = k * dot(direction, advected) - omega * time;
38 height += amplitude * sin(phase);
39 slope_x += amplitude * k * direction.x * cos(phase);
40 slope_z += amplitude * k * direction.y * cos(phase);
41 }
42 return vec3(height, slope_x, slope_z);
Contents