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 @@ -62,3 +62,8 @@ jobs:
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
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: Test the editor's page
if: steps.affected.outputs.web == 'true'
run: node apps/viewer/web/editor/test.mjs
11 changes: 10 additions & 1 deletion apps/bake/src/scene_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,14 +1175,21 @@ pub(crate) fn build_commands(
scene.collide[instance as usize] = *how;
}
}
for (range, owner) in ranges
let placements = &models[model];
let placed = placements
.whole
.iter()
.chain(placements.parts.iter().map(|(_, at)| at));
for ((range, owner), placement) in ranges
.iter()
.zip(collide.whole_owners.iter().chain(&collide.part_owners))
.zip(placed)
{
if let Some(id) = owner {
scene.sources.push(Source {
id: id.clone(),
instances: range.clone(),
placement: *placement,
});
}
}
Expand Down Expand Up @@ -1210,6 +1217,7 @@ pub(crate) fn build_commands(
scene.sources.push(Source {
id: CIRCUIT_SOURCE.to_owned(),
instances: first..scene.instances.len() as u32,
placement: Mat4::IDENTITY,
});
}
scene.ground = Some(built.ground);
Expand Down Expand Up @@ -1242,6 +1250,7 @@ pub(crate) fn build_commands(
scene.sources.push(Source {
id: TERRAIN_SOURCE.to_owned(),
instances: first..scene.instances.len() as u32,
placement: Mat4::IDENTITY,
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions apps/viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ required-features = ["gameplay"]
[features]
default = ["gameplay"]
gameplay = ["dep:solstice-game"]
# The editor's page (docs/editor.md): picking, moving instances and the map of what made them.
# Only `editor.html` loads a build with it.
editor = []

[dependencies]
solstice-asset.workspace = true
Expand Down
123 changes: 123 additions & 0 deletions apps/viewer/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub struct WebViewer {
/// What scripts logged, and errors, for the page to take.
#[cfg(feature = "gameplay")]
messages: Vec<crate::play::Message>,
/// Which entity of the scene's project made which instances, for the editor.
#[cfg(feature = "editor")]
sources: Vec<solstice_asset::Source>,
}

/// How far around the camera a scene read in parts loads what isn't in view, in metres.
Expand Down Expand Up @@ -209,6 +212,8 @@ impl WebViewer {
play: None,
#[cfg(feature = "gameplay")]
messages: Vec::new(),
#[cfg(feature = "editor")]
sources: scene.sources,
})
}

Expand Down Expand Up @@ -917,3 +922,121 @@ pub fn read_header(bytes: &[u8]) -> Result<Vec<f64>, JsValue> {
header.manifest_bytes as f64,
])
}

