Merge pull request #121 from ludovic111/fix/creeper-white-flash
OverlayTexture texels from one tested table; creepers flash white while swelling
This commit is contained in:
Generated
+1
@@ -889,6 +889,7 @@ dependencies = [
|
||||
"horizon-world",
|
||||
"log",
|
||||
"miniz_oxide 0.8.9",
|
||||
"naga",
|
||||
"png",
|
||||
"pollster",
|
||||
"serde_json",
|
||||
|
||||
@@ -37,3 +37,7 @@ rng-trace = ["horizon-core/rng-trace"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
# Parses and validates the composed entity shader (already built for wgpu).
|
||||
naga = { version = "30.0.1", features = ["wgsl-in"] }
|
||||
|
||||
@@ -48,12 +48,14 @@ fn entity_light(normal: vec3<f32>, nether: bool) -> f32 {
|
||||
let l1 = normalize(vec3(-0.2, select(1.0, -1.0, nether), 0.7));
|
||||
return min(1.0, (max(0.0, dot(l0, normal)) + max(0.0, dot(l1, normal))) * 0.6 + 0.4);
|
||||
}
|
||||
// The OverlayTexture texel entity.vsh fetches: rows 0-7 hold the hurt red
|
||||
// 0xB2FF0000; row 10 is white with alpha (1 - u / 15 * 0.75) * 255, u = 15
|
||||
// for the primed TNT flash (OverlayTexture.u(1.0)), so 63. Alpha 1 changes nothing.
|
||||
// The OverlayTexture texel entity.vsh fetches (constants from overlay.rs):
|
||||
// the hurt red for overlays 1 and 8, the white row at column 15 for the
|
||||
// primed TNT flash (5) and at column overlay - 16 for 16..=31 (a creeper's
|
||||
// swelling). Alpha 1 changes nothing.
|
||||
fn overlay_texel(overlay: u32) -> vec4<f32> {
|
||||
if overlay == 1u || overlay == 8u { return vec4(1.0, 0.0, 0.0, 178.0 / 255.0); }
|
||||
if overlay == 5u { return vec4(1.0, 1.0, 1.0, 63.0 / 255.0); }
|
||||
if overlay == 1u || overlay == 8u { return HURT_OVERLAY; }
|
||||
if overlay == 5u { return vec4(1.0, 1.0, 1.0, WHITE_OVERLAY_ALPHA[15]); }
|
||||
if overlay >= 16u && overlay < 32u { return vec4(1.0, 1.0, 1.0, WHITE_OVERLAY_ALPHA[overlay - 16u]); }
|
||||
return vec4(1.0);
|
||||
}
|
||||
@vertex fn vs_entity(@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
|
||||
|
||||
@@ -23,6 +23,11 @@ const CRUMBLING_BLEND: wgpu::BlendState = wgpu::BlendState {
|
||||
alpha: wgpu::BlendComponent::REPLACE,
|
||||
};
|
||||
|
||||
/// entity.wgsl after the OverlayTexture constants it reads.
|
||||
fn entity_shader() -> String {
|
||||
crate::overlay::wgsl_constants() + include_str!("entity.wgsl")
|
||||
}
|
||||
|
||||
// Separate special materials without changing entity order or their overlays.
|
||||
fn batch_entities(
|
||||
entities: &[Vertex],
|
||||
@@ -826,7 +831,7 @@ impl Renderer {
|
||||
hud.atlas_texture = Some(atlas.clone());
|
||||
let entity_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Horizon entities"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("entity.wgsl").into()),
|
||||
source: wgpu::ShaderSource::Wgsl(entity_shader().into()),
|
||||
});
|
||||
let entity_bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("Entity bindings"),
|
||||
@@ -2573,6 +2578,16 @@ mod tests {
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn entity_shader_validates_with_its_overlay_constants() {
|
||||
let module = naga::front::wgsl::parse_str(&super::entity_shader()).unwrap();
|
||||
naga::valid::Validator::new(
|
||||
naga::valid::ValidationFlags::all(),
|
||||
naga::valid::Capabilities::all(),
|
||||
)
|
||||
.validate(&module)
|
||||
.unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn cracks_multiply_instead_of_covering_the_block() {
|
||||
let blend = super::CRUMBLING_BLEND;
|
||||
assert_eq!(blend.color.src_factor, wgpu::BlendFactor::Dst);
|
||||
|
||||
@@ -97,6 +97,7 @@ pub mod options_file;
|
||||
pub mod options_fixes;
|
||||
pub mod options_horizon;
|
||||
pub mod options_ui;
|
||||
pub mod overlay;
|
||||
pub mod pack_screen;
|
||||
pub mod packs;
|
||||
pub mod particles;
|
||||
|
||||
@@ -778,6 +778,25 @@ fn species_rotations(m: &Mob, alpha: f32) -> glam::Mat4 {
|
||||
_ => glam::Mat4::IDENTITY,
|
||||
}
|
||||
}
|
||||
/// Creeper.getSwelling: the fuse's progress, past 1 in its last two ticks.
|
||||
fn creeper_swelling(m: &Mob, alpha: f32) -> f32 {
|
||||
(m.previous_swell as f32 + alpha * (m.swell - m.previous_swell) as f32) / 28.0
|
||||
}
|
||||
/// LivingEntityRenderer.getOverlayCoords for the body: the hurt red, or a
|
||||
/// swelling creeper's white flash. Layers take the red only (progress 0).
|
||||
fn body_overlay(m: &Mob, alpha: f32) -> u32 {
|
||||
let white = if m.species() == Species::Creeper {
|
||||
crate::overlay::creeper_progress(creeper_swelling(m, alpha))
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
crate::overlay::living(white, red_overlay(m))
|
||||
}
|
||||
/// LivingEntityRenderState.hasRedOverlay: hurt or dying (not a creaking
|
||||
/// tearing down).
|
||||
fn red_overlay(m: &Mob) -> bool {
|
||||
m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m))
|
||||
}
|
||||
fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 {
|
||||
match m.species() {
|
||||
Species::Wither => {
|
||||
@@ -785,10 +804,9 @@ fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 {
|
||||
}
|
||||
Species::Creeper => {
|
||||
// CreeperRenderer.scale: swelling with a wobble.
|
||||
let s = ((m.previous_swell as f32 + (m.swell - m.previous_swell) as f32 * alpha)
|
||||
/ 28.0)
|
||||
.clamp(0.0, 1.0);
|
||||
let wobble = 1.0 + (s * 100.0).msin() * s * 0.01;
|
||||
let g = creeper_swelling(m, alpha);
|
||||
let wobble = 1.0 + (g * 100.0).msin() * g * 0.01;
|
||||
let s = g.clamp(0.0, 1.0);
|
||||
let s4 = s * s * s * s;
|
||||
let xz = (1.0 + s4 * 0.4) * wobble;
|
||||
let y = (1.0 + s4 * 0.1) / wobble;
|
||||
@@ -923,8 +941,7 @@ fn render_visible(
|
||||
out: &mut Vec<Vertex>,
|
||||
) {
|
||||
let base = base_transform(m, alpha, camera);
|
||||
let overlay =
|
||||
u32::from(m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m)));
|
||||
let overlay = u32::from(red_overlay(m));
|
||||
let s = anim::state(m, alpha);
|
||||
let (model_key, texture) = body(m);
|
||||
let model = m
|
||||
@@ -942,7 +959,7 @@ fn render_visible(
|
||||
uv,
|
||||
body_color(m, alpha),
|
||||
light,
|
||||
overlay,
|
||||
body_overlay(m, alpha),
|
||||
out,
|
||||
);
|
||||
}
|
||||
@@ -1013,8 +1030,7 @@ pub fn render_single(
|
||||
out: &mut Vec<Vertex>,
|
||||
) {
|
||||
let base = base_transform(m, alpha, camera);
|
||||
let overlay =
|
||||
u32::from(m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m)));
|
||||
let overlay = body_overlay(m, alpha);
|
||||
let s = anim::state(m, alpha);
|
||||
let model = m.kind.model.clone();
|
||||
let mut poses = Poses::new(&model);
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
//! OverlayTexture: the hurt red and white flash entity.wgsl mixes over a lit
|
||||
//! entity before the lightmap (entity.fsh), and the overlay codes models carry.
|
||||
//!
|
||||
//! Vanilla's 16 by 16 texture: rows 0 to 7 are the hurt red, row 10 is white
|
||||
//! with an alpha falling with u; LivingEntityRenderer.getOverlayCoords picks
|
||||
//! u from the white overlay progress and row 3 when the entity is hurt.
|
||||
|
||||
/// The overlay code of white flashes: `WHITE + u`, u in 1..=15.
|
||||
pub const WHITE: u32 = 16;
|
||||
/// The hurt red (rows 0 to 7) as RGBA bytes: 0xB2FF0000.
|
||||
pub const HURT_TEXEL: [u8; 4] = [255, 0, 0, 0xB2];
|
||||
|
||||
/// The white row's alpha at column u: `(int) ((1 - u / 15 * 0.75) * 255)`
|
||||
/// in float precision.
|
||||
pub fn white_alpha(u: u32) -> u8 {
|
||||
((1.0f32 - u as f32 / 15.0 * 0.75) * 255.0) as u8
|
||||
}
|
||||
|
||||
/// OverlayTexture.u: the column of a white overlay progress.
|
||||
pub fn white_column(progress: f32) -> u32 {
|
||||
(progress * 15.0) as u32
|
||||
}
|
||||
|
||||
/// LivingEntityRenderer.getOverlayCoords as entity.wgsl's overlay code: the
|
||||
/// red row hides the white column; column 0 of the white row is opaque white
|
||||
/// with full alpha, so no overlay.
|
||||
pub fn living(white_progress: f32, red: bool) -> u32 {
|
||||
if red {
|
||||
return 1;
|
||||
}
|
||||
match white_column(white_progress) {
|
||||
0 => 0,
|
||||
u => WHITE + u.min(15),
|
||||
}
|
||||
}
|
||||
|
||||
/// CreeperRenderer.getWhiteOverlayProgress: flashing every other tenth of
|
||||
/// the swelling, at least half white.
|
||||
pub fn creeper_progress(swelling: f32) -> f32 {
|
||||
if (swelling * 10.0) as i32 % 2 == 0 {
|
||||
0.0
|
||||
} else {
|
||||
swelling.clamp(0.5, 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The texels as WGSL constants, prepended to entity.wgsl.
|
||||
pub fn wgsl_constants() -> String {
|
||||
let [r, g, b, a] = HURT_TEXEL.map(|v| format!("{v}.0 / 255.0"));
|
||||
let white: Vec<String> = (0..16)
|
||||
.map(|u| format!("{}.0 / 255.0", white_alpha(u)))
|
||||
.collect();
|
||||
format!(
|
||||
"// OverlayTexture (overlay.rs): the hurt texel and the white row's alpha by column.\n\
|
||||
const HURT_OVERLAY = vec4({r}, {g}, {b}, {a});\n\
|
||||
const WHITE_OVERLAY_ALPHA = array<f32, 16>({});\n",
|
||||
white.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn texels_are_vanilla_s() {
|
||||
// OverlayTexture's constructor, printed by the 26.3 JVM.
|
||||
let vanilla = [
|
||||
255, 242, 229, 216, 203, 191, 178, 165, 152, 140, 127, 114, 101, 89, 76, 63,
|
||||
];
|
||||
assert_eq!((0..16).map(white_alpha).collect::<Vec<_>>(), vanilla);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes([HURT_TEXEL[3], HURT_TEXEL[0], HURT_TEXEL[1], HURT_TEXEL[2]]),
|
||||
0xB2FF_0000
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creeper_flashes_like_vanilla() {
|
||||
// (swelling, OverlayTexture.u(getWhiteOverlayProgress)) from the JVM.
|
||||
let vanilla = [
|
||||
(0.05, 0),
|
||||
(0.1, 7),
|
||||
(0.19, 7),
|
||||
(0.2, 0),
|
||||
(0.3, 7),
|
||||
(0.49, 0),
|
||||
(0.5, 7),
|
||||
(0.55, 8),
|
||||
(0.7, 10),
|
||||
(0.75, 11),
|
||||
(0.95, 14),
|
||||
(1.0, 0),
|
||||
];
|
||||
for (swelling, u) in vanilla {
|
||||
assert_eq!(white_column(creeper_progress(swelling)), u, "{swelling}");
|
||||
}
|
||||
assert_eq!(living(creeper_progress(0.55), false), WHITE + 8);
|
||||
assert_eq!(living(creeper_progress(0.55), true), 1);
|
||||
assert_eq!(living(0.0, false), 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user