Draw translucent item quads without depth writes so water shows through glass items

This commit is contained in:
Kacpherek
2026-10-26 21:31:51 +01:00
parent cb43e32787
commit d75ef5e4f7
5 changed files with 88 additions and 9 deletions
+40 -5
View File
@@ -37,7 +37,9 @@ mod crumbling_tests;
mod entity_batch;
mod weather;
use entity_batch::EntityBatch;
pub use entity_batch::{SHADOW_OVERLAY, TRANSLUCENT_PARTICLE_OVERLAY, WATER_MASK_OVERLAY};
pub use entity_batch::{
SHADOW_OVERLAY, TRANSLUCENT_ITEM_OVERLAY, TRANSLUCENT_PARTICLE_OVERLAY, WATER_MASK_OVERLAY,
};
/// Window.getAppropriateLineWidth, in framebuffer pixels.
pub fn outline_width(width: u32) -> f32 {
@@ -391,6 +393,7 @@ pub struct Renderer {
weather: weather::Weather,
crumbling_pipeline: wgpu::RenderPipeline,
shadow_blob_pipeline: wgpu::RenderPipeline,
translucent_item_pipeline: wgpu::RenderPipeline,
/// `RenderPipelines.WATER_MASK`: depth writes only, no colour.
water_mask_pipeline: wgpu::RenderPipeline,
cloud_pipeline: wgpu::RenderPipeline,
@@ -1837,6 +1840,19 @@ impl Renderer {
alpha_blend,
false,
);
// RenderPipelines.ITEM_TRANSLUCENT: blended, depth tested. Drawn before
// translucent terrain, it cannot write depth: the water behind a
// glass item would fail the test and vanish (vanilla sorts these in
// its order independent transparency pass instead).
let translucent_item_pipeline = entity_pipeline(
"Translucent items",
None,
false,
wgpu::CompareFunction::GreaterEqual,
Default::default(),
alpha_blend,
false,
);
let cloud_pipeline = entity_pipeline(
"Clouds",
Some(wgpu::Face::Back),
@@ -2371,6 +2387,7 @@ impl Renderer {
weather,
crumbling_pipeline,
shadow_blob_pipeline,
translucent_item_pipeline,
water_mask_pipeline,
cloud_pipeline,
cloud_mesh: Default::default(),
@@ -3981,6 +3998,7 @@ impl Renderer {
water_mask_quads,
shadow_quads,
translucent_particle_quads,
translucent_item_quads,
] = self
.entity_batch
.update(entities, frame.additive, frame.hand, frame.see_through);
@@ -3990,7 +4008,8 @@ impl Renderer {
+ translucent_crumbling_quads
+ water_mask_quads
+ shadow_quads
+ translucent_particle_quads;
+ translucent_particle_quads
+ translucent_item_quads;
let cloud_quads = if let Some(input) = &frame.clouds {
let (rebuilt, uniform) =
crate::spans::time("clouds", || self.cloud_mesh.prepare(input));
@@ -4513,6 +4532,17 @@ impl Renderer {
pass.draw_indexed(0..entity_quads * 6, 0, 0..1);
pass.set_bind_group(0, &self.group, &[]);
}
if translucent_item_quads > 0 {
pass.set_pipeline(&self.translucent_item_pipeline);
pass.set_bind_group(0, &self.entity_group, &[]);
pass.set_vertex_buffer(0, self.entity_buffer.slice(..));
pass.draw_indexed(
0..translucent_item_quads * 6,
((world_quads - translucent_item_quads) * 4) as i32,
0..1,
);
pass.set_bind_group(0, &self.group, &[]);
}
if !pack_draws_weather {
self.weather.draw(&mut pass, &self.entity_group);
}
@@ -4530,7 +4560,11 @@ impl Renderer {
pass.set_vertex_buffer(0, self.entity_buffer.slice(..));
pass.draw_indexed(
0..shadow_quads * 6,
((world_quads - translucent_particle_quads - shadow_quads) * 4) as i32,
((world_quads
- translucent_item_quads
- translucent_particle_quads
- shadow_quads)
* 4) as i32,
0..1,
);
pass.set_bind_group(0, &self.group, &[]);
@@ -4611,7 +4645,8 @@ impl Renderer {
pass.set_vertex_buffer(0, self.entity_buffer.slice(..));
pass.draw_indexed(
0..translucent_particle_quads * 6,
((world_quads - translucent_particle_quads) * 4) as i32,
((world_quads - translucent_item_quads - translucent_particle_quads) * 4)
as i32,
0..1,
);
pass.set_bind_group(0, &self.group, &[]);
@@ -5240,7 +5275,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, 0, 1, 1]);
assert_eq!(counts, [4, 1, 1, 0, 1, 1, 0]);
assert_eq!(
vertices
.as_chunks::<4>()
@@ -5,14 +5,18 @@ use super::Vertex;
/// ordinary entities, breaking cracks (11), translucent breaking cracks (12),
/// the boat water mask (13, `RenderPipelines.WATER_MASK`), entity shadows
/// (14, `RenderPipelines.ENTITY_SHADOW`) and translucent-layer particles (15,
/// drawn after translucent terrain: `SubmitNodeCollection.submitQuadParticleGroup`).
const MATERIALS: usize = 6;
/// drawn after translucent terrain: `SubmitNodeCollection.submitQuadParticleGroup`)
/// and translucent item quads (32, drawn without depth writes).
const MATERIALS: usize = 7;
/// The overlay of depth-only quads that hide water (`RenderTypes.waterMask`).
pub const WATER_MASK_OVERLAY: u32 = 13;
/// The overlay of entity shadow quads (`RenderTypes.entityShadow`).
pub const SHADOW_OVERLAY: u32 = 14;
/// The overlay of translucent-layer particles (`SingleQuadParticle.Layer.translucent`).
pub const TRANSLUCENT_PARTICLE_OVERLAY: u32 = 15;
/// The overlay of item quads with translucent pixels (`Sheets.translucentItemSheet`,
/// `RenderPipelines.ITEM_TRANSLUCENT`); past the creeper swell range (16 to 31).
pub const TRANSLUCENT_ITEM_OVERLAY: u32 = 32;
#[derive(Default)]
pub(super) struct EntityBatch {
@@ -45,6 +49,7 @@ impl EntityBatch {
WATER_MASK_OVERLAY => 3,
SHADOW_OVERLAY => 4,
TRANSLUCENT_PARTICLE_OVERLAY => 5,
TRANSLUCENT_ITEM_OVERLAY => 6,
_ => 0,
};
self.buckets[material][counts[material]] = index;
+39 -2
View File
@@ -46,6 +46,9 @@ pub struct ItemQuad {
pub foil: Foil,
/// `MaterialInfo.lightEmission` (block light floor).
pub light_emission: u8,
/// The quad's sprite has translucent pixels (`MaterialInfo.itemRenderType`
/// is a translucent sheet): drawn with the translucent entities.
pub translucent: bool,
}
/// Which glint render type a quad uses.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -159,7 +162,7 @@ impl ItemRenderState {
let m = pose * layer.matrix;
match &layer.geometry {
LayerGeometry::Quads(baked) => {
submit_quads(baked, layer, self.display, m, sprites, sink);
submit_quads(renderer, baked, layer, self.display, m, sprites, sink);
}
LayerGeometry::Special { model, argument } => special::submit(
renderer,
@@ -203,6 +206,7 @@ pub fn argb(color: i32) -> u32 {
/// `ItemFeatureRenderer.prepareMainSubmit` for one cuboid layer.
fn submit_quads(
renderer: &ItemRenderer,
baked: &BakedItemModel,
layer: &LayerRender,
display: DisplayContext,
@@ -216,6 +220,8 @@ fn submit_quads(
FoilType::Special => Foil::Special,
};
let _ = display;
// Quads of one layer mostly share a sprite: look its translucency up once.
let mut last: Option<(&str, bool)> = None;
for q in &baked.quads {
let Some(r) = sprites
.get(sprite_name(&q.material.sprite))
@@ -239,6 +245,17 @@ fn submit_quads(
let pos = q.positions.map(|p| m.transform_point3(Vec3::from(p)));
let uv =
q.uv.map(|[u, v]| [r.u0 + (r.u1 - r.u0) * u, r.v0 + (r.v1 - r.v0) * v]);
let translucent = match last {
Some((sprite, translucent)) if sprite == q.material.sprite => translucent,
_ => {
let translucent = q.material.force_translucent
|| renderer
.sprites
.translucent(sprite_name(&q.material.sprite));
last = Some((&q.material.sprite, translucent));
translucent
}
};
sink.quad(ItemQuad {
pos,
uv,
@@ -246,6 +263,7 @@ fn submit_quads(
color: argb(color),
foil,
light_emission: q.light_emission,
translucent,
});
}
}
@@ -260,6 +278,9 @@ struct FileSprites {
root: PathBuf,
permutations: Vec<PalettedPermutations>,
cache: Mutex<HashMap<String, Option<SpriteMask>>>,
/// Sprites with a pixel that is neither fully transparent nor opaque
/// (`Transparency.hasTranslucent`), filled in as sprites load.
translucent: Mutex<HashMap<String, bool>>,
}
impl SpriteLookup for FileSprites {
fn mask(&self, sprite: &str) -> Option<SpriteMask> {
@@ -283,6 +304,13 @@ fn argb_pixels(rgba: &[u8]) -> Vec<u32> {
.collect()
}
impl FileSprites {
fn translucent(&self, sprite: &str) -> bool {
let known = |this: &Self| this.translucent.lock().ok()?.get(sprite).copied();
known(self).unwrap_or_else(|| {
self.mask(sprite);
known(self).unwrap_or(false)
})
}
fn png(&self, sprite: &str, directory: &str) -> PathBuf {
crate::atlas_sources::texture_path(&self.root, sprite, directory)
}
@@ -303,6 +331,10 @@ impl FileSprites {
(w, h, pixels)
}
};
let translucent = argb.iter().any(|p| matches!(p >> 24, 1..=254));
if let Ok(mut cache) = self.translucent.lock() {
cache.insert(sprite.to_owned(), translucent);
}
let alpha: Vec<u8> = argb.iter().map(|p| (p >> 24) as u8).collect();
let animation = fs::read_to_string(png.with_extension("png.mcmeta"))
.ok()
@@ -452,6 +484,7 @@ impl ItemRenderer {
root: root.to_path_buf(),
permutations,
cache: Mutex::default(),
translucent: Mutex::default(),
},
baked: Mutex::default(),
extents: Mutex::default(),
@@ -693,7 +726,11 @@ impl ItemSink for WorldSink<'_> {
} else {
self.light
};
let overlay = if q.foil == Foil::None { 0 } else { 7 };
let overlay = match (q.foil, q.translucent) {
(Foil::None, true) => crate::gfx::TRANSLUCENT_ITEM_OVERLAY,
(Foil::None, false) => 0,
_ => 7,
};
for (p, uv) in q.pos.iter().zip(q.uv) {
self.out.push(Vertex {
normal: [0; 4],
@@ -358,6 +358,7 @@ mod tests {
color,
foil: Foil::None,
light_emission: 0,
translucent: false,
}
}
@@ -119,6 +119,7 @@ fn draw(
color,
foil,
light_emission: 0,
translucent: false,
});
}
for (child_name, child) in &part.children {