diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl index 269c6a80c..e56607466 100644 --- a/crates/horizon-client/src/entity.wgsl +++ b/crates/horizon-client/src/entity.wgsl @@ -30,12 +30,14 @@ struct Out { let l = vec2(f32(light.y), f32(light.x)); let lm = textureSampleLevel(lightmap, lightmap_sampler, clamp(l / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0); - var c = color * lm; + // overlay 2: celestial bodies use their vertex colour unlit. + var c = select(color * lm, color, overlay == 2u); // Hurt overlay (OverlayTexture red tint). if overlay == 1u { c = vec4(mix(c.rgb, vec3(1.0, 0.0, 0.0), 0.3), c.a); } out.color = c; - out.spherical = length(position); - out.cylindrical = max(length(position.xz), abs(position.y)); + // Sky geometry (clouds, celestial bodies) is exempt from render-distance fog. + out.spherical = select(length(position), 0.0, overlay >= 2u); + out.cylindrical = select(max(length(position.xz), abs(position.y)), 0.0, overlay >= 2u); return out; } fn linear_fog(d: f32, start: f32, end: f32) -> f32 { @@ -50,3 +52,8 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { linear_fog(in.cylindrical, globals.fog.z, globals.fog.w)); return vec4(mix(color.rgb, globals.fog_color.rgb, f * globals.fog_color.a), color.a); } + +@fragment fn fs_celestial(in: Out) -> @location(0) vec4 { + let color = textureSample(atlas, atlas_sampler, in.uv) * in.color; + return color; +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index bb44ad312..619acf9a6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -56,6 +56,8 @@ struct Options { timeout: Duration, report: Option, world: String, + look: Option<(f32, f32)>, + time: Option, } impl Options { fn read() -> Result> { @@ -78,6 +80,8 @@ impl Options { timeout: Duration::from_secs(120), report: None, world: "New World".into(), + look: None, + time: None, }; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { @@ -114,6 +118,11 @@ impl Options { "--smoke-report" => o.report = Some(value.into()), "--screenshot-after" => o.screenshot_after = Some(value.parse()?), "--world" => o.world = value, + "--time" => o.time = Some(value.parse()?), + "--look" => { + let (y, p) = value.split_once(',').ok_or("--look takes YAW,PITCH")?; + o.look = Some((y.parse()?, p.parse()?)); + } _ => return Err(format!("unknown option {arg}").into()), } } @@ -157,6 +166,8 @@ pub struct Resources { mob_types: HashMap>, providers: Providers, chest_model: Option, + /// clouds.png: (width, height, filled cells). + clouds: (u32, u32, Vec), } struct Game<'a> { res: &'a mut Resources, @@ -2134,6 +2145,119 @@ impl Game<'_> { } Ok(()) } + /// SkyRenderer.renderSunMoonAndStars: bodies on a frame rotated Y -90 then X by angle. + fn celestial_geometry(&self, sky: &horizon_client::environment::Sky) -> Vec { + let mut out = Vec::new(); + let atlas = &self.res.atlas; + let frame = |angle: f32| glam::Mat4::from_rotation_y((-90.0f32).to_radians()) * glam::Mat4::from_rotation_x(angle.to_radians()); + let mut body = |name: &str, angle: f32, size: f32, color: u32| { + let Some(r) = atlas.sprites.get(name).copied() else { return }; + let m = frame(angle); + let corners = [(-size, -size), (size, -size), (size, size), (-size, size)]; + let uv = [[r.u0, r.v0], [r.u1, r.v0], [r.u1, r.v1], [r.u0, r.v1]]; + for ((x, z), uv) in corners.iter().zip(uv) { + let p = m.transform_point3(glam::Vec3::new(*x, 100.0, *z)); + out.push(Vertex { pos: p.to_array(), uv, layer: 2, color: color, light: 0 }); + } + }; + body("environment/celestial/sun", sky.sun_angle, 30.0, 0xFFFF_FFFF); + let phases = ["full_moon", "waning_gibbous", "third_quarter", "waning_crescent", "new_moon", "waxing_crescent", "first_quarter", "waxing_gibbous"]; + let phase = phases[(self.world.day_time / 24000).rem_euclid(8) as usize]; + body(&format!("environment/celestial/moon/{phase}"), sky.moon_angle, 20.0, 0xFFFF_FFFF); + if sky.star_brightness > 0.0 { + let b = (sky.star_brightness.clamp(0.0, 1.0) * 255.0) as u32; + let color = b << 24 | b << 16 | b << 8 | b; + let white = atlas.sprites.get("horizon/white").copied(); + let m = frame(sky.star_angle); + if let Some(r) = white { + let (cu, cv) = ((r.u0 + r.u1) / 2.0, (r.v0 + r.v1) / 2.0); + // SkyRenderer.buildStars with its fixed seed. + let mut random = horizon_core::random::LegacyRandom::new(10842); + for _ in 0..1500 { + let x = random.next_float() * 2.0 - 1.0; + let y = random.next_float() * 2.0 - 1.0; + let z = random.next_float() * 2.0 - 1.0; + let size = 0.15 + random.next_float() * 0.1; + let l = x * x + y * y + z * z; + if l <= 0.010_000_001 || l >= 1.0 { + continue; + } + let center = glam::Vec3::new(x, y, z).normalize() * 100.0; + let z_rot = (random.next_double() * std::f64::consts::TAU) as f32; + let forward = -center.normalize(); + let right = glam::Vec3::Y.cross(forward).normalize_or(glam::Vec3::X); + let up = forward.cross(right); + let (s, c) = (-z_rot).sin_cos(); + let (r2, u2) = (right * c + up * s, up * c - right * s); + for (a, b) in [(size, -size), (size, size), (-size, size), (-size, -size)] { + let p = m.transform_point3(center + r2 * a + u2 * b); + out.push(Vertex { pos: p.to_array(), uv: [cu, cv], layer: 2, color, light: 0 }); + } + } + } + } + out + } + /// CloudRenderer (fancy): 12x4x12 cells from clouds.png drifting east. + fn cloud_geometry(&self, sky: &horizon_client::environment::Sky, camera: glam::DVec3, alpha: f64, out: &mut Vec) { + let (w, h, cells) = &self.res.clouds; + let (w, h) = (i64::from(*w), i64::from(*h)); + let Some(white) = self.res.atlas.sprites.get("horizon/white").copied() else { return }; + let uv = [(white.u0 + white.u1) / 2.0, (white.v0 + white.v1) / 2.0]; + let cloud_height = 192.33; + let offset = (self.world.game_time.rem_euclid(w * 400)) as f64 + alpha; + let mut cloud_x = camera.x + offset * 0.030_000_001; + let mut cloud_z = camera.z + 3.96; + let (tw, th) = (w as f64 * 12.0, h as f64 * 12.0); + cloud_x -= (cloud_x / tw).floor() * tw; + cloud_z -= (cloud_z / th).floor() * th; + let cell_x = (cloud_x / 12.0).floor() as i64; + let cell_z = (cloud_z / 12.0).floor() as i64; + let x_in = (cloud_x - cell_x as f64 * 12.0) as f32; + let z_in = (cloud_z - cell_z as f64 * 12.0) as f32; + let bottom = (cloud_height - camera.y) as f32; + let filled = |cx: i64, cz: i64| cells[(cz.rem_euclid(h) * w + cx.rem_euclid(w)) as usize]; + let radius = (self.streaming.radius * 16 / 12 + 1) as i64; + let c = sky.cloud_color; + let color = |shade: f32| { + let ch = |v: f32| ((v * shade).clamp(0.0, 1.0) * 255.0) as u32; + ch(c[3].max(0.0).min(1.0)) << 24 | ch(c[2]) << 16 | ch(c[1]) << 8 | ch(c[0]) + }; + let light = 240 | 240 << 8; + for dz in -radius..=radius { + for dx in -radius..=radius { + if dx * dx + dz * dz > radius * radius { + continue; + } + let (cx, cz) = (cell_x + dx, cell_z + dz); + if !filled(cx, cz) { + continue; + } + let x0 = dx as f32 * 12.0 - x_in; + let z0 = dz as f32 * 12.0 - z_in; + let (x1, z1, y0, y1) = (x0 + 12.0, z0 + 12.0, bottom, bottom + 4.0); + let mut quad = |p: [[f32; 3]; 4], shade: f32| { + for v in p { + out.push(Vertex { pos: v, uv, layer: 2, color: color(shade), light }); + } + }; + quad([[x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0]], 1.0); + quad([[x1, y0, z0], [x1, y0, z1], [x0, y0, z1], [x0, y0, z0]], 0.7); + if !filled(cx, cz - 1) { + quad([[x0, y0, z0], [x0, y1, z0], [x1, y1, z0], [x1, y0, z0]], 0.8); + } + if !filled(cx, cz + 1) { + quad([[x1, y0, z1], [x1, y1, z1], [x0, y1, z1], [x0, y0, z1]], 0.8); + } + if !filled(cx - 1, cz) { + quad([[x0, y0, z1], [x0, y1, z1], [x0, y1, z0], [x0, y0, z0]], 0.9); + } + if !filled(cx + 1, cz) { + quad([[x1, y0, z0], [x1, y1, z0], [x1, y1, z1], [x1, y0, z1]], 0.9); + } + } + } + } fn save_game(&mut self) -> Result<()> { let keys: Vec<_> = self.world.modified.drain().collect(); let chunks: Vec<_> = keys @@ -2204,6 +2328,9 @@ impl Game<'_> { }; let hud = self.hud(size); let entities = self.entity_geometry(camera, alpha); + let celestial = self.celestial_geometry(&sky); + let mut entities = entities; + self.cloud_geometry(&sky, camera, alpha, &mut entities); let water_fog = { let c = info.water_fog; [(c >> 16 & 255) as f32 / 255.0, (c >> 8 & 255) as f32 / 255.0, (c & 255) as f32 / 255.0] @@ -2220,6 +2347,7 @@ impl Game<'_> { outline, hud: &hud, entities: &entities, + celestial: &celestial, game_time: self.world.game_time as f32, ticks: self.ticks, }; @@ -2601,7 +2729,7 @@ pub fn run() -> Result<()> { let audio = Audio::new(&root); let mut world = World::new(facts.air, level.seed); world.game_time = level.time; - world.day_time = level.day_time; + world.day_time = options.time.unwrap_or(level.day_time); let creative = level.creative || options.creative; let mut resources = Resources { assets, @@ -2615,6 +2743,9 @@ pub fn run() -> Result<()> { layer_names, providers: Providers::load(&root), chest_model: horizon_client::mobs::load_model(&root, "minecraft:chest#main"), + clouds: horizon_client::hud::decode(&root.join("assets/minecraft/textures/environment/clouds.png")) + .map(|(w, h, px)| (w, h, px.chunks_exact(4).map(|p| p[3] > 0).collect())) + .unwrap_or((1, 1, vec![false])), mob_types: MobTypes::load(&root)?.types.into_iter().map(|(k, v)| (k, Arc::new(v))).collect(), }; let mut inventory = vec![None; 36]; @@ -2630,6 +2761,7 @@ pub fn run() -> Result<()> { } } let now = Instant::now(); + let look = options.look; let mut game = Game { res: &mut resources, collision: &collision, @@ -2643,8 +2775,8 @@ pub fn run() -> Result<()> { previous_position: Vec3::ZERO, eye: 1.62, previous_eye: 1.62, - yaw: level.rotation[0], - pitch: level.rotation[1], + yaw: look.map_or(level.rotation[0], |l| l.0), + pitch: look.map_or(level.rotation[1], |l| l.1), inventory, selected: 0, carried: None, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 694747527..3f40ea556 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -45,6 +45,8 @@ pub struct Frame<'a> { pub hud: &'a HudBatch, /// Camera-relative entity geometry textured from the shared atlas. pub entities: &'a [Vertex], + /// Camera-relative sun, moon and stars (drawn behind everything, additive). + pub celestial: &'a [Vertex], pub game_time: f32, pub ticks: u64, } @@ -60,6 +62,8 @@ pub struct Renderer { sky: wgpu::RenderPipeline, lines: wgpu::RenderPipeline, entity_pipeline: wgpu::RenderPipeline, + celestial_pipeline: wgpu::RenderPipeline, + celestial_buffer: wgpu::Buffer, entity_group: wgpu::BindGroup, entity_buffer: wgpu::Buffer, entity_capacity: u64, @@ -571,6 +575,54 @@ impl Renderer { multiview_mask: None, cache: None, }); + let celestial_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Celestial bodies"), + layout: Some(&entity_layout), + vertex: wgpu::VertexState { + module: &entity_shader, + entry_point: Some("vs_entity"), + compilation_options: Default::default(), + buffers: &entity_vertex, + }, + primitive: wgpu::PrimitiveState { + cull_mode: None, + ..Default::default() + }, + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(false), + depth_compare: Some(wgpu::CompareFunction::Always), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &entity_shader, + entry_point: Some("fs_celestial"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format: config.format.remove_srgb_suffix(), + // RenderPipelines.CELESTIAL: additive (src alpha, one). + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::One, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent::OVER, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview_mask: None, + cache: None, + }); + let celestial_buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Celestial vertices"), + size: 2048 * 4 * std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); let entity_capacity = 1 << 14; let entity_buffer = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Entity vertices"), @@ -590,6 +642,8 @@ impl Renderer { sky, lines, entity_pipeline, + celestial_pipeline, + celestial_buffer, entity_group, entity_buffer, entity_capacity, @@ -944,6 +998,14 @@ impl Renderer { self.queue .write_buffer(&self.entity_buffer, 0, bytemuck::cast_slice(frame.entities)); } + let celestial_vertices = frame.celestial.len().min(2048 * 4); + if celestial_vertices > 0 { + self.queue.write_buffer( + &self.celestial_buffer, + 0, + bytemuck::cast_slice(&frame.celestial[..celestial_vertices]), + ); + } let entity_quads = (entity_vertices / 4) as u32; if entity_quads > self.index_quads { self.index_quads = entity_quads.next_power_of_two(); @@ -990,6 +1052,14 @@ impl Renderer { if !frame.underwater { pass.set_pipeline(&self.sky); pass.draw(0..3, 0..1); + if celestial_vertices > 0 { + pass.set_pipeline(&self.celestial_pipeline); + pass.set_bind_group(0, &self.entity_group, &[]); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(0, self.celestial_buffer.slice(..)); + pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1); + pass.set_bind_group(0, &self.group, &[]); + } } pass.set_pipeline(&self.solid); pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 187b0b365..d930472b7 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -31,7 +31,7 @@ pub struct HudAtlas { glyphs: HashMap, white: Rect, } -fn decode(path: &Path) -> Result<(u32, u32, Vec)> { +pub fn decode(path: &Path) -> Result<(u32, u32, Vec)> { let mut decoder = png::Decoder::new(std::io::BufReader::new(fs::File::open(path)?)); decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); let mut reader = decoder.read_info()?; diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index 585942a77..406e718fc 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -94,15 +94,6 @@ struct SkyOut { // Void below the horizon. color = mix(color, globals.sky_color.rgb * 0.2, smoothstep(0.0, -0.3, up) * (1.0 - globals.misc.x)); } - // Sun disc. - let sun_dot = dot(view, normalize(globals.sun.xyz)); - if sun_dot > 0.9985 { color = mix(color, vec3(1.0, 1.0, 0.85), 0.95); } - let moon_dot = dot(view, -normalize(globals.sun.xyz)); - if moon_dot > 0.9993 { color = mix(color, vec3(0.85, 0.87, 0.95), 0.9); } - // Stars. - let cell = floor(view * 300.0); - let h = fract(sin(dot(cell, vec3(12.9898, 78.233, 37.719))) * 43758.5453); - if h > 0.9985 && up > 0.0 { color += vec3(globals.sun.w * 2.0); } return vec4(color, 1.0); }