Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ jobs:
cargo clippy --release --target wasm32-unknown-unknown -p viewer --lib -- -D warnings
cargo build --release --target wasm32-unknown-unknown -p viewer --lib --no-default-features --features editor
cargo clippy --release --target wasm32-unknown-unknown -p viewer --lib --no-default-features --features editor -- -D warnings
- name: Build the editor's bake for the browser
if: steps.affected.outputs.all == 'true' || contains(steps.affected.outputs.packages, 'bake-web')
run: |
cargo build --release --target wasm32-unknown-unknown -p bake-web
cargo clippy --release --target wasm32-unknown-unknown -p bake-web -- -D warnings
- name: Test the editor's page
if: steps.affected.outputs.web == 'true'
run: node apps/viewer/web/editor/test.mjs
33 changes: 29 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"
[workspace.dependencies]
solstice-anim = { path = "crates/solstice-anim" }
solstice-asset = { path = "crates/solstice-asset" }
solstice-bake = { path = "crates/solstice-bake" }
solstice-builder = { path = "crates/solstice-builder" }
solstice-import = { path = "crates/solstice-import" }
solstice-physics = { path = "crates/solstice-physics" }
Expand Down
18 changes: 18 additions & 0 deletions apps/bake-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "bake-web"
description = "The bake, built for the browser: the editor's Web Worker bakes a project's scene with it (docs/editor.md)."
version.workspace = true
edition.workspace = true
license.workspace = true

[lib]
name = "solstice_bake_web"
crate-type = ["cdylib", "rlib"]

[dependencies]
solstice-bake.workspace = true
solstice-project.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
82 changes: 82 additions & 0 deletions apps/bake-web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! The bake, for the browser (`docs/editor.md`, "One bake"): the editor's Web Worker hands it a
//! project's scene and gets back the scene file the viewer plays, the same bytes the native
//! `bake --project` writes for it.
//!
//! Surfaces take minutes to make, so the worker first hands over ready-made ones
//! (`bake --surfaces`) with [`provide_layer`]. Files a scene names, such as models and actors,
//! can't be read here yet: a scene that needs them is refused with what it asked for.

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

/// Bakes a project's scene, given as its file's text. `name` names it in errors.
pub fn bake(scene: &str, name: &str) -> Result<Vec<u8>, String> {
let scene = solstice_project::Scene::parse(scene).map_err(|e| format!("{name}: {e}"))?;
let (built, _warnings) =
solstice_bake::project::build_scene(&scene, name, std::path::Path::new("."), None)?;
Ok(solstice_bake::project::encode(built))
}

/// Hands over a ready-made texture layer, from a file `bake --surfaces` wrote, under `key`.
/// Returns its edge length.
pub fn provide(key: &str, file: &[u8]) -> Result<u32, String> {
let (layer, size) = solstice_bake::textures::decode_layer(file)?;
solstice_bake::textures::provide(key, size, layer);
Ok(size)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)]
pub fn start() {
console_error_panic_hook::set_once();
}

/// See [`bake`].
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn bake_scene(scene: &str, name: &str) -> Result<Vec<u8>, JsValue> {
bake(scene, name).map_err(|e| JsValue::from_str(&e))
}

/// See [`provide`].
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn provide_layer(key: &str, file: &[u8]) -> Result<u32, JsValue> {
provide(key, file).map_err(|e| JsValue::from_str(&e))
}

/// The key a built-in surface's layer is provided under.
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn surface_key(name: &str) -> String {
solstice_bake::textures::surface_key(name, 0)
}

#[cfg(test)]
mod tests {
use super::*;

/// A blank scene's terrain bakes to the bytes `bake --project` gives, with no files read.
#[test]
fn a_blank_scene_bakes_as_the_native_bake_does() {
let text = r#"{
"format": 1,
"camera": { "position": [0.0, 20.0, 60.0], "yaw_radians": 0.0, "pitch_radians": -0.2 },
"terrain": { "size": 64.0, "cells": 32, "height": 0.3, "shore_x": 20.0 },
"sea": { "level": -0.6, "ocean": "coastal" }
}"#;
let scene = solstice_project::Scene::parse(text).unwrap();
let (native, _) = solstice_bake::project::build_scene(
&scene,
"t.scene",
std::path::Path::new("."),
Some(32),
)
.unwrap();
let mut small = text.to_owned();
small = small.replace("\"format\": 1,", "\"format\": 1,\n \"texture_size\": 32,");
let baked = bake(&small, "t.scene").unwrap();
assert_eq!(baked, solstice_bake::project::encode(native));
assert!(bake("{", "t.scene").unwrap_err().contains("t.scene"));
}
}
6 changes: 1 addition & 5 deletions apps/bake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ edition.workspace = true
license.workspace = true