/// What the editor's page needs (`docs/editor.md`): the map of which entity made which
/// instances, moving and hiding instances, the camera's matrices, and picking.
#[cfg(feature = "editor")]
#[wasm_bindgen]
impl WebViewer {
/// The ids of the entities the scene's map names, one to a line, in its order. Empty for a
/// scene not baked from a project.
pub fn source_ids(&self) -> String {
let ids: Vec<&str> = self.sources.iter().map(|s| s.id.as_str()).collect();
ids.join("\n")
}

/// Each mapped entity's instances, as its first and one past its last, in the map's order.
pub fn source_ranges(&self) -> Vec<u32> {
self.sources
.iter()
.flat_map(|s| [s.instances.start, s.instances.end])
.collect()
}

/// Each mapped entity's placement as the bake made it, 16 numbers by column, in the map's
/// order.
pub fn source_placements(&self) -> Vec<f32> {
self.sources
.iter()
.flat_map(|s| s.placement.to_cols_array())
.collect()
}

pub fn instance_count(&self) -> u32 {
self.renderer.instance_count()
}

/// Where an instance is drawn, 16 numbers by column; empty if there is no such instance.
pub fn instance_transform(&self, instance: u32) -> Vec<f32> {
self.renderer
.instance_transform(instance)
.map(|m| m.to_cols_array().to_vec())
.unwrap_or_default()
}

/// An instance's bounding sphere in the world: centre x, y, z and radius; empty if none.
pub fn instance_bounds(&self, instance: u32) -> Vec<f32> {
self.renderer
.instance_bounds(instance)
.map(|(c, r)| vec![c.x, c.y, c.z, r])
.unwrap_or_default()
}

/// Puts an instance at `world` (16 numbers by column) from the next frame. `jump` puts it
/// there with no motion between, as an undo should; otherwise it moves there, as a drag does.
/// False if there is no such instance or the matrix can't be inverted.
pub fn set_instance_transform(&mut self, instance: u32, world: &[f32], jump: bool) -> bool {
let Ok(columns) = <[f32; 16]>::try_from(world) else {
return false;
};
let world = glam::Mat4::from_cols_array(&columns);
if !world.is_finite() || world.determinant().abs() < 1e-12 {
return false;
}
if jump {
self.renderer.place_instance(instance, world)
} else {
self.renderer.move_instance(instance, world)
}
}

/// Makes an instance movable while it is dragged, and static again after, so the cached
/// shadows and GI are redrawn once at the end rather than every frame of the drag.
pub fn set_instance_movable(&mut self, instance: u32, movable: bool) -> bool {
self.renderer.set_instance_movable(instance, movable)
}

pub fn set_instance_hidden(&mut self, instance: u32, hidden: bool) -> bool {
self.renderer.set_instance_hidden(instance, hidden)
}

/// The camera's view and projection, 16 numbers by column each, for the page to draw the
/// gizmo over the scene and turn the mouse into rays. The projection has reversed depth and
/// no far plane.
pub fn view_projection(&self) -> Vec<f32> {
let camera = &self.camera.camera;
let aspect = self.config.width as f32 / self.config.height.max(1) as f32;
let mut out = camera.view().to_cols_array().to_vec();
out.extend(camera.projection(aspect).to_cols_array());
out
}

/// How fast the camera flies, in metres a second (Shift goes five times faster).
pub fn set_camera_speed(&mut self, metres_per_second: f32) {
if metres_per_second.is_finite() && metres_per_second > 0.0 {
self.camera.speed = metres_per_second;
}
}

pub fn camera_speed(&self) -> f32 {
self.camera.speed
}

/// Asks what is under a point of the canvas, as fractions of its width and height from the
/// top left. `take_pick` gives the answer a frame or two later.
pub fn request_pick(&mut self, x: f32, y: f32) {
self.renderer.request_pick(x, y);
}

/// The last pick's answer, once: the instance (or -1 for the sky) and its reversed depth.
/// Empty while it isn't ready.
pub fn take_pick(&mut self) -> Vec<f64> {
match self.renderer.take_pick() {
None => Vec::new(),
Some(picked) => vec![
picked.instance.map_or(-1.0, f64::from),
f64::from(picked.depth),
],
}
}
}
146 changes: 146 additions & 0 deletions apps/viewer/web/editor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Solstice Editor</title>
<meta name="description" content="Build scenes for Solstice in the browser: select, move, turn and scale what is in them, with undo. Agents can drive it through window.solstice.">
<style>
:root {
color-scheme: dark;
--bg: #15171a;
--panel: #1d2024;
--panel-2: #24282d;
--line: #31363c;
--text: #d9dde2;
--muted: #8b939c;
--accent: #3e8ef7;
--select: #ff9f1a;
--x: #e5484d;
--y: #46a758;
--z: #3e63dd;
font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
body {
display: grid;
grid-template: "bar bar bar" auto "left view right" 1fr "left log right" 132px "status status status" auto / 260px 1fr 320px;
}
#bar {
grid-area: bar; display: flex; gap: 6px; align-items: center; padding: 6px 10px;
background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
#bar .brand { font-weight: 600; margin-right: 8px; }
#bar .group { display: flex; gap: 2px; padding: 2px; background: var(--panel-2); border-radius: 6px; }
#bar button, .tool {
border: 0; background: transparent; padding: 5px 9px; border-radius: 4px; cursor: pointer;
}
#bar button:hover:not(:disabled) { background: #333a42; }
#bar button:disabled { opacity: 0.4; cursor: default; }
#bar button[aria-pressed="true"] { background: var(--accent); color: white; }
#bar label { display: flex; align-items: center; gap: 4px; color: var(--muted); }
#bar input[type="number"] { width: 56px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 3px 5px; }
#bar .spacer { flex: 1; }
#bar a { color: var(--muted); text-decoration: none; padding: 5px 9px; }
#bar a:hover { color: var(--text); }
aside { background: var(--panel); overflow: auto; min-height: 0; }
#left { grid-area: left; border-right: 1px solid var(--line); display: flex; flex-direction: column; }
#right { grid-area: right; border-left: 1px solid var(--line); }
aside h2 {
margin: 0; padding: 8px 10px; font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase;
color: var(--muted); border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1;
}
#outliner { display: flex; flex-direction: column; min-height: 0; flex: 1; }
#outliner .search { margin: 8px; padding: 5px 8px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; }
#outliner .tree { overflow: auto; flex: 1; padding-bottom: 8px; }
.row { display: flex; align-items: center; gap: 5px; height: 24px; padding-right: 4px; cursor: default; white-space: nowrap; }
.row:hover { background: #262b31; }
.row.selected { background: #2b4a75; }
.row .twisty { width: 12px; color: var(--muted); cursor: pointer; text-align: center; flex: none; }
.row .icon { width: 14px; text-align: center; flex: none; color: var(--muted); }
.row .icon.mesh { color: #9fb7d8; } .row .icon.actor { color: #e2a65c; } .row .icon.light { color: #ffd60a; }
.row .icon.wood { color: var(--y); } .row .icon.pond { color: #5cc8e2; }
.row .label { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.row .eye { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 0 4px; opacity: 0; }
.row:hover .eye, .row .eye.off { opacity: 1; }
#details { padding-bottom: 12px; }
#details details { border-bottom: 1px solid var(--line); }
#details summary { padding: 7px 10px; cursor: pointer; font-weight: 600; background: var(--panel-2); }
#details .field { display: grid; grid-template-columns: 78px 1fr; gap: 6px; align-items: center; padding: 4px 10px; }
#details .name { color: var(--muted); }
#details input[type="text"], #details textarea {
width: 100%; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 3px 6px; resize: vertical;
}
#details input[readonly] { color: var(--muted); }
#details .vector { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
#details .number { display: flex; align-items: stretch; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
#details .number input { width: 100%; min-width: 0; border: 0; background: transparent; padding: 3px 4px; }
#details .axis { width: 16px; display: grid; place-items: center; font-size: 10px; font-weight: 700; color: white; cursor: ew-resize; user-select: none; flex: none; }
#details .axis.x { background: var(--x); } #details .axis.y { background: var(--y); } #details .axis.z { background: var(--z); }
#details .axis.s { background: #6b7280; }
#details .value { color: var(--muted); overflow-wrap: anywhere; }
#details .hint, #details .empty { color: var(--muted); }
#details .empty { padding: 10px; }
main { grid-area: view; position: relative; min-width: 0; min-height: 0; background: black; }
#view, #overlay-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#overlay-canvas { outline: none; touch-action: none; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; background: var(--bg); }
#loading-text.error { color: #ff8a8a; max-width: 60ch; }
#draft {
position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: var(--panel-2);
border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; display: flex; gap: 8px; align-items: center;
}
#draft button { border: 1px solid var(--line); background: var(--panel); border-radius: 4px; padding: 3px 8px; cursor: pointer; }
#log { grid-area: log; overflow: auto; background: #111316; border-top: 1px solid var(--line); padding: 4px 8px; font: 12px/1.5 ui-monospace, Consolas, monospace; }
.log-warn { color: #e2c05c; } .log-error { color: #ff8a8a; } .log-info { color: var(--muted); }
#status { grid-area: status; display: flex; gap: 16px; padding: 4px 10px; background: var(--panel); border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
#status #selected { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40%; }
#help { position: absolute; right: 10px; bottom: 10px; color: rgba(255, 255, 255, 0.55); font-size: 11px; text-align: right; pointer-events: none; text-shadow: 0 1px 2px black; }
@media (max-width: 900px) {
body { grid-template: "bar" auto "view" 1fr "left" 30vh "right" 30vh "status" auto / 100%; overflow: auto; }
#log, #help { display: none; }
#left, #right { border: 0; border-top: 1px solid var(--line); }
}
</style>
</head>
<body>
<header id="bar">
<span class="brand">Solstice Editor</span>
<div class="group">
<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>
</div>
<div class="group" role="group" aria-label="Gizmo">
<button id="mode-move" type="button" title="Move (W)">Move</button>
<button id="mode-rotate" type="button" title="Rotate (E)">Rotate</button>
<button id="mode-scale" type="button" title="Scale (R)">Scale</button>
</div>
<div class="group">
<button id="space" type="button" title="Handles in the world's axes or the object's">World</button>
<button id="snap" type="button" title="Snap while dragging (hold Ctrl to do the opposite)">Snap</button>
</div>
<label>Grid <input id="snap-move" type="number" min="0.01" step="0.05" title="Metres a move snaps to"></label>
<label>Angle <input id="snap-angle" type="number" min="1" step="1" title="Degrees a turn snaps to"></label>
<div class="group"><button id="frame" type="button" title="Frame the selection (F)">Frame</button></div>
<span class="spacer"></span>
<a id="view-scene" href="./">View scene</a>
</header>
<aside id="left"><h2>Outliner</h2><div id="outliner"></div></aside>
<main>
<canvas id="view"></canvas>
<canvas id="overlay-canvas" tabindex="0" aria-label="Viewport: click to select, drag the handles to move"></canvas>
<div id="help">Click select · W E R move/rotate/scale · F frame · Right-drag + WASD fly · Alt-drag orbit · Middle-drag pan · Wheel zoom</div>
<div id="draft" hidden>Unsaved edits from an earlier visit.
<button id="draft-restore" type="button">Restore</button><button id="draft-discard" type="button">Discard</button></div>
<div id="loading"><div id="loading-text">Starting…</div></div>
</main>
<aside id="right"><h2>Details</h2><div id="details"></div></aside>
<div id="log" role="log" aria-label="Output log"></div>
<footer id="status"><span id="selected">Nothing selected</span><span id="fps"></span><span id="hint"></span></footer>
<script type="module" src="./editor/main.js"></script>
</body>
</html>
Loading
Loading