786 lines
24 KiB
Rust
786 lines
24 KiB
Rust
use bytemuck::{Pod, Zeroable};
|
|
use glam::Vec3;
|
|
|
|
/// Interleaved vertex: position + normal + color
|
|
#[repr(C)]
|
|
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
|
|
pub struct Vertex {
|
|
pub position: [f32; 3],
|
|
pub normal: [f32; 3],
|
|
pub color: [f32; 3],
|
|
}
|
|
|
|
impl Vertex {
|
|
pub fn layout() -> wgpu::VertexBufferLayout<'static> {
|
|
wgpu::VertexBufferLayout {
|
|
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
|
step_mode: wgpu::VertexStepMode::Vertex,
|
|
attributes: &[
|
|
// position
|
|
wgpu::VertexAttribute {
|
|
offset: 0,
|
|
shader_location: 0,
|
|
format: wgpu::VertexFormat::Float32x3,
|
|
},
|
|
// normal
|
|
wgpu::VertexAttribute {
|
|
offset: 12,
|
|
shader_location: 1,
|
|
format: wgpu::VertexFormat::Float32x3,
|
|
},
|
|
// color
|
|
wgpu::VertexAttribute {
|
|
offset: 24,
|
|
shader_location: 2,
|
|
format: wgpu::VertexFormat::Float32x3,
|
|
},
|
|
],
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Generate flat-shaded triangle list (no index buffer — each triangle gets its own
|
|
/// vertices so they have per-face normals).
|
|
pub struct Mesh {
|
|
pub vertices: Vec<Vertex>,
|
|
}
|
|
|
|
/// Push a box defined by min/max corners with a given color and correct normals.
|
|
fn push_box(verts: &mut Vec<Vertex>, min: Vec3, max: Vec3, color: [f32; 3]) {
|
|
let corners = [
|
|
Vec3::new(min.x, min.y, min.z), // 0: left-bottom-back
|
|
Vec3::new(max.x, min.y, min.z), // 1: right-bottom-back
|
|
Vec3::new(max.x, max.y, min.z), // 2: right-top-back
|
|
Vec3::new(min.x, max.y, min.z), // 3: left-top-back
|
|
Vec3::new(min.x, min.y, max.z), // 4: left-bottom-front
|
|
Vec3::new(max.x, min.y, max.z), // 5: right-bottom-front
|
|
Vec3::new(max.x, max.y, max.z), // 6: right-top-front
|
|
Vec3::new(min.x, max.y, max.z), // 7: left-top-front
|
|
];
|
|
|
|
// 6 faces, 2 triangles each, CCW winding from outside
|
|
let faces: &[([usize; 3], Vec3)] = &[
|
|
// front (+Z)
|
|
([4, 5, 6], Vec3::Z),
|
|
([4, 6, 7], Vec3::Z),
|
|
// back (-Z)
|
|
([1, 0, 3], Vec3::NEG_Z),
|
|
([1, 3, 2], Vec3::NEG_Z),
|
|
// right (+X)
|
|
([5, 1, 2], Vec3::X),
|
|
([5, 2, 6], Vec3::X),
|
|
// left (-X)
|
|
([0, 4, 7], Vec3::NEG_X),
|
|
([0, 7, 3], Vec3::NEG_X),
|
|
// top (+Y)
|
|
([3, 7, 6], Vec3::Y),
|
|
([3, 6, 2], Vec3::Y),
|
|
// bottom (-Y)
|
|
([0, 1, 5], Vec3::NEG_Y),
|
|
([0, 5, 4], Vec3::NEG_Y),
|
|
];
|
|
|
|
for (idx, normal) in faces {
|
|
for &i in idx {
|
|
verts.push(Vertex {
|
|
position: corners[i].into(),
|
|
normal: (*normal).into(),
|
|
color,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Build a mesh from line segments as thin flat quads in the XY plane.
|
|
fn line_segments_mesh(
|
|
segments: &[([f32; 2], [f32; 2])],
|
|
thickness: f32,
|
|
z: f32,
|
|
color: [f32; 3],
|
|
) -> Mesh {
|
|
let mut vertices = Vec::new();
|
|
let normal = [0.0, 0.0, 1.0];
|
|
let ht = thickness / 2.0;
|
|
|
|
for &([x0, y0], [x1, y1]) in segments {
|
|
let dx = x1 - x0;
|
|
let dy = y1 - y0;
|
|
let len = (dx * dx + dy * dy).sqrt();
|
|
if len < 1e-6 {
|
|
continue;
|
|
}
|
|
// Perpendicular (90 deg CCW from direction)
|
|
let px = -dy / len * ht;
|
|
let py = dx / len * ht;
|
|
|
|
// v0=start+perp, v1=start-perp, v2=end-perp, v3=end+perp
|
|
let v0 = [x0 + px, y0 + py, z];
|
|
let v1 = [x0 - px, y0 - py, z];
|
|
let v2 = [x1 - px, y1 - py, z];
|
|
let v3 = [x1 + px, y1 + py, z];
|
|
|
|
vertices.push(Vertex {
|
|
position: v0,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: v1,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: v2,
|
|
normal,
|
|
color,
|
|
});
|
|
|
|
vertices.push(Vertex {
|
|
position: v0,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: v2,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: v3,
|
|
normal,
|
|
color,
|
|
});
|
|
}
|
|
|
|
Mesh { vertices }
|
|
}
|
|
|
|
impl Mesh {
|
|
/// Create an octahedron with the given color.
|
|
pub fn octahedron(color: [f32; 3]) -> Self {
|
|
let top = Vec3::Y;
|
|
let bottom = -Vec3::Y;
|
|
let front = Vec3::Z;
|
|
let back = -Vec3::Z;
|
|
let left = -Vec3::X;
|
|
let right = Vec3::X;
|
|
|
|
let faces: &[[Vec3; 3]] = &[
|
|
// Upper 4
|
|
[top, front, right],
|
|
[top, right, back],
|
|
[top, back, left],
|
|
[top, left, front],
|
|
// Lower 4
|
|
[bottom, right, front],
|
|
[bottom, back, right],
|
|
[bottom, left, back],
|
|
[bottom, front, left],
|
|
];
|
|
|
|
let vertices = faces
|
|
.iter()
|
|
.flat_map(|[a, b, c]| {
|
|
let normal = (*b - *a).cross(*c - *a).normalize();
|
|
[
|
|
Vertex {
|
|
position: (*a).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*b).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*c).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
]
|
|
})
|
|
.collect();
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Create a 3D crosshair at the origin with tick marks on all three axes.
|
|
/// Axes span from -`extent` to +`extent` with ticks at each integer.
|
|
/// Each axis is colored: X=red, Y=green, Z=blue.
|
|
/// Positive direction is fully saturated, negative is desaturated.
|
|
pub fn crosshair(extent: f32) -> Self {
|
|
let mut vertices = Vec::new();
|
|
|
|
let axis_hw = 0.015; // axis half-width
|
|
let tick_len = 0.15; // tick half-length
|
|
let tick_hw = 0.012; // tick half-width
|
|
|
|
// Desaturate a color by mixing toward gray
|
|
let desat = |c: [f32; 3]| -> [f32; 3] {
|
|
let t = 0.35; // how much of the original color to keep
|
|
let gray = 0.4;
|
|
[
|
|
c[0] * t + gray * (1.0 - t),
|
|
c[1] * t + gray * (1.0 - t),
|
|
c[2] * t + gray * (1.0 - t),
|
|
]
|
|
};
|
|
|
|
// Saturated axis colors
|
|
let x_pos: [f32; 3] = [1.0, 0.15, 0.15]; // red
|
|
let y_pos: [f32; 3] = [0.15, 1.0, 0.15]; // green
|
|
let z_pos: [f32; 3] = [0.3, 0.4, 1.0]; // blue
|
|
let x_neg = desat(x_pos);
|
|
let y_neg = desat(y_pos);
|
|
let z_neg = desat(z_pos);
|
|
|
|
// Axis lines — split at origin into positive and negative halves
|
|
// X axis
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(0.0, -axis_hw, -axis_hw),
|
|
Vec3::new(extent, axis_hw, axis_hw),
|
|
x_pos,
|
|
);
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-extent, -axis_hw, -axis_hw),
|
|
Vec3::new(0.0, axis_hw, axis_hw),
|
|
x_neg,
|
|
);
|
|
// Y axis
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-axis_hw, 0.0, -axis_hw),
|
|
Vec3::new(axis_hw, extent, axis_hw),
|
|
y_pos,
|
|
);
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-axis_hw, -extent, -axis_hw),
|
|
Vec3::new(axis_hw, 0.0, axis_hw),
|
|
y_neg,
|
|
);
|
|
// Z axis
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-axis_hw, -axis_hw, 0.0),
|
|
Vec3::new(axis_hw, axis_hw, extent),
|
|
z_pos,
|
|
);
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-axis_hw, -axis_hw, -extent),
|
|
Vec3::new(axis_hw, axis_hw, 0.0),
|
|
z_neg,
|
|
);
|
|
|
|
// Tick marks at each integer position
|
|
let min_tick = (-extent) as i32;
|
|
let max_tick = extent as i32;
|
|
for i in min_tick..=max_tick {
|
|
let p = i as f32;
|
|
if i == 0 {
|
|
continue;
|
|
}
|
|
|
|
let (xc, yc, zc) = if i > 0 {
|
|
(x_pos, y_pos, z_pos)
|
|
} else {
|
|
(x_neg, y_neg, z_neg)
|
|
};
|
|
|
|
// Ticks on X axis: small bars in Y direction
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(p - tick_hw, -tick_len, -tick_hw),
|
|
Vec3::new(p + tick_hw, tick_len, tick_hw),
|
|
xc,
|
|
);
|
|
// Ticks on Y axis: small bars in X direction
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-tick_len, p - tick_hw, -tick_hw),
|
|
Vec3::new(tick_len, p + tick_hw, tick_hw),
|
|
yc,
|
|
);
|
|
// Ticks on Z axis: small bars in Y direction
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-tick_hw, -tick_len, p - tick_hw),
|
|
Vec3::new(tick_hw, tick_len, p + tick_hw),
|
|
zc,
|
|
);
|
|
}
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Create an icosphere with a conical spike protruding from it,
|
|
/// making rotation clearly visible.
|
|
pub fn icosphere_with_spike(subdivisions: u32, color: [f32; 3], spike_color: [f32; 3]) -> Self {
|
|
let mut mesh = Self::icosphere(subdivisions, color);
|
|
|
|
// Add a cone spike pointing in +Z direction
|
|
let spike_base_radius = 0.3;
|
|
let spike_base_z = 0.9;
|
|
let spike_tip = Vec3::new(0.0, 0.0, 1.8);
|
|
let segments = 12;
|
|
|
|
// Generate base ring points
|
|
let base_points: Vec<Vec3> = (0..segments)
|
|
.map(|i| {
|
|
let angle = (i as f32 / segments as f32) * std::f32::consts::TAU;
|
|
Vec3::new(
|
|
angle.cos() * spike_base_radius,
|
|
angle.sin() * spike_base_radius,
|
|
spike_base_z,
|
|
)
|
|
})
|
|
.collect();
|
|
|
|
// Side triangles of the cone
|
|
for i in 0..segments {
|
|
let a = base_points[i];
|
|
let b = base_points[(i + 1) % segments];
|
|
let normal = (b - a).cross(spike_tip - a).normalize();
|
|
mesh.vertices.push(Vertex {
|
|
position: a.into(),
|
|
normal: normal.into(),
|
|
color: spike_color,
|
|
});
|
|
mesh.vertices.push(Vertex {
|
|
position: b.into(),
|
|
normal: normal.into(),
|
|
color: spike_color,
|
|
});
|
|
mesh.vertices.push(Vertex {
|
|
position: spike_tip.into(),
|
|
normal: normal.into(),
|
|
color: spike_color,
|
|
});
|
|
}
|
|
|
|
// Base cap (so the spike isn't open at the bottom)
|
|
let base_center = Vec3::new(0.0, 0.0, spike_base_z);
|
|
let base_normal = Vec3::NEG_Z;
|
|
for i in 0..segments {
|
|
let a = base_points[(i + 1) % segments];
|
|
let b = base_points[i];
|
|
mesh.vertices.push(Vertex {
|
|
position: base_center.into(),
|
|
normal: base_normal.into(),
|
|
color: spike_color,
|
|
});
|
|
mesh.vertices.push(Vertex {
|
|
position: a.into(),
|
|
normal: base_normal.into(),
|
|
color: spike_color,
|
|
});
|
|
mesh.vertices.push(Vertex {
|
|
position: b.into(),
|
|
normal: base_normal.into(),
|
|
color: spike_color,
|
|
});
|
|
}
|
|
|
|
mesh
|
|
}
|
|
|
|
/// Create an icosphere by subdividing an icosahedron, then projecting
|
|
/// vertices onto the unit sphere. `subdivisions` of 2-3 looks nice.
|
|
pub fn icosphere(subdivisions: u32, color: [f32; 3]) -> Self {
|
|
// Golden ratio
|
|
let phi = (1.0 + 5.0_f32.sqrt()) / 2.0;
|
|
|
|
// 12 vertices of an icosahedron (normalized to unit sphere)
|
|
let raw_verts: Vec<Vec3> = vec![
|
|
Vec3::new(-1.0, phi, 0.0),
|
|
Vec3::new(1.0, phi, 0.0),
|
|
Vec3::new(-1.0, -phi, 0.0),
|
|
Vec3::new(1.0, -phi, 0.0),
|
|
Vec3::new(0.0, -1.0, phi),
|
|
Vec3::new(0.0, 1.0, phi),
|
|
Vec3::new(0.0, -1.0, -phi),
|
|
Vec3::new(0.0, 1.0, -phi),
|
|
Vec3::new(phi, 0.0, -1.0),
|
|
Vec3::new(phi, 0.0, 1.0),
|
|
Vec3::new(-phi, 0.0, -1.0),
|
|
Vec3::new(-phi, 0.0, 1.0),
|
|
]
|
|
.into_iter()
|
|
.map(|v| v.normalize())
|
|
.collect();
|
|
|
|
// 20 faces of the icosahedron
|
|
let mut triangles: Vec<[Vec3; 3]> = vec![
|
|
[raw_verts[0], raw_verts[11], raw_verts[5]],
|
|
[raw_verts[0], raw_verts[5], raw_verts[1]],
|
|
[raw_verts[0], raw_verts[1], raw_verts[7]],
|
|
[raw_verts[0], raw_verts[7], raw_verts[10]],
|
|
[raw_verts[0], raw_verts[10], raw_verts[11]],
|
|
[raw_verts[1], raw_verts[5], raw_verts[9]],
|
|
[raw_verts[5], raw_verts[11], raw_verts[4]],
|
|
[raw_verts[11], raw_verts[10], raw_verts[2]],
|
|
[raw_verts[10], raw_verts[7], raw_verts[6]],
|
|
[raw_verts[7], raw_verts[1], raw_verts[8]],
|
|
[raw_verts[3], raw_verts[9], raw_verts[4]],
|
|
[raw_verts[3], raw_verts[4], raw_verts[2]],
|
|
[raw_verts[3], raw_verts[2], raw_verts[6]],
|
|
[raw_verts[3], raw_verts[6], raw_verts[8]],
|
|
[raw_verts[3], raw_verts[8], raw_verts[9]],
|
|
[raw_verts[4], raw_verts[9], raw_verts[5]],
|
|
[raw_verts[2], raw_verts[4], raw_verts[11]],
|
|
[raw_verts[6], raw_verts[2], raw_verts[10]],
|
|
[raw_verts[8], raw_verts[6], raw_verts[7]],
|
|
[raw_verts[9], raw_verts[8], raw_verts[1]],
|
|
];
|
|
|
|
// Subdivide
|
|
for _ in 0..subdivisions {
|
|
let mut new_tris = Vec::with_capacity(triangles.len() * 4);
|
|
for [a, b, c] in &triangles {
|
|
let ab = ((*a + *b) / 2.0).normalize();
|
|
let bc = ((*b + *c) / 2.0).normalize();
|
|
let ca = ((*c + *a) / 2.0).normalize();
|
|
new_tris.push([*a, ab, ca]);
|
|
new_tris.push([*b, bc, ab]);
|
|
new_tris.push([*c, ca, bc]);
|
|
new_tris.push([ab, bc, ca]);
|
|
}
|
|
triangles = new_tris;
|
|
}
|
|
|
|
// Flat-shade: each face gets its own normal
|
|
let vertices = triangles
|
|
.iter()
|
|
.flat_map(|[a, b, c]| {
|
|
let normal = (*b - *a).cross(*c - *a).normalize();
|
|
[
|
|
Vertex {
|
|
position: (*a).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*b).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*c).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
]
|
|
})
|
|
.collect();
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Filled circle in XY plane (triangle fan from center), normals pointing +Z.
|
|
pub fn disc(segments: u32, radius: f32, color: [f32; 3]) -> Self {
|
|
let mut vertices = Vec::new();
|
|
let normal = [0.0, 0.0, 1.0];
|
|
let center = [0.0, 0.0, 0.0];
|
|
|
|
for i in 0..segments {
|
|
let a0 = (i as f32 / segments as f32) * std::f32::consts::TAU;
|
|
let a1 = ((i + 1) as f32 / segments as f32) * std::f32::consts::TAU;
|
|
vertices.push(Vertex {
|
|
position: center,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [a0.cos() * radius, a0.sin() * radius, 0.0],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [a1.cos() * radius, a1.sin() * radius, 0.0],
|
|
normal,
|
|
color,
|
|
});
|
|
}
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Thin annulus in XY plane at given z height.
|
|
pub fn ring(
|
|
segments: u32,
|
|
inner_radius: f32,
|
|
outer_radius: f32,
|
|
z: f32,
|
|
color: [f32; 3],
|
|
) -> Self {
|
|
let mut vertices = Vec::new();
|
|
let normal = [0.0, 0.0, 1.0];
|
|
|
|
for i in 0..segments {
|
|
let a0 = (i as f32 / segments as f32) * std::f32::consts::TAU;
|
|
let a1 = ((i + 1) as f32 / segments as f32) * std::f32::consts::TAU;
|
|
|
|
let in0 = [inner_radius * a0.cos(), inner_radius * a0.sin(), z];
|
|
let in1 = [inner_radius * a1.cos(), inner_radius * a1.sin(), z];
|
|
let out0 = [outer_radius * a0.cos(), outer_radius * a0.sin(), z];
|
|
let out1 = [outer_radius * a1.cos(), outer_radius * a1.sin(), z];
|
|
|
|
vertices.push(Vertex {
|
|
position: in0,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: out0,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: out1,
|
|
normal,
|
|
color,
|
|
});
|
|
|
|
vertices.push(Vertex {
|
|
position: in0,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: out1,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: in1,
|
|
normal,
|
|
color,
|
|
});
|
|
}
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Concentric rings + two perpendicular cross lines for scanner grid overlay.
|
|
pub fn scanner_grid(num_rings: u32, radius: f32, line_width: f32, color: [f32; 3]) -> Self {
|
|
let mut vertices = Vec::new();
|
|
let normal = [0.0, 0.0, 1.0];
|
|
let z = 0.001;
|
|
let hw = line_width / 2.0;
|
|
let segments = 64u32;
|
|
|
|
// Concentric rings
|
|
for ring in 1..=num_rings {
|
|
let r = radius * (ring as f32 / (num_rings + 1) as f32);
|
|
let inner = r - hw;
|
|
let outer = r + hw;
|
|
|
|
for i in 0..segments {
|
|
let a0 = (i as f32 / segments as f32) * std::f32::consts::TAU;
|
|
let a1 = ((i + 1) as f32 / segments as f32) * std::f32::consts::TAU;
|
|
|
|
let p0_in = [inner * a0.cos(), inner * a0.sin(), z];
|
|
let p1_in = [inner * a1.cos(), inner * a1.sin(), z];
|
|
let p0_out = [outer * a0.cos(), outer * a0.sin(), z];
|
|
let p1_out = [outer * a1.cos(), outer * a1.sin(), z];
|
|
|
|
vertices.push(Vertex {
|
|
position: p0_in,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: p0_out,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: p1_out,
|
|
normal,
|
|
color,
|
|
});
|
|
|
|
vertices.push(Vertex {
|
|
position: p0_in,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: p1_out,
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: p1_in,
|
|
normal,
|
|
color,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Cross lines (flat quads through center)
|
|
// X-axis line
|
|
vertices.push(Vertex {
|
|
position: [-radius, -hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [radius, -hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [radius, hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [-radius, -hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [radius, hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [-radius, hw, z],
|
|
normal,
|
|
color,
|
|
});
|
|
|
|
// Y-axis line
|
|
vertices.push(Vertex {
|
|
position: [-hw, -radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [hw, -radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [hw, radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [-hw, -radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [hw, radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
vertices.push(Vertex {
|
|
position: [-hw, radius, z],
|
|
normal,
|
|
color,
|
|
});
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Vertical box from z=0 to z=height, centered on XY at origin.
|
|
pub fn thin_box(half_width: f32, height: f32, color: [f32; 3]) -> Self {
|
|
let mut vertices = Vec::new();
|
|
push_box(
|
|
&mut vertices,
|
|
Vec3::new(-half_width, -half_width, 0.0),
|
|
Vec3::new(half_width, half_width, height),
|
|
color,
|
|
);
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Small elongated octahedron for laser bullets, stretched along Z.
|
|
pub fn bullet() -> Self {
|
|
let color = [1.0, 0.2, 0.1];
|
|
let half_w = 0.06;
|
|
let half_len = 0.3;
|
|
|
|
let top = Vec3::new(0.0, half_w, 0.0);
|
|
let bottom = Vec3::new(0.0, -half_w, 0.0);
|
|
let left = Vec3::new(-half_w, 0.0, 0.0);
|
|
let right = Vec3::new(half_w, 0.0, 0.0);
|
|
let front = Vec3::new(0.0, 0.0, half_len);
|
|
let back = Vec3::new(0.0, 0.0, -half_len);
|
|
|
|
let faces: &[[Vec3; 3]] = &[
|
|
[front, top, right],
|
|
[front, right, bottom],
|
|
[front, bottom, left],
|
|
[front, left, top],
|
|
[back, right, top],
|
|
[back, bottom, right],
|
|
[back, left, bottom],
|
|
[back, top, left],
|
|
];
|
|
|
|
let vertices = faces
|
|
.iter()
|
|
.flat_map(|[a, b, c]| {
|
|
let normal = (*b - *a).cross(*c - *a).normalize();
|
|
[
|
|
Vertex {
|
|
position: (*a).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*b).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
Vertex {
|
|
position: (*c).into(),
|
|
normal: normal.into(),
|
|
color,
|
|
},
|
|
]
|
|
})
|
|
.collect();
|
|
|
|
Self { vertices }
|
|
}
|
|
|
|
/// Compound axis label glyph: sign (+/-) on left, letter (X/Y) on right.
|
|
pub fn axis_label(
|
|
positive: bool,
|
|
is_x: bool,
|
|
size: f32,
|
|
thickness: f32,
|
|
color: [f32; 3],
|
|
) -> Self {
|
|
let mut segments: Vec<([f32; 2], [f32; 2])> = Vec::new();
|
|
let s = size;
|
|
|
|
// Sign (left side, centered at x = -1.5*s)
|
|
segments.push(([-2.2 * s, 0.0], [-0.8 * s, 0.0])); // horizontal bar
|
|
if positive {
|
|
segments.push(([-1.5 * s, -0.7 * s], [-1.5 * s, 0.7 * s])); // vertical bar
|
|
}
|
|
|
|
// Letter (right side, centered at x = 0.5*s)
|
|
if is_x {
|
|
segments.push(([-0.5 * s, -s], [1.5 * s, s]));
|
|
segments.push(([-0.5 * s, s], [1.5 * s, -s]));
|
|
} else {
|
|
segments.push(([0.5 * s, 0.0], [-0.5 * s, s]));
|
|
segments.push(([0.5 * s, 0.0], [1.5 * s, s]));
|
|
segments.push(([0.5 * s, 0.0], [0.5 * s, -s]));
|
|
}
|
|
|
|
line_segments_mesh(&segments, thickness, 0.0, color)
|
|
}
|
|
}
|