use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use glam::{DQuat, DVec3, IVec3};
use roxlap_formats::vxl::Vxl;
use roxlap_scene::{
ChunkGenerator, ChunkStore, GridTransform, Scene, SpanOp, StreamRadius, VoxColor, CHUNK_SIZE_XY,
};
const GRASS: VoxColor = VoxColor::rgb(0x4d, 0x8a, 0x3a);
const STONE: VoxColor = VoxColor::rgb(0x8a, 0x8a, 0x8a);
const RED: VoxColor = VoxColor::rgb(0xc0, 0x30, 0x30);
#[derive(Debug)]
struct FloorGenerator;
impl ChunkGenerator for FloorGenerator {
fn generate(&self, chunk_idx: IVec3) -> Vxl {
let mut vxl = Vxl::empty(CHUNK_SIZE_XY);
if chunk_idx.z == 0 {
let light = (chunk_idx.x + chunk_idx.y).rem_euclid(2) == 0;
let color = if light {
VoxColor::rgb(0x74, 0x9e, 0x58)
} else {
VoxColor::rgb(0x5d, 0x84, 0x46)
};
roxlap_formats::edit::set_rect(&mut vxl, [0, 0, 240], [127, 127, 255], Some(color));
}
vxl
}
}
#[derive(Debug, Default)]
struct MemoryStore {
chunks: Mutex<HashMap<IVec3, (Vxl, u64)>>,
}
impl ChunkStore for MemoryStore {
fn store(&self, chunk_idx: IVec3, vxl: &Vxl, version: u64) {
let entry = (vxl.clone(), version);
self.chunks
.lock()
.expect("store poisoned")
.insert(chunk_idx, entry);
}
fn load(&self, chunk_idx: IVec3) -> Option<(Vxl, u64)> {
self.chunks
.lock()
.expect("store poisoned")
.get(&chunk_idx)
.cloned()
}
}
fn main() {
let mut scene = Scene::new();
let ground_id = scene.add_grid(GridTransform::at(DVec3::ZERO));
let ship_id = scene.add_grid(GridTransform {
origin: DVec3::new(300.0, -80.0, -40.0),
rotation: DQuat::from_rotation_z(0.5),
voxel_world_size: 1.0,
});
scene.grid_mut(ship_id).expect("just added").render_sky = false;
let ground = scene.grid_mut(ground_id).expect("just added");
ground.set_rect(
IVec3::new(-200, -200, 200),
IVec3::new(199, 199, 254),
Some(GRASS),
);
ground.set_sphere(IVec3::new(0, 0, 195), 12, Some(STONE));
ground.set_rect(IVec3::new(-3, -200, 190), IVec3::new(3, 199, 196), None);
assert!(ground.voxel_solid(IVec3::new(0, 0, 210)));
assert!(!ground.voxel_solid(IVec3::new(0, 100, 193)));
assert_eq!(ground.voxel_color(IVec3::new(50, 50, 200)), Some(GRASS));
let (lo, hi) = (IVec3::new(20, 20, 200), IVec3::new(24, 24, 204));
ground.set_rect(lo, hi, Some(RED));
assert_eq!(ground.voxel_color(IVec3::new(22, 22, 200)), Some(GRASS)); ground.set_rect(lo, hi, None); ground.set_rect(lo, hi, Some(RED)); assert_eq!(ground.voxel_color(IVec3::new(22, 22, 200)), Some(RED));
ground.set_sphere_with_colfunc(IVec3::new(60, 60, 200), 8, SpanOp::Carve, |_x, _y, z| {
let depth = (z - 192).clamp(0, 15) as u8;
VoxColor::rgb(0x6b, 0x40 + depth * 3, 0x2f)
});
scene.grid_mut(ground_id).expect("ground exists").name = Some("ground".into());
let bytes = scene.save_snapshot(); let restored = Scene::load_snapshot(&bytes).expect("self-authored snapshot");
assert_eq!(restored.grid_count(), 2);
let (_, ground2) = restored
.grids()
.find(|(_, g)| g.name.as_deref() == Some("ground"))
.expect("rebind by name");
assert_eq!(ground2.voxel_color(IVec3::new(22, 22, 200)), Some(RED));
let stream_id = scene.add_grid(GridTransform::at(DVec3::new(0.0, 4096.0, 0.0)));
let store = Arc::new(MemoryStore::default());
let grid = scene.grid_mut(stream_id).expect("just added");
grid.set_generator(Some(Arc::new(FloorGenerator)));
grid.set_chunk_store(Some(store));
grid.stream_radius = StreamRadius::new(256.0, 384.0);
let camera_near = DVec3::new(0.0, 4096.0, 100.0);
scene.pump_streaming_sync(camera_near); let grid = scene.grid(stream_id).expect("still there");
assert!(grid.chunk_count() > 0); assert!(grid.voxel_solid(IVec3::new(5, 5, 240)));
scene
.grid_mut(stream_id)
.expect("still there")
.set_voxel(IVec3::new(5, 5, 240), None); scene.pump_streaming_sync(camera_near + DVec3::new(0.0, 100_000.0, 0.0));
let grid = scene.grid(stream_id).expect("still there");
assert!(grid.chunk(IVec3::ZERO).is_none()); assert!(grid.chunk_count() > 0); scene.pump_streaming_sync(camera_near);
let grid = scene.grid(stream_id).expect("re-streamed");
assert!(!grid.voxel_solid(IVec3::new(5, 5, 240)));
let mut scaled = Scene::new();
let planet = scaled.add_grid(GridTransform::at_scale(DVec3::ZERO, 2.0));
scaled
.grid_mut(planet)
.expect("just added")
.set_voxel(IVec3::new(5, 5, 10), Some(STONE));
let hit = scaled
.raycast(DVec3::new(11.0, 11.0, 0.0), DVec3::new(0.0, 0.0, 1.0), 64.0)
.expect("ray hits the scaled voxel");
assert_eq!(hit.voxel, IVec3::new(5, 5, 10)); assert!((hit.t - 20.0).abs() < 1e-4);
println!("book_scene_graph: all scene-graph assertions hold");
}