From e3cea08736907a624a37c3207bc7993b7a55516c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 18:26:23 +0200 Subject: [PATCH] Boats: the water patch hides water inside the hull; the riding camera turns smoothly BoatRenderer draws its water_patch part with RenderTypes.waterMask (depth only) unless the boat is under water, so translucent water behind it fails the depth test inside the hull. Horizon loaded the part but never drew it: the water surface showed through the boat. The patch now draws with a depth-only Water mask pipeline after the entities and before translucent terrain (rafts have none, as RaftRenderer). Turning in a boat stepped the camera at the tick rate: positionRider adds the boat's deltaRotation to the passenger's yRot after its yRotO was kept, and LocalPlayer.getViewYRot interpolates a passenger's yaw between them. The camera now spreads the turn over the tick's frames. Fixes #1016 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity.wgsl | 5 ++ crates/horizon-client/src/game/boats.rs | 20 +++++ crates/horizon-client/src/game/perspective.rs | 10 ++- crates/horizon-client/src/gfx.rs | 80 ++++++++++++++++--- crates/horizon-client/src/gfx/entity_batch.rs | 23 ++++-- 5 files changed, 118 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl index c6305c697..aa384f711 100644 --- a/crates/horizon-client/src/entity.wgsl +++ b/crates/horizon-client/src/entity.wgsl @@ -188,3 +188,8 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { let color = textureSample(atlas, atlas_sampler, in.uv) * in.color * in.light_color; return color; } + +// RenderPipelines.WATER_MASK: depth only (the pipeline masks the colour off). +@fragment fn fs_water_mask() -> @location(0) vec4 { + return vec4(0.0); +} diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index f9740598e..433c588d0 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -141,6 +141,8 @@ pub(super) fn boat_wood(item: &str) -> Option<&str> { pub(super) struct BoatModels { pub models: HashMap, pub chest_models: HashMap, + /// `ModelLayers.BOAT_WATER_PATCH`, shared by every boat (not rafts). + pub water_patch: Option, } impl BoatModels { /// Whether a boat of this wood (with or without a chest) can be drawn. @@ -175,6 +177,7 @@ impl BoatModels { Self { models, chest_models, + water_patch: document.and_then(|d| d.model("minecraft:boat#water_patch")), } } } @@ -785,6 +788,23 @@ impl Game<'_> { 0, out, ); + // BoatRenderer.submitTypeAdditions: the water patch hides the water + // inside the hull unless the boat is under water (RaftRenderer has none). + if let Some(patch) = self.boat_models.water_patch.as_ref() + && &*b.wood != "bamboo" + && !matches!(b.status, Status::UnderWater | Status::UnderFlowingWater) + { + horizon_client::mobs::render::draw( + patch, + base, + &Poses::new(patch), + uv, + 0xFFFF_FFFF, + packed, + horizon_client::gfx::WATER_MASK_OVERLAY, + out, + ); + } self.cull_entity_vertices(out, entity_start); } } diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index cdc7fa2d8..ec46120dd 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -223,6 +223,14 @@ impl Game<'_> { .riding .and_then(|id| self.mobs.iter().find(|m| m.id == id)) .map(|_| (1.0, 4.0)); + // AbstractBoat.positionRider turns a passenger by the boat's + // deltaRotation after its yRotO was saved, and LocalPlayer.getViewYRot + // interpolates a passenger's yaw: the boat's turn is spread over the + // frames of a tick. + let ride_turn = self + .riding + .and_then(|id| self.boats.iter().find(|b| b.id == id)) + .map_or(0.0, |b| b.delta_rotation); let entity = CameraEntity { position: [p.position.x, p.position.y, p.position.z], previous: [ @@ -235,7 +243,7 @@ impl Game<'_> { x_rot: self.pitch, y_rot: self.yaw, x_rot_o: self.pitch, - y_rot_o: self.yaw, + y_rot_o: self.yaw - ride_turn, eye_height: p.dimensions.eye_height, living: Some(LivingCamera { scale, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 5141b6089..304352918 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -34,6 +34,7 @@ fn entity_shader() -> String { mod entity_batch; use entity_batch::EntityBatch; +pub use entity_batch::WATER_MASK_OVERLAY; /// Window.getAppropriateLineWidth, in framebuffer pixels. pub fn outline_width(width: u32) -> f32 { @@ -317,6 +318,8 @@ pub struct Renderer { crosshair_globals: wgpu::Buffer, entity_pipeline: wgpu::RenderPipeline, crumbling_pipeline: wgpu::RenderPipeline, + /// `RenderPipelines.WATER_MASK`: depth writes only, no colour. + water_mask_pipeline: wgpu::RenderPipeline, cloud_pipeline: wgpu::RenderPipeline, cloud_mesh: crate::clouds::Mesh, cloud_buffer: wgpu::Buffer, @@ -1539,7 +1542,16 @@ impl Renderer { step_mode: wgpu::VertexStepMode::Vertex, attributes: &entity_attributes, })]; - let entity_pipeline = |label, cull_mode, depth_write, compare, bias, blend, clouds| { + #[allow(clippy::too_many_arguments)] + let entity_pipeline_with = |label, + cull_mode, + depth_write, + compare, + bias, + blend, + clouds, + fragment: &str, + write_mask| { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(label), layout: Some(if clouds { @@ -1567,18 +1579,43 @@ impl Renderer { multisample: Default::default(), fragment: Some(wgpu::FragmentState { module: &entity_shader, - entry_point: Some("fs_entity"), + entry_point: Some(fragment), compilation_options: Default::default(), targets: &[Some(wgpu::ColorTargetState { format: config.format.remove_srgb_suffix(), blend: Some(blend), - write_mask: wgpu::ColorWrites::ALL, + write_mask, })], }), multiview_mask: None, cache: None, }) }; + let entity_pipeline = |label, cull_mode, depth_write, compare, bias, blend, clouds| { + entity_pipeline_with( + label, + cull_mode, + depth_write, + compare, + bias, + blend, + clouds, + "fs_entity", + wgpu::ColorWrites::ALL, + ) + }; + // RenderPipelines.WATER_MASK: the boat's water patch writes depth only. + let water_mask_pipeline = entity_pipeline_with( + "Water mask", + None, + true, + wgpu::CompareFunction::GreaterEqual, + Default::default(), + alpha_blend, + false, + "fs_water_mask", + wgpu::ColorWrites::empty(), + ); let crumbling_pipeline = entity_pipeline( "Breaking cracks", Some(wgpu::Face::Back), @@ -2058,6 +2095,7 @@ impl Renderer { crosshair_globals, entity_pipeline, crumbling_pipeline, + water_mask_pipeline, cloud_pipeline, cloud_mesh: Default::default(), cloud_buffer, @@ -3183,14 +3221,19 @@ impl Renderer { self.uploads .buffer(&mut encoder, &self.outline, 0, bytemuck::cast_slice(&lines)); } - let [entity_quads, crumbling_quads, translucent_crumbling_quads] = - self.entity_batch.update( - frame.entities, - frame.additive, - frame.hand, - frame.see_through, - ); - let world_quads = entity_quads + crumbling_quads + translucent_crumbling_quads; + let [ + entity_quads, + crumbling_quads, + translucent_crumbling_quads, + water_mask_quads, + ] = self.entity_batch.update( + frame.entities, + frame.additive, + frame.hand, + frame.see_through, + ); + let world_quads = + entity_quads + crumbling_quads + translucent_crumbling_quads + water_mask_quads; let cloud_quads = if let Some(input) = &frame.clouds { let (rebuilt, uniform) = crate::spans::time("clouds", || self.cloud_mesh.prepare(input)); @@ -3552,6 +3595,19 @@ impl Renderer { pass.draw_indexed(0..crumbling_quads * 6, (entity_quads * 4) as i32, 0..1); pass.set_bind_group(0, &self.group, &[]); } + // BoatRenderer's water patch (RenderTypes.waterMask): depth only, + // before translucent terrain, so no water shows inside the boat. + if water_mask_quads > 0 { + pass.set_pipeline(&self.water_mask_pipeline); + pass.set_bind_group(0, &self.entity_group, &[]); + pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); + pass.draw_indexed( + 0..water_mask_quads * 6, + ((entity_quads + crumbling_quads + translucent_crumbling_quads) * 4) as i32, + 0..1, + ); + pass.set_bind_group(0, &self.group, &[]); + } if additive_quads > 0 { pass.set_pipeline(&self.additive_pipeline); pass.set_bind_group(0, &self.entity_group, &[]); @@ -4116,7 +4172,7 @@ mod tests { let mut batch = super::EntityBatch::default(); let counts = batch.update(&entities, &quad(3, 5), &quad(0, 6), &quad(0, 8)); let vertices = &batch.combined; - assert_eq!(counts, [4, 1, 1]); + assert_eq!(counts, [4, 1, 1, 0]); assert_eq!( vertices .as_chunks::<4>() diff --git a/crates/horizon-client/src/gfx/entity_batch.rs b/crates/horizon-client/src/gfx/entity_batch.rs index e7bfccb96..999113761 100644 --- a/crates/horizon-client/src/gfx/entity_batch.rs +++ b/crates/horizon-client/src/gfx/entity_batch.rs @@ -1,12 +1,19 @@ //! Stable entity material grouping with renderer-owned, reusable CPU scratch. use super::Vertex; +/// Entity materials by a quad's overlay (`Vertex::layer`), in draw order: +/// ordinary entities, breaking cracks (11), translucent breaking cracks (12) +/// and the boat water mask (13, `RenderPipelines.WATER_MASK`). +const MATERIALS: usize = 4; +/// The overlay of depth-only quads that hide water (`RenderTypes.waterMask`). +pub const WATER_MASK_OVERLAY: u32 = 13; + #[derive(Default)] pub(super) struct EntityBatch { // Quad indices avoid copying vertices twice while grouping materials. // Keep initialized high-water storage; local counts delimit active entries. // Each bucket can hold all quads, removing per-quad Vec growth checks. - buckets: [Vec; 3], + buckets: [Vec; MATERIALS], pub(super) combined: Vec, } @@ -17,18 +24,19 @@ impl EntityBatch { additive: &[Vertex], hand: &[Vertex], see_through: &[Vertex], - ) -> [u32; 3] { + ) -> [u32; MATERIALS] { let quads = entities.as_chunks::<4>().0; for bucket in &mut self.buckets { if bucket.len() < quads.len() { bucket.resize(quads.len(), 0); } } - let mut counts = [0usize; 3]; + let mut counts = [0usize; MATERIALS]; for (index, quad) in quads.iter().enumerate() { let material = match quad[0].layer { 11 => 1, 12 => 2, + WATER_MASK_OVERLAY => 3, _ => 0, }; self.buckets[material][counts[material]] = index; @@ -77,14 +85,15 @@ mod tests { entities: &[Vertex], additive: &[Vertex], hand: &[Vertex], - ) -> (Vec, [u32; 3]) { + ) -> (Vec, [u32; MATERIALS]) { let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len()); - let mut counts = [0; 3]; - for (i, layer) in [0, 11, 12].into_iter().enumerate() { + let mut counts = [0; MATERIALS]; + for (i, layer) in [0, 11, 12, 13].into_iter().enumerate() { for quad in entities.as_chunks::<4>().0 { let material = match quad[0].layer { 11 => 11, 12 => 12, + 13 => 13, _ => 0, }; if material == layer { @@ -104,7 +113,7 @@ mod tests { pos: [index as f32, 2.0, 3.0], uv: [index as f32, 5.0], layer: if mixed { - [7, 11, 12, 4, 0][index / 4 % 5] + [7, 11, 12, 4, 13, 0][index / 4 % 6] } else { 0 },