diff --git a/apps/bake/src/circuit.rs b/apps/bake/src/circuit.rs index 470263d..acd0ab6 100644 --- a/apps/bake/src/circuit.rs +++ b/apps/bake/src/circuit.rs @@ -30,9 +30,7 @@ use rustc_hash::FxHashMap; use solstice_asset::{Heightfield, Instance, Material, SceneAsset}; use solstice_builder::{build_virtual_mesh, BuildOptions, Mesh}; use solstice_game::{Blocker, Track}; -use solstice_procgen::foliage::bark::BARK_TILE_METRES; use solstice_procgen::foliage::grass::Meadow; -use solstice_procgen::foliage::tree::Species; use solstice_procgen::foliage::Rng; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; @@ -686,14 +684,7 @@ fn blades(scene: &mut SceneAsset, meadow: &Meadow) -> u32 { scene .textures .push(meadow.atlas(0).bake(scene.texture_size)); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: layer, - flags: Material::TWO_SIDED | Material::FOLIAGE, - ..Default::default() - }); + scene.materials.push(crate::foliage::blades(layer)); scene.materials.len() as u32 - 1 } @@ -729,60 +720,15 @@ pub fn build(scene: &mut SceneAsset, track: &Track, size: f32) -> Built { // ---- Woods beyond the barriers: thick inland, thinning to open ground towards the cliffs. println!(" trees..."); - let mut grown = Vec::new(); - for (name, variants) in [("oak", 2u32), ("birch", 2), ("fir", 2), ("bush", 2)] { - let species = Species::by_name(name).expect("a built-in species"); - let first_mesh = scene.meshes.len() as u32; - for seed in 0..variants { - let plant = species.grow(seed); - scene - .meshes - .push(build_virtual_mesh(&plant.wood, &BuildOptions::default())); - scene - .meshes - .push(build_virtual_mesh(&plant.leaves, &BuildOptions::default())); - } - let (bark, leaf) = (species.bark, species.leaves.kind); - let bark_layer = scene.textures.len() as i32; - scene - .textures - .push(bark.surface(0).bake(scene.texture_size)); - let leaf_layer = scene.textures.len() as i32; - scene.textures.push(leaf.atlas(0).bake(scene.texture_size)); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - uv_scale: 1.0 / BARK_TILE_METRES, - normal_strength: 1.0, - texture_layer: bark_layer, - ..Default::default() - }); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: leaf_layer, - flags: Material::TWO_SIDED | Material::FOLIAGE, - ..Default::default() - }); - let leaves = scene.materials.len() as u32 - 1; - grown.push((first_mesh, variants, leaves - 1, leaves)); - } + let grown = crate::foliage::grow(scene, &[("oak", 2), ("birch", 2), ("fir", 2), ("bush", 2)]); let plant = |scene: &mut SceneAsset, kind: usize, x: f32, z: f32, k: u32| { - let (first_mesh, variants, bark, leaves) = grown[kind]; - let mesh = first_mesh + (hash(x as i32, z as i32, k + 2) % variants) * 2; + let copy = hash(x as i32, z as i32, k + 2); let turn = (hash(x as i32, z as i32, k) % 1000) as f32 / 1000.0 * std::f32::consts::TAU; let scale = 0.8 + (hash(x as i32, z as i32, k + 1) % 1000) as f32 / 1000.0 * 0.5; let transform = Mat4::from_translation(Vec3::new(x, land.at(x, z) - 0.05, z)) * Mat4::from_rotation_y(turn) * Mat4::from_scale(Vec3::splat(scale)); - for (mesh, material) in [(mesh, bark), (mesh + 1, leaves)] { - scene.instances.push(Instance { - mesh, - material, - transform, - }); - } + grown[kind].place(scene, copy, transform); }; // Whether a tree may stand here: on dry, gentle ground, clear of the barrier. let open = |x: f32, z: f32, clear: f32| { @@ -994,6 +940,19 @@ mod tests { Track::new("Ring", samples, vec![140, 280]).unwrap() } + #[test] + fn the_verge_grass_sways_as_grass() { + let mut scene = SceneAsset { + texture_size: 16, + ..Default::default() + }; + let verge = Meadow::by_name("verge").expect("a built-in grass"); + let blade = blades(&mut scene, &verge) as usize; + let flags = scene.materials[blade].flags; + assert_eq!(flags & Material::SWAYS, Material::SWAYS); + assert_eq!(flags & Material::TREE, 0, "grass sways as grass"); + } + #[test] fn the_land_is_level_with_the_road_and_becomes_its_own_further_off() { let track = ring(30.0); diff --git a/apps/bake/src/foliage.rs b/apps/bake/src/foliage.rs new file mode 100644 index 0000000..7c77b6c --- /dev/null +++ b/apps/bake/src/foliage.rs @@ -0,0 +1,140 @@ +//! What the scene builders that plant things share: the materials of bark, leaves and grass +//! blades, each flagged to sway in the wind, and a wood's species grown into a scene. + +use glam::Mat4; +use solstice_asset::{Instance, Material, SceneAsset}; +use solstice_builder::{build_virtual_mesh, BuildOptions}; +use solstice_procgen::foliage::bark::BARK_TILE_METRES; +use solstice_procgen::foliage::tree::Species; + +/// A tree's wood: its trunk leans and sways, its branches bob. +pub fn bark(texture_layer: i32) -> Material { + Material { + base_color: [1.0; 4], + roughness: 1.0, + uv_scale: 1.0 / BARK_TILE_METRES, + normal_strength: 1.0, + texture_layer, + flags: Material::SWAYS | Material::TREE, + ..Default::default() + } +} + +/// A tree's leaves: they sway with the wood they hang from, and flutter. +pub fn leaves(texture_layer: i32) -> Material { + Material { + base_color: [1.0; 4], + roughness: 1.0, + normal_strength: 1.0, + texture_layer, + flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS | Material::TREE, + ..Default::default() + } +} + +/// Grass blades, which sway as grass. +pub fn blades(texture_layer: i32) -> Material { + Material { + base_color: [1.0; 4], + roughness: 1.0, + normal_strength: 1.0, + texture_layer, + flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS, + ..Default::default() + } +} + +/// A species grown into a scene: copies from different seeds, each two meshes, wood then +/// leaves, and its two materials. +#[derive(Clone, Copy, Debug)] +pub struct Grown { + pub first_mesh: u32, + pub variants: u32, + pub bark: u32, + pub leaves: u32, +} + +impl Grown { + /// Places copy `copy` (taken modulo the copies there are) where `transform` puts it. + pub fn place(&self, scene: &mut SceneAsset, copy: u32, transform: Mat4) { + let mesh = self.first_mesh + (copy % self.variants) * 2; + for (mesh, material) in [(mesh, self.bark), (mesh + 1, self.leaves)] { + scene.instances.push(Instance { + mesh, + material, + transform, + }); + } + } +} + +/// Grows each built-in species named, that many copies of each, into `scene`: their meshes, +/// their bark and leaf textures, and their materials. +pub fn grow(scene: &mut SceneAsset, plants: &[(&str, u32)]) -> Vec { + plants + .iter() + .map(|&(name, variants)| { + let species = Species::by_name(name).expect("a built-in species"); + let first_mesh = scene.meshes.len() as u32; + for seed in 0..variants { + let plant = species.grow(seed); + let options = BuildOptions::default(); + scene.meshes.push(build_virtual_mesh(&plant.wood, &options)); + scene + .meshes + .push(build_virtual_mesh(&plant.leaves, &options)); + } + let bark_layer = scene.textures.len() as i32; + scene + .textures + .push(species.bark.surface(0).bake(scene.texture_size)); + let leaf_layer = scene.textures.len() as i32; + scene + .textures + .push(species.leaves.kind.atlas(0).bake(scene.texture_size)); + scene.materials.push(bark(bark_layer)); + scene.materials.push(leaves(leaf_layer)); + let leaves = scene.materials.len() as u32 - 1; + Grown { + first_mesh, + variants, + bark: leaves - 1, + leaves, + } + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_copy_is_placed_as_its_wood_then_its_leaves() { + let grown = Grown { + first_mesh: 10, + variants: 2, + bark: 4, + leaves: 5, + }; + let mut scene = SceneAsset::default(); + // Copy 3 of two is copy 1: meshes 12 and 13. + grown.place(&mut scene, 3, Mat4::IDENTITY); + let placed: Vec<(u32, u32)> = scene + .instances + .iter() + .map(|i| (i.mesh, i.material)) + .collect(); + assert_eq!(placed, [(12, 4), (13, 5)]); + } + + #[test] + fn every_plant_material_sways_and_only_trees_sway_as_trees() { + for material in [bark(0), leaves(0), blades(0)] { + assert_ne!(material.flags & Material::SWAYS, 0); + } + assert_ne!(bark(0).flags & Material::TREE, 0); + assert_ne!(leaves(0).flags & Material::TREE, 0); + assert_eq!(blades(0).flags & Material::TREE, 0); + } +} diff --git a/apps/bake/src/forest.rs b/apps/bake/src/forest.rs index 533a03d..53bd858 100644 --- a/apps/bake/src/forest.rs +++ b/apps/bake/src/forest.rs @@ -5,7 +5,7 @@ use glam::{Mat4, Vec3}; use solstice_asset::scene::Viewpoint; use solstice_asset::{Instance, Material, SceneAsset}; use solstice_builder::{build_virtual_mesh, BuildOptions}; -use solstice_procgen::foliage::bark::{Bark, BARK_TILE_METRES}; +use solstice_procgen::foliage::bark::Bark; use solstice_procgen::foliage::grass::Meadow; use solstice_procgen::foliage::leaf::LeafKind; use solstice_procgen::foliage::tree::Species; @@ -133,14 +133,7 @@ pub fn build(texture_size: u32) -> SceneAsset { macro_variation: 1.0, ..Default::default() }, - Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: 1, - flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS, - ..Default::default() - }, + crate::foliage::blades(1), ]; // Each plant is two meshes, wood then leaves, placed together. @@ -163,23 +156,8 @@ pub fn build(texture_size: u32) -> SceneAsset { let leaf: LeafKind = species.leaves.kind; let bark_layer = layers.layer(bark.name(), || bark.surface(0)); let leaf_layer = layers.layer(leaf.name(), || leaf.atlas(0)); - materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - uv_scale: 1.0 / BARK_TILE_METRES, - normal_strength: 1.0, - texture_layer: bark_layer, - flags: Material::SWAYS | Material::TREE, - ..Default::default() - }); - materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: leaf_layer, - flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS | Material::TREE, - ..Default::default() - }); + materials.push(crate::foliage::bark(bark_layer)); + materials.push(crate::foliage::leaves(leaf_layer)); grown.push(Grown { first_mesh, variants, diff --git a/apps/bake/src/land.rs b/apps/bake/src/land.rs index addbe38..602db1f 100644 --- a/apps/bake/src/land.rs +++ b/apps/bake/src/land.rs @@ -23,9 +23,7 @@ use glam::{Mat4, Vec2, Vec3}; use solstice_asset::{Instance, Material, SceneAsset}; use solstice_builder::{build_virtual_mesh, BuildOptions, Mesh}; -use solstice_procgen::foliage::bark::BARK_TILE_METRES; use solstice_procgen::foliage::grass::Meadow; -use solstice_procgen::foliage::tree::Species; use solstice_procgen::foliage::Rng; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; @@ -438,14 +436,7 @@ fn add_meadow( first_mesh + PATCH_VARIANTS + which * SHAPE_VARIANTS + seed }; let blades = layer(scene, meadow.atlas(0)); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: blades, - flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS, - ..Default::default() - }); + scene.materials.push(crate::foliage::blades(blades)); let material = scene.materials.len() as u32 - 1; let cells = (radius * 2.0 / size) as i32; let quarter = std::f32::consts::FRAC_PI_2; @@ -556,58 +547,16 @@ fn add_woods( kind_at: &dyn Fn(Vec2) -> usize, ) { println!(" growing the woods..."); - // Each plant: its meshes, wood then leaves for each grown copy, and its two materials. - let mut grown = Vec::new(); - for (name, variants) in [("oak", 2u32), ("birch", 2), ("bush", 2)] { - let species = Species::by_name(name).expect("a built-in species"); - let first_mesh = scene.meshes.len() as u32; - for seed in 0..variants { - let plant = species.grow(seed); - scene - .meshes - .push(build_virtual_mesh(&plant.wood, &BuildOptions::default())); - scene - .meshes - .push(build_virtual_mesh(&plant.leaves, &BuildOptions::default())); - } - let bark = layer(scene, species.bark.surface(0)); - let leaf = layer(scene, species.leaves.kind.atlas(0)); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - uv_scale: 1.0 / BARK_TILE_METRES, - normal_strength: 1.0, - texture_layer: bark, - flags: Material::SWAYS | Material::TREE, - ..Default::default() - }); - scene.materials.push(Material { - base_color: [1.0; 4], - roughness: 1.0, - normal_strength: 1.0, - texture_layer: leaf, - flags: Material::TWO_SIDED | Material::FOLIAGE | Material::SWAYS | Material::TREE, - ..Default::default() - }); - let leaves = scene.materials.len() as u32 - 1; - grown.push((first_mesh, variants, leaves - 1, leaves)); - } + let grown = crate::foliage::grow(scene, &[("oak", 2), ("birch", 2), ("bush", 2)]); let mut rng = Rng::new(11, 5); let mut trees: Vec = Vec::new(); let mut bushes: Vec = Vec::new(); let place = |scene: &mut SceneAsset, which: usize, at: Vec2, rng: &mut Rng| { - let (first_mesh, variants, bark, leaves) = grown[which]; - let mesh = first_mesh + rng.index(variants as usize) as u32 * 2; + let copy = rng.index(grown[which].variants as usize) as u32; let transform = Mat4::from_translation(Vec3::new(at.x, height(at.x, at.y) - 0.05, at.y)) * Mat4::from_rotation_y(rng.range(0.0, std::f32::consts::TAU)) * Mat4::from_scale(Vec3::splat(rng.range(0.8, 1.25))); - for (mesh, material) in [(mesh, bark), (mesh + 1, leaves)] { - scene.instances.push(Instance { - mesh, - material, - transform, - }); - } + grown[which].place(scene, copy, transform); }; for &(centre, radius, spacing) in &plan.woods { for _ in 0..(radius * radius * 2.0) as u32 { diff --git a/apps/bake/src/main.rs b/apps/bake/src/main.rs index 0b5654b..79ba7c1 100644 --- a/apps/bake/src/main.rs +++ b/apps/bake/src/main.rs @@ -31,6 +31,7 @@ mod circuit; mod collision_view; +mod foliage; mod forest; mod gallery; mod land; @@ -231,6 +232,12 @@ fn parse_args() -> Result { other => return Err(format!("unknown argument: {other}")), } } + if options.only && !options.plants { + return Err( + "--only keeps just the species and grass files given: it needs --plants, --species or --grass" + .to_owned(), + ); + } Ok(options) } diff --git a/apps/bake/src/scene_file.rs b/apps/bake/src/scene_file.rs index 5059832..bae0647 100644 --- a/apps/bake/src/scene_file.rs +++ b/apps/bake/src/scene_file.rs @@ -893,6 +893,8 @@ pub(crate) fn build_commands( let mut land_plan = crate::land::Plan::default(); let mut water = None; let mut circuit = None; + // Whether there has been a circuit, and a line of land, to refuse the two together. + let (mut has_land, mut has_circuit) = (false, false); // Grouped by file, keeping first-seen order, so that each model is imported only once. let mut order: Vec = Vec::new(); let mut models: HashMap = HashMap::new(); @@ -910,6 +912,28 @@ pub(crate) fn build_commands( } in commands { let at = |e: String| format!("{origin}: {e}"); + // A circuit builds its own land, ground and planting: the lines that would build + // another, one inside it, are refused wherever in the file they come. + let land = match &command { + Command::Terrain { .. } => Some("terrain"), + Command::Land { .. } => Some("land"), + Command::Path { .. } => Some("path"), + Command::Yard { .. } => Some("yard"), + Command::Clear { .. } => Some("clear"), + Command::Meadow(_) => Some("meadow"), + Command::Wood { .. } => Some("wood"), + Command::Water { .. } => Some("water"), + _ => None, + }; + let circuits = matches!(command, Command::Circuit(_)); + has_land |= land.is_some(); + has_circuit |= circuits; + if (land.is_some() || circuits) && has_land && has_circuit { + let word = land.unwrap_or("circuit"); + return Err(at(format!( + "`{word}` with `circuit`: a circuit builds its own land, so leave out `terrain`, `water` and the land's lines" + ))); + } match Some(command) { None => {} Some(Command::TextureSize(size)) => scene.texture_size = size, @@ -1688,6 +1712,42 @@ mod tests { assert_eq!(scene.gameplay.len(), 1); } + /// A circuit's track: three points. + const TRACK: &[u8] = b"p 0 0 0 1 0 0 6 6 +p 0 0 50 1 0 0 6 6 +p 50 0 50 1 0 0 6 6 +"; + + #[test] + fn a_circuit_and_a_terrain_are_not_both_built() { + let folder = Folder::with(&[ + ( + "scene.txt", + b"terrain 400 64 0.3 52\ntrack c.track\ncircuit 900\n", + ), + ( + "c.track", + b"p 0 0 0 1 0 0 6 6\np 0 0 50 1 0 0 6 6\np 50 0 50 1 0 0 6 6\n", + ), + ]); + let error = build(&folder.0.join("scene.txt"), None).unwrap_err(); + assert!(error.contains("a circuit builds its own land"), "{error}"); + assert!(error.contains(":3:"), "on the circuit's line: {error}"); + // And the other way round, with a line of the land's after the circuit. + let folder = Folder::with(&[ + ( + "scene.txt", + b"track c.track +circuit 900 +wood 0 0 40 9 +", + ), + ("c.track", TRACK), + ]); + let error = build(&folder.0.join("scene.txt"), None).unwrap_err(); + assert!(error.contains(":3: `wood` with `circuit`"), "{error}"); + } + #[test] fn a_scene_without_actors_has_no_gameplay() { let (gltf, bin) = two_triangles();