[dependencies]
solstice-bake.workspace = true
solstice-project.workspace = true
solstice-anim.workspace = true
solstice-asset.workspace = true
solstice-builder.workspace = true
solstice-game.workspace = true
solstice-script.workspace = true
solstice-import.workspace = true
solstice-procgen.workspace = true
glam.workspace = true
rayon.workspace = true
rustc-hash.workspace = true

[dev-dependencies]
solstice-physics.workspace = true
8 changes: 4 additions & 4 deletions apps/bake/src/forest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A meadow at the edge of a wood, to show procedural foliage.

use crate::meshes;
use glam::{Mat4, Vec3};
use solstice_asset::scene::Viewpoint;
use solstice_asset::{Instance, Material, SceneAsset};
use solstice_bake::meshes;
use solstice_builder::{build_virtual_mesh, BuildOptions};
use solstice_procgen::foliage::bark::Bark;
use solstice_procgen::foliage::grass::Meadow;
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn build(texture_size: u32) -> SceneAsset {
macro_variation: 1.0,
..Default::default()
},
crate::foliage::blades(1),
solstice_bake::foliage::blades(1),
];

// Each plant is two meshes, wood then leaves, placed together.
Expand All @@ -156,8 +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(crate::foliage::bark(bark_layer));
materials.push(crate::foliage::leaves(leaf_layer));
materials.push(solstice_bake::foliage::bark(bark_layer));
materials.push(solstice_bake::foliage::leaves(leaf_layer));
grown.push(Grown {
first_mesh,
variants,
Expand Down
2 changes: 1 addition & 1 deletion apps/bake/src/gallery.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A scene showing every surface in the procedural library, for judging how each one looks.

use crate::meshes;
use glam::{Mat4, Vec3};
use solstice_asset::scene::Viewpoint;
use solstice_asset::{Instance, Material, SceneAsset};
use solstice_bake::meshes;
use solstice_builder::{build_virtual_mesh, BuildOptions};
use solstice_procgen::surfaces;

Expand Down
44 changes: 38 additions & 6 deletions apps/bake/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
//! bake --scene SCENE.txt --show-collision [--output FILE]
//! bake --project SCENE.scene [--texture-size N] [--output FILE]
//! bake --convert SCENE.txt
//! bake --surfaces FOLDER [--texture-size N]
//! ```
//!
//! `--project` bakes a scene of an editor project (`docs/editor.md`), to the project's
//! `build/SCENE.solstice` unless `--output` says otherwise. `--convert` bakes nothing: it writes
//! a scene's text as a project's `SCENE.scene` beside it, and lists it in the `solstice.project`
//! there.
//!
//! `--surfaces` writes the surfaces a land is laid with, each as a scene file holding one
//! compressed texture layer, for the editor's bake in the browser to take ready-made rather than
//! spend minutes making (`solstice_bake::textures`): `surface-NAME-0-SIZE.solstice`.
//!
//! `--check-character` bakes nothing: it checks a rigged character as the export tool wrote it
//! (`tools/blender/solstice_export.py`), prints what it holds and what is wrong with it, and
//! fails if anything is.
Expand All @@ -29,20 +34,15 @@
//!
//! Textures are block compressed unless `--raw-textures` is given.

mod circuit;
mod collision_view;
mod foliage;
mod forest;
mod gallery;
mod land;
mod meshes;
mod plants;
mod project;
mod scene;
mod scene_file;

use scene::Quality;
use solstice_asset::SceneAsset;
use solstice_bake::{project, scene_file};
use solstice_import::{import_gltf, ImportOptions};
use std::path::{Path, PathBuf};
use std::time::Instant;
Expand All @@ -66,6 +66,8 @@ struct Options {
gallery: bool,
/// The procedural meadow and wood, instead of the demo scene.
forest: bool,
/// A folder to write the land's surfaces to, ready-made for the editor.
surfaces: Option<PathBuf>,
/// Bakes what the scene collides with, instead of how it looks.
show_collision: bool,
/// A row of every species, instead of the demo scene.
Expand Down Expand Up @@ -151,6 +153,7 @@ fn parse_args() -> Result<Options, String> {
raw_textures: false,
gallery: false,
forest: false,
surfaces: None,
show_collision: false,
plants: false,
only: false,
Expand Down Expand Up @@ -218,6 +221,7 @@ fn parse_args() -> Result<Options, String> {
"--raw-textures" => options.raw_textures = true,
"--gallery" => options.gallery = true,
"--forest" => options.forest = true,
"--surfaces" => options.surfaces = Some(PathBuf::from(value()?)),
"--show-collision" => options.show_collision = true,
"--plants" => options.plants = true,
"--only" => options.only = true,
Expand Down Expand Up @@ -336,11 +340,39 @@ fn check_character(path: &std::path::Path, roles: Option<&PathBuf>) -> Result<()
}
}

/// The surfaces a scene's land is laid with: its bands, and the plain terrain's sand.
const LAND_SURFACES: [&str; 3] = ["grass", "sand", "dirt"];

/// Writes each of the land's surfaces, compressed, as a scene file of one layer.
fn write_surfaces(folder: &Path, size: u32) -> Result<(), String> {
use solstice_bake::textures;
std::fs::create_dir_all(folder).map_err(|e| format!("{}: {e}", folder.display()))?;
for name in LAND_SURFACES {
let start = Instant::now();
let bytes = textures::encode_layer(textures::surface(name, size), size);
let path = folder.join(format!(
"{}-{size}.solstice",
textures::surface_key(name, 0)
));
std::fs::write(&path, &bytes).map_err(|e| format!("{}: {e}", path.display()))?;
println!(
"wrote {} ({:.1} MB) in {:.1}s",
path.display(),
bytes.len() as f64 / 1e6,
start.elapsed().as_secs_f32()
);
}
Ok(())
}

fn main() -> Result<(), String> {
let mut options = parse_args()?;
if options.scene_file.is_some() && options.project_scene.is_some() {
return Err("give --scene or --project, not both".into());
}
if let Some(folder) = &options.surfaces {
return write_surfaces(folder, options.scene_texture_size.unwrap_or(1024));
}
if let Some(path) = &options.convert {
let written = project::convert_file(path)?;
println!("wrote {}", written.display());
Expand Down
2 changes: 1 addition & 1 deletion apps/bake/src/plants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! species, then any species files given, each followed by a second plant grown from another
//! seed; and in front of them a square of every built-in breed of grass, then any grass files.

use crate::meshes;
use glam::{Mat4, Vec3};
use solstice_asset::scene::Viewpoint;
use solstice_asset::{Instance, Material, SceneAsset, TextureLayer};
use solstice_bake::meshes;
use solstice_builder::{build_virtual_mesh, BuildOptions};
use solstice_procgen::foliage::bark::BARK_TILE_METRES;
use solstice_procgen::foliage::grass::Meadow;
Expand Down
2 changes: 1 addition & 1 deletion apps/bake/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Lays out the demo scene: a ruined temple in a desert canyon.

use crate::meshes;
use glam::{Mat4, Quat, Vec3};
use rayon::prelude::*;
use solstice_asset::{Instance, Light, Material, SceneAsset, Viewpoint};
use solstice_bake::meshes;
use solstice_builder::{build_virtual_mesh, BuildOptions, Mesh};
use solstice_procgen::noise::{fbm, random};
use solstice_procgen::surface::TILE_METRES;
Expand Down
1 change: 1 addition & 0 deletions apps/viewer/web/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<header id="bar">
<span class="brand">Solstice Editor</span>
<div class="group">
<button id="new" type="button" title="A new scene from blank terrain">New</button>
<button id="save" type="button" title="Save the scene (Ctrl+S)">Save</button>
<button id="undo" type="button" title="Undo (Ctrl+Z)">Undo</button>
<button id="redo" type="button" title="Redo (Ctrl+Y)">Redo</button>
Expand Down
Loading
Loading