From 003f0c2e46259e52da7f28e76b6ba2c583080caa Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:24:50 +1100 Subject: [PATCH 1/2] Editor: a new scene from blank terrain, baked in the browser New makes a scene from a template (terrain, land bands and a coastal sea) and bakes it in the browser in about two seconds; with nothing selected, the details panel shows the scene's settings, and a change bakes it again and shows it, keeping the camera and selection. A new scene is kept in the browser as it changes and reopened by ?draft=NAME. Agents do the same through project.new, scene.settings, scene.set, bake.run and bake.status. The scene building moves out of apps/bake into a solstice-bake library, which apps/bake-web builds for the browser and the editor runs in a Web Worker. The land's surfaces, which take minutes to make, are made once by `bake --surfaces` in the site's build and fetched ready-made; the compressor leaves such layers as they are. A scene baked in the browser is byte for byte the one `bake --project` makes of it natively. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 5 + Cargo.lock | 33 +- Cargo.toml | 1 + apps/bake-web/Cargo.toml | 18 ++ apps/bake-web/src/lib.rs | 82 +++++ apps/bake/Cargo.toml | 6 +- apps/bake/src/forest.rs | 8 +- apps/bake/src/gallery.rs | 2 +- apps/bake/src/main.rs | 44 ++- apps/bake/src/plants.rs | 2 +- apps/bake/src/scene.rs | 2 +- apps/viewer/web/editor.html | 1 + apps/viewer/web/editor/bake-worker.js | 41 +++ apps/viewer/web/editor/commands.js | 11 + apps/viewer/web/editor/main.js | 291 +++++++++++++++--- apps/viewer/web/editor/panels.js | 55 +++- apps/viewer/web/editor/scenes.js | 103 +++++++ apps/viewer/web/loader.js | 13 +- crates/solstice-bake/Cargo.toml | 22 ++ .../solstice-bake}/src/circuit.rs | 13 +- .../solstice-bake}/src/circuit/furniture.rs | 0 .../solstice-bake}/src/circuit/ground.rs | 0 .../solstice-bake}/src/circuit/line.rs | 0 .../solstice-bake}/src/circuit/paving.rs | 5 +- .../solstice-bake}/src/circuit/verge.rs | 0 .../solstice-bake}/src/foliage.rs | 0 .../bake => crates/solstice-bake}/src/land.rs | 21 +- crates/solstice-bake/src/lib.rs | 11 + .../solstice-bake}/src/meshes.rs | 0 .../solstice-bake}/src/project.rs | 20 +- .../solstice-bake}/src/scene_file.rs | 3 +- crates/solstice-bake/src/textures.rs | 148 +++++++++ crates/solstice-builder/src/texture.rs | 10 + docs/editor-api.md | 59 +++- docs/editor.md | 36 ++- scripts/build-web.sh | 10 + scripts/check-changed.sh | 8 +- 37 files changed, 992 insertions(+), 92 deletions(-) create mode 100644 apps/bake-web/Cargo.toml create mode 100644 apps/bake-web/src/lib.rs create mode 100644 apps/viewer/web/editor/bake-worker.js create mode 100644 apps/viewer/web/editor/scenes.js create mode 100644 crates/solstice-bake/Cargo.toml rename {apps/bake => crates/solstice-bake}/src/circuit.rs (99%) rename {apps/bake => crates/solstice-bake}/src/circuit/furniture.rs (100%) rename {apps/bake => crates/solstice-bake}/src/circuit/ground.rs (100%) rename {apps/bake => crates/solstice-bake}/src/circuit/line.rs (100%) rename {apps/bake => crates/solstice-bake}/src/circuit/paving.rs (99%) rename {apps/bake => crates/solstice-bake}/src/circuit/verge.rs (100%) rename {apps/bake => crates/solstice-bake}/src/foliage.rs (100%) rename {apps/bake => crates/solstice-bake}/src/land.rs (98%) create mode 100644 crates/solstice-bake/src/lib.rs rename {apps/bake => crates/solstice-bake}/src/meshes.rs (100%) rename {apps/bake => crates/solstice-bake}/src/project.rs (97%) rename {apps/bake => crates/solstice-bake}/src/scene_file.rs (99%) create mode 100644 crates/solstice-bake/src/textures.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ef1e4c..97a7159 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Cargo.lock b/Cargo.lock index 2da4027..a7c4bc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,16 +265,23 @@ version = "0.2.0" dependencies = [ "glam 0.33.12", "rayon", - "rustc-hash 2.1.3", - "solstice-anim", "solstice-asset", + "solstice-bake", "solstice-builder", "solstice-game", "solstice-import", - "solstice-physics", "solstice-procgen", "solstice-project", - "solstice-script", +] + +[[package]] +name = "bake-web" +version = "0.2.0" +dependencies = [ + "console_error_panic_hook", + "solstice-bake", + "solstice-project", + "wasm-bindgen", ] [[package]] @@ -3191,6 +3198,24 @@ dependencies = [ "miniz_oxide 0.8.9", ] +[[package]] +name = "solstice-bake" +version = "0.2.0" +dependencies = [ + "glam 0.33.12", + "rayon", + "rustc-hash 2.1.3", + "solstice-anim", + "solstice-asset", + "solstice-builder", + "solstice-game", + "solstice-import", + "solstice-physics", + "solstice-procgen", + "solstice-project", + "solstice-script", +] + [[package]] name = "solstice-builder" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 03e8aee..5c5d1e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/apps/bake-web/Cargo.toml b/apps/bake-web/Cargo.toml new file mode 100644 index 0000000..de96946 --- /dev/null +++ b/apps/bake-web/Cargo.toml @@ -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" diff --git a/apps/bake-web/src/lib.rs b/apps/bake-web/src/lib.rs new file mode 100644 index 0000000..fce1a21 --- /dev/null +++ b/apps/bake-web/src/lib.rs @@ -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, 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 { + 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, 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 { + 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")); + } +} diff --git a/apps/bake/Cargo.toml b/apps/bake/Cargo.toml index 002972e..e6dda60 100644 --- a/apps/bake/Cargo.toml +++ b/apps/bake/Cargo.toml @@ -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 diff --git a/apps/bake/src/forest.rs b/apps/bake/src/forest.rs index 53bd858..64edd80 100644 --- a/apps/bake/src/forest.rs +++ b/apps/bake/src/forest.rs @@ -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; @@ -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. @@ -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, diff --git a/apps/bake/src/gallery.rs b/apps/bake/src/gallery.rs index fec80f4..953be61 100644 --- a/apps/bake/src/gallery.rs +++ b/apps/bake/src/gallery.rs @@ -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; diff --git a/apps/bake/src/main.rs b/apps/bake/src/main.rs index 79ba7c1..19729c8 100644 --- a/apps/bake/src/main.rs +++ b/apps/bake/src/main.rs @@ -13,6 +13,7 @@ //! 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 @@ -20,6 +21,10 @@ //! 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. @@ -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; @@ -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, /// Bakes what the scene collides with, instead of how it looks. show_collision: bool, /// A row of every species, instead of the demo scene. @@ -151,6 +153,7 @@ fn parse_args() -> Result { raw_textures: false, gallery: false, forest: false, + surfaces: None, show_collision: false, plants: false, only: false, @@ -218,6 +221,7 @@ fn parse_args() -> Result { "--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, @@ -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()); diff --git a/apps/bake/src/plants.rs b/apps/bake/src/plants.rs index b6460b0..123656b 100644 --- a/apps/bake/src/plants.rs +++ b/apps/bake/src/plants.rs @@ -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; diff --git a/apps/bake/src/scene.rs b/apps/bake/src/scene.rs index f703614..8a624c3 100644 --- a/apps/bake/src/scene.rs +++ b/apps/bake/src/scene.rs @@ -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; diff --git a/apps/viewer/web/editor.html b/apps/viewer/web/editor.html index 5c630ef..a832820 100644 --- a/apps/viewer/web/editor.html +++ b/apps/viewer/web/editor.html @@ -110,6 +110,7 @@
Solstice Editor
+ diff --git a/apps/viewer/web/editor/bake-worker.js b/apps/viewer/web/editor/bake-worker.js new file mode 100644 index 0000000..b5bebae --- /dev/null +++ b/apps/viewer/web/editor/bake-worker.js @@ -0,0 +1,41 @@ +// The editor's bake, off the page's thread (docs/editor.md, "One bake"): takes a project's +// scene, gives back the scene file the viewer plays. Loaded only when the editor first bakes. +// +// In: `{ id, scene, name, content }`, where `scene` is the scene file's text and `content` the +// folder holding ready-made surfaces (`bake --surfaces`). Out: `{ id, bytes, ms }`, or +// `{ id, error }`. + +import init, { bake_scene, provide_layer, surface_key } from "../bake-wasm/solstice_bake_web.js"; + +/// The surfaces a land is laid with, which take minutes to make and so come ready-made. +const LAND_SURFACES = ["grass", "sand", "dirt"]; + +let ready = null; +/// Surfaces handed over already, by key and size. +const provided = new Set(); + +async function provideSurfaces(content, size) { + await Promise.all(LAND_SURFACES.map(async (name) => { + const key = surface_key(name); + if (provided.has(`${key}@${size}`)) return; + const response = await fetch(`${content}${key}-${size}.solstice`).catch(() => null); + // Without one, the bake makes the surface itself: right, but slow. + if (!response?.ok) return; + provide_layer(key, new Uint8Array(await response.arrayBuffer())); + provided.add(`${key}@${size}`); + })); +} + +onmessage = async ({ data: { id, scene, name, content } }) => { + try { + ready ??= init(); + await ready; + const size = JSON.parse(scene).texture_size ?? 1024; + await provideSurfaces(content, size); + const start = performance.now(); + const bytes = bake_scene(scene, name); + postMessage({ id, bytes, ms: performance.now() - start }, [bytes.buffer]); + } catch (error) { + postMessage({ id, error: String(error?.message ?? error) }); + } +}; diff --git a/apps/viewer/web/editor/commands.js b/apps/viewer/web/editor/commands.js index 968db6a..07377a6 100644 --- a/apps/viewer/web/editor/commands.js +++ b/apps/viewer/web/editor/commands.js @@ -46,6 +46,9 @@ function check(name, schema, args) { case "string[]": if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) fail(); break; + case "object": + if (value !== null && (typeof value !== "object" || Array.isArray(value))) fail(); + break; default: break; } out[key] = value; @@ -61,6 +64,7 @@ function jsonSchema(schema) { boolean: { type: "boolean" }, vec3: { type: "array", items: { type: "number" }, minItems: 3, maxItems: 3 }, "string[]": { type: "array", items: { type: "string" } }, + object: { type: ["object", "null"] }, }; return { type: "object", @@ -104,6 +108,13 @@ export class History { return step; } + /// Forgets every step, as a new scene does. + clear() { + this.#done = []; + this.#undone = []; + this.onChange(); + } + /// What can be undone, oldest first, and what can be redone, next first. list() { const describe = (step) => ({ label: step.label, by: step.by }); diff --git a/apps/viewer/web/editor/main.js b/apps/viewer/web/editor/main.js index 00c8bcb..1122fae 100644 --- a/apps/viewer/web/editor/main.js +++ b/apps/viewer/web/editor/main.js @@ -9,7 +9,8 @@ import { eulerFromColumns, scale as scaleVector, sub, tidy, tidyAngles, } from "./math.js"; import { Details, Outliner } from "./panels.js"; -import { Project, kindOf } from "./project.js"; +import { Project, kindOf, writeScene } from "./project.js"; +import { Baker, SETTINGS, blankScene, checkSetting } from "./scenes.js"; const params = new URLSearchParams(location.search); const el = (id) => document.getElementById(id); @@ -17,7 +18,9 @@ const canvas = el("view"); const overlay = el("overlay-canvas"); const context = overlay.getContext("2d"); -const sceneName = params.get("scene") ?? "cove"; +/// The scene's name: from ?scene= for one served beside the page, ?draft= for one kept in this +/// browser, or "untitled" for a new one (?new). +let sceneName = params.get("draft") ?? (params.has("new") ? "untitled" : params.get("scene") ?? "cove"); if (!/^[\w-]+$/.test(sceneName)) throw new Error("invalid scene name"); const sceneBase = params.get("scenes") ?? "assets/"; const API_VERSION = 1; @@ -47,44 +50,76 @@ function fail(text) { log.add("error", text); } +/// The viewer's module, once loaded, and the budgets every scene is opened with. +let wasm = null; +const budgets = { + geometryBudget: (Number(params.get("geometry-mb")) || 128) * 1048576, + textureBudget: (Number(params.get("texture-mb")) || 64) * 1048576, +}; +const baker = new Baker(params.get("content") ?? "content/"); + +const fitCanvas = () => { + const ratio = Math.min(window.devicePixelRatio || 1, 2); + const box = canvas.getBoundingClientRect(); + return [Math.max(1, Math.round(box.width * ratio)), Math.max(1, Math.round(box.height * ratio)), ratio]; +}; + +/// Opens a baked scene on the canvas: from `url`, or from `bytes` the bake just gave. +async function openViewer(source) { + [canvas.width, canvas.height] = fitCanvas(); + return openScene(wasm, canvas, { + ...source, + ...budgets, + onStage: (text) => { el("loading-text").textContent = text; }, + onDownload: (received) => { + if (received !== null) el("loading-text").textContent = `Downloading scene: ${(received / 1e6).toFixed(0)} MB`; + }, + }); +} + async function main() { if (!navigator.gpu || !(await requestAdapter())) { fail("This browser has no WebGPU adapter for this GPU. Try a recent Chrome or Edge."); return; } - const wasm = await import("../editor-wasm/solstice_viewer.js"); + wasm = await import("../editor-wasm/solstice_viewer.js"); await wasm.default(); - el("loading-text").textContent = "Loading scene…"; - const resize = () => { - const ratio = Math.min(window.devicePixelRatio || 1, 2); - const box = canvas.getBoundingClientRect(); - return [Math.max(1, Math.round(box.width * ratio)), Math.max(1, Math.round(box.height * ratio)), ratio]; - }; - [canvas.width, canvas.height] = resize(); - const { viewer, streamer, file } = await openScene(wasm, canvas, { - url: `${sceneBase}${sceneName}.solstice`, - geometryBudget: (Number(params.get("geometry-mb")) || 128) * 1048576, - textureBudget: (Number(params.get("texture-mb")) || 64) * 1048576, - onStage: (text) => { el("loading-text").textContent = text; }, - onDownload: (received) => { - if (received !== null) el("loading-text").textContent = `Downloading scene: ${(received / 1e6).toFixed(0)} MB`; - }, - }); - // The project: the scene file beside the baked scene, if there is one. - let sceneJson = null; - const projectUrl = params.get("project") ?? `${sceneBase}${sceneName}.scene`; - try { - const response = await fetch(projectUrl); - if (response.ok) sceneJson = await response.text(); - } catch { /* none */ } - const project = new Project(sceneJson ? JSON.parse(sceneJson) : null, viewer); - if (!project.saveable) { - log.add("warn", `No project at ${projectUrl}: each instance is listed alone, and can be moved but not saved.`); - } else if (viewer.source_ids() === "") { + // A new scene, or one kept in this browser, is baked here; one served beside the page comes + // baked, with its project beside it. + let scene = null; + let opened; + if (params.has("new") || params.has("draft")) { + scene = params.has("draft") ? JSON.parse(await readDraft(sceneName) ?? "null") : blankScene(); + if (!scene) { + fail(`There is no scene called ${sceneName} kept in this browser.`); + return; + } + el("loading-text").textContent = "Baking the scene…"; + const baked = await baker.bake(writeScene(scene), `${sceneName}.scene`); + opened = await openViewer({ bytes: baked.bytes }); + log.add("info", `Baked ${sceneName} in ${(baked.ms / 1000).toFixed(1)} s.`); + } else { + opened = await openViewer({ url: `${sceneBase}${sceneName}.solstice` }); + const projectUrl = params.get("project") ?? `${sceneBase}${sceneName}.scene`; + try { + const response = await fetch(projectUrl); + if (response.ok) scene = JSON.parse(await response.text()); + } catch { /* none */ } + if (!scene) { + log.add("warn", `No project at ${projectUrl}: each instance is listed alone, and can be moved but not saved.`); + } + } + const project = new Project(scene, opened.viewer); + if (scene && opened.viewer.source_ids() === "" && project.entities.size) { log.add("warn", "This scene was baked without the map of its entities (bake it with --project): nothing can be picked or moved."); } - editor.start(viewer, streamer, file, project, sceneJson, resize); + editor.start(opened.viewer, opened.streamer, opened.file, project, fitCanvas); + // A new scene is kept in this browser from the start, and a reload opens it again. + if (params.has("new")) { + keepAsDraft(editor); + history.replaceState(null, "", `?draft=${sceneName}`); + } } /// Everything the editor holds, and the commands that change it. @@ -126,12 +161,11 @@ const editor = { this.previews.clear(); }, - start(viewer, streamer, file, project, sceneJson, resize) { + start(viewer, streamer, file, project, resize) { this.viewer = viewer; this.streamer = streamer; this.file = file; this.project = project; - this.savedJson = sceneJson; this.resize = resize; this.commands = new Commands(this); defineCommands(this.commands, this); @@ -154,6 +188,76 @@ const editor = { requestAnimationFrame((now) => this.tick(now, now)); }, + /// Puts a newly baked scene in place of the one shown, keeping the camera, the selection and + /// what is hidden. + async swap(bytes, scene) { + const camera = Array.from(this.viewer.camera()); + const old = this.viewer; + // No frame is drawn while there is no viewer. + this.viewer = null; + old.free(); + const opened = await openViewer({ bytes }); + this.viewer = opened.viewer; + this.streamer = opened.streamer; + this.file = opened.file; + this.viewer.set_camera(...camera); + this.project = new Project(scene, this.viewer); + this.selection = new Set([...this.selection].filter((id) => this.project.entities.has(id))); + for (const id of this.hidden) { + for (const instance of this.project.instancesOf(id)) this.viewer.set_instance_hidden(instance, true); + } + this.picks = []; + this.refresh(); + }, + + /// The bake: what is in flight, whether another is wanted after it, and how the last went. + bake: { running: null, again: false, timer: null, lastMs: null, lastError: null }, + + /// Bakes the scene again soon, after a change to what only a bake makes, such as the terrain. + /// Changes close together bake once. + rebakeSoon() { + clearTimeout(this.bake.timer); + this.bake.timer = setTimeout(() => this.rebake(), 400); + el("hint").textContent = "Baking soon…"; + }, + + /// Bakes the scene as it is now and shows it. Resolves when what was asked for is shown. + async rebake() { + clearTimeout(this.bake.timer); + if (this.bake.running) { + this.bake.again = true; + await this.bake.running; + return this.bake.running; + } + const run = (async () => { + do { + this.bake.again = false; + el("hint").textContent = "Baking…"; + const scene = structuredClone(this.project.scene); + try { + const baked = await baker.bake(writeScene(scene), `${sceneName}.scene`); + if (this.bake.again) continue; + await this.swap(baked.bytes, this.project.scene); + this.bake.lastMs = baked.ms; + this.bake.lastError = null; + el("hint").textContent = `Baked in ${(baked.ms / 1000).toFixed(1)} s`; + log.add("info", `Baked ${sceneName} in ${(baked.ms / 1000).toFixed(1)} s.`); + } catch (error) { + this.bake.lastError = error.message ?? String(error); + el("hint").textContent = "The bake failed: see the log"; + log.add("error", `The bake failed: ${this.bake.lastError}`); + } + } while (this.bake.again); + })(); + this.bake.running = run; + try { + await run; + } finally { + if (this.bake.running === run) this.bake.running = null; + } + return null; + }, + refresh() { this.outliner.render(); this.details.render(); @@ -165,6 +269,10 @@ const editor = { }, tick(now, previous) { + if (!this.viewer) { + requestAnimationFrame((next) => this.tick(next, now)); + return; + } const delta = Math.min((now - previous) / 1000, 0.1); this.streamer.pump(); const fly = this.input.flying ? this.input.axes() : [0, 0, 0]; @@ -720,6 +828,91 @@ function defineCommands(commands, editor) { return { frames: editor.frames }; }, }); + commands.define("scene.settings", { + description: "The settings a scene has one of: its terrain, land bands, sea, texture size and starting camera, as the project holds them. Each is null if the scene has none.", + run: () => { + const scene = editor.project.scene; + return { + terrain: scene.terrain ?? null, + land: scene.land ?? null, + sea: scene.sea ?? null, + texture_size: scene.texture_size ?? 1024, + camera: scene.camera ?? null, + fields: Object.fromEntries(Object.entries(SETTINGS).map(([name, setting]) => + [name, { description: setting.description, fields: setting.fields }])), + }; + }, + }); + commands.define("scene.set", { + description: "Changes the scene's settings: `terrain` { size, cells, height, shore_x }, `land` { sand, dirt }, `sea` { level, ocean }, each whole, or null to take it away. One undo step. The scene bakes again in the browser; bake.run waits for it.", + changes: true, + args: { + terrain: { type: "object", description: "{ size, cells, height, shore_x }: metres across, cells along a side (8 to 1024; more takes longer), hill height in metres, and the x where the shore begins (the sea lies towards +X). Null for no ground." }, + land: { type: "object", description: "{ sand, dirt }: metres of sand from the water, then of dirt; grass beyond. Null for plain ground." }, + sea: { type: "object", description: "{ level, ocean }: the calm sea's height, and a built-in ocean (coastal). Null for no sea." }, + }, + example: { terrain: { size: 256, cells: 128, height: 6, shore_x: 40 } }, + run: (args) => { + if (!editor.project.saveable) throw new CommandError("not_a_project", "this scene has no project to change"); + if (!Object.keys(args).length) throw new CommandError("bad_arguments", "scene.set takes terrain, land or sea"); + for (const [name, value] of Object.entries(args)) { + const problem = checkSetting(name, value); + if (problem) throw new CommandError("bad_arguments", problem); + } + const scene = editor.project.scene; + const before = Object.fromEntries(Object.keys(args).map((name) => [name, structuredClone(scene[name] ?? null)])); + const apply = (values) => { + for (const [name, value] of Object.entries(values)) { + if (value === null) delete scene[name]; + else scene[name] = structuredClone(value); + } + editor.refresh(); + editor.rebakeSoon(); + }; + apply(args); + return { + result: { baking: true }, + step: { label: `Change ${Object.keys(args).join(" and ")}`, undo: () => apply(before), redo: () => apply(args) }, + }; + }, + }); + commands.define("bake.run", { + description: "Bakes the scene in the browser now, or waits for the bake already asked for, and shows it. Gives how long it took and any error.", + run: async () => { + await editor.rebake(); + if (editor.bake.lastError) throw new CommandError("bake_failed", editor.bake.lastError); + return { ms: Math.round(editor.bake.lastMs ?? 0), instances: editor.viewer.instance_count() }; + }, + }); + commands.define("bake.status", { + description: "Whether a bake is running or waiting to, and how the last one went.", + run: () => ({ + running: Boolean(editor.bake.running), + waiting: Boolean(editor.bake.timer) && !editor.bake.running, + last_ms: editor.bake.lastMs === null ? null : Math.round(editor.bake.lastMs), + last_error: editor.bake.lastError, + }), + }); + commands.define("project.new", { + description: "Starts a new scene from blank terrain: an island of sand, dirt and grass in a coastal sea, baked in the browser and kept there as it changes. The scene shown now is left; save it first if it has changes.", + args: { name: { type: "string", description: "Its name: letters, digits, - and _ (default untitled)." } }, + example: { name: "island" }, + run: async ({ name = "untitled" }) => { + if (!/^[\w-]+$/.test(name)) throw new CommandError("bad_arguments", "a name is letters, digits, - and _"); + const scene = blankScene(); + const baked = await baker.bake(writeScene(scene), `${name}.scene`); + sceneName = name; + editor.commands.history.clear(); + editor.selection.clear(); + editor.hidden.clear(); + await editor.swap(baked.bytes, scene); + editor.viewer.set_camera(...scene.camera.position, scene.camera.yaw_radians, scene.camera.pitch_radians); + keepAsDraft(editor); + history.replaceState(null, "", `?draft=${name}`); + log.add("info", `New scene ${name}, baked in ${(baked.ms / 1000).toFixed(1)} s.`); + return { scene: name, ms: Math.round(baked.ms) }; + }, + }); commands.define("project.get", { description: "The scene's project file as it would be saved, and whether it has changes not yet saved.", run: () => ({ @@ -978,6 +1171,12 @@ function bindToolbar(editor) { el("undo").addEventListener("click", () => editor.call("history.undo")); el("redo").addEventListener("click", () => editor.call("history.redo")); el("save").addEventListener("click", () => save(editor)); + el("new").addEventListener("click", async () => { + if (editor.dirty && !params.has("draft") && !confirm("Leave this scene's unsaved changes for a new scene?")) return; + el("hint").textContent = "Baking a new scene…"; + const outcome = await editor.call("project.new", {}); + if (!outcome.ok) log.add("error", outcome.error.message); + }); for (const mode of ["move", "rotate", "scale"]) { el(`mode-${mode}`).addEventListener("click", () => editor.call("gizmo.set", { mode })); } @@ -995,13 +1194,29 @@ function bindToolbar(editor) { // ---- Saving: to the browser as you go, and to a file when asked. -const DRAFT = `${sceneName}.scene`; +const draftFile = (name = sceneName) => `${name}.scene`; async function draftDirectory() { const root = await navigator.storage?.getDirectory?.(); return root ? root.getDirectoryHandle("drafts", { create: true }) : null; } +/// A scene kept in this browser, as its file's text, or null. +async function readDraft(name) { + try { + const folder = await draftDirectory(); + return await (await (await folder?.getFileHandle(draftFile(name)))?.getFile())?.text() ?? null; + } catch { + return null; + } +} + +/// Keeps the scene in this browser now, as autosave does after each change. +function keepAsDraft(editor) { + editor.dirty = true; + scheduleAutosave(editor); +} + let autosaveTimer = null; function scheduleAutosave(editor) { if (!editor.project.saveable) return; @@ -1010,7 +1225,7 @@ function scheduleAutosave(editor) { try { const folder = await draftDirectory(); if (!folder) return; - const writable = await (await folder.getFileHandle(DRAFT, { create: true })).createWritable(); + const writable = await (await folder.getFileHandle(draftFile(), { create: true })).createWritable(); await writable.write(editor.project.toJson()); await writable.close(); el("hint").textContent = "Draft kept in this browser"; @@ -1022,10 +1237,10 @@ function scheduleAutosave(editor) { /// Offers to bring back a draft left from an earlier visit, if it differs from the scene served. async function offerDraft(editor) { - if (!editor.project.saveable) return; + if (!editor.project.saveable || params.has("draft") || params.has("new")) return; try { const folder = await draftDirectory(); - const text = await (await (await folder?.getFileHandle(DRAFT))?.getFile())?.text(); + const text = await (await (await folder?.getFileHandle(draftFile()))?.getFile())?.text(); if (!text || text === editor.project.toJson()) return; const draft = JSON.parse(text); el("draft").hidden = false; @@ -1072,7 +1287,7 @@ async function offerDraft(editor) { }; el("draft-discard").onclick = async () => { el("draft").hidden = true; - await folder.removeEntry(DRAFT).catch(() => {}); + await folder.removeEntry(draftFile()).catch(() => {}); }; } catch { /* no draft */ } } diff --git a/apps/viewer/web/editor/panels.js b/apps/viewer/web/editor/panels.js index ddb6106..b96025c 100644 --- a/apps/viewer/web/editor/panels.js +++ b/apps/viewer/web/editor/panels.js @@ -3,6 +3,7 @@ // anything itself. import { kindOf } from "./project.js"; +import { SETTINGS } from "./scenes.js"; const ICONS = { folder: "▤", mesh: "◆", actor: "●", light: "✦", blocker: "▭", wood: "♣", clear: "○", @@ -141,9 +142,60 @@ function numberField(label, value, step, onChange, onScrub) { return make("label", { className: "number" }, [name, input]); } +/// What each setting and field is called on the panel. +const LABELS = { + terrain: "Terrain", land: "Land", sea: "Sea", + size: "Size (m)", cells: "Cells", height: "Hills (m)", shore_x: "Shore x", + sand: "Sand (m)", dirt: "Dirt (m)", level: "Level (m)", ocean: "Ocean", +}; + +/// What a setting starts as when it is turned on. +const STARTING = { + terrain: { size: 256, cells: 128, height: 3, shore_x: 64 }, + land: { sand: 10, dirt: 5 }, + sea: { level: -0.6, ocean: "coastal" }, +}; + export class Details { #element; + /// The scene's own settings, shown when nothing is selected: each a section that can be + /// turned on or off, whose fields bake the scene again when changed. + #renderScene() { + const scene = this.editor.project.scene; + this.#element.append(make("p", { + className: "empty", + textContent: "Nothing selected: these are the scene's own settings. A change bakes the scene again.", + })); + for (const [name, setting] of Object.entries(SETTINGS)) { + const value = scene[name]; + const toggle = make("input", { type: "checkbox", checked: Boolean(value), title: `Has ${name}` }); + toggle.addEventListener("click", (e) => e.stopPropagation()); + toggle.addEventListener("change", () => + this.editor.call("scene.set", { [name]: toggle.checked ? (value ?? STARTING[name]) : null })); + const summary = make("summary", {}, [toggle, ` ${LABELS[name]}`]); + const rows = []; + if (value) { + for (const [key, field] of Object.entries(setting.fields)) { + const set = (v) => this.editor.call("scene.set", { [name]: { ...value, [key]: v } }); + let input; + if (field.text) { + input = make("input", { type: "text", value: value[key] }); + input.addEventListener("change", () => set(input.value)); + input.addEventListener("keydown", (e) => e.stopPropagation()); + } else { + input = numberField("·", value[key], field.step, (v) => { + const clamped = Math.min(field.max, Math.max(field.min, field.integer ? Math.round(v) : v)); + if (clamped !== value[key]) set(clamped); + }, () => {}); + } + rows.push(make("div", { className: "field" }, [make("span", { className: "name", textContent: LABELS[key] ?? key }), input])); + } + } + this.#element.append(make("details", { open: Boolean(value), title: setting.description }, [summary, ...rows])); + } + } + constructor(element, editor) { this.#element = element; this.editor = editor; @@ -154,7 +206,8 @@ export class Details { const ids = [...selection]; this.#element.replaceChildren(); if (ids.length === 0) { - this.#element.append(make("p", { className: "empty", textContent: "Select something to see its details." })); + if (project.saveable) this.#renderScene(); + else this.#element.append(make("p", { className: "empty", textContent: "Select something to see its details." })); return; } if (ids.length > 1) { diff --git a/apps/viewer/web/editor/scenes.js b/apps/viewer/web/editor/scenes.js new file mode 100644 index 0000000..7cd8b39 --- /dev/null +++ b/apps/viewer/web/editor/scenes.js @@ -0,0 +1,103 @@ +// New scenes, and baking them in the browser (docs/editor.md, phase 2): the template a new scene +// starts from, the settings a scene has one of, and the worker that bakes a scene to the file the +// viewer plays. + +/// A new scene: an island of sand, dirt and grass in a coastal sea, seen from offshore. +export function blankScene() { + return { + format: 1, + note: "A new scene.", + texture_size: 1024, + // Out over the sea, which lies towards +X, looking back at the land (along -X). + camera: { position: [150, 30, 0], yaw_radians: Math.PI / 2, pitch_radians: -0.25 }, + terrain: { size: 256, cells: 128, height: 3, shore_x: 64 }, + land: { sand: 10, dirt: 5 }, + sea: { level: -0.6, ocean: "coastal" }, + entities: [], + }; +} + +/// The settings a scene has one of, each with its fields and their ranges. They are checked +/// here as the bake checks them, so a bad value is refused when it is set, not when it bakes. +export const SETTINGS = { + terrain: { + description: "The ground: metres across, cells along each side, how tall its hills are, and the x where the shore begins (the sea lies towards +X).", + fields: { + size: { min: 16, max: 4096, step: 1 }, + cells: { min: 8, max: 1024, step: 1, integer: true }, + height: { min: 0, max: 200, step: 0.1 }, + shore_x: { min: -4096, max: 4096, step: 1 }, + }, + }, + land: { + description: "The terrain in bands: metres of sand in from the water, then of dirt; grass beyond.", + fields: { sand: { min: 0, max: 500, step: 0.5 }, dirt: { min: 0, max: 500, step: 0.5 } }, + }, + sea: { + description: "An open sea: its calm level, and a built-in ocean's name.", + fields: { level: { min: -100, max: 100, step: 0.1 }, ocean: { text: true } }, + }, +}; + +/// Checks a scene setting's value: an object with the fields `SETTINGS` lists, or null to take +/// it away. Gives back an error's message, or null if it is good. +export function checkSetting(name, value) { + const setting = SETTINGS[name]; + if (!setting) return `no setting \`${name}\`; scene.settings lists them`; + if (value === null) return null; + if (typeof value !== "object" || Array.isArray(value)) return `\`${name}\` is an object of its fields, or null`; + for (const key of Object.keys(value)) { + if (!(key in setting.fields)) return `\`${name}\` has no field \`${key}\`; it has ${Object.keys(setting.fields).join(", ")}`; + } + for (const [key, field] of Object.entries(setting.fields)) { + const v = value[key]; + if (v === undefined) return `\`${name}\` needs \`${key}\``; + if (field.text) { + if (typeof v !== "string" || !/^[\w.-]+$/.test(v)) return `\`${name}.${key}\` is a name with no spaces`; + continue; + } + if (typeof v !== "number" || !Number.isFinite(v) || v < field.min || v > field.max) { + return `\`${name}.${key}\` is a number from ${field.min} to ${field.max}`; + } + if (field.integer && !Number.isInteger(v)) return `\`${name}.${key}\` is a whole number`; + } + return null; +} + +/// Bakes scenes in a Web Worker, one at a time. +export class Baker { + #worker = null; + #next = 1; + #waiting = new Map(); + /// Where the ready-made surfaces are. + content; + + /// `content` is relative to the page; the worker is given it whole, as it would otherwise + /// read it relative to its own script. + constructor(content) { + this.content = new URL(content, location.href).href; + } + + /// Bakes a scene's file text. Resolves to `{ bytes, ms }`; rejects with the bake's error. + bake(text, name) { + if (!this.#worker) { + this.#worker = new Worker(new URL("./bake-worker.js", import.meta.url), { type: "module" }); + this.#worker.onmessage = ({ data }) => { + const waiting = this.#waiting.get(data.id); + this.#waiting.delete(data.id); + if (data.error) waiting?.reject(new Error(data.error)); + else waiting?.resolve({ bytes: data.bytes, ms: data.ms }); + }; + this.#worker.onerror = (event) => { + for (const waiting of this.#waiting.values()) waiting.reject(new Error(event.message || "the bake's worker failed")); + this.#waiting.clear(); + this.#worker = null; + }; + } + const id = this.#next++; + return new Promise((resolve, reject) => { + this.#waiting.set(id, { resolve, reject }); + this.#worker.postMessage({ id, scene: text, name, content: this.content }); + }); + } +} diff --git a/apps/viewer/web/loader.js b/apps/viewer/web/loader.js index 33059b2..923d345 100644 --- a/apps/viewer/web/loader.js +++ b/apps/viewer/web/loader.js @@ -71,6 +71,14 @@ export class SceneFile { this.#onDownload = onDownload; } + /// A scene file already in memory, as the editor's bake gives it. + static fromBytes(bytes) { + const file = new SceneFile("memory"); + file.#whole = bytes; + file.bytesFetched = bytes.length; + return file; + } + async read(start, end, signal) { if (this.#whole) return this.#whole.subarray(start, end); const response = await fetch(this.#url, { headers: { Range: `bytes=${start}-${end - 1}` }, signal }); @@ -425,7 +433,10 @@ export async function requestAdapter() { /// streamer's options; and `onDownload`, `onTablesProgress` and `onStage` to follow it. export async function openScene(wasm, canvas, options) { const { WebViewer, header_bytes, read_header } = wasm; - const file = new SceneFile(options.url, options.onDownload); + // `bytes` opens a scene file already in memory instead of `url`. + const file = options.bytes + ? SceneFile.fromBytes(options.bytes) + : new SceneFile(options.url, options.onDownload); const tables = options.tablesUrl ? new SceneFile(options.tablesUrl, options.onDownload) : file; const [manifestOffset, manifestBytes] = read_header(await tables.read(0, header_bytes())); const manifestEnd = manifestOffset + manifestBytes; diff --git a/crates/solstice-bake/Cargo.toml b/crates/solstice-bake/Cargo.toml new file mode 100644 index 0000000..169a0f5 --- /dev/null +++ b/crates/solstice-bake/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "solstice-bake" +description = "Builds scenes from a project or a scene text: terrain, land, foliage, models and gameplay, into a SceneAsset. Shared by the native bake and the editor's in-browser bake." +version.workspace = true +edition.workspace = true +license.workspace = true + +[dependencies] +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 diff --git a/apps/bake/src/circuit.rs b/crates/solstice-bake/src/circuit.rs similarity index 99% rename from apps/bake/src/circuit.rs rename to crates/solstice-bake/src/circuit.rs index acd0ab6..cbf3fda 100644 --- a/apps/bake/src/circuit.rs +++ b/crates/solstice-bake/src/circuit.rs @@ -34,7 +34,6 @@ use solstice_procgen::foliage::grass::Meadow; use solstice_procgen::foliage::Rng; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; -use solstice_procgen::surfaces; /// How far beyond the road and what lies beside it the land is kept level with it: room for /// the verge and the barrier. @@ -663,9 +662,11 @@ fn lay(scene: &mut SceneAsset, pieces: &[[Vec3; 4]], material: u32) { } } -fn textured(scene: &mut SceneAsset, surface: solstice_procgen::surface::Surface, tint: f32) -> u32 { +fn textured(scene: &mut SceneAsset, surface: &str, tint: f32) -> u32 { let layer = scene.textures.len() as i32; - scene.textures.push(surface.bake(scene.texture_size)); + scene + .textures + .push(crate::textures::surface(surface, scene.texture_size)); scene.materials.push(Material { base_color: [tint, tint, tint, 1.0], roughness: 1.0, @@ -697,9 +698,9 @@ pub fn build(scene: &mut SceneAsset, track: &Track, size: f32) -> Built { let land = Land::new(&island, &road, size); println!(" textures..."); - let grass = textured(scene, surfaces::grass(0), 1.0); - let rock = textured(scene, surfaces::rock(0), 0.8); - let sand = textured(scene, surfaces::sand(0), 0.68); + let grass = textured(scene, "grass", 1.0); + let rock = textured(scene, "rock", 0.8); + let sand = textured(scene, "sand", 0.68); // ---- The land, in its three kinds. println!(" land..."); diff --git a/apps/bake/src/circuit/furniture.rs b/crates/solstice-bake/src/circuit/furniture.rs similarity index 100% rename from apps/bake/src/circuit/furniture.rs rename to crates/solstice-bake/src/circuit/furniture.rs diff --git a/apps/bake/src/circuit/ground.rs b/crates/solstice-bake/src/circuit/ground.rs similarity index 100% rename from apps/bake/src/circuit/ground.rs rename to crates/solstice-bake/src/circuit/ground.rs diff --git a/apps/bake/src/circuit/line.rs b/crates/solstice-bake/src/circuit/line.rs similarity index 100% rename from apps/bake/src/circuit/line.rs rename to crates/solstice-bake/src/circuit/line.rs diff --git a/apps/bake/src/circuit/paving.rs b/crates/solstice-bake/src/circuit/paving.rs similarity index 99% rename from apps/bake/src/circuit/paving.rs rename to crates/solstice-bake/src/circuit/paving.rs index 5b553ae..9a4b163 100644 --- a/apps/bake/src/circuit/paving.rs +++ b/crates/solstice-bake/src/circuit/paving.rs @@ -9,7 +9,6 @@ use solstice_game::{Blocker, Track}; use solstice_procgen::foliage::Rng; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; -use solstice_procgen::surfaces; /// How many shades of rubber there are, from the faint edge of the racing line to its middle. const SHADES: usize = 5; @@ -157,11 +156,11 @@ impl Paints { let stone = scene.textures.len() as i32; scene .textures - .push(surfaces::asphalt(0).bake(scene.texture_size)); + .push(crate::textures::surface("asphalt", scene.texture_size)); let grit = scene.textures.len() as i32; scene .textures - .push(surfaces::gravel(0).bake(scene.texture_size)); + .push(crate::textures::surface("gravel", scene.texture_size)); // Rubber darkens the road and fills its grain, shade by shade towards the middle of // the line. let rubber = std::array::from_fn(|shade| { diff --git a/apps/bake/src/circuit/verge.rs b/crates/solstice-bake/src/circuit/verge.rs similarity index 100% rename from apps/bake/src/circuit/verge.rs rename to crates/solstice-bake/src/circuit/verge.rs diff --git a/apps/bake/src/foliage.rs b/crates/solstice-bake/src/foliage.rs similarity index 100% rename from apps/bake/src/foliage.rs rename to crates/solstice-bake/src/foliage.rs diff --git a/apps/bake/src/land.rs b/crates/solstice-bake/src/land.rs similarity index 98% rename from apps/bake/src/land.rs rename to crates/solstice-bake/src/land.rs index 602db1f..8591f5d 100644 --- a/apps/bake/src/land.rs +++ b/crates/solstice-bake/src/land.rs @@ -27,7 +27,6 @@ use solstice_procgen::foliage::grass::Meadow; use solstice_procgen::foliage::Rng; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; -use solstice_procgen::surfaces; /// The kinds of ground, in the order of [`Plan::kind`]'s answers. pub const SAND: usize = 0; @@ -147,9 +146,11 @@ pub fn blended_terrain( mesh } -/// A texture layer for `surface`, added to the scene. -fn layer(scene: &mut SceneAsset, surface: solstice_procgen::surface::Surface) -> i32 { - scene.textures.push(surface.bake(scene.texture_size)); +/// A texture layer for the built-in surface `name`, added to the scene. +fn layer(scene: &mut SceneAsset, name: &str) -> i32 { + scene + .textures + .push(crate::textures::surface(name, scene.texture_size)); scene.textures.len() as i32 - 1 } @@ -171,8 +172,8 @@ pub fn add( // them. Each darkened as it is seen in the field, so they sit beside the scans: turf about // 0.75 of its texture, damp sand 0.68, earth 0.62. let first = scene.textures.len() as i32; - for surface in [surfaces::grass(0), surfaces::sand(0), surfaces::dirt(0)] { - layer(scene, surface); + for name in ["grass", "sand", "dirt"] { + layer(scene, name); } for (k, tone) in [0.75, 0.68, 0.62].into_iter().enumerate() { scene.materials.push(Material { @@ -435,7 +436,13 @@ fn add_meadow( .expect("a shape with meshes") as u32; first_mesh + PATCH_VARIANTS + which * SHAPE_VARIANTS + seed }; - let blades = layer(scene, meadow.atlas(0)); + let texture_size = scene.texture_size; + scene.textures.push(crate::textures::layer( + "grass-atlas-default-0", + texture_size, + || meadow.atlas(0).bake(texture_size), + )); + let blades = scene.textures.len() as i32 - 1; scene.materials.push(crate::foliage::blades(blades)); let material = scene.materials.len() as u32 - 1; let cells = (radius * 2.0 / size) as i32; diff --git a/crates/solstice-bake/src/lib.rs b/crates/solstice-bake/src/lib.rs new file mode 100644 index 0000000..f7b5d6a --- /dev/null +++ b/crates/solstice-bake/src/lib.rs @@ -0,0 +1,11 @@ +//! Builds scenes (`docs/editor.md`, "One bake"): from an editor project or a scene's text, into a +//! [`solstice_asset::SceneAsset`] ready to page and write. The native `bake` command and the +//! editor's bake in the browser both use it, so the two give the same scene. + +pub mod circuit; +pub mod foliage; +pub mod land; +pub mod meshes; +pub mod project; +pub mod scene_file; +pub mod textures; diff --git a/apps/bake/src/meshes.rs b/crates/solstice-bake/src/meshes.rs similarity index 100% rename from apps/bake/src/meshes.rs rename to crates/solstice-bake/src/meshes.rs diff --git a/apps/bake/src/project.rs b/crates/solstice-bake/src/project.rs similarity index 97% rename from apps/bake/src/project.rs rename to crates/solstice-bake/src/project.rs index 32bb440..e9dada0 100644 --- a/apps/bake/src/project.rs +++ b/crates/solstice-bake/src/project.rs @@ -43,7 +43,25 @@ pub fn build(path: &Path, texture_size: Option) -> Result<(SceneAsset, Vec< let name = path .file_name() .map_or(String::new(), |n| n.to_string_lossy().into()); - scene_file::build_commands(&folder, lower(&scene, &name)?, texture_size) + build_scene(&scene, &name, &folder, texture_size) +} + +/// Builds a project's scene, already read, with its files named relative to `folder`. `name` +/// names the scene in errors. +pub fn build_scene( + scene: &Scene, + name: &str, + folder: &Path, + texture_size: Option, +) -> Result<(SceneAsset, Vec), String> { + scene_file::build_commands(folder, lower(scene, name)?, texture_size) +} + +/// A built scene as its file holds it: textures compressed, paged and encoded, as `bake` writes +/// it and the editor's bake in the browser does too. +pub fn encode(mut scene: SceneAsset) -> Vec { + solstice_builder::compress_textures(&mut scene); + solstice_asset::file::encode(&solstice_builder::paginate(&scene)) } /// The commands a scene says, in the order the scene text would have them: its settings, then diff --git a/apps/bake/src/scene_file.rs b/crates/solstice-bake/src/scene_file.rs similarity index 99% rename from apps/bake/src/scene_file.rs rename to crates/solstice-bake/src/scene_file.rs index aaac19f..0bccf41 100644 --- a/apps/bake/src/scene_file.rs +++ b/crates/solstice-bake/src/scene_file.rs @@ -44,7 +44,6 @@ use solstice_game::{resolve, ActorFile, ActorLine, Blocker, InputMap, SceneGame, use solstice_import::{append_model, import_gltf, ImportOptions, ModelPlacements}; use solstice_procgen::noise::fbm; use solstice_procgen::surface::TILE_METRES; -use solstice_procgen::surfaces; use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -496,7 +495,7 @@ fn add_terrain(scene: &mut SceneAsset, size: f32, cells: u32, height: f32, shore let layer = scene.textures.len() as i32; scene .textures - .push(surfaces::sand(0).bake(scene.texture_size)); + .push(crate::textures::surface("sand", scene.texture_size)); scene.materials.push(Material { // Darkened to about 0.35 albedo, as damp beach sand is, so that it sits correctly next to // scanned materials and does not skew auto exposure. diff --git a/crates/solstice-bake/src/textures.rs b/crates/solstice-bake/src/textures.rs new file mode 100644 index 0000000..e6547cf --- /dev/null +++ b/crates/solstice-bake/src/textures.rs @@ -0,0 +1,148 @@ +//! Ready-made texture layers. Procedural surfaces such as grass and sand are the same in every +//! scene and take minutes to generate, far too long for the editor's bake in the browser +//! (`docs/editor.md`). They are made once by `bake --surfaces`, as ordinary scene files holding +//! one layer each, and the browser hands them over with [`provide`] before it bakes. A bake asks +//! for each layer through [`layer`], which gives the one provided or makes it. +//! +//! Provided layers are block compressed, as `bake --surfaces` wrote them; the bake's compression +//! leaves them as they are (`solstice_builder::compress_textures`). A natively baked scene +//! provides none, so it is made exactly as before. + +use solstice_asset::{file, SceneAsset, TextureEncoding, TextureLayer}; +use solstice_builder::{compress_textures, paginate}; +use std::sync::Mutex; + +/// Layers provided, by key and edge length. +static PROVIDED: Mutex> = Mutex::new(Vec::new()); + +/// The key of a built-in surface, as `solstice_procgen::surfaces::by_name` names it. +pub fn surface_key(name: &str, seed: u32) -> String { + format!("surface-{name}-{seed}") +} + +/// A built-in surface's layer at `size`, seed 0: the one provided, or else generated. +/// +/// # Panics +/// If the procedural library has no surface called `name`; callers name built-in ones. +pub fn surface(name: &str, size: u32) -> TextureLayer { + layer(&surface_key(name, 0), size, || { + solstice_procgen::surfaces::by_name(name, 0) + .unwrap_or_else(|| panic!("no built-in surface `{name}`")) + .bake(size) + }) +} + +/// Hands over a ready-made layer, for any bake from now on that asks for `key` at `size`. +pub fn provide(key: &str, size: u32, layer: TextureLayer) { + let mut provided = PROVIDED.lock().unwrap_or_else(|e| e.into_inner()); + provided.retain(|(k, s, _)| !(k == key && *s == size)); + provided.push((key.to_owned(), size, layer)); +} + +/// Whether a layer for `key` at `size` has been provided. +pub fn provided(key: &str, size: u32) -> bool { + let provided = PROVIDED.lock().unwrap_or_else(|e| e.into_inner()); + provided.iter().any(|(k, s, _)| k == key && *s == size) +} + +/// The layer for `key` at `size`: the one provided, or else `make`'s. +pub fn layer(key: &str, size: u32, make: impl FnOnce() -> TextureLayer) -> TextureLayer { + let found = { + let provided = PROVIDED.lock().unwrap_or_else(|e| e.into_inner()); + provided + .iter() + .find(|(k, s, _)| k == key && *s == size) + .map(|(_, _, layer)| layer.clone()) + }; + found.unwrap_or_else(make) +} + +/// A scene file holding one layer, compressed, as `bake --surfaces` writes them. +pub fn encode_layer(layer: TextureLayer, size: u32) -> Vec { + let mut scene = SceneAsset { + texture_size: size, + textures: vec![layer], + ..Default::default() + }; + compress_textures(&mut scene); + file::encode(&paginate(&scene)) +} + +/// The layer in a scene file `encode_layer` wrote, whole, and its edge length. +pub fn decode_layer(bytes: &[u8]) -> Result<(TextureLayer, u32), String> { + let loaded = file::decode(bytes).map_err(|e| e.to_string())?; + let scene = &loaded.scene; + if scene.base_textures.len() != 1 || loaded.upper_textures.len() != 1 { + return Err("a surface's file holds one texture layer".into()); + } + if scene.texture_encoding != TextureEncoding::Block { + return Err("a surface's file is block compressed".into()); + } + let (upper, base) = (&loaded.upper_textures[0], &scene.base_textures[0]); + let join = |a: &[Vec], b: &[Vec]| a.iter().chain(b).cloned().collect(); + Ok(( + TextureLayer { + color_mips: join(&upper.color_mips, &base.color_mips), + normal_mips: join(&upper.normal_mips, &base.normal_mips), + material_mips: join(&upper.material_mips, &base.material_mips), + }, + scene.texture_size, + )) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parts(layer: &TextureLayer) -> [&Vec>; 3] { + [&layer.color_mips, &layer.normal_mips, &layer.material_mips] + } + + fn raw_layer(size: u32) -> TextureLayer { + let mips = |channels: usize| -> Vec> { + (0..size.trailing_zeros() + 1) + .map(|level| { + let edge = (size >> level) as usize; + (0..edge * edge * channels) + .map(|i| (i * 7 + level as usize) as u8) + .collect() + }) + .collect() + }; + TextureLayer { + color_mips: mips(4), + normal_mips: mips(2), + material_mips: mips(2), + } + } + + #[test] + fn a_layer_comes_back_as_it_was_compressed() { + let size = 64; + let bytes = encode_layer(raw_layer(size), size); + let (layer, read_size) = decode_layer(&bytes).unwrap(); + assert_eq!(read_size, size); + let mut expected = SceneAsset { + texture_size: size, + textures: vec![raw_layer(size)], + ..Default::default() + }; + compress_textures(&mut expected); + assert_eq!(parts(&layer), parts(&expected.textures[0])); + } + + #[test] + fn a_provided_layer_is_used_and_others_are_made() { + let size = 16; + let made = || raw_layer(size); + let key = "surface-test-provided-0"; + assert_eq!(parts(&layer(key, size, made)), parts(&raw_layer(size))); + let mut other = raw_layer(size); + other.color_mips[0][0] ^= 1; + provide(key, size, other.clone()); + assert!(provided(key, size)); + assert_eq!(parts(&layer(key, size, made)), parts(&other)); + // Another size is made afresh. + assert_eq!(parts(&layer(key, 8, || raw_layer(8))), parts(&raw_layer(8))); + } +} diff --git a/crates/solstice-builder/src/texture.rs b/crates/solstice-builder/src/texture.rs index 0f9d15f..1965271 100644 --- a/crates/solstice-builder/src/texture.rs +++ b/crates/solstice-builder/src/texture.rs @@ -243,6 +243,16 @@ pub fn compress_textures(scene: &mut SceneAsset) { } let size = scene.texture_size; for layer in &mut scene.textures { + // A layer made ready and compressed already, as the editor's bake is given + // (`solstice-bake`'s textures), is a quarter the size of a raw one and stays as it is. + let raw_bytes = size as usize * size as usize * 4; + if layer + .color_mips + .first() + .is_some_and(|mip| mip.len() != raw_bytes) + { + continue; + } for (level, mip) in layer.color_mips.iter_mut().enumerate() { *mip = encode_bc7(mip, size >> level); } diff --git a/docs/editor-api.md b/docs/editor-api.md index 6491170..bd443bf 100644 --- a/docs/editor-api.md +++ b/docs/editor-api.md @@ -23,7 +23,7 @@ solstice.version // 1, raised when a command changes Every call returns `{ ok: true, result }` or `{ ok: false, error: { code, message } }`, and never throws. Error codes are `unknown_command`, `bad_arguments` (a missing, misspelt or mistyped argument, named in the message), `unknown_id`, `no_transform`, `nothing_selected`, -`nothing_to_frame`, `not_drawing` (a `pick` or `wait` while the tab is hidden, which draws no +`nothing_to_frame`, `not_a_project`, `bake_failed`, `not_drawing` (a `pick` or `wait` while the tab is hidden, which draws no frames: bring it to the front) and `failed`. Changes an agent makes are steps in the same history as a person's, marked `agent` in @@ -60,6 +60,19 @@ await solstice.call("log.read", { level: "error" }); await solstice.call("history.undo"); ``` +Building a scene from blank terrain: + +```js +await solstice.call("project.new", { name: "island" }); // baked in the browser in a few seconds +await solstice.call("scene.set", { terrain: { size: 256, cells: 128, height: 12, shore_x: 30 } }); +await solstice.call("scene.set", { land: { sand: 18, dirt: 8 } }); +await solstice.call("bake.run"); // waits for the bake, and shows it +await solstice.call("project.get"); // the scene's file, to save or read +``` + +A new scene is kept in the browser as it changes (`editor.html?draft=NAME` opens it again), and +its bake in the browser gives the same file `bake --project` gives for it on a PC. + ## Commands ### `scene.info` @@ -280,6 +293,50 @@ The scene's project file as it would be saved, and whether it has changes not ye No arguments. +### `project.new` + +Starts a new scene from blank terrain: an island of sand, dirt and grass in a coastal sea, baked in the browser and kept there as it changes. The scene shown now is left; save it first if it has changes. + +| Argument | Type | Needed | Meaning | +|---|---|---|---| +| `name` | string | | Its name: letters, digits, - and _ (default untitled). | + +```js +await solstice.call("project.new", {"name":"island"}) +``` + +### `scene.settings` + +The settings a scene has one of: its terrain, land bands, sea, texture size and starting camera, as the project holds them, with each field's range. Each is null if the scene has none. + +No arguments. + +### `scene.set` + +Changes the scene's settings, each given whole, or null to take it away. One undo step. The scene bakes again in the browser; `bake.run` waits for it. + +| Argument | Type | Needed | Meaning | +|---|---|---|---| +| `terrain` | object or null | | `{ size, cells, height, shore_x }`: metres across (16 to 4096), cells along a side (8 to 1024; more takes longer), hill height in metres, and the x where the shore begins (the sea lies towards +X). Null for no ground. | +| `land` | object or null | | `{ sand, dirt }`: metres of sand from the water, then of dirt; grass beyond. Null for plain ground. | +| `sea` | object or null | | `{ level, ocean }`: the calm sea's height, and a built-in ocean (`coastal`). Null for no sea. | + +```js +await solstice.call("scene.set", {"terrain":{"size":256,"cells":128,"height":6,"shore_x":40}}) +``` + +### `bake.run` + +Bakes the scene in the browser now, or waits for the bake already asked for, and shows it. Gives how long it took; a bake that fails is a `bake_failed` error with the bake's message. + +No arguments. + +### `bake.status` + +Whether a bake is running or waiting to, and how the last one went. + +No arguments. + ### `gizmo.set` The gizmo: its mode (move, rotate, scale), its space (world, local), and snapping (on, and its steps). diff --git a/docs/editor.md b/docs/editor.md index 23e7b19..2032b71 100644 --- a/docs/editor.md +++ b/docs/editor.md @@ -548,15 +548,33 @@ on a phone. **Milestone**: Claude, through Claude in Chrome, opens the editor on Cove, lists the courtyard's entities, moves the statue, reads frame stats and undoes, while you watch. -2. **A new scene from blank terrain.** File ▸ New makes a project from a template: terrain, - sea, sky and a camera. The bake runs in the browser: `solstice-bake` as a library, in a Web - Worker, for the terrain, its land bands, the sea and the meadow. The terrain's and the - land's settings are edited in the details panel and rebuilt as you change them. The project - is kept in the browser or in a folder on disk, and Bake writes the scene file the viewer - plays, the same file `bake --project` writes natively. Commands: `project.new`, `bake.run`, - `bake.status`, `terrain.set`, `env.set` saved with the scene. - **Milestone**: from an empty page, make an island with sand, grass and sea, save it, reopen - it, and open it in the viewer; an agent does the same through the API. +2. **A new scene from blank terrain.** New makes a scene from a template: terrain, land bands + and a coastal sea, seen from offshore. With nothing selected, the details panel shows the + scene's own settings (terrain, land, sea), and a change bakes the scene again in the + browser and shows it, keeping the camera and the selection. The scene is kept in the browser + as it changes (`editor.html?draft=NAME`), and saved to a file as any other. Commands: + `project.new`, `scene.settings`, `scene.set`, `bake.run`, `bake.status`. + + How the bake runs in the browser: the scene building moved out of `apps/bake` into the + `solstice-bake` library, built for wasm as `apps/bake-web` (7.7 MB, fetched only when the + editor first bakes) and run in a Web Worker. What it measured, one thread: + + | | Native | Browser | + |---|---|---| + | The three land surfaces at 1024², generated | 2.5 min | far longer | + | A 400 m terrain of 512 cells (524 k triangles), clusters built | 45 s | — | + | A 256 m terrain of 128 cells, with land and sea, from ready-made surfaces | — | 1.7–2.5 s | + | The same, 256 cells | — | 15 s | + + So the surfaces, which are the same in every scene, are made once by `bake --surfaces` (in + the site's build) and fetched ready-made, 4.2 MB each, as ordinary scene files of one + compressed layer (`solstice_bake::textures`). A new scene's terrain is 2 m cells, which bakes + in about two seconds; finer terrain waits on a faster cluster builder or on baking the + terrain in tiles across workers. A scene baked in the browser is byte for byte the one + `bake --project` makes of the same project natively (checked: the same SHA-256). + + **Milestone**: from an empty page, make an island with sand, grass and sea, change it, reload + and find it kept; an agent does the same through the API. 3. **Trees, water and weather.** Woods, meadows and clearings as shapes with handles, planted and regrown in the worker, area by area; tree species and grass from the built-in ones and from species files, edited in a panel; ponds with handles; the scene's weather and time of diff --git a/scripts/build-web.sh b/scripts/build-web.sh index 85110a8..8ef393d 100644 --- a/scripts/build-web.sh +++ b/scripts/build-web.sh @@ -31,6 +31,16 @@ RUSTFLAGS="-C target-feature=+simd128" cargo build --release --target wasm32-u # crate builds again. cargo build --release --target wasm32-unknown-unknown -p viewer --lib --no-default-features --features editor "$bindgen" --target web --no-typescript --out-dir dist/editor-wasm target/wasm32-unknown-unknown/release/solstice_viewer.wasm +# The editor's bake, built for the browser (apps/bake-web), which its Web Worker loads only when +# it first bakes; and the surfaces a land is laid with, which take minutes to make, made here once +# for every visitor (`bake --surfaces`). A local build reuses any already in assets/content. +cargo build --release --target wasm32-unknown-unknown -p bake-web +"$bindgen" --target web --no-typescript --out-dir dist/bake-wasm target/wasm32-unknown-unknown/release/solstice_bake_web.wasm +if [ ! -e assets/content/surface-grass-0-1024.solstice ]; then + cargo run --release -p bake -- --surfaces assets/content +fi +mkdir -p dist/content +cp assets/content/*.solstice dist/content/ cp apps/viewer/web/index.html apps/viewer/web/editor.html apps/viewer/web/loader.js dist/ cp -r apps/viewer/web/editor dist/ # Scene files are already compressed where it helps, part by part, so that the page can fetch diff --git a/scripts/check-changed.sh b/scripts/check-changed.sh index 4534a69..544231d 100755 --- a/scripts/check-changed.sh +++ b/scripts/check-changed.sh @@ -35,7 +35,7 @@ case "$result" in web=false for name in $result; do packages+=(-p "$name") - [[ "$name" == viewer ]] && web=true + [[ "$name" == viewer || "$name" == bake-web ]] && web=true done ;; esac @@ -48,5 +48,11 @@ if [[ "$web" == true ]]; then cargo clippy --release --target wasm32-unknown-unknown -p viewer --lib --no-default-features -- -D warnings cargo build --release --target wasm32-unknown-unknown -p viewer --lib cargo clippy --release --target wasm32-unknown-unknown -p viewer --lib -- -D warnings + # The editor's own build, and its bake for the browser (docs/editor.md). + 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 + cargo build --release --target wasm32-unknown-unknown -p bake-web + cargo clippy --release --target wasm32-unknown-unknown -p bake-web -- -D warnings + node apps/viewer/web/editor/test.mjs fi echo "All checks passed for: $result" From 6960de42e98d597248a375528b4e49d759d1c29e Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:30:22 +1100 Subject: [PATCH 2/2] Editor: never left without a scene, and New waits its turn From the review: putting a newly baked scene in place no longer leaves the editor without one if it can't be opened (the last scene comes back); commands wait while a scene is put in place, and the viewport's handlers do nothing then; New waits for a bake in flight and never shows it, refuses to replace a scene kept in the browser or to drop unsaved changes unless told to, and writes the scene it leaves first; a bake that finishes after a change is baked again rather than shown out of date; picks waiting on a replaced viewer are answered; the sea takes only a built-in ocean; a worker whose start failed tries again; scene.set is refused where the browser can't bake the scene; and bake.status no longer says a bake is waiting after it ran. Co-Authored-By: Claude Opus 5.5 --- apps/viewer/web/editor/bake-worker.js | 6 +- apps/viewer/web/editor/commands.js | 2 + apps/viewer/web/editor/main.js | 142 +++++++++++++++++++++----- apps/viewer/web/editor/panels.js | 5 +- apps/viewer/web/editor/scenes.js | 6 +- 5 files changed, 127 insertions(+), 34 deletions(-) diff --git a/apps/viewer/web/editor/bake-worker.js b/apps/viewer/web/editor/bake-worker.js index b5bebae..a717ec9 100644 --- a/apps/viewer/web/editor/bake-worker.js +++ b/apps/viewer/web/editor/bake-worker.js @@ -28,7 +28,11 @@ async function provideSurfaces(content, size) { onmessage = async ({ data: { id, scene, name, content } }) => { try { - ready ??= init(); + // A start that failed, such as a fetch that didn't arrive, is tried again next time. + ready ??= init().catch((error) => { + ready = null; + throw error; + }); await ready; const size = JSON.parse(scene).texture_size ?? 1024; await provideSurfaces(content, size); diff --git a/apps/viewer/web/editor/commands.js b/apps/viewer/web/editor/commands.js index 07377a6..042f291 100644 --- a/apps/viewer/web/editor/commands.js +++ b/apps/viewer/web/editor/commands.js @@ -147,6 +147,8 @@ export class Commands { `no command \`${name}\`; solstice.help() lists them`); } const checked = check(name, command.args ?? {}, args ?? {}); + // While a newly baked scene is put in place there is no viewer to act on: wait for it. + await this.context.whenReady?.(); const outcome = await command.run(checked, this.context, by); const { step, result } = outcome && "step" in outcome ? outcome : { step: null, result: outcome }; if (step) this.history.push({ ...step, by }); diff --git a/apps/viewer/web/editor/main.js b/apps/viewer/web/editor/main.js index 1122fae..f245c65 100644 --- a/apps/viewer/web/editor/main.js +++ b/apps/viewer/web/editor/main.js @@ -97,10 +97,11 @@ async function main() { } el("loading-text").textContent = "Baking the scene…"; const baked = await baker.bake(writeScene(scene), `${sceneName}.scene`); - opened = await openViewer({ bytes: baked.bytes }); + opened = { ...await openViewer({ bytes: baked.bytes }), source: { bytes: baked.bytes } }; log.add("info", `Baked ${sceneName} in ${(baked.ms / 1000).toFixed(1)} s.`); } else { - opened = await openViewer({ url: `${sceneBase}${sceneName}.solstice` }); + const url = `${sceneBase}${sceneName}.solstice`; + opened = { ...await openViewer({ url }), source: { url } }; const projectUrl = params.get("project") ?? `${sceneBase}${sceneName}.scene`; try { const response = await fetch(projectUrl); @@ -114,6 +115,7 @@ async function main() { if (scene && opened.viewer.source_ids() === "" && project.entities.size) { log.add("warn", "This scene was baked without the map of its entities (bake it with --project): nothing can be picked or moved."); } + editor.source = opened.source; editor.start(opened.viewer, opened.streamer, opened.file, project, fitCanvas); // A new scene is kept in this browser from the start, and a reload opens it again. if (params.has("new")) { @@ -193,10 +195,35 @@ const editor = { async swap(bytes, scene) { const camera = Array.from(this.viewer.camera()); const old = this.viewer; - // No frame is drawn while there is no viewer. + // One canvas holds one viewer: the old one goes first, and no frame is drawn, nor command + // run, until the new one is there (`whenReady`). + let ready; + this.ready = new Promise((resolve) => { ready = resolve; }); this.viewer = null; + // A drag or a scrub in progress belonged to the scene being left. + this.previews.clear(); + // Picks waiting on the old viewer will never be answered by it. + for (const pick of this.picks) pick.resolve({ id: null, instance: null, point: null }); + this.picks = []; + this.pickStale = false; old.free(); - const opened = await openViewer({ bytes }); + let opened; + try { + opened = await openViewer({ bytes }); + this.source = { bytes }; + } catch (error) { + // The new scene can't be shown: show the last one again, so the editor goes on working. + opened = await openViewer(this.source); + scene = this.project.scene; + this.viewer = opened.viewer; + this.streamer = opened.streamer; + this.file = opened.file; + this.viewer.set_camera(...camera); + this.project = new Project(scene, this.viewer); + ready(); + this.refresh(); + throw error; + } this.viewer = opened.viewer; this.streamer = opened.streamer; this.file = opened.file; @@ -206,37 +233,53 @@ const editor = { for (const id of this.hidden) { for (const instance of this.project.instancesOf(id)) this.viewer.set_instance_hidden(instance, true); } - this.picks = []; + ready(); this.refresh(); }, + /// Resolves once a viewer is shown: at once, but for while a new scene is put in place. + whenReady() { + return this.viewer ? Promise.resolve() : this.ready; + }, + /// The bake: what is in flight, whether another is wanted after it, and how the last went. - bake: { running: null, again: false, timer: null, lastMs: null, lastError: null }, + bake: { running: null, again: false, timer: null, lastMs: null, lastError: null, generation: 0 }, /// Bakes the scene again soon, after a change to what only a bake makes, such as the terrain. /// Changes close together bake once. rebakeSoon() { clearTimeout(this.bake.timer); - this.bake.timer = setTimeout(() => this.rebake(), 400); + this.bake.timer = setTimeout(() => { + this.bake.timer = null; + this.rebake(); + }, 400); el("hint").textContent = "Baking soon…"; }, /// Bakes the scene as it is now and shows it. Resolves when what was asked for is shown. async rebake() { clearTimeout(this.bake.timer); + this.bake.timer = null; if (this.bake.running) { this.bake.again = true; await this.bake.running; return this.bake.running; } + const generation = this.bake.generation; const run = (async () => { do { this.bake.again = false; el("hint").textContent = "Baking…"; - const scene = structuredClone(this.project.scene); + const text = writeScene(this.project.scene); try { - const baked = await baker.bake(writeScene(scene), `${sceneName}.scene`); - if (this.bake.again) continue; + const baked = await baker.bake(text, `${sceneName}.scene`); + // New, while it baked: this scene is gone. + if (generation !== this.bake.generation) return; + // Changed while it baked: what was baked is out of date, so bake again. + if (this.bake.again || writeScene(this.project.scene) !== text) { + this.bake.again = true; + continue; + } await this.swap(baked.bytes, this.project.scene); this.bake.lastMs = baked.ms; this.bake.lastError = null; @@ -854,6 +897,12 @@ function defineCommands(commands, editor) { example: { terrain: { size: 256, cells: 128, height: 6, shore_x: 40 } }, run: (args) => { if (!editor.project.saveable) throw new CommandError("not_a_project", "this scene has no project to change"); + // Models and actors are read from files, which the bake in the browser can't read yet. + const placed = [...editor.project.entities.values()].find((e) => e.mesh || e.actor); + if (placed) { + throw new CommandError("not_bakeable_here", + `this scene places ${placed.name} from a file, which the browser can't bake yet: change it in the project and bake it with bake --project`); + } if (!Object.keys(args).length) throw new CommandError("bad_arguments", "scene.set takes terrain, land or sea"); for (const [name, value] of Object.entries(args)) { const problem = checkSetting(name, value); @@ -895,10 +944,28 @@ function defineCommands(commands, editor) { }); commands.define("project.new", { description: "Starts a new scene from blank terrain: an island of sand, dirt and grass in a coastal sea, baked in the browser and kept there as it changes. The scene shown now is left; save it first if it has changes.", - args: { name: { type: "string", description: "Its name: letters, digits, - and _ (default untitled)." } }, + args: { + name: { type: "string", description: "Its name: letters, digits, - and _ (default untitled)." }, + replace: { type: "boolean", description: "Replace a scene of that name kept in this browser. Without it, an existing one is refused." }, + discard: { type: "boolean", description: "Leave the scene shown now even though it has unsaved changes. Without it, they stop New." }, + }, example: { name: "island" }, - run: async ({ name = "untitled" }) => { + run: async ({ name = "untitled", replace = false, discard = false }) => { if (!/^[\w-]+$/.test(name)) throw new CommandError("bad_arguments", "a name is letters, digits, - and _"); + // A scene kept in the browser is its own save; a served one with changes isn't saved. + const unsaved = editor.dirty && !params.has("draft") && !location.search.includes("draft="); + if (unsaved && !discard) { + throw new CommandError("unsaved_changes", "the scene shown has unsaved changes: save it, or pass discard: true"); + } + if (!replace && name !== sceneName && await readDraft(name)) { + throw new CommandError("exists", `a scene called ${name} is kept in this browser already: pass replace: true, or choose another name`); + } + // Whatever was baking belongs to the scene being left. + clearTimeout(editor.bake.timer); + editor.bake.timer = null; + editor.bake.generation += 1; + await editor.bake.running; + await flushAutosave(editor); const scene = blankScene(); const baked = await baker.bake(writeScene(scene), `${name}.scene`); sceneName = name; @@ -974,6 +1041,7 @@ function bindInput(editor) { overlay.addEventListener("contextmenu", (e) => e.preventDefault()); overlay.addEventListener("pointerdown", (e) => { + if (!editor.viewer) return; overlay.focus(); const m = fraction(e); const camera = editor.camera(); @@ -1009,6 +1077,7 @@ function bindInput(editor) { drag = { kind: "click", start: m, additive: e.ctrlKey || e.metaKey || e.shiftKey }; }); overlay.addEventListener("pointermove", (e) => { + if (!editor.viewer) return; const m = fraction(e); if (!drag) { const primary = editor.primary(); @@ -1055,7 +1124,7 @@ function bindInput(editor) { const was = drag; drag = null; input.flying = false; - if (!was) return; + if (!was || !editor.viewer) return; if (was.kind === "gizmo") { const changes = new Map(); if (was.last) { @@ -1093,6 +1162,7 @@ function bindInput(editor) { overlay.addEventListener("pointercancel", () => finish(null)); overlay.addEventListener("wheel", (e) => { e.preventDefault(); + if (!editor.viewer) return; if (input.flying) { // As in Unreal: the wheel while looking sets how fast the camera flies. const speed = editor.viewer.camera_speed() * (e.deltaY < 0 ? 1.25 : 0.8); @@ -1141,7 +1211,8 @@ function bindInput(editor) { const [width, height] = editor.resize(); if (width !== canvas.width || height !== canvas.height) { [canvas.width, canvas.height] = [width, height]; - editor.viewer.resize(width, height); + // A viewer being put in place is opened at the canvas's size then. + editor.viewer?.resize(width, height); } }).observe(canvas); } @@ -1172,9 +1243,13 @@ function bindToolbar(editor) { el("redo").addEventListener("click", () => editor.call("history.redo")); el("save").addEventListener("click", () => save(editor)); el("new").addEventListener("click", async () => { - if (editor.dirty && !params.has("draft") && !confirm("Leave this scene's unsaved changes for a new scene?")) return; + const kept = location.search.includes("draft="); + if (editor.dirty && !kept && !confirm("Leave this scene's unsaved changes for a new scene?")) return; el("hint").textContent = "Baking a new scene…"; - const outcome = await editor.call("project.new", {}); + // A new scene takes the first free name: untitled, untitled-2... + let name = "untitled"; + for (let n = 2; name !== sceneName && await readDraft(name); n += 1) name = `untitled-${n}`; + const outcome = await editor.call("project.new", { name, discard: true }); if (!outcome.ok) log.add("error", outcome.error.message); }); for (const mode of ["move", "rotate", "scale"]) { @@ -1211,6 +1286,14 @@ async function readDraft(name) { } } +/// Writes any autosave still waiting, before the scene it belongs to is left. +async function flushAutosave(editor) { + if (!autosaveTimer) return; + clearTimeout(autosaveTimer); + autosaveTimer = null; + await writeDraft(editor); +} + /// Keeps the scene in this browser now, as autosave does after each change. function keepAsDraft(editor) { editor.dirty = true; @@ -1218,20 +1301,25 @@ function keepAsDraft(editor) { } let autosaveTimer = null; + +async function writeDraft(editor) { + try { + const folder = await draftDirectory(); + if (!folder) return; + const writable = await (await folder.getFileHandle(draftFile(), { create: true })).createWritable(); + await writable.write(editor.project.toJson()); + await writable.close(); + el("hint").textContent = "Draft kept in this browser"; + } catch (error) { + log.add("warn", `Couldn't keep a draft in this browser: ${error.message ?? error}`); + } +} function scheduleAutosave(editor) { if (!editor.project.saveable) return; clearTimeout(autosaveTimer); - autosaveTimer = setTimeout(async () => { - try { - const folder = await draftDirectory(); - if (!folder) return; - const writable = await (await folder.getFileHandle(draftFile(), { create: true })).createWritable(); - await writable.write(editor.project.toJson()); - await writable.close(); - el("hint").textContent = "Draft kept in this browser"; - } catch (error) { - log.add("warn", `Couldn't keep a draft in this browser: ${error.message ?? error}`); - } + autosaveTimer = setTimeout(() => { + autosaveTimer = null; + writeDraft(editor); }, 800); } diff --git a/apps/viewer/web/editor/panels.js b/apps/viewer/web/editor/panels.js index b96025c..8691a26 100644 --- a/apps/viewer/web/editor/panels.js +++ b/apps/viewer/web/editor/panels.js @@ -179,10 +179,9 @@ export class Details { for (const [key, field] of Object.entries(setting.fields)) { const set = (v) => this.editor.call("scene.set", { [name]: { ...value, [key]: v } }); let input; - if (field.text) { - input = make("input", { type: "text", value: value[key] }); + if (field.names) { + input = make("select", {}, field.names.map((n) => make("option", { value: n, textContent: n, selected: n === value[key] }))); input.addEventListener("change", () => set(input.value)); - input.addEventListener("keydown", (e) => e.stopPropagation()); } else { input = numberField("·", value[key], field.step, (v) => { const clamped = Math.min(field.max, Math.max(field.min, field.integer ? Math.round(v) : v)); diff --git a/apps/viewer/web/editor/scenes.js b/apps/viewer/web/editor/scenes.js index 7cd8b39..50c6a04 100644 --- a/apps/viewer/web/editor/scenes.js +++ b/apps/viewer/web/editor/scenes.js @@ -35,7 +35,7 @@ export const SETTINGS = { }, sea: { description: "An open sea: its calm level, and a built-in ocean's name.", - fields: { level: { min: -100, max: 100, step: 0.1 }, ocean: { text: true } }, + fields: { level: { min: -100, max: 100, step: 0.1 }, ocean: { names: ["coastal", "clear", "ocean", "murky"] } }, }, }; @@ -52,8 +52,8 @@ export function checkSetting(name, value) { for (const [key, field] of Object.entries(setting.fields)) { const v = value[key]; if (v === undefined) return `\`${name}\` needs \`${key}\``; - if (field.text) { - if (typeof v !== "string" || !/^[\w.-]+$/.test(v)) return `\`${name}.${key}\` is a name with no spaces`; + if (field.names) { + if (!field.names.includes(v)) return `\`${name}.${key}\` is one of ${field.names.join(", ")}`; continue; } if (typeof v !== "number" || !Number.isFinite(v) || v < field.min || v > field.max) {