Merge pull request #1079 from ludovic111/fix/night-sky-stars-cost
🤖 Agent: merged. Stars built once (fixes #1054, Visivel and rdbtCVS). Integration proof (main + #1079 #1081 #1082 #1083 #1085 #1086 #1087), 2026-10-21: desktop parity 103/105; the 2 others (world_reopen, save_roundtrip) need a display and pass on the Mac mini. Verify passed.
This commit is contained in:
@@ -6765,8 +6765,13 @@ impl Game<'_> {
|
||||
}
|
||||
/// SkyRenderer.renderSunMoonAndStars: bodies on a frame rotated Y -90 then X by angle.
|
||||
/// A Java pack's `sunPathRotation` tilts it about Z in between, and its
|
||||
/// `sun = false` / `moon = false` leave those out, as Iris does.
|
||||
fn celestial_geometry(&self, sky: &horizon_client::environment::Sky) -> Vec<Vertex> {
|
||||
/// `sun = false` / `moon = false` leave those out, as Iris does. Returns
|
||||
/// the sun and moon quads, and the stars' frame and brightness (the
|
||||
/// renderer keeps their geometry, built once).
|
||||
fn celestial_geometry(
|
||||
&self,
|
||||
sky: &horizon_client::environment::Sky,
|
||||
) -> (Vec<Vertex>, Option<(glam::Mat4, f32)>) {
|
||||
let mut out = Vec::new();
|
||||
let atlas = &self.gui.atlas;
|
||||
let (tilt, keep) = self.java_sky().unwrap_or_default();
|
||||
@@ -6821,46 +6826,10 @@ impl Game<'_> {
|
||||
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 {
|
||||
normal: [0; 4],
|
||||
pos: p.to_array(),
|
||||
uv: [cu, cv],
|
||||
layer: 2,
|
||||
color,
|
||||
light: 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
// SkyRenderer.renderStars: the static star buffer on the same frame.
|
||||
let stars =
|
||||
(sky.star_brightness > 0.0).then(|| (frame(sky.star_angle), sky.star_brightness));
|
||||
(out, stars)
|
||||
}
|
||||
/// Load (or create) a world folder under saves/ and start streaming it.
|
||||
fn open_world(
|
||||
@@ -9351,8 +9320,8 @@ impl Game<'_> {
|
||||
}
|
||||
.to_owned()
|
||||
};
|
||||
let celestial = if title && !live {
|
||||
Vec::new()
|
||||
let (celestial, stars) = if title && !live {
|
||||
(Vec::new(), None)
|
||||
} else if self.dim == Dim::Overworld
|
||||
&& self.potions.effects.get("darkness").is_none()
|
||||
&& self.potions.effects.get("blindness").is_none()
|
||||
@@ -9360,9 +9329,9 @@ impl Game<'_> {
|
||||
// Camera.doesMobEffectBlockSky: no sun, moon or stars.
|
||||
self.celestial_geometry(&sky)
|
||||
} else if self.dim == Dim::End {
|
||||
self.end_sky_geometry()
|
||||
(self.end_sky_geometry(), None)
|
||||
} else {
|
||||
Vec::new()
|
||||
(Vec::new(), None)
|
||||
};
|
||||
let hand = if title || self.scoping() {
|
||||
Vec::new()
|
||||
@@ -9458,6 +9427,7 @@ impl Game<'_> {
|
||||
} else {
|
||||
&[]
|
||||
},
|
||||
stars: stars.filter(|_| self.settings.horizon.sky || title),
|
||||
hand: &hand,
|
||||
hand_fov_degrees: self.hud_fov_degrees(alpha as f32, camera.to_array()),
|
||||
screen: &screen,
|
||||
@@ -11895,6 +11865,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
|
||||
additive: &[],
|
||||
see_through: &[],
|
||||
celestial: &[],
|
||||
stars: None,
|
||||
hand: &[],
|
||||
hand_fov_degrees: 70.0,
|
||||
screen: &[],
|
||||
|
||||
@@ -67,6 +67,7 @@ mod occlusion;
|
||||
mod pack_stages;
|
||||
pub mod panorama;
|
||||
mod resident;
|
||||
mod stars;
|
||||
mod timing;
|
||||
pub mod translucency;
|
||||
pub(crate) mod upload;
|
||||
@@ -189,8 +190,13 @@ pub struct Frame<'a> {
|
||||
pub additive: &'a [Vertex],
|
||||
/// Name tags over everything, without depth (`TEXT_SEE_THROUGH`).
|
||||
pub see_through: &'a [Vertex],
|
||||
/// Camera-relative sun, moon and stars (drawn behind everything, additive).
|
||||
/// Camera-relative sun and moon, or the End sky box (drawn behind
|
||||
/// everything, additive).
|
||||
pub celestial: &'a [Vertex],
|
||||
/// The stars, drawn from their static buffer after the sun and moon:
|
||||
/// the sky's rotation (camera-relative world from star space) and the
|
||||
/// star brightness.
|
||||
pub stars: Option<(Mat4, f32)>,
|
||||
/// First-person hand and held item, drawn after clearing depth.
|
||||
pub hand: &'a [Vertex],
|
||||
/// `CameraRenderState.hudFov`: the hand's own field of view
|
||||
@@ -321,6 +327,7 @@ pub struct Renderer {
|
||||
see_through_pipeline: wgpu::RenderPipeline,
|
||||
celestial_pipeline: wgpu::RenderPipeline,
|
||||
celestial_buffer: wgpu::Buffer,
|
||||
stars: stars::Stars,
|
||||
panorama: panorama::Panorama,
|
||||
/// `RenderPipelines.WORLD_BORDER` and its buffers.
|
||||
border_pipeline: wgpu::RenderPipeline,
|
||||
@@ -1999,6 +2006,7 @@ impl Renderer {
|
||||
let world_post = world_post::Post::new(&device, config.format.remove_srgb_suffix());
|
||||
let uploads = upload::Uploads::new(&device);
|
||||
let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK);
|
||||
let stars = stars::Stars::new(&device, config.format.remove_srgb_suffix());
|
||||
let panorama = panorama::Panorama::new(
|
||||
&device,
|
||||
&queue,
|
||||
@@ -2060,6 +2068,7 @@ impl Renderer {
|
||||
see_through_pipeline,
|
||||
celestial_pipeline,
|
||||
celestial_buffer,
|
||||
stars,
|
||||
panorama,
|
||||
border_pipeline,
|
||||
border_group,
|
||||
@@ -3236,6 +3245,14 @@ impl Renderer {
|
||||
bytemuck::cast_slice(all_entities),
|
||||
);
|
||||
}
|
||||
if let Some((rotation, brightness)) = frame.stars {
|
||||
self.stars.prepare(
|
||||
&mut self.uploads,
|
||||
&mut encoder,
|
||||
view_proj * rotation,
|
||||
brightness,
|
||||
);
|
||||
}
|
||||
let celestial_vertices = frame.celestial.len().min(2048 * 4);
|
||||
if celestial_vertices > 0 {
|
||||
self.uploads.buffer(
|
||||
@@ -3445,6 +3462,10 @@ impl Renderer {
|
||||
pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
}
|
||||
if !frame.title && frame.stars.is_some() {
|
||||
self.stars.draw(&mut pass);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
}
|
||||
if !frame.title && frame.dark_disc {
|
||||
pass.set_pipeline(&self.dark_disc);
|
||||
pass.draw(0..3, 0..1);
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
//! The night sky's stars, built once into a static vertex buffer as
|
||||
//! `SkyRenderer.buildStars` does, and drawn each frame with one small uniform
|
||||
//! (the sky's rotation and the star brightness) instead of being regenerated
|
||||
//! and uploaded every frame.
|
||||
use glam::{Mat4, Vec3};
|
||||
use horizon_core::random::RandomSource;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
/// `SkyRenderer.buildStars` with its fixed seed: up to 1500 quads on a
|
||||
/// sphere of radius 100, each turned to face the centre and spun about its
|
||||
/// own axis, as triangles (two per quad).
|
||||
pub fn star_triangles() -> Vec<[f32; 3]> {
|
||||
let mut random = horizon_core::random::LegacyRandom::new(10842);
|
||||
let mut out = Vec::with_capacity(1500 * 6);
|
||||
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 = Vec3::new(x, y, z).normalize() * 100.0;
|
||||
let z_rot = (random.next_double() * std::f64::consts::TAU) as f32;
|
||||
// Matrix3f.rotateTowards(-center, up).rotateZ(-zRot).
|
||||
let forward = -center.normalize();
|
||||
let right = Vec3::Y.cross(forward).normalize_or(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);
|
||||
let corner = |a: f32, b: f32| (center + r2 * a + u2 * b).to_array();
|
||||
let quad = [
|
||||
corner(size, -size),
|
||||
corner(size, size),
|
||||
corner(-size, size),
|
||||
corner(-size, -size),
|
||||
];
|
||||
out.extend([quad[0], quad[1], quad[2], quad[0], quad[2], quad[3]]);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Uniform {
|
||||
clip_from_sky: [[f32; 4]; 4],
|
||||
color: [f32; 4],
|
||||
}
|
||||
|
||||
pub(super) struct Stars {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
group: wgpu::BindGroup,
|
||||
uniform: wgpu::Buffer,
|
||||
vertices: wgpu::Buffer,
|
||||
count: u32,
|
||||
}
|
||||
|
||||
impl Stars {
|
||||
pub(super) fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
|
||||
let triangles = star_triangles();
|
||||
let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Stars vertex buffer"),
|
||||
contents: bytemuck::cast_slice(&triangles),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
let uniform = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Stars rotation and brightness"),
|
||||
size: std::mem::size_of::<Uniform>() as u64,
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("Stars bindings"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
}],
|
||||
});
|
||||
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("Stars resources"),
|
||||
layout: &layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: uniform.as_entire_binding(),
|
||||
}],
|
||||
});
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Stars shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("stars.wgsl").into()),
|
||||
});
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Stars layout"),
|
||||
bind_group_layouts: &[Some(&layout)],
|
||||
..Default::default()
|
||||
});
|
||||
let attributes = wgpu::vertex_attr_array![0 => Float32x3];
|
||||
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Stars"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: Some("vs_stars"),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[Some(wgpu::VertexBufferLayout {
|
||||
array_stride: 12,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &attributes,
|
||||
})],
|
||||
},
|
||||
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: &shader,
|
||||
entry_point: Some("fs_stars"),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format,
|
||||
// RenderPipelines.STARS: the celestial blend (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,
|
||||
});
|
||||
Self {
|
||||
pipeline,
|
||||
group,
|
||||
uniform,
|
||||
vertices,
|
||||
count: triangles.len() as u32,
|
||||
}
|
||||
}
|
||||
|
||||
/// This frame's sky rotation (camera-relative world from star space,
|
||||
/// times the view projection) and star brightness.
|
||||
pub(super) fn prepare(
|
||||
&self,
|
||||
uploads: &mut super::upload::Uploads,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
clip_from_sky: Mat4,
|
||||
brightness: f32,
|
||||
) {
|
||||
let b = brightness.clamp(0.0, 1.0);
|
||||
let uniform = Uniform {
|
||||
clip_from_sky: clip_from_sky.to_cols_array_2d(),
|
||||
color: [b; 4],
|
||||
};
|
||||
uploads.buffer(encoder, &self.uniform, 0, bytemuck::bytes_of(&uniform));
|
||||
}
|
||||
|
||||
pub(super) fn draw(&self, pass: &mut wgpu::RenderPass<'_>) {
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
pass.set_vertex_buffer(0, self.vertices.slice(..));
|
||||
pass.draw(0..self.count, 0..1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn stars_lie_on_the_sky_sphere_as_whole_quads() {
|
||||
let triangles = star_triangles();
|
||||
// About 52% of the cube's random points fall inside the unit ball.
|
||||
assert!((700 * 6..1500 * 6).contains(&triangles.len()));
|
||||
assert_eq!(triangles.len() % 6, 0);
|
||||
for p in &triangles {
|
||||
let r = Vec3::from_array(*p).length();
|
||||
assert!((99.9..100.1).contains(&r), "{r}");
|
||||
}
|
||||
assert_eq!(triangles, star_triangles());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// RenderPipelines.STARS: positions only, coloured by the star brightness.
|
||||
struct Stars {
|
||||
// Projection * view * the sky's rotation (SkyRenderer.renderSunMoonAndStars).
|
||||
clip_from_sky: mat4x4<f32>,
|
||||
// starBrightness in every channel (RenderSystem.setShaderColor).
|
||||
color: vec4<f32>,
|
||||
}
|
||||
@group(0) @binding(0) var<uniform> stars: Stars;
|
||||
|
||||
@vertex fn vs_stars(@location(0) position: vec3<f32>) -> @builtin(position) vec4<f32> {
|
||||
return stars.clip_from_sky * vec4(position, 1.0);
|
||||
}
|
||||
|
||||
@fragment fn fs_stars() -> @location(0) vec4<f32> {
|
||||
return stars.color;
|
||||
}
|
||||
Reference in New Issue
Block a user