134 lines
3.7 KiB
Rust
134 lines
3.7 KiB
Rust
use glam::{Mat4, Quat, Vec3};
|
|
use rand::Rng;
|
|
|
|
use crate::mesh::Mesh;
|
|
|
|
/// A placed object in the scene
|
|
pub struct SceneObject {
|
|
pub mesh_index: usize,
|
|
pub position: Vec3,
|
|
pub rotation_axis: Vec3,
|
|
pub rotation_speed: f32,
|
|
pub scale: f32,
|
|
pub base_rotation: f32,
|
|
pub color: [f32; 3],
|
|
}
|
|
|
|
impl SceneObject {
|
|
pub fn transform(&self, time: f32) -> Mat4 {
|
|
let angle = self.base_rotation + time * self.rotation_speed;
|
|
let rotation = Quat::from_axis_angle(self.rotation_axis, angle);
|
|
Mat4::from_translation(self.position)
|
|
* Mat4::from_quat(rotation)
|
|
* Mat4::from_scale(Vec3::splat(self.scale))
|
|
}
|
|
}
|
|
|
|
/// Model uniform for per-object transform
|
|
#[repr(C)]
|
|
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
|
pub struct ModelUniform {
|
|
pub transform: [[f32; 4]; 4],
|
|
pub opacity: f32,
|
|
pub _padding: [f32; 3],
|
|
}
|
|
|
|
/// Color palette — saturated, cheerful colors
|
|
const COLORS: &[[f32; 3]] = &[
|
|
[1.0, 0.3, 0.35], // coral red
|
|
[0.3, 0.85, 0.5], // mint green
|
|
[0.3, 0.5, 1.0], // cornflower blue
|
|
[1.0, 0.75, 0.2], // golden yellow
|
|
[0.85, 0.35, 0.9], // orchid purple
|
|
[0.2, 0.9, 0.9], // cyan
|
|
[1.0, 0.55, 0.2], // tangerine
|
|
[0.5, 1.0, 0.3], // lime
|
|
];
|
|
|
|
/// Build all the meshes and objects for the scene.
|
|
/// Returns (meshes, objects) where each object references a mesh by index.
|
|
pub fn build_scene() -> (Vec<Mesh>, Vec<SceneObject>) {
|
|
let mut rng = rand::rng();
|
|
let mut meshes = Vec::new();
|
|
let mut objects = Vec::new();
|
|
|
|
// Create a few mesh variants
|
|
// Icospheres at different subdivision levels
|
|
for &color in &COLORS[..4] {
|
|
meshes.push(Mesh::icosphere(2, color));
|
|
}
|
|
// Octahedra
|
|
for &color in &COLORS[4..] {
|
|
meshes.push(Mesh::octahedron(color));
|
|
}
|
|
|
|
// Number of shape meshes (for random object selection)
|
|
let num_shape_meshes = meshes.len();
|
|
|
|
// Crosshair at origin (static, grid-aligned)
|
|
let crosshair_index = meshes.len();
|
|
meshes.push(Mesh::crosshair(5.0));
|
|
objects.push(SceneObject {
|
|
mesh_index: crosshair_index,
|
|
position: Vec3::ZERO,
|
|
rotation_axis: Vec3::Y,
|
|
rotation_speed: 0.0,
|
|
scale: 1.0,
|
|
base_rotation: 0.0,
|
|
color: [0.5, 0.5, 0.5],
|
|
});
|
|
|
|
// Rotating spiked ball at origin
|
|
let spiked_ball_index = meshes.len();
|
|
meshes.push(Mesh::icosphere_with_spike(
|
|
2,
|
|
[0.3, 0.5, 1.0],
|
|
[1.0, 0.3, 0.35],
|
|
));
|
|
objects.push(SceneObject {
|
|
mesh_index: spiked_ball_index,
|
|
position: Vec3::ZERO,
|
|
rotation_axis: Vec3::new(0.3, 0.1, 1.0).normalize(),
|
|
rotation_speed: 0.5,
|
|
scale: 0.8,
|
|
base_rotation: 0.0,
|
|
color: [0.3, 0.5, 1.0],
|
|
});
|
|
|
|
let num_objects = 30;
|
|
for i in 0..num_objects {
|
|
let mesh_index = i % num_shape_meshes;
|
|
let spread = 40.0;
|
|
|
|
let position = Vec3::new(
|
|
rng.random_range(-spread..spread),
|
|
rng.random_range(-spread..spread),
|
|
rng.random_range(-spread * 0.5..spread * 0.5),
|
|
);
|
|
|
|
let rotation_axis = Vec3::new(
|
|
rng.random_range(-1.0..1.0),
|
|
rng.random_range(-1.0..1.0),
|
|
rng.random_range(-1.0..1.0),
|
|
)
|
|
.normalize_or(Vec3::Z);
|
|
|
|
let rotation_speed =
|
|
rng.random_range(0.2..1.5) * if rng.random_bool(0.5) { 1.0 } else { -1.0 };
|
|
let scale = rng.random_range(0.5..2.5);
|
|
let base_rotation = rng.random_range(0.0..std::f32::consts::TAU);
|
|
|
|
objects.push(SceneObject {
|
|
mesh_index,
|
|
position,
|
|
rotation_axis,
|
|
rotation_speed,
|
|
scale,
|
|
base_rotation,
|
|
color: COLORS[mesh_index],
|
|
});
|
|
}
|
|
|
|
(meshes, objects)
|
|
}
|