Merge #47 (selection outline width and depth) into review-cosmic

Resolve the gfx.rs conflict with #42 and keep the shared Globals uniform
consistent: Rust, terrain.wgsl and entity.wgsl now all end with
time, crumbling_sprite, outline_params. The automatic merge left
terrain.wgsl reading outline_params at crumbling_sprite's offset. A test
compares the three declarations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-13 22:29:31 +02:00
3 changed files with 119 additions and 34 deletions
+2
View File
@@ -16,6 +16,8 @@ struct Globals {
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000)
time: vec4<f32>,
crumbling_sprite: vec4<f32>,
// Selection outline parameters (read by terrain.wgsl).
outline_params: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var atlas: texture_2d<f32>;
+84 -30
View File
@@ -50,6 +50,29 @@ fn batch_entities(
(combined, counts)
}
/// Window.getAppropriateLineWidth, in framebuffer pixels.
pub fn outline_width(width: u32) -> f32 {
2.5f32.max(width as f32 / 1920.0 * 2.5)
}
/// ScreenSize, stroke width and ProjectionType.PERSPECTIVE layering.
pub fn outline_params(width: u32, height: u32) -> [f32; 4] {
[
width as f32,
height.max(1) as f32,
outline_width(width),
1.0 - 1.0 / 4096.0,
]
}
/// Camera-relative segment endpoints; no shape expansion in vanilla 26.3.
pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6]> {
edges
.iter()
.map(|e| std::array::from_fn(|i| (e[i] - camera.to_array()[i % 3]) as f32))
.collect()
}
mod arena;
mod occlusion;
mod timing;
@@ -72,7 +95,10 @@ struct Globals {
end_portal_sprites: [[f32; 4]; 2],
/// x: vanilla's `GameTime` uniform; yzw unused.
time: [f32; 4],
/// Atlas rect of the current destroy stage (entity.wgsl).
crumbling_sprite: [f32; 4],
/// Framebuffer size, selection stroke width, perspective layering scale.
outline_params: [f32; 4],
}
pub type SectionKey = (i32, i32, i32);
struct SectionGpu {
@@ -495,10 +521,10 @@ impl Renderer {
attributes: &instance_attributes,
}),
];
let line_attributes = wgpu::vertex_attr_array![0=>Float32x3];
let line_attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x3];
let line_buffers = [Some(wgpu::VertexBufferLayout {
array_stride: 12,
step_mode: wgpu::VertexStepMode::Vertex,
array_stride: 24,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &line_attributes,
})];
let alpha_blend = wgpu::BlendState::ALPHA_BLENDING;
@@ -601,7 +627,7 @@ impl Renderer {
"vs_line",
"fs_line",
&line_buffers,
wgpu::PrimitiveTopology::LineList,
wgpu::PrimitiveTopology::TriangleStrip,
None,
false,
true,
@@ -1821,6 +1847,7 @@ impl Renderer {
end_portal_sprites: frame.end_portal_sprites,
crumbling_sprite: frame.crumbling_sprite,
time: [frame.shader_game_time, 0.0, 0.0, 0.0],
outline_params: outline_params(self.config.width, self.config.height),
};
self.queue
.write_buffer(&self.globals, 0, bytemuck::bytes_of(&globals));
@@ -1945,31 +1972,7 @@ impl Renderer {
}
let outline_edges = frame.outline.len().min(MAX_OUTLINE_EDGES as usize);
if outline_edges > 0 {
// A hair outside the block, like vanilla's line depth offset.
let lines: Vec<[f32; 3]> = frame.outline[..outline_edges]
.iter()
.flat_map(|e| {
let center = [
(e[0] + e[3]) / 2.0,
(e[1] + e[4]) / 2.0,
(e[2] + e[5]) / 2.0,
];
let point = |i: usize| -> [f32; 3] {
std::array::from_fn(|a| {
let v = e[i + a];
let grow = if v < center[a] {
-0.002
} else if v > center[a] {
0.002
} else {
0.0
};
(v - [camera.x, camera.y, camera.z][a]) as f32 + grow
})
};
[point(0), point(3)]
})
.collect();
let lines = outline_vertices(&frame.outline[..outline_edges], camera);
self.queue
.write_buffer(&self.outline, 0, bytemuck::cast_slice(&lines));
}
@@ -2140,7 +2143,7 @@ impl Renderer {
if outline_edges > 0 {
pass.set_pipeline(&self.lines);
pass.set_vertex_buffer(0, self.outline.slice(..));
pass.draw(0..outline_edges as u32 * 2, 0..1);
pass.draw(0..4, 0..outline_edges as u32);
}
pass.set_pipeline(&self.translucent);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
@@ -2474,4 +2477,55 @@ mod tests {
assert_eq!(blend.alpha.src_factor, wgpu::BlendFactor::One);
assert_eq!(blend.alpha.dst_factor, wgpu::BlendFactor::Zero);
}
#[test]
fn shader_globals_match_the_rust_layout() {
// terrain.wgsl and entity.wgsl bind the same buffer: same fields, same order.
let fields = |source: &str| -> Vec<String> {
let body = source.split("struct Globals {").nth(1).unwrap();
body[..body.find('}').unwrap()]
.lines()
.filter_map(|l| l.trim().split_once(':').map(|(name, _)| name.to_string()))
.filter(|name| !name.starts_with("//"))
.collect()
};
let terrain = fields(include_str!("terrain.wgsl"));
assert_eq!(terrain, fields(include_str!("entity.wgsl")));
let rust = fields(include_str!("gfx.rs"));
assert_eq!(terrain, rust);
}
#[test]
fn selection_outline_uses_vanilla_pixel_width() {
for width in [640, 1280, 1920] {
assert_eq!(super::outline_width(width), 2.5);
}
assert_eq!(super::outline_width(2560), 2560.0 / 1920.0 * 2.5);
assert_eq!(super::outline_width(3840), 5.0);
}
#[test]
fn selection_outline_has_vanilla_perspective_layering() {
assert_eq!(
super::outline_params(1280, 720),
[1280.0, 720.0, 2.5, 0.99975586]
);
assert_eq!(
super::outline_params(3840, 0),
[3840.0, 1.0, 5.0, 0.99975586]
);
}
#[test]
fn selection_outline_preserves_shape_edges_without_inflation() {
let edges = [[2.0, 97.5, 3.0, 3.0, 97.5, 3.0]];
assert_eq!(
super::outline_vertices(&edges, glam::DVec3::new(0.5, 98.62, 0.5)),
vec![[
1.5,
(97.5 - 98.62) as f32,
2.5,
2.5,
(97.5 - 98.62) as f32,
2.5
]]
);
assert!(super::outline_vertices(&[], glam::DVec3::ZERO).is_empty());
}
}
+33 -4
View File
@@ -18,6 +18,9 @@ struct Globals {
end_portal_sprites: array<vec4<f32>, 2>,
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000)
time: vec4<f32>,
// Atlas rect of the current destroy stage (read by entity.wgsl).
crumbling_sprite: vec4<f32>,
outline_params: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var sprites: texture_2d_array<f32>;
@@ -173,10 +176,36 @@ fn sunrise_weight(d: vec3<f32>) -> f32 {
return vec4(sky_fog(vec3(0.0), dist), 1.0);
}
struct LineOut { @builtin(position) clip: vec4<f32> }
@vertex fn vs_line(@location(0) position: vec3<f32>) -> LineOut {
// rendertype_lines.vsh: screen-space stroke and camera-space depth separation.
fn outline_clip(vertex: u32, start: vec3<f32>, end: vec3<f32>) -> vec4<f32> {
let position = select(start, end, vertex >= 2u);
let normal = normalize(end - start);
let view_shrink = 1.0 - 1.0 / 256.0;
let line_start = globals.view_proj * vec4((position * globals.outline_params.w) * view_shrink, 1.0);
let line_end = globals.view_proj * vec4(((position + normal) * globals.outline_params.w) * view_shrink, 1.0);
let ndc1 = line_start.xyz / line_start.w;
let ndc2 = line_end.xyz / line_end.w;
let screen = globals.outline_params.xy;
let direction = normalize((ndc2.xy - ndc1.xy) * screen);
var offset = vec2(-direction.y, direction.x) * globals.outline_params.z / screen;
if offset.x < 0.0 { offset = -offset; }
if vertex % 2u != 0u { offset = -offset; }
return vec4((ndc1 + vec3(offset, 0.0)) * line_start.w, line_start.w);
}
struct LineOut {
@builtin(position) clip: vec4<f32>,
@location(0) spherical: f32,
@location(1) cylindrical: f32,
}
@vertex fn vs_line(@builtin(vertex_index) vertex: u32,
@location(0) start: vec3<f32>, @location(1) end: vec3<f32>) -> LineOut {
let position = select(start, end, vertex >= 2u);
var out: LineOut;
out.clip = globals.view_proj * vec4(position, 1.0);
out.clip = outline_clip(vertex, start, end);
out.spherical = length(position);
out.cylindrical = max(length(position.xz), abs(position.y));
return out;
}
@fragment fn fs_line() -> @location(0) vec4<f32> { return vec4(0.0, 0.0, 0.0, 0.4); }
@fragment fn fs_line(in: LineOut) -> @location(0) vec4<f32> {
return apply_fog(vec4(0.0, 0.0, 0.0, 0.4), in.spherical, in.cylindrical);
}