2ce21900ae
main's ease_peek and onPeekAmountChange; client_tick eases with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1475 lines
50 KiB
Rust
1475 lines
50 KiB
Rust
//! Mob rendering (`LivingEntityRenderer.submit` and the render layers):
|
|
//! variant/baby model and texture selection, setupRotations, per-species
|
|
//! scale, and extra layers (sheep wool, spider eyes, drowned/stray outer
|
|
//! layers, villager overlays, collars...).
|
|
use super::{
|
|
Mob, MobData, ModelNormals, Part, Species,
|
|
anim::{self, Poses},
|
|
living::wrap,
|
|
};
|
|
use crate::mesher::Vertex;
|
|
use horizon_core::geometry::trig::Mth;
|
|
use std::f32::consts::PI;
|
|
|
|
/// A texture lookup into the shared atlas (name without namespace -> uv rect).
|
|
pub type Atlas<'a> = &'a dyn Fn(&str) -> Option<[f32; 4]>;
|
|
|
|
/// One draw of a model with a texture.
|
|
pub struct Layer {
|
|
pub model: String,
|
|
pub texture: String,
|
|
pub color: u32,
|
|
pub full_bright: bool,
|
|
}
|
|
/// DyeColor.getTextureDiffuseColor (RGB).
|
|
pub const DYE: [u32; 16] = [
|
|
0xF9FFFE, 0xF9801D, 0xC74EBD, 0x3AB3DA, 0xFED83D, 0x80C71F, 0xF38BAA, 0x474F52, 0x9D9D97,
|
|
0x169C9C, 0x8932B8, 0x3C44AA, 0x835432, 0x5E7C16, 0xB02E26, 0x1D1D21,
|
|
];
|
|
pub const DYE_NAMES: [&str; 16] = [
|
|
"white",
|
|
"orange",
|
|
"magenta",
|
|
"light_blue",
|
|
"yellow",
|
|
"lime",
|
|
"pink",
|
|
"gray",
|
|
"light_gray",
|
|
"cyan",
|
|
"purple",
|
|
"blue",
|
|
"brown",
|
|
"green",
|
|
"red",
|
|
"black",
|
|
];
|
|
/// ABGR vertex colour from an RGB value.
|
|
pub fn abgr(rgb: u32) -> u32 {
|
|
let (r, g, b) = ((rgb >> 16) & 255, (rgb >> 8) & 255, rgb & 255);
|
|
0xFF00_0000 | b << 16 | g << 8 | r
|
|
}
|
|
/// ColorLerper.Type.SHEEP colour: white is 0xE6E6E6, others 75% of the dye.
|
|
pub fn sheep_wool_rgb(color: u8) -> u32 {
|
|
if color == 0 {
|
|
return 0xE6E6E6;
|
|
}
|
|
let src = DYE[color as usize & 15];
|
|
let f = |c: u32| ((c as f32 * 0.75).floor() as u32).min(255);
|
|
f(src >> 16 & 255) << 16 | f(src >> 8 & 255) << 8 | f(src & 255)
|
|
}
|
|
pub const VILLAGER_TYPES: [&str; 7] = [
|
|
"plains", "desert", "jungle", "savanna", "snow", "swamp", "taiga",
|
|
];
|
|
pub const PROFESSIONS: [&str; 15] = [
|
|
"none",
|
|
"armorer",
|
|
"butcher",
|
|
"cartographer",
|
|
"cleric",
|
|
"farmer",
|
|
"fisherman",
|
|
"fletcher",
|
|
"leatherworker",
|
|
"librarian",
|
|
"mason",
|
|
"nitwit",
|
|
"shepherd",
|
|
"toolsmith",
|
|
"weaponsmith",
|
|
];
|
|
fn baby_or(m: &Mob, adult: &str, baby: &str) -> String {
|
|
if m.is_baby() && m.kind.layer(baby).is_some() {
|
|
baby.to_owned()
|
|
} else {
|
|
adult.to_owned()
|
|
}
|
|
}
|
|
fn climate(m: &Mob) -> &'static str {
|
|
match m.extra.variant {
|
|
1 => "warm",
|
|
2 => "cold",
|
|
_ => "temperate",
|
|
}
|
|
}
|
|
/// `AvatarRenderer` entities: a mannequin draws exactly like a player —
|
|
/// the player model, its resolved skin and the same scale.
|
|
fn avatar(m: &Mob) -> bool {
|
|
matches!(&*m.kind.id, "minecraft:player" | "minecraft:mannequin")
|
|
}
|
|
/// The model layer key and texture for the mob's body.
|
|
pub fn body(m: &Mob) -> (String, String) {
|
|
use Species as S;
|
|
let baby = m.is_baby();
|
|
let b = |s: &str| {
|
|
if baby {
|
|
format!("{s}_baby")
|
|
} else {
|
|
s.to_owned()
|
|
}
|
|
};
|
|
if avatar(m) {
|
|
return (
|
|
"main".into(),
|
|
m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()),
|
|
);
|
|
}
|
|
match m.species() {
|
|
S::Llama | S::TraderLlama => {
|
|
let name = if m.species() == S::Llama {
|
|
"llama"
|
|
} else {
|
|
"trader_llama"
|
|
};
|
|
(
|
|
baby_or(m, &format!("{name}#main"), &format!("{name}_baby#main")),
|
|
super::llama::texture(m),
|
|
)
|
|
}
|
|
s if super::equine::is_equine(s) => {
|
|
let name = super::equine::model_name(s);
|
|
(
|
|
baby_or(m, &format!("{name}#main"), &format!("{name}_baby#main")),
|
|
super::equine::texture(m),
|
|
)
|
|
}
|
|
S::Wolf => (
|
|
baby_or(m, "wolf#main", "wolf_baby#main"),
|
|
super::tame::texture(m),
|
|
),
|
|
S::HappyGhast => (
|
|
baby_or(m, "happy_ghast#main", "happy_ghast_baby#main"),
|
|
if baby {
|
|
"entity/ghast/happy_ghast_baby".into()
|
|
} else {
|
|
"entity/ghast/happy_ghast".into()
|
|
},
|
|
),
|
|
S::Nautilus | S::ZombieNautilus => (
|
|
super::nautilus::model(m).into(),
|
|
super::nautilus::texture(m),
|
|
),
|
|
S::SulfurCube => super::sulfur_cube::body(m),
|
|
S::Sniffer => (
|
|
baby_or(m, "sniffer#main", "sniffer_baby#main"),
|
|
if baby {
|
|
"entity/sniffer/snifflet".into()
|
|
} else {
|
|
"entity/sniffer/sniffer".into()
|
|
},
|
|
),
|
|
S::Shulker => ("shulker#main".into(), super::shulker::texture(m)),
|
|
S::Vex => ("vex#main".into(), super::vex::texture(m).into()),
|
|
S::Wither => ("wither#main".into(), super::wither::texture(m).into()),
|
|
S::Ghast => (
|
|
"ghast#main".into(),
|
|
if m.extra.flag {
|
|
"entity/ghast/ghast_shooting".into()
|
|
} else {
|
|
"entity/ghast/ghast".into()
|
|
},
|
|
),
|
|
S::Cat => (
|
|
baby_or(m, "cat#main", "cat_baby#main"),
|
|
super::tame::cat_texture(m),
|
|
),
|
|
S::Frog => (
|
|
"frog#main".into(),
|
|
format!("entity/frog/frog_{}", climate(m)),
|
|
),
|
|
S::Rabbit => (
|
|
baby_or(m, "rabbit#main", "rabbit_baby#main"),
|
|
b(&m.kind.texture),
|
|
),
|
|
S::Pig => {
|
|
let model = if baby {
|
|
"pig_baby#main".into()
|
|
} else if m.extra.variant == 2 {
|
|
"cold_pig#main".into()
|
|
} else {
|
|
"pig#main".into()
|
|
};
|
|
(model, b(&format!("entity/pig/pig_{}", climate(m))))
|
|
}
|
|
S::Cow => {
|
|
let model = match (m.extra.variant, baby) {
|
|
(1, false) => "warm_cow#main",
|
|
(1, true) => "warm_cow_baby#main",
|
|
(2, false) => "cold_cow#main",
|
|
(2, true) => "cold_cow_baby#main",
|
|
(_, true) => "cow_baby#main",
|
|
_ => "cow#main",
|
|
};
|
|
(model.into(), b(&format!("entity/cow/cow_{}", climate(m))))
|
|
}
|
|
S::Mooshroom => (
|
|
baby_or(m, "mooshroom#main", "mooshroom_baby#main"),
|
|
b(if m.extra.variant == 1 {
|
|
"entity/cow/mooshroom_brown"
|
|
} else {
|
|
"entity/cow/mooshroom_red"
|
|
}),
|
|
),
|
|
S::Chicken => {
|
|
let model = if baby {
|
|
"chicken_baby#main"
|
|
} else if m.extra.variant == 2 {
|
|
"cold_chicken#main"
|
|
} else {
|
|
"chicken#main"
|
|
};
|
|
(
|
|
model.into(),
|
|
b(&format!("entity/chicken/chicken_{}", climate(m))),
|
|
)
|
|
}
|
|
S::Sheep => (
|
|
baby_or(m, "sheep#main", "sheep_baby#main"),
|
|
b("entity/sheep/sheep"),
|
|
),
|
|
S::Zombie => (
|
|
baby_or(m, "zombie#main", "zombie_baby#main"),
|
|
b("entity/zombie/zombie"),
|
|
),
|
|
S::Husk => (
|
|
baby_or(m, "husk#main", "husk_baby#main"),
|
|
b("entity/zombie/husk"),
|
|
),
|
|
S::Drowned => (
|
|
baby_or(m, "drowned#main", "drowned_baby#main"),
|
|
b("entity/zombie/drowned"),
|
|
),
|
|
S::ZombieVillager => (
|
|
baby_or(m, "zombie_villager#main", "zombie_villager_baby#main"),
|
|
"entity/zombie_villager/zombie_villager".into(),
|
|
),
|
|
S::Villager => (
|
|
baby_or(m, "villager#main", "villager_baby#main"),
|
|
b("entity/villager/villager"),
|
|
),
|
|
S::Piglin => (
|
|
baby_or(m, "piglin#main", "piglin_baby#main"),
|
|
b("entity/piglin/piglin"),
|
|
),
|
|
S::ZombifiedPiglin => (
|
|
baby_or(m, "zombified_piglin#main", "zombified_piglin_baby#main"),
|
|
b("entity/piglin/zombified_piglin"),
|
|
),
|
|
S::Hoglin => (
|
|
baby_or(m, "hoglin#main", "hoglin_baby#main"),
|
|
b("entity/hoglin/hoglin"),
|
|
),
|
|
S::Zoglin => (
|
|
baby_or(m, "zoglin#main", "zoglin_baby#main"),
|
|
b("entity/hoglin/zoglin"),
|
|
),
|
|
S::Strider => (
|
|
baby_or(m, "strider#main", "strider_baby#main"),
|
|
b(if m.extra.flag {
|
|
"entity/strider/strider_cold"
|
|
} else {
|
|
"entity/strider/strider"
|
|
}),
|
|
),
|
|
// BeeRenderer.getTextureLocation: angry, carrying nectar, or both.
|
|
S::Bee => {
|
|
let angry = super::bee::get(m).is_some_and(|b| b.anger_end > 0);
|
|
let nectar = super::bee::has_nectar(m);
|
|
let texture = match (angry, nectar) {
|
|
(true, true) => "entity/bee/bee_angry_nectar",
|
|
(true, false) => "entity/bee/bee_angry",
|
|
(false, true) => "entity/bee/bee_nectar",
|
|
(false, false) => "entity/bee/bee",
|
|
};
|
|
let layer = baby_or(m, "bee#main", "bee_baby#main");
|
|
let texture = if layer.starts_with("bee_baby") {
|
|
b(texture)
|
|
} else {
|
|
texture.to_owned()
|
|
};
|
|
(layer, texture)
|
|
}
|
|
S::Squid => (
|
|
baby_or(m, "squid#main", "squid_baby#main"),
|
|
b("entity/squid/squid"),
|
|
),
|
|
S::GlowSquid => (
|
|
baby_or(m, "glow_squid#main", "glow_squid_baby#main"),
|
|
b("entity/squid/glow_squid"),
|
|
),
|
|
S::Dolphin => (
|
|
baby_or(m, "dolphin#main", "dolphin_baby#main"),
|
|
b("entity/dolphin/dolphin"),
|
|
),
|
|
S::Pufferfish => {
|
|
let model = match m.extra.size {
|
|
0 => "pufferfish_small#main",
|
|
1 => "pufferfish_medium#main",
|
|
_ => "pufferfish_big#main",
|
|
};
|
|
(model.into(), "entity/fish/pufferfish".into())
|
|
}
|
|
S::TropicalFish => {
|
|
if m.extra.variant < 6 {
|
|
(
|
|
"tropical_fish_small#main".into(),
|
|
"entity/fish/tropical_a".into(),
|
|
)
|
|
} else {
|
|
(
|
|
"tropical_fish_large#main".into(),
|
|
"entity/fish/tropical_b".into(),
|
|
)
|
|
}
|
|
}
|
|
// CopperGolemOxidationLevels: the texture of the weathering.
|
|
S::CopperGolem => (m.kind.model_key(), super::copper_golem::texture(m)),
|
|
_ => (m.kind.model_key(), m.kind.texture.clone()),
|
|
}
|
|
}
|
|
/// LivingEntityRenderer.getModelTint (tropical fish base colour).
|
|
pub fn body_color(m: &Mob, alpha: f32) -> u32 {
|
|
match m.species() {
|
|
Species::Wolf => super::tame::tint(m, alpha),
|
|
Species::TropicalFish => abgr(DYE[m.extra.variant2 as usize & 15]),
|
|
_ => 0xFFFF_FFFF,
|
|
}
|
|
}
|
|
/// Extra render layers drawn over the body with the same pose.
|
|
pub fn layers(m: &Mob) -> Vec<Layer> {
|
|
use Species as S;
|
|
let mut v = Vec::new();
|
|
if avatar(m) {
|
|
return v;
|
|
}
|
|
let white = 0xFFFF_FFFF;
|
|
let push = |v: &mut Vec<Layer>, model: &str, texture: &str, color: u32, full_bright: bool| {
|
|
v.push(Layer {
|
|
model: model.into(),
|
|
texture: texture.into(),
|
|
color,
|
|
full_bright,
|
|
})
|
|
};
|
|
match m.species() {
|
|
// WolfCollarLayer.
|
|
S::Wolf if m.extra.tame => {
|
|
let (model, texture) = if m.is_baby() {
|
|
("wolf_baby#main", "entity/wolf/wolf_collar_baby")
|
|
} else {
|
|
("wolf#main", "entity/wolf/wolf_collar")
|
|
};
|
|
push(
|
|
&mut v,
|
|
model,
|
|
texture,
|
|
abgr(DYE[m.extra.color as usize & 15]),
|
|
false,
|
|
);
|
|
}
|
|
// SimpleEquipmentLayer: the harness over the body.
|
|
S::HappyGhast if !m.is_baby() => {
|
|
if let Some(texture) = m
|
|
.extra
|
|
.head
|
|
.as_deref()
|
|
.and_then(super::happy_ghast::harness_texture)
|
|
{
|
|
push(&mut v, "happy_ghast_harness#main", &texture, white, false);
|
|
}
|
|
}
|
|
// BreezeWindLayer (the scroll is not reproduced: atlas sprites do
|
|
// not wrap) and BreezeEyesLayer.
|
|
S::Breeze => {
|
|
push(
|
|
&mut v,
|
|
"breeze#wind",
|
|
"entity/breeze/breeze_wind",
|
|
white,
|
|
false,
|
|
);
|
|
push(
|
|
&mut v,
|
|
"breeze#eyes",
|
|
"entity/breeze/breeze_eyes",
|
|
white,
|
|
true,
|
|
);
|
|
}
|
|
// LlamaDecorLayer: the carpet, or the trader's blanket.
|
|
S::Llama | S::TraderLlama => {
|
|
if let Some((model, texture)) = super::llama::decor(m) {
|
|
push(&mut v, &model, &texture, white, false);
|
|
}
|
|
}
|
|
// HorseMarkingLayer, the saddle and the horse armour.
|
|
s if super::equine::is_equine(s) => {
|
|
let name = super::equine::model_name(s);
|
|
let body = if m.is_baby() {
|
|
format!("{name}_baby#main")
|
|
} else {
|
|
format!("{name}#main")
|
|
};
|
|
if let Some(tex) = super::equine::markings(m) {
|
|
push(&mut v, &body, &tex, white, false);
|
|
}
|
|
if m.extra.saddled && !m.is_baby() {
|
|
push(
|
|
&mut v,
|
|
&format!("{name}#saddle"),
|
|
super::equine::saddle_texture(s),
|
|
white,
|
|
false,
|
|
);
|
|
}
|
|
if let Some(armor) = m.extra.head.as_deref()
|
|
&& let Some(material) = armor
|
|
.strip_prefix("minecraft:")
|
|
.and_then(|a| a.strip_suffix("_horse_armor"))
|
|
{
|
|
let tex = format!("entity/equipment/horse_body/{material}");
|
|
let color = if material == "leather" {
|
|
abgr(0xA06540)
|
|
} else {
|
|
white
|
|
};
|
|
// UndeadHorseRenderer bakes its own armour layer.
|
|
let layer = if matches!(s, S::ZombieHorse | S::SkeletonHorse) {
|
|
"undead_horse_armor#main"
|
|
} else {
|
|
"horse_armor#main"
|
|
};
|
|
push(&mut v, layer, &tex, color, false);
|
|
}
|
|
}
|
|
// SimpleEquipmentLayer: pig and strider saddles.
|
|
S::Pig if m.extra.saddled && !m.is_baby() => push(
|
|
&mut v,
|
|
"pig#saddle",
|
|
"entity/equipment/pig_saddle/saddle",
|
|
white,
|
|
false,
|
|
),
|
|
S::Strider if m.extra.saddled && !m.is_baby() => push(
|
|
&mut v,
|
|
"strider#saddle",
|
|
"entity/equipment/strider_saddle/saddle",
|
|
white,
|
|
false,
|
|
),
|
|
// CatCollarLayer.
|
|
S::Cat if m.extra.tame => {
|
|
let (model, texture) = if m.is_baby() {
|
|
("cat_baby#collar", "entity/cat/cat_collar_baby")
|
|
} else {
|
|
("cat#collar", "entity/cat/cat_collar")
|
|
};
|
|
push(
|
|
&mut v,
|
|
model,
|
|
texture,
|
|
abgr(DYE[m.extra.color as usize & 15]),
|
|
false,
|
|
);
|
|
}
|
|
// IronGolemCrackinessLayer.
|
|
S::IronGolem => {
|
|
if let Some(texture) = super::golem::crackiness(m) {
|
|
push(&mut v, "iron_golem#main", texture, white, false);
|
|
}
|
|
}
|
|
// NautilusRenderer: the armour and saddle SimpleEquipmentLayers.
|
|
S::Nautilus | S::ZombieNautilus if !m.is_baby() => {
|
|
if let Some(texture) = m
|
|
.extra
|
|
.head
|
|
.as_deref()
|
|
.and_then(super::nautilus::armor_texture)
|
|
{
|
|
push(&mut v, "nautilus_armor#main", &texture, white, false);
|
|
}
|
|
if m.extra.saddled {
|
|
push(
|
|
&mut v,
|
|
"nautilus#saddle",
|
|
"entity/equipment/nautilus_saddle/saddle",
|
|
white,
|
|
false,
|
|
);
|
|
}
|
|
}
|
|
// SulfurCubeInnerLayer.
|
|
S::SulfurCube => {
|
|
if let Some((model, texture)) = super::sulfur_cube::inner(m) {
|
|
push(&mut v, model, texture, white, false);
|
|
}
|
|
}
|
|
// SlimeOuterLayer: the translucent outer cube.
|
|
S::Slime => push(&mut v, "slime#outer", "entity/slime/slime", white, false),
|
|
S::Sheep if !m.extra.sheared => {
|
|
let color = abgr(sheep_wool_rgb(m.extra.color));
|
|
if !m.is_baby() && m.extra.color != 0 {
|
|
push(
|
|
&mut v,
|
|
"sheep#wool_undercoat",
|
|
"entity/sheep/sheep_wool_undercoat",
|
|
color,
|
|
false,
|
|
);
|
|
}
|
|
if m.is_baby() {
|
|
push(
|
|
&mut v,
|
|
"sheep_baby#wool",
|
|
"entity/sheep/sheep_wool_baby",
|
|
color,
|
|
false,
|
|
);
|
|
} else {
|
|
push(
|
|
&mut v,
|
|
"sheep#wool",
|
|
"entity/sheep/sheep_wool",
|
|
color,
|
|
false,
|
|
);
|
|
}
|
|
}
|
|
S::Creaking if super::creaking::eyes_glowing(m) => push(
|
|
&mut v,
|
|
"creaking#eyes",
|
|
"entity/creaking/creaking_eyes",
|
|
white,
|
|
true,
|
|
),
|
|
// The copper golem's eyes glow (LivingEntityEmissiveLayer, eyes render type).
|
|
S::CopperGolem => {
|
|
let eyes = super::copper_golem::eye_texture(m);
|
|
push(&mut v, "copper_golem#eyes", &eyes, white, true);
|
|
}
|
|
S::Spider | S::CaveSpider => push(
|
|
&mut v,
|
|
&m.kind.model_key(),
|
|
"entity/spider/spider_eyes",
|
|
white,
|
|
true,
|
|
),
|
|
S::Enderman => push(
|
|
&mut v,
|
|
"enderman#main",
|
|
"entity/enderman/enderman_eyes",
|
|
white,
|
|
true,
|
|
),
|
|
S::Phantom => push(
|
|
&mut v,
|
|
"phantom#main",
|
|
"entity/phantom/phantom_eyes",
|
|
white,
|
|
true,
|
|
),
|
|
S::Drowned => {
|
|
if m.is_baby() {
|
|
push(
|
|
&mut v,
|
|
"drowned_baby#outer",
|
|
"entity/zombie/drowned_outer_layer_baby",
|
|
white,
|
|
false,
|
|
);
|
|
} else {
|
|
push(
|
|
&mut v,
|
|
"drowned#outer",
|
|
"entity/zombie/drowned_outer_layer",
|
|
white,
|
|
false,
|
|
);
|
|
}
|
|
}
|
|
S::Stray => push(
|
|
&mut v,
|
|
"stray#outer",
|
|
"entity/skeleton/stray_overlay",
|
|
white,
|
|
false,
|
|
),
|
|
S::Bogged if !m.extra.sheared => push(
|
|
&mut v,
|
|
"bogged#outer",
|
|
"entity/skeleton/bogged_overlay",
|
|
white,
|
|
false,
|
|
),
|
|
// EnergySwirlLayer (the scroll is not reproduced: atlas sprites do not wrap).
|
|
S::Creeper if m.extra.charged => push(
|
|
&mut v,
|
|
"creeper#armor",
|
|
"entity/creeper/creeper_armor",
|
|
0x80FF_FFFF,
|
|
true,
|
|
),
|
|
S::TropicalFish => {
|
|
// TropicalFishPatternLayer: the pattern tinted with the pattern colour.
|
|
let (model, letter, n) = if m.extra.variant < 6 {
|
|
("tropical_fish_small#pattern", "a", m.extra.variant + 1)
|
|
} else {
|
|
("tropical_fish_large#pattern", "b", m.extra.variant - 5)
|
|
};
|
|
push(
|
|
&mut v,
|
|
model,
|
|
&format!("entity/fish/tropical_{letter}_pattern_{n}"),
|
|
abgr(DYE[m.extra.variant3 as usize & 15]),
|
|
false,
|
|
);
|
|
}
|
|
S::Villager | S::ZombieVillager => {
|
|
let base = if m.species() == S::Villager {
|
|
"villager"
|
|
} else {
|
|
"zombie_villager"
|
|
};
|
|
let model = if m.is_baby() {
|
|
format!("{base}_baby#main")
|
|
} else {
|
|
format!("{base}#main")
|
|
};
|
|
let t = VILLAGER_TYPES[m.extra.variant as usize % VILLAGER_TYPES.len()];
|
|
let folder = if m.species() == S::Villager {
|
|
"entity/villager"
|
|
} else {
|
|
"entity/zombie_villager"
|
|
};
|
|
// VillagerProfessionLayer: a baby's type texture is in `baby/`.
|
|
let group = if m.is_baby() { "baby" } else { "type" };
|
|
push(
|
|
&mut v,
|
|
&model,
|
|
&format!("{folder}/{group}/{t}"),
|
|
white,
|
|
false,
|
|
);
|
|
let prof = PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()];
|
|
if !m.is_baby() && prof != "none" {
|
|
push(
|
|
&mut v,
|
|
&model,
|
|
&format!("{folder}/profession/{prof}"),
|
|
white,
|
|
false,
|
|
);
|
|
if prof != "nitwit" {
|
|
// `LEVEL_LOCATIONS` clamps the level to 1..=5.
|
|
let level = ["stone", "iron", "gold", "emerald", "diamond"]
|
|
[(m.extra.variant3.max(1) as usize - 1).min(4)];
|
|
push(
|
|
&mut v,
|
|
&model,
|
|
&format!("{folder}/profession_level/{level}"),
|
|
white,
|
|
false,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
humanoid_armor(m, &mut v);
|
|
v
|
|
}
|
|
/// `HumanoidArmorLayer`: the armour models of the mob's main model with the
|
|
/// equipment textures (leather tinted, with its overlay).
|
|
fn humanoid_armor(m: &Mob, v: &mut Vec<Layer>) {
|
|
if super::equine::is_equine(m.species()) {
|
|
return;
|
|
}
|
|
let (model, _) = body(m);
|
|
let Some(base) = model.strip_suffix("#main") else {
|
|
return;
|
|
};
|
|
let slots = [
|
|
(&m.extra.head, "helmet"),
|
|
(&m.extra.chest, "chestplate"),
|
|
(&m.extra.legs, "leggings"),
|
|
(&m.extra.feet, "boots"),
|
|
];
|
|
for (item, layer) in slots {
|
|
let Some(name) = item.as_deref().and_then(|i| i.strip_prefix("minecraft:")) else {
|
|
continue;
|
|
};
|
|
let Some(material) = name.strip_suffix(&format!("_{layer}")) else {
|
|
continue;
|
|
};
|
|
let asset = match material {
|
|
"golden" => "gold",
|
|
"turtle" => "turtle_scute",
|
|
other => other,
|
|
};
|
|
let folder = if layer == "leggings" {
|
|
"humanoid_leggings"
|
|
} else {
|
|
"humanoid"
|
|
};
|
|
let model = format!("{base}#{layer}");
|
|
let tint = if asset == "leather" {
|
|
abgr(0xA06540)
|
|
} else {
|
|
0xFFFF_FFFF
|
|
};
|
|
v.push(Layer {
|
|
model: model.clone(),
|
|
texture: format!("entity/equipment/{folder}/{asset}"),
|
|
color: tint,
|
|
full_bright: false,
|
|
});
|
|
if asset == "leather" {
|
|
v.push(Layer {
|
|
model,
|
|
texture: format!("entity/equipment/{folder}/leather_overlay"),
|
|
color: 0xFFFF_FFFF,
|
|
full_bright: false,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
impl super::MobType {
|
|
/// The main model layer key without the namespace.
|
|
pub fn model_key(&self) -> String {
|
|
super::species::def(self.species).model.to_owned()
|
|
}
|
|
}
|
|
/// The mob's root transform (LivingEntityRenderer.submit + setupRotations).
|
|
pub fn base_transform(m: &Mob, alpha: f32, camera: glam::DVec3) -> glam::Mat4 {
|
|
let a = f64::from(alpha);
|
|
let p = glam::DVec3::new(
|
|
m.previous.x + (m.position.x - m.previous.x) * a,
|
|
m.previous.y + (m.position.y - m.previous.y) * a,
|
|
m.previous.z + (m.position.z - m.previous.z) * a,
|
|
) - camera;
|
|
base_transform_at(m, alpha, glam::Mat4::from_translation(p.as_vec3()))
|
|
}
|
|
/// `base_transform` from a pose of the entity's origin (`EntityRenderDispatcher.submit`
|
|
/// under a pose stack, as `SpawnerRenderer` draws the mob inside a spawner).
|
|
pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 {
|
|
let mut body = m.previous_body_yaw + wrap(m.body_yaw - m.previous_body_yaw) * alpha;
|
|
if shaking(m) {
|
|
// LivingEntityRenderer.setupRotations: a rapid wobble.
|
|
let ticks = (m.age as f32 + alpha).floor();
|
|
body += (f64::from(ticks * 3.25).cos() * std::f64::consts::PI * 0.4) as f32;
|
|
}
|
|
// A sleeping villager, or another player lying in their bed.
|
|
let sleep = (m.species() == Species::Villager || avatar(m))
|
|
.then(|| super::villager::sleep_pose(m))
|
|
.flatten();
|
|
let rotation = match sleep {
|
|
// setupRotations for Pose.SLEEPING, with the head moved toward the pillow.
|
|
Some((angle, step)) => {
|
|
let head = (m.eye_y() - m.position.y) as f32 - 0.1;
|
|
glam::Mat4::from_translation(glam::Vec3::new(-step[0] * head, 0.0, -step[1] * head))
|
|
* glam::Mat4::from_rotation_y(angle.to_radians())
|
|
* glam::Mat4::from_rotation_z(90.0_f32.to_radians())
|
|
* glam::Mat4::from_rotation_y(270.0_f32.to_radians())
|
|
}
|
|
None => super::aquatic::rotations(m, alpha, body)
|
|
.unwrap_or_else(|| glam::Mat4::from_rotation_y((180.0 - body).to_radians())),
|
|
};
|
|
let mut base = origin * rotation;
|
|
if m.death_time > 0 && !super::creaking::tearing_down(m) {
|
|
let f = ((m.death_time as f32 + alpha - 1.0) / 20.0 * 1.6)
|
|
.sqrt()
|
|
.min(1.0);
|
|
base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians());
|
|
} else if m.extra.auto_spin_attack {
|
|
// Riptide: lying along the look direction, spinning 75 degrees a tick.
|
|
let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha;
|
|
base *= glam::Mat4::from_rotation_x((-90.0 - pitch).to_radians())
|
|
* glam::Mat4::from_rotation_y(((m.age as f32 + alpha) * -75.0).to_radians());
|
|
} else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) {
|
|
// isEntityUpsideDown
|
|
base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0))
|
|
* glam::Mat4::from_rotation_z(PI);
|
|
}
|
|
// AvatarRenderer.setupRotations, after LivingEntityRenderer's.
|
|
if m.extra.fall_flying {
|
|
let ticks = m.extra.fall_fly_ticks as f32 + alpha;
|
|
let scale = (ticks * ticks / 100.0).min(1.0);
|
|
let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha;
|
|
if !m.extra.auto_spin_attack {
|
|
base *= glam::Mat4::from_rotation_x((scale * (-90.0 - pitch)).to_radians());
|
|
}
|
|
let yaw = m.previous_head_yaw + wrap(m.head_yaw - m.previous_head_yaw) * alpha;
|
|
let (look_x, look_z) = (-(yaw.to_radians()).msin(), (yaw.to_radians()).mcos());
|
|
let v = m.velocity;
|
|
let view_scale = (pitch.to_radians()).mcos();
|
|
let (look_x, look_z) = (look_x * view_scale, look_z * view_scale);
|
|
let view_length = f64::from(look_x * look_x + look_z * look_z);
|
|
let speed = v.x * v.x + v.z * v.z;
|
|
if speed > f64::from(1.0e-5f32) && view_length > f64::from(1.0e-5f32) {
|
|
let dot =
|
|
(v.x * f64::from(look_x) + v.z * f64::from(look_z)) / (speed * view_length).sqrt();
|
|
let sign = (v.x * f64::from(look_z) - v.z * f64::from(look_x)).signum();
|
|
base *= glam::Mat4::from_rotation_y(
|
|
(sign * horizon_core::jmath::acos(dot.abs().min(1.0))) as f32,
|
|
);
|
|
}
|
|
} else if m.extra.swim.amount(alpha) > 0.0 {
|
|
// AvatarRenderer.setupRotations: tip forward with the swim amount,
|
|
// following the pitch in water, then lie along the swimming box.
|
|
let amount = m.extra.swim.amount(alpha);
|
|
let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha;
|
|
let target = if m.extra.swim.in_water {
|
|
-90.0 - pitch
|
|
} else {
|
|
-90.0
|
|
};
|
|
base *= glam::Mat4::from_rotation_x((amount * target).to_radians());
|
|
if m.extra.swim.visually {
|
|
base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, -1.0, 0.3));
|
|
}
|
|
}
|
|
base *= species_rotations(m, alpha);
|
|
base *= glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0));
|
|
base *= species_scale(m, alpha);
|
|
base * glam::Mat4::from_translation(glam::Vec3::new(0.0, -1.501, 0.0))
|
|
}
|
|
/// `LivingEntityRenderer.isShaking`: a fully frozen mob (the skeleton's own
|
|
/// `isShaking` is its stray conversion only), converting zombies, piglins
|
|
/// and hoglins, a zombie villager being cured and a cold strider.
|
|
fn shaking(m: &Mob) -> bool {
|
|
if m.is_fully_frozen() && m.species() != Species::Skeleton {
|
|
return true;
|
|
}
|
|
match m.species() {
|
|
Species::Strider => super::strider::suffocating(m),
|
|
Species::ZombieVillager => super::zombie_villager::is_converting(m),
|
|
Species::Zombie | Species::Husk => m.extra.counter2 > 0,
|
|
Species::Piglin | Species::PiglinBrute | Species::Hoglin => {
|
|
!m.extra.flag2 && m.extra.counter2 > 0
|
|
}
|
|
_ => false,
|
|
}
|
|
}
|
|
fn flip_degrees(m: &Mob) -> f32 {
|
|
match m.species() {
|
|
Species::Spider | Species::CaveSpider | Species::Silverfish | Species::Endermite => 180.0,
|
|
_ => 90.0,
|
|
}
|
|
}
|
|
fn species_rotations(m: &Mob, alpha: f32) -> glam::Mat4 {
|
|
match m.species() {
|
|
Species::Cat => super::tame::cat_rotations(m, alpha),
|
|
Species::Shulker => super::shulker::rotations(m),
|
|
_ => 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 {
|
|
if avatar(m) {
|
|
// AvatarRenderer.scale, shared by the inventory and world renderer.
|
|
return glam::Mat4::from_scale(glam::Vec3::splat(0.9375));
|
|
}
|
|
match m.species() {
|
|
Species::Wither => {
|
|
glam::Mat4::from_scale(glam::Vec3::splat(super::wither::scale(m, alpha)))
|
|
}
|
|
Species::Creeper => {
|
|
// CreeperRenderer.scale: swelling with a wobble.
|
|
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;
|
|
glam::Mat4::from_scale(glam::Vec3::new(xz, y, xz))
|
|
}
|
|
Species::Slime | Species::MagmaCube => super::cube::scale(m, alpha),
|
|
Species::SulfurCube => super::sulfur_cube::scale(m, alpha),
|
|
_ => glam::Mat4::IDENTITY,
|
|
}
|
|
}
|
|
/// Draw a part tree with a pose map, texture rect, colour and light.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn draw(
|
|
part: &Part,
|
|
base: glam::Mat4,
|
|
poses: &Poses,
|
|
uv: [f32; 4],
|
|
color: u32,
|
|
light: u32,
|
|
overlay: u32,
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
draw_normals(
|
|
part,
|
|
base,
|
|
ModelNormals::new(base),
|
|
poses,
|
|
uv,
|
|
color,
|
|
light,
|
|
overlay,
|
|
out,
|
|
);
|
|
}
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn draw_normals(
|
|
part: &Part,
|
|
base: glam::Mat4,
|
|
normals: ModelNormals,
|
|
poses: &Poses,
|
|
uv: [f32; 4],
|
|
color: u32,
|
|
light: u32,
|
|
overlay: u32,
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
let p = poses.pose(part);
|
|
let m = base
|
|
* glam::Mat4::from_translation(glam::Vec3::new(p[0], p[1], p[2]) / 16.0)
|
|
* glam::Mat4::from_quat(glam::Quat::from_euler(
|
|
glam::EulerRot::ZYX,
|
|
p[5],
|
|
p[4],
|
|
p[3],
|
|
))
|
|
* glam::Mat4::from_scale(glam::Vec3::new(p[6], p[7], p[8]));
|
|
if poses.is_hidden(part) || p[6] == 0.0 && p[7] == 0.0 && p[8] == 0.0 {
|
|
return;
|
|
}
|
|
let normals = normals.part(p);
|
|
// `skips` is a name lookup: ask once per part, not once per polygon. A part
|
|
// hidden this way still draws its children, so only the polygons are gated.
|
|
if !poses.skips(part) {
|
|
for poly in &part.polygons {
|
|
let normal = poly.vertex_normal(normals);
|
|
for v in poly.vertices {
|
|
let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0);
|
|
out.push(Vertex {
|
|
normal,
|
|
pos: pos.to_array(),
|
|
uv: [
|
|
uv[0] + (uv[2] - uv[0]) * v[3],
|
|
uv[1] + (uv[3] - uv[1]) * v[4],
|
|
],
|
|
layer: overlay,
|
|
color,
|
|
light,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
for c in &part.children {
|
|
draw_normals(c, m, normals, poses, uv, color, light, overlay, out);
|
|
}
|
|
}
|
|
/// Render a mob with its variant model, texture and layers.
|
|
pub fn render(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
camera: glam::DVec3,
|
|
atlas: Atlas,
|
|
light: u32,
|
|
data: &MobData,
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
render_with(m, alpha, camera, atlas, light, data, &[], out);
|
|
}
|
|
/// `render` plus extra layers posed like the body (`HumanoidArmorLayer`:
|
|
/// the armour model's parts copy the body's): (model, texture rect, colour).
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn render_with(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
camera: glam::DVec3,
|
|
atlas: Atlas,
|
|
light: u32,
|
|
data: &MobData,
|
|
extra: &[(&Part, [f32; 4], u32)],
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
// The invisibility effect hides the model (a drinking wandering trader);
|
|
// an illusioner shows its four mirror images instead.
|
|
if super::trader::is_invisible(m) {
|
|
if m.species() == Species::Illusioner {
|
|
let age = m.age as f32 + alpha;
|
|
for (i, off) in super::illusioner::offsets(m, alpha).iter().enumerate() {
|
|
let w = super::illusioner::wobble(i, age);
|
|
let shifted =
|
|
camera - glam::DVec3::new(off[0] + w[0], off[1] + w[1], off[2] + w[2]);
|
|
let base = base_transform(m, alpha, shifted);
|
|
render_visible(m, alpha, base, atlas, light, data, extra, out);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
render_visible(
|
|
m,
|
|
alpha,
|
|
base_transform(m, alpha, camera),
|
|
atlas,
|
|
light,
|
|
data,
|
|
extra,
|
|
out,
|
|
);
|
|
}
|
|
/// A mob drawn under `origin` instead of at its position (the spinning mob
|
|
/// of `SpawnerRenderer.submitEntityInSpawner`).
|
|
pub fn render_posed(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
origin: glam::Mat4,
|
|
atlas: Atlas,
|
|
light: u32,
|
|
data: &MobData,
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
let base = base_transform_at(m, alpha, origin);
|
|
render_visible(m, alpha, base, atlas, light, data, &[], out);
|
|
}
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn render_visible(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
base: glam::Mat4,
|
|
atlas: Atlas,
|
|
light: u32,
|
|
data: &MobData,
|
|
extra: &[(&Part, [f32; 4], u32)],
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
let overlay = u32::from(red_overlay(m));
|
|
let s = anim::state(m, alpha);
|
|
let (model_key, texture) = body(m);
|
|
let model = m
|
|
.kind
|
|
.layer(&model_key)
|
|
.cloned()
|
|
.unwrap_or_else(|| m.kind.model.clone());
|
|
let mut poses = Poses::new(&model);
|
|
anim::setup(m, &s, &mut poses, &data.animations);
|
|
let draw_body = |out: &mut Vec<Vertex>| {
|
|
if let Some(uv) = atlas(&texture) {
|
|
draw(
|
|
&model,
|
|
base,
|
|
&poses,
|
|
uv,
|
|
body_color(m, alpha),
|
|
light,
|
|
body_overlay(m, alpha),
|
|
out,
|
|
);
|
|
}
|
|
};
|
|
// The translucent shell writes depth: draw the opaque core before it.
|
|
let shell_last = m.species() == Species::SulfurCube;
|
|
if !shell_last {
|
|
draw_body(out);
|
|
}
|
|
for (part, uv, color) in extra {
|
|
draw(part, base, &poses, *uv, *color, light, overlay, out);
|
|
}
|
|
let mut all_layers = layers(m);
|
|
// WardenRenderer: the translucent emissive layers fade with the animations.
|
|
if m.species() == Species::Warden {
|
|
for (model, texture, a) in super::warden::emissive_layers(m, alpha) {
|
|
if a > 0.0 {
|
|
all_layers.push(Layer {
|
|
model: model.into(),
|
|
texture: texture.into(),
|
|
color: ((a.clamp(0.0, 1.0) * 255.0) as u32) << 24 | 0x00FF_FFFF,
|
|
full_bright: true,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
// One Poses refilled for every layer (armour draws up to seven): it keeps
|
|
// its maps' capacity.
|
|
let mut layer_poses = Poses::default();
|
|
for layer in all_layers {
|
|
let Some(part) = m.kind.layer(&layer.model).cloned() else {
|
|
continue;
|
|
};
|
|
let Some(uv) = atlas(&layer.texture) else {
|
|
continue;
|
|
};
|
|
layer_poses.reset(&part);
|
|
anim::setup(m, &s, &mut layer_poses, &data.animations);
|
|
if matches!(m.species(), Species::Villager | Species::ZombieVillager)
|
|
&& let Some((folder, variant)) = layer
|
|
.texture
|
|
.split_once("/type/")
|
|
.or_else(|| layer.texture.split_once("/baby/"))
|
|
{
|
|
let profession = PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()];
|
|
data.villager_hats
|
|
.read()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
.apply(folder, variant, profession, &mut layer_poses);
|
|
}
|
|
let light = if layer.full_bright {
|
|
240 | 240 << 8
|
|
} else {
|
|
light
|
|
};
|
|
// EquipmentLayerRenderer draws armour with OverlayTexture.NO_OVERLAY: never
|
|
// the hurt tint, the glint (7) when enchanted.
|
|
let overlay = match armor_slot(&layer.model) {
|
|
Some(slot) if super::gear::enchanted(m, slot) => 7,
|
|
Some(_) => 0,
|
|
None => overlay,
|
|
};
|
|
let start = out.len();
|
|
draw(
|
|
&part,
|
|
base,
|
|
&layer_poses,
|
|
uv,
|
|
layer.color,
|
|
light,
|
|
overlay,
|
|
out,
|
|
);
|
|
if layer.full_bright && m.species() != Species::Warden {
|
|
for vertex in &mut out[start..] {
|
|
vertex.normal = [0; 4];
|
|
}
|
|
}
|
|
}
|
|
if shell_last {
|
|
draw_body(out);
|
|
}
|
|
}
|
|
/// The equipment slot of a `HumanoidArmorLayer` model (`zombie#helmet`).
|
|
fn armor_slot(model: &str) -> Option<usize> {
|
|
Some(match model.rsplit('#').next()? {
|
|
"helmet" => super::gear::HEAD,
|
|
"chestplate" => super::gear::CHEST,
|
|
"leggings" => super::gear::LEGS,
|
|
"boots" => super::gear::FEET,
|
|
_ => return None,
|
|
})
|
|
}
|
|
/// Render with a fixed texture rect (the earlier API).
|
|
pub fn render_single(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
camera: glam::DVec3,
|
|
uv: [f32; 4],
|
|
light: u32,
|
|
out: &mut Vec<Vertex>,
|
|
) {
|
|
let base = base_transform(m, alpha, camera);
|
|
let overlay = body_overlay(m, alpha);
|
|
let s = anim::state(m, alpha);
|
|
let model = m.kind.model.clone();
|
|
let mut poses = Poses::new(&model);
|
|
anim::setup(m, &s, &mut poses, &std::collections::HashMap::new());
|
|
draw(&model, base, &poses, uv, 0xFFFF_FFFF, light, overlay, out);
|
|
}
|
|
|
|
/// The transform of a named part (ModelPart.translateAndRotate down the tree).
|
|
pub fn part_matrix(
|
|
part: &Part,
|
|
parent: glam::Mat4,
|
|
poses: &Poses,
|
|
name: &str,
|
|
x_offset: f32,
|
|
) -> Option<glam::Mat4> {
|
|
let mut p = poses.pose(part);
|
|
if part.name == name {
|
|
p[0] += x_offset;
|
|
}
|
|
let m = parent
|
|
* glam::Mat4::from_translation(glam::Vec3::new(p[0], p[1], p[2]) / 16.0)
|
|
* glam::Mat4::from_quat(glam::Quat::from_euler(
|
|
glam::EulerRot::ZYX,
|
|
p[5],
|
|
p[4],
|
|
p[3],
|
|
))
|
|
* glam::Mat4::from_scale(glam::Vec3::new(p[6], p[7], p[8]));
|
|
if part.name == name {
|
|
return Some(m);
|
|
}
|
|
part.children
|
|
.iter()
|
|
.find_map(|c| part_matrix(c, m, poses, name, x_offset))
|
|
}
|
|
/// The hand of a mob in model space, up to the item's own transform
|
|
/// (`ItemInHandLayer.submitArmWithItem`).
|
|
fn hand_base(
|
|
m: &Mob,
|
|
model: &Part,
|
|
base: glam::Mat4,
|
|
poses: &Poses,
|
|
left: bool,
|
|
) -> Option<glam::Mat4> {
|
|
let skeleton = matches!(
|
|
m.species(),
|
|
Species::Skeleton
|
|
| Species::Stray
|
|
| Species::Bogged
|
|
| Species::Parched
|
|
| Species::WitherSkeleton
|
|
);
|
|
let arm = if left { "left_arm" } else { "right_arm" };
|
|
let offset = if skeleton {
|
|
if left { -1.0 } else { 1.0 }
|
|
} else {
|
|
0.0
|
|
};
|
|
let mut t = part_matrix(model, base, poses, arm, offset)?;
|
|
if m.species() == Species::Vex {
|
|
// `VexModel.translateToHand`: the held item is drawn at 0.55 scale.
|
|
t *= glam::Mat4::from_scale(glam::Vec3::splat(0.55));
|
|
t *= glam::Mat4::from_translation(glam::Vec3::new(
|
|
if left { -0.046_875 } else { 0.046_875 },
|
|
-0.156_25,
|
|
0.078_125,
|
|
));
|
|
}
|
|
let (ox, oy, oz) = if m.is_baby() {
|
|
(0.0, 1.0, -4.5)
|
|
} else {
|
|
(1.0, 2.0, -10.0)
|
|
};
|
|
t *= glam::Mat4::from_rotation_x((-90.0f32).to_radians()) * glam::Mat4::from_rotation_y(PI);
|
|
t *= glam::Mat4::from_translation(glam::Vec3::new(
|
|
if left { -ox } else { ox } / 16.0,
|
|
oy / 16.0,
|
|
oz / 16.0,
|
|
));
|
|
Some(t)
|
|
}
|
|
/// The transform of one of a mob's model parts, in camera space
|
|
/// (`ModelPart.translateAndRotate` down the tree).
|
|
pub fn part_transform(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
camera: glam::DVec3,
|
|
data: &MobData,
|
|
part: &str,
|
|
) -> Option<glam::Mat4> {
|
|
let base = base_transform(m, alpha, camera);
|
|
let s = anim::state(m, alpha);
|
|
let (model_key, _) = body(m);
|
|
let model = m
|
|
.kind
|
|
.layer(&model_key)
|
|
.cloned()
|
|
.unwrap_or_else(|| m.kind.model.clone());
|
|
let mut poses = Poses::new(&model);
|
|
anim::setup(m, &s, &mut poses, &data.animations);
|
|
part_matrix(&model, base, &poses, part, 0.0)
|
|
}
|
|
/// The pose a mob's held item is submitted under (`ItemInHandLayer`:
|
|
/// `translateToHand`, then the hand offset; the item model adds its own
|
|
/// display transform), or `None` when the mob shows no held items.
|
|
pub fn held_item_transform(
|
|
m: &Mob,
|
|
alpha: f32,
|
|
camera: glam::DVec3,
|
|
data: &MobData,
|
|
left: bool,
|
|
) -> Option<glam::Mat4> {
|
|
if !super::illager::shows_item(m) {
|
|
return None;
|
|
}
|
|
let base = base_transform(m, alpha, camera);
|
|
let s = anim::state(m, alpha);
|
|
let (model_key, _) = body(m);
|
|
let model = m
|
|
.kind
|
|
.layer(&model_key)
|
|
.cloned()
|
|
.unwrap_or_else(|| m.kind.model.clone());
|
|
let mut poses = Poses::new(&model);
|
|
anim::setup(m, &s, &mut poses, &data.animations);
|
|
hand_base(m, &model, base, &poses, left)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// VillagerProfessionLayer's textures (type, or baby for a baby;
|
|
/// profession; level) of every villager and zombie villager are in the
|
|
/// prepared resources: a missing one draws nothing.
|
|
#[test]
|
|
fn villager_layers_name_textures_that_exist() {
|
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../.local/horizon-client/26.3");
|
|
if !root.join("entity-models.json").exists() {
|
|
return;
|
|
}
|
|
let types = super::super::MobTypes::load(&root).unwrap();
|
|
for kind in ["minecraft:villager", "minecraft:zombie_villager"] {
|
|
let kind = std::sync::Arc::new(types.types[kind].clone());
|
|
for baby in [false, true] {
|
|
for t in 0..VILLAGER_TYPES.len() {
|
|
for p in 0..PROFESSIONS.len() {
|
|
let mut m =
|
|
Mob::new(1, kind.clone(), horizon_core::geometry::Vec3::ZERO, 0.0);
|
|
m.extra.age = if baby { -24000 } else { 0 };
|
|
(m.extra.variant, m.extra.variant2, m.extra.variant3) =
|
|
(t as u8, p as u8, 5);
|
|
let layers = layers(&m);
|
|
let group = if baby { "/baby/" } else { "/type/" };
|
|
assert!(
|
|
layers.iter().any(|l| l.texture.contains(group)),
|
|
"{kind:?} baby={baby}: no {group} layer"
|
|
);
|
|
for layer in layers {
|
|
let png = root
|
|
.join(format!("assets/minecraft/textures/{}.png", layer.texture));
|
|
assert!(png.is_file(), "missing {}", png.display());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn sulfur_cube_opaque_core_is_submitted_before_its_translucent_shell() {
|
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../.local/horizon-client/26.3");
|
|
if !root.join("entity-models.json").exists() {
|
|
return;
|
|
}
|
|
let mut types = super::super::MobTypes::load(&root).unwrap();
|
|
let kind = std::sync::Arc::new(types.types.remove("minecraft:sulfur_cube").unwrap());
|
|
let data = MobData {
|
|
states: Vec::new(),
|
|
placements: Default::default(),
|
|
heightmaps: Default::default(),
|
|
animations: Default::default(),
|
|
biome_tags: Default::default(),
|
|
entity_tags: Default::default(),
|
|
villager_hats: Default::default(),
|
|
pois: Default::default(),
|
|
};
|
|
let atlas = |name: &str| {
|
|
Some(if name.contains("_inner") {
|
|
[0.0, 0.0, 0.25, 0.25]
|
|
} else {
|
|
[0.5, 0.5, 0.75, 0.75]
|
|
})
|
|
};
|
|
for baby in [false, true] {
|
|
let mut mob = Mob::new(1, kind.clone(), horizon_core::geometry::Vec3::ZERO, 0.0);
|
|
mob.extra.age = if baby { -24000 } else { 0 };
|
|
super::super::sulfur_cube::finalize(&mut mob);
|
|
assert_eq!(mob.is_baby(), baby);
|
|
for posed in [false, true] {
|
|
let mut vertices = Vec::new();
|
|
if posed {
|
|
render_posed(
|
|
&mob,
|
|
0.5,
|
|
glam::Mat4::IDENTITY,
|
|
&atlas,
|
|
0xf0f0,
|
|
&data,
|
|
&mut vertices,
|
|
);
|
|
} else {
|
|
render(
|
|
&mob,
|
|
0.5,
|
|
glam::DVec3::ZERO,
|
|
&atlas,
|
|
0xf0f0,
|
|
&data,
|
|
&mut vertices,
|
|
);
|
|
}
|
|
assert_eq!(
|
|
vertices.len(),
|
|
48,
|
|
"both real six-face models must be drawn"
|
|
);
|
|
assert!(
|
|
vertices[..24].iter().all(|v| v.uv[0] <= 0.25),
|
|
"opaque core must precede shell"
|
|
);
|
|
assert!(vertices[24..].iter().all(|v| v.uv[0] >= 0.5));
|
|
}
|
|
mob.extra.head = Some("minecraft:stone".into());
|
|
let mut vertices = Vec::new();
|
|
render(
|
|
&mob,
|
|
0.5,
|
|
glam::DVec3::ZERO,
|
|
&atlas,
|
|
0xf0f0,
|
|
&data,
|
|
&mut vertices,
|
|
);
|
|
assert_eq!(
|
|
vertices.len(),
|
|
24,
|
|
"a swallowed block still hides the default core"
|
|
);
|
|
assert!(vertices.iter().all(|v| v.uv[0] >= 0.5));
|
|
}
|
|
}
|
|
/// `VexModel.translateToHand`: the held item is drawn at 0.55 scale and
|
|
/// moved by the vex's own offset, so the vex's sword is the tiny one
|
|
/// vanilla shows. It was drawn at the full size, like a zombie's.
|
|
#[test]
|
|
fn the_vex_holds_its_item_at_the_small_scale() {
|
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../.local/horizon-client/26.3");
|
|
if !root.join("entity-models.json").exists() {
|
|
return;
|
|
}
|
|
let types = super::super::MobTypes::load(&root).unwrap();
|
|
let kind = std::sync::Arc::new(types.types["minecraft:vex"].clone());
|
|
let mob = Mob::new(1, kind, horizon_core::geometry::Vec3::ZERO, 0.0);
|
|
let data = MobData {
|
|
states: Vec::new(),
|
|
placements: Default::default(),
|
|
heightmaps: Default::default(),
|
|
animations: Default::default(),
|
|
biome_tags: Default::default(),
|
|
entity_tags: Default::default(),
|
|
villager_hats: Default::default(),
|
|
pois: Default::default(),
|
|
};
|
|
for left in [false, true] {
|
|
let t = held_item_transform(&mob, 0.5, glam::DVec3::ZERO, &data, left).unwrap();
|
|
// The model's own part scale is 1, so the item's scale is the
|
|
// renderer's: `poseStack.scale(0.55F, 0.55F, 0.55F)`.
|
|
for axis in [t.x_axis, t.y_axis, t.z_axis] {
|
|
let scale = axis.truncate().length();
|
|
assert!(
|
|
(scale - 0.55).abs() < 1.0e-5,
|
|
"the vex's item is drawn at 0.55 scale, got {scale}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|