Port the vanilla particle engine and client animateTick effects
particles.rs becomes a ParticleEngine with ~80 vanilla particle types (constructors, ticks, size curves, light, sprite sets from particles/*.json) and camera-rotation quads. animate_tick.rs ports ClientLevel.animateTick: torches, furnaces, campfires (with block-entity smoke), candles, leaves drips and falling leaves, lava/water, fluid drips, portals, enchanting tables, end rods, spore blossoms, redstone, bubble columns and biome ambient particles. Rain splashes follow tickWeatherEffects. Adds a particle control command. 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
@@ -2,6 +2,7 @@
|
||||
//! block interaction, inventory and HUD.
|
||||
mod control;
|
||||
mod render_control;
|
||||
mod render_tick;
|
||||
use horizon_client::{
|
||||
Result,
|
||||
assets::Assets,
|
||||
@@ -278,6 +279,7 @@ pub struct Resources {
|
||||
mob_types: HashMap<String, Arc<MobType>>,
|
||||
providers: Providers,
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer,
|
||||
animate: horizon_client::animate_tick::AnimateTables,
|
||||
nether_environment: Environment,
|
||||
behaviours: Behaviours,
|
||||
names: Names,
|
||||
@@ -340,7 +342,7 @@ struct Game<'a> {
|
||||
audio: Audio,
|
||||
dropped: Vec<ItemEntity>,
|
||||
next_entity: u32,
|
||||
particles: Vec<Particle>,
|
||||
particles: horizon_client::particles::ParticleEngine,
|
||||
spawn_search: u32,
|
||||
mobs: Vec<Mob>,
|
||||
arrows: Vec<Arrow>,
|
||||
@@ -1397,13 +1399,7 @@ impl Game<'_> {
|
||||
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));
|
||||
if self.particles.len() > 4000 {
|
||||
let excess = self.particles.len() - 4000;
|
||||
self.particles.drain(..excess);
|
||||
}
|
||||
self.tick_particles();
|
||||
if self.right && self.use_delay == 0 && self.screen == Screen::Playing {
|
||||
self.use_item()?;
|
||||
self.use_delay = 4;
|
||||
@@ -2190,34 +2186,10 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Camera-facing particle quads.
|
||||
let (yaw, pitch) = (self.yaw.to_radians(), self.pitch.to_radians());
|
||||
let forward = glam::Vec3::new(-yaw.sin() * pitch.cos(), -pitch.sin(), yaw.cos() * pitch.cos());
|
||||
let right = forward.cross(glam::Vec3::Y).normalize_or_zero();
|
||||
let up = right.cross(forward).normalize_or_zero();
|
||||
// Particles (ParticleEngine.extract): camera-facing quads.
|
||||
let world = &self.world;
|
||||
self.particles.render(camera, self.yaw, self.pitch, alpha as f32, &|b| world.light(b[0], b[1], b[2]), &mut out);
|
||||
let atlas = &self.res.atlas;
|
||||
for p in &self.particles {
|
||||
let Some(r) = atlas.sprites.get(&p.sprite).copied() else { continue };
|
||||
let a = alpha;
|
||||
let pos = glam::DVec3::new(
|
||||
p.previous[0] + (p.position[0] - p.previous[0]) * a,
|
||||
p.previous[1] + (p.position[1] - p.previous[1]) * a,
|
||||
p.previous[2] + (p.position[2] - p.previous[2]) * a,
|
||||
) - camera;
|
||||
let c = pos.as_vec3();
|
||||
let light = self.world.light(p.position[0].floor() as i32, p.position[1].floor() as i32, p.position[2].floor() as i32);
|
||||
let packed = u32::from(light >> 4) * 16 | (u32::from(light & 15) * 16) << 8;
|
||||
// TerrainParticle colour: 0.6 base brightness times the block tint.
|
||||
let ch = |sft: u32| (((p.tint >> sft) & 255) as f32 * 0.6) as u32;
|
||||
let color = 0xFF00_0000 | ch(0) << 16 | ch(8) << 8 | ch(16);
|
||||
let (du, dv) = (r.u1 - r.u0, r.v1 - r.v0);
|
||||
let (u0, v0, u1, v1) = (r.u0 + du * p.uv[0], r.v0 + dv * p.uv[1], r.u0 + du * p.uv[2], r.v0 + dv * p.uv[3]);
|
||||
let s = p.size;
|
||||
for (k, uv) in [(-1.0, -1.0), (1.0, -1.0), (1.0, 1.0), (-1.0, 1.0)].iter().zip([[u0, v1], [u1, v1], [u1, v0], [u0, v0]]) {
|
||||
let v = c + right * (k.0 * s) + up * (k.1 * s);
|
||||
out.push(Vertex { pos: v.to_array(), uv, layer: 0, color, light: packed });
|
||||
}
|
||||
}
|
||||
// Breaking progress overlay (destroy_stage_0..9).
|
||||
if let Some(m) = &self.mining
|
||||
&& m.progress > 0.0
|
||||
@@ -4658,6 +4630,7 @@ pub fn run() -> Result<()> {
|
||||
let audio = Audio::new(&root);
|
||||
let world = World::new(facts.air, 0);
|
||||
let creative = options.creative;
|
||||
let animate = horizon_client::animate_tick::AnimateTables::load(&root, registry_owner.registry, &biomes)?;
|
||||
let mut resources = Resources {
|
||||
assets,
|
||||
facts,
|
||||
@@ -4670,6 +4643,7 @@ pub fn run() -> Result<()> {
|
||||
layer_names,
|
||||
providers: Providers::load(&root),
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer::load(&root, ®istry_owner.registry)?,
|
||||
animate,
|
||||
nether_environment: Environment::load_dimension(&root, "the_nether", false)?,
|
||||
behaviours: Behaviours::load(registry_owner.registry, &root)?,
|
||||
names: Names::load(&root),
|
||||
@@ -4691,6 +4665,7 @@ pub fn run() -> Result<()> {
|
||||
settings.render_distance = d;
|
||||
}
|
||||
let look = options.look;
|
||||
let particles = horizon_client::particles::ParticleEngine::new(&root, &resources.atlas.sprites);
|
||||
let mut game = Game {
|
||||
res: &mut resources,
|
||||
collision: &collision,
|
||||
@@ -4741,7 +4716,7 @@ pub fn run() -> Result<()> {
|
||||
audio,
|
||||
dropped: Vec::new(),
|
||||
next_entity: 0,
|
||||
particles: Vec::new(),
|
||||
particles,
|
||||
spawn_search: 0,
|
||||
mobs: Vec::new(),
|
||||
arrows: Vec::new(),
|
||||
|
||||
@@ -66,7 +66,7 @@ impl Game<'_> {
|
||||
let ok = || Ok(Some(json!({ "ok": true })));
|
||||
match words[0] {
|
||||
"help" => Ok(Some(json!({ "commands": HELP.iter().chain(super::render_control::HELP).collect::<Vec<_>>() }))),
|
||||
"setblock" | "fill" => self.render_command(&words),
|
||||
"setblock" | "fill" | "particle" => 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]))?;
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
//! updates (`setblock`, `fill`) to build test scenes.
|
||||
use super::Game;
|
||||
use horizon_client::Result;
|
||||
use horizon_core::random::RandomSource;
|
||||
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",
|
||||
"particle NAME X Y Z [DX DY DZ] [COUNT] spawn particles (vanilla /particle spread)",
|
||||
];
|
||||
|
||||
impl Game<'_> {
|
||||
@@ -68,6 +70,38 @@ impl Game<'_> {
|
||||
}
|
||||
Ok(Some(json!({ "ok": true, "blocks": count })))
|
||||
}
|
||||
"particle" => {
|
||||
use horizon_client::particles::{ParticleOptions, ParticleType};
|
||||
let ty = ParticleType::from_name(words.get(1).ok_or("missing particle")?)
|
||||
.ok_or("unknown particle type")?;
|
||||
let num = |i: usize, default: f64| -> Result<f64> {
|
||||
Ok(words.get(i).map_or(Ok(default), |w| w.parse::<f64>())?)
|
||||
};
|
||||
let at = [num(2, 0.0)?, num(3, 0.0)?, num(4, 0.0)?];
|
||||
let delta = [num(5, 0.0)?, num(6, 0.0)?, num(7, 0.0)?];
|
||||
let count = num(8, 1.0)? as u32;
|
||||
let options = match ty {
|
||||
ParticleType::Dust => ParticleOptions::dust([1.0, 0.0, 0.0], 1.0),
|
||||
ParticleType::Block | ParticleType::FallingDust | ParticleType::DustPillar => {
|
||||
ParticleOptions::block(ty, "block/stone".into(), 0x808080)
|
||||
}
|
||||
_ => ParticleOptions::simple(ty),
|
||||
};
|
||||
// ServerLevel.sendParticles: count 0 uses the delta as velocity.
|
||||
if count == 0 {
|
||||
self.particles.create(&options, at, delta);
|
||||
}
|
||||
for _ in 0..count {
|
||||
let r = self.particles.random();
|
||||
let p = [
|
||||
at[0] + r.next_gaussian() * delta[0],
|
||||
at[1] + r.next_gaussian() * delta[1],
|
||||
at[2] + r.next_gaussian() * delta[2],
|
||||
];
|
||||
self.particles.create(&options, p, [0.0; 3]);
|
||||
}
|
||||
Ok(Some(json!({ "ok": true, "live": self.particles.len() })))
|
||||
}
|
||||
other => Err(format!("unknown render command {other}").into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
//! Per-tick client visuals: the particle engine, `ClientLevel.animateTick`,
|
||||
//! campfire smoke and rain splashes (`ClientLevel.tickWeatherEffects`).
|
||||
use super::{Dim, Game};
|
||||
use horizon_client::{
|
||||
animate_tick::{AnimateContext, animate_tick, campfire_particles},
|
||||
biome::Biomes,
|
||||
blocks::{BlockFacts, Fluid},
|
||||
chunk::{HEIGHT, MIN_Y},
|
||||
particles::{ParticleLevel, ParticleOptions, ParticleType},
|
||||
world::World,
|
||||
};
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
|
||||
/// The first motion-blocking block at or below `from` (Heightmap MOTION_BLOCKING), plus one.
|
||||
fn motion_blocking(world: &World, facts: &BlockFacts, x: i32, z: i32, from: i32) -> i32 {
|
||||
let mut y = from.min(MIN_Y + HEIGHT - 1);
|
||||
while y > MIN_Y {
|
||||
let f = facts.get(world.block(x, y, z));
|
||||
if f.full_collision || f.fluid != Fluid::None || f.name.contains("leaves") {
|
||||
return y + 1;
|
||||
}
|
||||
y -= 1;
|
||||
}
|
||||
MIN_Y
|
||||
}
|
||||
/// `Biome.getPrecipitationAt` is rain (not snow) at this position.
|
||||
fn rains_at(world: &World, biomes: &Biomes, x: i32, y: i32, z: i32) -> bool {
|
||||
let Some(chunk) = world.chunks.get(&(x >> 4, z >> 4)) else {
|
||||
return false;
|
||||
};
|
||||
let b = biomes.get(chunk.biome(x, y, z));
|
||||
if !b.precipitation {
|
||||
return false;
|
||||
}
|
||||
let t = if y > 80 {
|
||||
b.temperature - (y - 80) as f32 * 0.05 / 40.0
|
||||
} else {
|
||||
b.temperature
|
||||
};
|
||||
t >= 0.15
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
pub(super) fn tick_particles(&mut self) {
|
||||
let eye = self.eye_position();
|
||||
if let Some(e) = eye {
|
||||
self.particles.camera = e;
|
||||
}
|
||||
let (rain, _) = if self.dim == Dim::Overworld {
|
||||
self.world.weather.levels(1.0)
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
let world = &self.world;
|
||||
let facts = &self.res.facts;
|
||||
let biomes = &self.res.biomes;
|
||||
let raining = rain > 0.2;
|
||||
let raining_at = |p: [i32; 3]| {
|
||||
raining
|
||||
&& motion_blocking(world, facts, p[0], p[2], p[1] + 64) <= p[1]
|
||||
&& rains_at(world, biomes, p[0], p[1], p[2])
|
||||
};
|
||||
let ctx = AnimateContext {
|
||||
world,
|
||||
facts,
|
||||
collision: self.collision,
|
||||
biomes,
|
||||
registry: self.res.assets.registry,
|
||||
raining_at: &raining_at,
|
||||
};
|
||||
if let Some(e) = eye {
|
||||
let block = [
|
||||
e[0].floor() as i32,
|
||||
e[1].floor() as i32,
|
||||
e[2].floor() as i32,
|
||||
];
|
||||
animate_tick(&self.res.animate, &mut self.particles, &ctx, block);
|
||||
if rain > 0.0 {
|
||||
// ClientLevel.tickWeatherEffects: rain splashes (weather radius 10).
|
||||
let mut random = LegacyRandom::new(self.world.game_time.wrapping_mul(312_987_231));
|
||||
let radius = 10;
|
||||
let diameter = 2 * radius + 1;
|
||||
let count = (0.225 * (diameter * diameter) as f32 * rain * rain) as i32;
|
||||
for _ in 0..count {
|
||||
let x = block[0] + random.next_int_bound(diameter).unwrap_or(0) - radius;
|
||||
let z = block[2] + random.next_int_bound(diameter).unwrap_or(0) - radius;
|
||||
let top = motion_blocking(world, facts, x, z, block[1] + 64);
|
||||
if top <= MIN_Y
|
||||
|| top > block[1] + 10
|
||||
|| top < block[1] - 10
|
||||
|| !rains_at(world, biomes, x, top, z)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let below = top - 1;
|
||||
let (bx, bz) = (random.next_double(), random.next_double());
|
||||
let state = world.block(x, below, z);
|
||||
let f = facts.get(state);
|
||||
let block_top = match self.collision.cached_shapes().cached_shape(state) {
|
||||
Ok(Some(shape)) if !shape.is_empty() => {
|
||||
shape.max_at(horizon_core::geometry::Axis::Y, bx, bz)
|
||||
}
|
||||
_ => 0.0,
|
||||
};
|
||||
let fluid_top = if f.fluid == Fluid::None {
|
||||
0.0
|
||||
} else {
|
||||
f64::from(f.fluid_level) / 9.0
|
||||
};
|
||||
let smoke = f.fluid == Fluid::Lava
|
||||
|| &*f.name == "minecraft:magma_block"
|
||||
|| (f.name.ends_with("campfire")
|
||||
&& self.res.animate.is_lit_campfire(state.0));
|
||||
let ty = if smoke {
|
||||
ParticleType::Smoke
|
||||
} else {
|
||||
ParticleType::Rain
|
||||
};
|
||||
let p = [
|
||||
f64::from(x) + bx,
|
||||
f64::from(below) + block_top.max(fluid_top),
|
||||
f64::from(z) + bz,
|
||||
];
|
||||
self.particles
|
||||
.add(&ParticleOptions::simple(ty), p, [0.0; 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(r) = &self.renderer {
|
||||
campfire_particles(
|
||||
&self.res.animate,
|
||||
&mut self.particles,
|
||||
&ctx,
|
||||
r.block_entities(),
|
||||
);
|
||||
}
|
||||
let level = ParticleLevel {
|
||||
world,
|
||||
facts,
|
||||
collision: self.collision,
|
||||
player: self
|
||||
.player
|
||||
.as_ref()
|
||||
.map(|p| ([p.position.x, p.position.y, p.position.z], p.velocity.y)),
|
||||
};
|
||||
self.particles.tick(&level);
|
||||
for (name, at, volume, pitch) in std::mem::take(&mut self.particles.sounds) {
|
||||
self.audio.play(name, Some(at), volume, pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Horizon native client: resources, world streaming, rendering and the
|
||||
//! bindings between the validated simulation pieces and the game loop.
|
||||
#![forbid(unsafe_code)]
|
||||
pub mod animate_tick;
|
||||
pub mod assets;
|
||||
pub mod attribute_updates;
|
||||
pub mod audio;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user