38b16f44cb
Breaking, mining, landing and other block particles took the block's up face and a fixed grass or foliage colour, so grass blocks crumbled as tinted grass tops (moss-like). Vanilla's TerrainParticle uses the model's particle material (first variant / first multipart selector) and tint source 0's colorAsTerrainParticle: the biome-blended in-world colour, none for grass blocks (BlockTintSources.grassBlock) or blocks without a source. The in-wall screen overlay and block markers use the particle material too. Fixes #1034 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
295 lines
11 KiB
Rust
295 lines
11 KiB
Rust
//! Bind prepared registry states to model choices; bake each transformed model
|
|
//! once and share its geometry across positions. Resource failure recovery and
|
|
//! resource-pack reloads remain separate work.
|
|
use crate::{Result, sprites::Sprites};
|
|
use horizon_assets::{
|
|
bake::{BlockModelTransform, ModelQuad, bake_block_model},
|
|
blockstate::{
|
|
BlockStateDefinition, Variant, VariantSet,
|
|
bind::{Properties, PropertySchema, PropertyValues, SelectedModels},
|
|
choice::CompiledChoices,
|
|
},
|
|
};
|
|
use horizon_core::{
|
|
position::BlockPos,
|
|
random::{LegacyRandom, position_seed},
|
|
};
|
|
use horizon_world::{
|
|
chunk::BlockState,
|
|
registry::{BlockRegistry, BlockStateId},
|
|
};
|
|
use serde_json::Value;
|
|
use std::{
|
|
collections::{BTreeMap, BTreeSet},
|
|
fs,
|
|
path::Path,
|
|
};
|
|
|
|
pub struct BakedFace {
|
|
pub quad: ModelQuad,
|
|
pub layer: u32,
|
|
pub opaque: bool,
|
|
/// Fractional alpha: drawn in the sorted translucent pass.
|
|
pub translucent: bool,
|
|
}
|
|
pub struct BakedModel {
|
|
pub ambient_occlusion: bool,
|
|
/// The model's particle slot also exists for models without block faces.
|
|
pub particle: Option<u32>,
|
|
pub faces: Vec<BakedFace>,
|
|
pub cube: Option<[super::cube::CubeStyle; 6]>,
|
|
}
|
|
type VariantKey = (String, [u8; 3], bool);
|
|
pub struct StateModels {
|
|
choices: Vec<CompiledChoices>,
|
|
states: BTreeMap<BlockStateId, (usize, SelectedModels)>,
|
|
baked: BTreeMap<VariantKey, BakedModel>,
|
|
}
|
|
fn key(variant: &Variant) -> VariantKey {
|
|
(variant.model.clone(), variant.rotations, variant.uv_lock)
|
|
}
|
|
impl StateModels {
|
|
pub fn load(
|
|
root: &Path,
|
|
registry: &BlockRegistry,
|
|
blocks: &[&str],
|
|
sprites: &mut Sprites,
|
|
) -> Result<Self> {
|
|
let facts: BTreeMap<String, Value> =
|
|
serde_json::from_slice(&fs::read(root.join("model-states.json"))?)?;
|
|
Self::from_facts(root, registry, blocks, sprites, &facts)
|
|
}
|
|
/// Bind and bake models using an already parsed model-state document.
|
|
pub fn from_facts(
|
|
root: &Path,
|
|
registry: &BlockRegistry,
|
|
blocks: &[&str],
|
|
sprites: &mut Sprites,
|
|
facts: &BTreeMap<String, Value>,
|
|
) -> Result<Self> {
|
|
Self::from_facts_with_models(
|
|
root,
|
|
registry,
|
|
blocks,
|
|
sprites,
|
|
facts,
|
|
&crate::model_library::ParsedModels::default(),
|
|
)
|
|
}
|
|
pub(crate) fn from_facts_with_models(
|
|
root: &Path,
|
|
registry: &BlockRegistry,
|
|
blocks: &[&str],
|
|
sprites: &mut Sprites,
|
|
facts: &BTreeMap<String, Value>,
|
|
models: &crate::model_library::ParsedModels,
|
|
) -> Result<Self> {
|
|
let mut choices = Vec::new();
|
|
let mut states = BTreeMap::new();
|
|
let mut variants = BTreeSet::new();
|
|
let mut by_name: std::collections::HashMap<&str, Vec<BlockStateId>> =
|
|
std::collections::HashMap::new();
|
|
for i in 0..registry.len() {
|
|
let id = BlockStateId(i as u32);
|
|
if let Some(state) = registry.state(id) {
|
|
by_name.entry(state.name.as_str()).or_default().push(id);
|
|
}
|
|
}
|
|
for block in blocks.iter().copied().collect::<BTreeSet<_>>() {
|
|
// Vanilla blocks come bare; content mods' blocks with their namespace.
|
|
let (namespace, path) = block.split_once(':').unwrap_or(("minecraft", block));
|
|
let name = format!("{namespace}:{path}");
|
|
let facts = facts.get(&name).ok_or("missing block model state schema")?;
|
|
let schema: PropertySchema = facts["schema"]
|
|
.as_object()
|
|
.ok_or("invalid model schema")?
|
|
.iter()
|
|
.map(|(key, value)| {
|
|
let values = if let Some(range) = value.get("integer") {
|
|
PropertyValues::Integer {
|
|
minimum: i32::try_from(
|
|
range[0]
|
|
.as_i64()
|
|
.ok_or("invalid integer property minimum")?,
|
|
)?,
|
|
maximum: i32::try_from(
|
|
range[1]
|
|
.as_i64()
|
|
.ok_or("invalid integer property maximum")?,
|
|
)?,
|
|
}
|
|
} else {
|
|
PropertyValues::Named(
|
|
value["named"]
|
|
.as_array()
|
|
.ok_or("missing named property values")?
|
|
.iter()
|
|
.map(|v| {
|
|
v.as_str()
|
|
.map(str::to_owned)
|
|
.ok_or("invalid named property value")
|
|
})
|
|
.collect::<std::result::Result<_, _>>()?,
|
|
)
|
|
};
|
|
Ok((key.clone(), values))
|
|
})
|
|
.collect::<Result<_>>()?;
|
|
let possible: Vec<Properties> = serde_json::from_value(facts["states"].clone())?;
|
|
// Vanilla's BlockStateModelLoader: a definition that does not
|
|
// read or bind is logged and its block gets no model; the other
|
|
// blocks still load.
|
|
let loaded = fs::read_to_string(
|
|
root.join(format!("assets/{namespace}/blockstates/{path}.json")),
|
|
)
|
|
.map_err(|e| e.to_string())
|
|
.and_then(|text| BlockStateDefinition::from_json(&text).map_err(|e| e.to_string()))
|
|
.and_then(|definition| {
|
|
let bound = definition
|
|
.bind(&schema, &possible)
|
|
.map_err(|e| e.to_string())?;
|
|
Ok((definition, bound))
|
|
});
|
|
let (definition, bound) = match loaded {
|
|
Ok(loaded) => loaded,
|
|
Err(e) => {
|
|
log::warn!(
|
|
"Horizon: blockstate definition of {namespace}:{path} not loaded: {e}"
|
|
);
|
|
continue;
|
|
}
|
|
};
|
|
let native_order: Vec<_> = by_name
|
|
.get(name.as_str())
|
|
.map(Vec::as_slice)
|
|
.unwrap_or_default()
|
|
.iter()
|
|
.map(|&id| (id, ®istry.state(id).expect("indexed state").properties))
|
|
.collect();
|
|
if native_order.len() != possible.len()
|
|
|| native_order
|
|
.iter()
|
|
.zip(&possible)
|
|
.any(|((_, a), b)| *a != b)
|
|
{
|
|
return Err(
|
|
"model state definitions disagree with the imported block registry".into(),
|
|
);
|
|
}
|
|
for ((id, _), selected) in native_order.into_iter().zip(bound) {
|
|
if let Some(selected) = selected {
|
|
states.insert(id, (choices.len(), selected));
|
|
}
|
|
}
|
|
for choice in definition
|
|
.variants
|
|
.iter()
|
|
.flatten()
|
|
.map(|(_, choice)| choice)
|
|
.chain(
|
|
definition
|
|
.multipart
|
|
.iter()
|
|
.flatten()
|
|
.map(|selector| &selector.variant),
|
|
)
|
|
{
|
|
match choice {
|
|
VariantSet::Single(v) => {
|
|
variants.insert(key(v));
|
|
}
|
|
VariantSet::Weighted(v) => variants.extend(v.iter().map(|(v, _)| key(v))),
|
|
}
|
|
}
|
|
choices.push(CompiledChoices::new(&definition)?);
|
|
}
|
|
let names: Vec<String> = variants.iter().map(|(name, _, _)| name.clone()).collect();
|
|
let library = match models.block_library(root, names.clone()) {
|
|
Ok(library) => library,
|
|
Err(e) => {
|
|
log::warn!("Horizon: block models incomplete ({e}); using the missing model");
|
|
models.block_library_or_missing(root, names)?
|
|
}
|
|
};
|
|
let mut baked = BTreeMap::new();
|
|
for (name, turns, lock) in variants {
|
|
let model = library.resolve(&name)?;
|
|
let particle = model
|
|
.material("particle")?
|
|
.map(|material| sprites.insert(root, &material.sprite))
|
|
.transpose()?;
|
|
let faces: Vec<BakedFace> =
|
|
bake_block_model(&model, BlockModelTransform::new(turns, lock))?
|
|
.into_iter()
|
|
.map(|quad| {
|
|
let layer = sprites.insert(root, &quad.material.sprite)?;
|
|
Ok(BakedFace {
|
|
translucent: quad.material.force_translucent
|
|
|| sprites.translucent[layer as usize],
|
|
quad,
|
|
layer,
|
|
opaque: sprites.opaque[layer as usize],
|
|
})
|
|
})
|
|
.collect::<Result<_>>()?;
|
|
let cube = super::cube::cube_styles(&faces);
|
|
baked.insert(
|
|
(name, turns, lock),
|
|
BakedModel {
|
|
ambient_occlusion: model.ambient_occlusion,
|
|
particle,
|
|
faces,
|
|
cube,
|
|
},
|
|
);
|
|
}
|
|
Ok(Self {
|
|
choices,
|
|
states,
|
|
baked,
|
|
})
|
|
}
|
|
pub fn collect(
|
|
&self,
|
|
state: BlockStateId,
|
|
position: BlockPos,
|
|
) -> Result<Vec<(&Variant, &BakedModel)>> {
|
|
// Blocks use their position seed; contained/displayed blocks can supply one.
|
|
self.collect_seeded(state, position_seed(position))
|
|
}
|
|
/// `BlockStateModel.particleMaterial`: the particle sprite layer of the
|
|
/// state's first variant (see `CompiledChoices::particle_variant`).
|
|
pub fn particle(&self, state: BlockStateId) -> Option<u32> {
|
|
let (choice, selected) = self.states.get(&state)?;
|
|
let variant = self.choices[*choice].particle_variant(selected)?;
|
|
self.baked.get(&key(variant))?.particle
|
|
}
|
|
pub fn collect_seeded(
|
|
&self,
|
|
state: BlockStateId,
|
|
seed: i64,
|
|
) -> Result<Vec<(&Variant, &BakedModel)>> {
|
|
let (choice, selected) = self
|
|
.states
|
|
.get(&state)
|
|
.ok_or("unbound gallery block state")?;
|
|
let mut random = LegacyRandom::new(seed);
|
|
let mut variants = Vec::new();
|
|
self.choices[*choice].collect(selected, &mut random, &mut variants)?;
|
|
variants
|
|
.into_iter()
|
|
.map(|v| Ok((v, self.baked.get(&key(v)).ok_or("unbaked block model")?)))
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
pub fn descriptor(name: &str, overrides: &[(&str, &str)]) -> BlockState {
|
|
BlockState {
|
|
name: format!("minecraft:{name}"),
|
|
properties: overrides
|
|
.iter()
|
|
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
|
|
.collect(),
|
|
}
|
|
}
|