Render block entities: chests, shulker boxes, banners, skulls, bells, pots, conduits, books, copper golem statues
Sections now report their block-entity blocks; a new block_entity_render module draws them each frame with vanilla's baked layer models, renderer transforms, animations (chest lids, banner wave, enchanting-table book) and entity diffuse lighting. Adds setblock/fill control commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
//! The playable game loop: streaming world, player simulation at 20 ticks/s,
|
||||
//! block interaction, inventory and HUD.
|
||||
mod control;
|
||||
mod render_control;
|
||||
use horizon_client::{
|
||||
Result,
|
||||
assets::Assets,
|
||||
@@ -276,7 +277,7 @@ pub struct Resources {
|
||||
layer_names: HashMap<u32, String>,
|
||||
mob_types: HashMap<String, Arc<MobType>>,
|
||||
providers: Providers,
|
||||
chest_model: Option<horizon_client::mobs::Part>,
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer,
|
||||
nether_environment: Environment,
|
||||
behaviours: Behaviours,
|
||||
names: Names,
|
||||
@@ -1392,6 +1393,10 @@ impl Game<'_> {
|
||||
self.bob.1 += (target - self.bob.1) * 0.4;
|
||||
}
|
||||
self.tick_block_entities()?;
|
||||
if let Some(r) = &self.renderer {
|
||||
let eye = self.player.as_ref().map(|p| [p.position.x, p.position.y + f64::from(self.eye), p.position.z]);
|
||||
self.res.block_entity_render.tick(r.block_entities(), eye);
|
||||
}
|
||||
let facts = &self.res.facts;
|
||||
let world = &self.world;
|
||||
self.particles.retain_mut(|p| p.tick(world, |id| facts.states[id as usize].full_collision));
|
||||
@@ -2110,49 +2115,22 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Chests (ChestRenderer): rotate by facing around the block centre, lift the lid.
|
||||
if let Some(model) = &self.res.chest_model {
|
||||
for (pos, entity) in &self.world.block_entities {
|
||||
if !matches!(entity, BlockEntity::Chest(_)) {
|
||||
continue;
|
||||
}
|
||||
let state = self.world.block(pos.0, pos.1, pos.2);
|
||||
let name = &self.res.facts.get(state).name;
|
||||
let texture = match &**name {
|
||||
"minecraft:chest" => "entity/chest/normal",
|
||||
"minecraft:trapped_chest" => "entity/chest/trapped",
|
||||
_ => continue,
|
||||
};
|
||||
let origin = glam::DVec3::new(f64::from(pos.0), f64::from(pos.1), f64::from(pos.2)) - camera;
|
||||
if origin.length_squared() > 64.0 * 64.0 {
|
||||
continue;
|
||||
}
|
||||
let Some(r) = self.res.atlas.sprites.get(texture).copied() else { continue };
|
||||
let facing = self.res.assets.registry.state(state).and_then(|s| s.properties.get("facing").cloned()).unwrap_or_default();
|
||||
let yrot: f32 = match facing.as_str() {
|
||||
"west" => 90.0,
|
||||
"north" => 180.0,
|
||||
"east" => 270.0,
|
||||
_ => 0.0,
|
||||
};
|
||||
let (prev, cur) = self.chest_open.get(pos).copied().unwrap_or((0.0, 0.0));
|
||||
let o = prev + (cur - prev) * alpha as f32;
|
||||
let open = 1.0 - (1.0 - o).powi(3);
|
||||
let base = glam::Mat4::from_translation(origin.as_vec3())
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.5, 0.0, 0.5))
|
||||
* glam::Mat4::from_rotation_y((-yrot).to_radians())
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(-0.5, 0.0, -0.5));
|
||||
let light = self.world.light(pos.0, pos.1, pos.2);
|
||||
let packed = u32::from(light >> 4) * 16 | (u32::from(light & 15) * 16) << 8;
|
||||
let pose = |part: &horizon_client::mobs::Part| {
|
||||
let mut p = part.pose;
|
||||
if part.name == "lid" || part.name == "lock" {
|
||||
p[3] = -(open * std::f32::consts::FRAC_PI_2);
|
||||
}
|
||||
p
|
||||
};
|
||||
horizon_client::mobs::render_model(model, base, &pose, [r.u0, r.v0, r.u1, r.v1], packed, 0, &mut out);
|
||||
}
|
||||
// Block entities (BlockEntityRenderers): chests, shulker boxes, banners, skulls...
|
||||
if let Some(r) = &self.renderer {
|
||||
let lid = |p: [i32; 3]| {
|
||||
let (prev, cur) = self.chest_open.get(&(p[0], p[1], p[2])).copied().unwrap_or((0.0, 0.0));
|
||||
prev + (cur - prev) * alpha as f32
|
||||
};
|
||||
let scene = horizon_client::block_entity_render::Scene {
|
||||
camera,
|
||||
partial: alpha as f32,
|
||||
game_time: self.world.game_time,
|
||||
world: &self.world,
|
||||
sprites: &self.res.atlas.sprites,
|
||||
nether: self.dim == Dim::Nether,
|
||||
lid: &lid,
|
||||
};
|
||||
self.res.block_entity_render.render(&scene, r.block_entities(), &mut out);
|
||||
}
|
||||
// Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet.
|
||||
if let Some(r) = self.res.atlas.sprites.get("entity/experience_orb").copied() {
|
||||
@@ -4691,7 +4669,7 @@ pub fn run() -> Result<()> {
|
||||
icons: HashMap::new(),
|
||||
layer_names,
|
||||
providers: Providers::load(&root),
|
||||
chest_model: horizon_client::mobs::load_model(&root, "minecraft:chest#main"),
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer::load(&root, ®istry_owner.registry)?,
|
||||
nether_environment: Environment::load_dimension(&root, "the_nether", false)?,
|
||||
behaviours: Behaviours::load(registry_owner.registry, &root)?,
|
||||
names: Names::load(&root),
|
||||
|
||||
@@ -65,7 +65,8 @@ impl Game<'_> {
|
||||
};
|
||||
let ok = || Ok(Some(json!({ "ok": true })));
|
||||
match words[0] {
|
||||
"help" => Ok(Some(json!({ "commands": HELP }))),
|
||||
"help" => Ok(Some(json!({ "commands": HELP.iter().chain(super::render_control::HELP).collect::<Vec<_>>() }))),
|
||||
"setblock" | "fill" => self.render_command(&words),
|
||||
"status" => Ok(Some(self.control_status())),
|
||||
"key" => {
|
||||
let key = key_code(arg(1)?).ok_or_else(|| format!("unknown key {}", words[1]))?;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
//! Control-socket commands for inspecting rendering: place blocks without
|
||||
//! updates (`setblock`, `fill`) to build test scenes.
|
||||
use super::Game;
|
||||
use horizon_client::Result;
|
||||
use horizon_world::registry::BlockStateId;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const HELP: &[&str] = &[
|
||||
"setblock X Y Z STATE set a block state (name[prop=value,...]) without updates",
|
||||
"fill X1 Y1 Z1 X2 Y2 Z2 STATE fill a box with a block state without updates",
|
||||
];
|
||||
|
||||
impl Game<'_> {
|
||||
/// Parse `name[k=v,...]` into a registry state (defaults for missing properties).
|
||||
fn parse_state(&self, text: &str) -> Result<BlockStateId> {
|
||||
let (name, props) = match text.split_once('[') {
|
||||
Some((n, rest)) => (n, rest.trim_end_matches(']')),
|
||||
None => (text, ""),
|
||||
};
|
||||
let name = if name.contains(':') {
|
||||
name.to_owned()
|
||||
} else {
|
||||
format!("minecraft:{name}")
|
||||
};
|
||||
let registry = &self.res.assets.registry;
|
||||
let default = registry
|
||||
.default_state(&name)
|
||||
.ok_or_else(|| format!("unknown block {name}"))?;
|
||||
let mut state = registry
|
||||
.state(default)
|
||||
.ok_or("missing default state")?
|
||||
.clone();
|
||||
for pair in props.split(',').filter(|p| !p.is_empty()) {
|
||||
let (k, v) = pair
|
||||
.split_once('=')
|
||||
.ok_or_else(|| format!("bad property {pair}"))?;
|
||||
if !state.properties.contains_key(k) {
|
||||
return Err(format!("{name} has no property {k}").into());
|
||||
}
|
||||
state.properties.insert(k.to_owned(), v.to_owned());
|
||||
}
|
||||
Ok(registry.resolve(&state)?)
|
||||
}
|
||||
pub(super) fn render_command(&mut self, words: &[&str]) -> Result<Option<Value>> {
|
||||
let int = |i: usize| -> Result<i32> {
|
||||
Ok(words
|
||||
.get(i)
|
||||
.ok_or_else(|| format!("missing argument {i}"))?
|
||||
.parse::<i32>()?)
|
||||
};
|
||||
match words[0] {
|
||||
"setblock" => {
|
||||
let state = self.parse_state(words.get(4).ok_or("missing state")?)?;
|
||||
self.world.set(int(1)?, int(2)?, int(3)?, state)?;
|
||||
Ok(Some(json!({ "ok": true, "state": state.0 })))
|
||||
}
|
||||
"fill" => {
|
||||
let state = self.parse_state(words.get(7).ok_or("missing state")?)?;
|
||||
let (a, b) = ([int(1)?, int(2)?, int(3)?], [int(4)?, int(5)?, int(6)?]);
|
||||
let mut count = 0;
|
||||
for x in a[0].min(b[0])..=a[0].max(b[0]) {
|
||||
for y in a[1].min(b[1])..=a[1].max(b[1]) {
|
||||
for z in a[2].min(b[2])..=a[2].max(b[2]) {
|
||||
self.world.set(x, y, z, state)?;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Some(json!({ "ok": true, "blocks": count })))
|
||||
}
|
||||
other => Err(format!("unknown render command {other}").into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,8 @@ pub struct Renderer {
|
||||
outline: wgpu::Buffer,
|
||||
depth: wgpu::TextureView,
|
||||
sections: HashMap<SectionKey, SectionGpu>,
|
||||
/// Block-entity blocks per meshed section (world position, state id).
|
||||
block_entities: HashMap<SectionKey, Vec<([i32; 3], u32)>>,
|
||||
/// VisGraph face connectivity per meshed section (cave culling).
|
||||
visibility: HashMap<SectionKey, u64>,
|
||||
reachable: std::collections::HashSet<SectionKey>,
|
||||
@@ -686,6 +688,7 @@ impl Renderer {
|
||||
outline,
|
||||
depth,
|
||||
sections: HashMap::new(),
|
||||
block_entities: HashMap::new(),
|
||||
visibility: HashMap::new(),
|
||||
reachable: std::collections::HashSet::new(),
|
||||
reachable_from: None,
|
||||
@@ -748,6 +751,11 @@ impl Renderer {
|
||||
if self.visibility.insert(key, mesh.visibility) != Some(mesh.visibility) {
|
||||
self.visibility_dirty = true;
|
||||
}
|
||||
if mesh.block_entities.is_empty() {
|
||||
self.block_entities.remove(&key);
|
||||
} else {
|
||||
self.block_entities.insert(key, mesh.block_entities.clone());
|
||||
}
|
||||
let solid = (mesh.solid.len() / 4) as u32;
|
||||
let translucent = (mesh.translucent.len() / 4) as u32;
|
||||
if solid + translucent == 0 {
|
||||
@@ -780,7 +788,12 @@ impl Renderer {
|
||||
},
|
||||
);
|
||||
}
|
||||
/// Block-entity blocks of every meshed section (see `SectionMesh::block_entities`).
|
||||
pub fn block_entities(&self) -> impl Iterator<Item = &([i32; 3], u32)> {
|
||||
self.block_entities.values().flatten()
|
||||
}
|
||||
pub fn clear_sections(&mut self) {
|
||||
self.block_entities.clear();
|
||||
self.sections.clear();
|
||||
self.visibility.clear();
|
||||
self.visibility_dirty = true;
|
||||
@@ -820,6 +833,7 @@ impl Renderer {
|
||||
self.visibility_dirty = false;
|
||||
}
|
||||
pub fn remove(&mut self, key: SectionKey) {
|
||||
self.block_entities.remove(&key);
|
||||
self.visibility.remove(&key);
|
||||
self.visibility_dirty = true;
|
||||
self.sections.remove(&key);
|
||||
|
||||
@@ -5,6 +5,7 @@ pub mod assets;
|
||||
pub mod attribute_updates;
|
||||
pub mod audio;
|
||||
pub mod biome;
|
||||
pub mod block_entity_render;
|
||||
pub mod blocks;
|
||||
pub mod chunk;
|
||||
pub mod clock;
|
||||
|
||||
@@ -32,10 +32,14 @@ pub struct SectionMesh {
|
||||
/// VisGraph: bit `a * 6 + b` is set when faces `a` and `b` (Direction
|
||||
/// order) are connected through non-opaque blocks.
|
||||
pub visibility: u64,
|
||||
/// Blocks with a block-entity renderer (world position, state id), drawn
|
||||
/// each frame by `block_entity_render` like vanilla's per-section
|
||||
/// `SectionCompiler.Results.blockEntities`.
|
||||
pub block_entities: Vec<([i32; 3], u32)>,
|
||||
}
|
||||
impl Default for SectionMesh {
|
||||
fn default() -> Self {
|
||||
Self { solid: Vec::new(), translucent: Vec::new(), visibility: ALL_VISIBLE }
|
||||
Self { solid: Vec::new(), translucent: Vec::new(), visibility: ALL_VISIBLE, block_entities: Vec::new() }
|
||||
}
|
||||
}
|
||||
pub const ALL_VISIBLE: u64 = (1 << 36) - 1;
|
||||
@@ -109,6 +113,8 @@ struct Info {
|
||||
fluid: Fluid,
|
||||
level: u8,
|
||||
model: bool,
|
||||
/// The state has a block entity (candidates for block-entity renderers).
|
||||
block_entity: bool,
|
||||
tints: [Tint; 2],
|
||||
}
|
||||
pub struct MeshTables {
|
||||
@@ -189,6 +195,7 @@ impl MeshTables {
|
||||
fluid: s.fluid,
|
||||
level: s.fluid_level,
|
||||
model: s.render_shape == RenderShape::Model,
|
||||
block_entity: s.block_entity,
|
||||
tints,
|
||||
}
|
||||
})
|
||||
@@ -336,6 +343,9 @@ impl Mesher<'_> {
|
||||
continue;
|
||||
}
|
||||
let state = info[id as usize];
|
||||
if state.block_entity {
|
||||
out.block_entities.push(([center.x * 16 + x, base_y + y, center.z * 16 + z], u32::from(id)));
|
||||
}
|
||||
if state.solid
|
||||
&& Direction::ALL.iter().all(|d| {
|
||||
let [dx, dy, dz] = d.normal();
|
||||
|
||||
Reference in New Issue
Block a user