Wind-blown leaves and plants carry their own motion vectors

The terrain vertex shader built prev_clip from the current wind-displaced position plus the
camera step, so the motion buffer knew only the camera: leaves and plants sway at 0.2-0.6 Hz
with up to 0.14 blocks of amplitude, and their history lagged the sway — the shimmer on foliage
and the distant treeline. The wind is a sum of whole-cycle waves over 256 blocks and 600 s, so
the previous frame's displacement is the same sway evaluated at the previous wind time and
strength: gfx.rs keeps them in taa_prev and passes them in Globals.hd_motion.zw (this frame's
when there is no previous one, so the wind's own motion is zero then), and terrain.wgsl's
wind_offset_at evaluates them for prev_clip. The spatial argument stays this frame's camera
wrap: the vertex's world position does not move. World-pass colour is unchanged (prev_clip
feeds only the motion buffer); the shadow pass inherits the globals and ignores prev_clip.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-21 16:50:43 +03:00
parent 26ee82398d
commit acfb379aa8
3 changed files with 38 additions and 18 deletions
+20 -9
View File
@@ -324,7 +324,8 @@ struct Globals {
prev_view_proj: [[f32; 4]; 4],
/// xyz: the camera's translation since that frame.
camera_delta: [f32; 4],
/// Render-target width and height in pixels for current-to-previous motion vectors.
/// Render-target width and height in pixels for current-to-previous motion vectors; zw the
/// previous frame's wind seconds and strength (terrain.wgsl evaluates the wind at them).
hd_motion: [f32; 4],
}
pub type SectionKey = (i32, i32, i32);
@@ -752,9 +753,9 @@ pub struct Renderer {
/// The far cascade as last drawn (matrix, camera, light) and the frames since.
hd_far: Option<(Mat4, glam::DVec3, Vec3, u32)>,
/// HD's TAA: frames jittered so far, and the last one's unjittered view-projection, camera,
/// drawn (jittered) view-projection and jitter in render pixels.
/// drawn (jittered) view-projection, jitter in render pixels and wind (seconds, strength).
taa_frame: u32,
taa_prev: Option<(Mat4, glam::DVec3, Mat4, [f32; 2])>,
taa_prev: Option<(Mat4, glam::DVec3, Mat4, [f32; 2], (f32, f32))>,
hd: Option<hd::Hd>,
mod_params: (Vec<f32>, f32),
/// Time the shader pack's passes on the GPU (F3, or `HORIZON_SHADER_PACK_TIMING`).
@@ -4666,6 +4667,15 @@ impl Renderer {
// drawn matrix); each temporal consumer decodes it for its own history grid (gfx/hd).
let unjittered = self.view_projection(frame);
let taa = hd_wanted && hd::taa_enabled();
// This frame's wind (seconds, strength); the motion vectors evaluate the previous frame's
// (kept in taa_prev) so wind-blown leaves and plants carry their own motion.
let wind_now = if hd_wanted && hd::wind_enabled() {
// Stronger gusts in rain.
let rain = frame.sky.rain.clamp(0.0, 1.0);
(hd::wind_seconds(), 1.0 + rain * 1.2)
} else {
(0.0, 0.0)
};
let mut jitter_px = [0.0; 2];
let view_proj = if taa {
let window = [self.config.width, self.config.height];
@@ -4681,7 +4691,7 @@ impl Renderer {
};
let taa_prev = std::mem::replace(
&mut self.taa_prev,
taa.then_some((unjittered, frame.camera, view_proj, jitter_px)),
taa.then_some((unjittered, frame.camera, view_proj, jitter_px, wind_now)),
);
// AirDef fork: a cut starts the temporal histories over (no ghost of the last shot).
let taa_prev = if std::mem::take(&mut self.cut) {
@@ -4782,11 +4792,9 @@ impl Renderer {
lights
},
hd_wind: if hd_wanted && hd::wind_enabled() {
// Stronger gusts in rain.
let rain = frame.sky.rain.clamp(0.0, 1.0);
[
1.0 + rain * 1.2,
hd::wind_seconds(),
wind_now.1,
wind_now.0,
frame.camera.x.rem_euclid(256.0) as f32,
frame.camera.z.rem_euclid(256.0) as f32,
]
@@ -4801,7 +4809,10 @@ impl Renderer {
},
hd_motion: {
let size = self.world_post.size().unwrap_or([self.config.width, self.config.height]);
[size[0] as f32, size[1] as f32, 0.0, 0.0]
// zw: the previous frame's wind (seconds, strength) for the motion vectors; this
// frame's when there is no previous one, so the wind's own motion is zero then.
let was = taa_prev.map_or(wind_now, |p| p.4);
[size[0] as f32, size[1] as f32, was.0, was.1]
},
};
self.uploads
+4 -2
View File
@@ -8,8 +8,10 @@
//! - `levels` (`Rgba8Unorm`): x = sky light, y = block light, both the raw lightmap coordinate / 240
//! (0..1); zw unused.
//! - `motion` (`Rg16Float`): current-to-previous displacement in render-target pixels, with both
//! projections as actually drawn, jitter included. Mod models overwrite camera/static motion with
//! their previous drawn transforms in `motion_models_pass`; vanilla entities and wind remain camera-only.
//! projections as actually drawn, jitter included. Mod models and vanilla entities overwrite
//! camera/static motion with their previous drawn transforms (`motion_models_pass`,
//! `vs_entity_motion`); wind-blown leaves and plants carry the previous frame's wind displacement
//! (`terrain.wgsl` evaluates the wind at `Globals.hd_motion.zw`).
//! - `depth` (`Depth32Float`): camera depth, **reversed-Z** (cleared to 0 = far / nothing, 1 = near).
//! An untouched pixel (depth 0) is sky.
//! - Readers: AO, lighting, water composite, TAA/TAAU (depth). Written by `gbuffer_pass`; the caller
+14 -7
View File
@@ -161,9 +161,8 @@ fn material_params(mat_class: u32) -> vec2<f32> {
// AirDef fork, HD wind: waves of whole cycles over 256 blocks and 600 seconds, so they hold still
// in the world and do not jump when the camera's coordinates wrap. Returns the sway along the
// wind (x, z) and a flutter (y), each about -1..1.
fn wind_sway(w: vec2<f32>) -> vec3<f32> {
fn wind_sway(w: vec2<f32>, t: f32) -> vec3<f32> {
let tau = 6.2831853;
let t = globals.hd_wind.y;
let a1 = tau * (5.0 * w.x + 3.0 * w.y) / 256.0 + tau * 120.0 / 600.0 * t;
let a2 = tau * (-2.0 * w.x + 7.0 * w.y) / 256.0 + tau * 200.0 / 600.0 * t;
let a3 = tau * (23.0 * w.x - 17.0 * w.y) / 256.0 + tau * 350.0 / 600.0 * t;
@@ -177,11 +176,16 @@ fn wind_sway(w: vec2<f32>) -> vec3<f32> {
// continuous function of the world position, so neighbouring blocks stay joined; plants are
// pinned at the bottom edge of their sprite.
fn wind_offset(pos: vec3<f32>, uv: vec2<f32>, layer: u32) -> vec3<f32> {
if globals.hd_wind.x <= 0.0 { return vec3(0.0); }
return wind_offset_at(pos, uv, layer, globals.hd_wind.x, globals.hd_wind.y);
}
// The same sway at another wind strength and time: the motion vectors evaluate the previous
// frame's wind (globals.hd_motion.zw) so leaves and plants carry their own motion, not only the
// camera's. The spatial argument stays this frame's: the vertex's world position does not move.
fn wind_offset_at(pos: vec3<f32>, uv: vec2<f32>, layer: u32, k: f32, t: f32) -> vec3<f32> {
if k <= 0.0 { return vec3(0.0); }
let mat_class = material_class(layer);
if mat_class == 0u || mat_class > MAT_PLANT_UPPER { return vec3(0.0); }
let s = wind_sway(pos.xz + globals.hd_wind.zw);
let k = globals.hd_wind.x;
let s = wind_sway(pos.xz + globals.hd_wind.zw, t);
if mat_class == MAT_LEAVES {
return vec3(s.x, s.y * 0.5, s.z) * (0.025 * k);
}
@@ -194,9 +198,11 @@ fn wind_offset(pos: vec3<f32>, uv: vec2<f32>, layer: u32) -> vec3<f32> {
fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
light: vec2<u32>, origin: vec3<f32>) -> TerrainOut {
let pos = position + origin + wind_offset(position + origin, uv, layer);
let was = position + origin
+ wind_offset_at(position + origin, uv, layer, globals.hd_motion.w, globals.hd_motion.z);
var out: TerrainOut;
out.clip = globals.view_proj * vec4(pos, 1.0);
out.prev_clip = globals.prev_view_proj * vec4(pos + globals.camera_delta.xyz, 1.0);
out.prev_clip = globals.prev_view_proj * vec4(was + globals.camera_delta.xyz, 1.0);
out.uv = uv;
out.layer = layer;
let block = max(f32(light.y), dynamic_block_light(pos));
@@ -239,7 +245,8 @@ struct GBufferOut {
}
// The motion buffer's contract: current-to-previous in jittered render pixels (both matrices as
// drawn, so a still surface writes the two frames' jitter difference). The temporal passes decode
// it for their history grid (gfx/hd/taa.wgsl); DLSS receives it unchanged.
// it for their history grid (gfx/hd/taa.wgsl); DLSS receives it unchanged with the MVJittered flag
// (gfx/hd/dlss.rs), which tells NGX to undo the jitter difference itself.
fn motion_pixels(pos: vec3<f32>, previous: vec4<f32>) -> vec2<f32> {
let current = globals.view_proj * vec4(pos, 1.0);
if current.w <= 0.0 || previous.w <= 0.0 { return vec2<f32>(0.0); }