Start desktop and browser sessions with an empty graph (#29)

This commit is contained in:
Zachary Aflalo authored and GitHub committed 2026-11-04 06:17:40 +01:00
1 parent f1bb82e8e0
commit c97f92abff
5 files changed
+75 -15

No files matched your search

+2 -2
View File
@@ -131,7 +131,7 @@ ray-traced ambient occlusion and a shadowed key light).
git clone https://github.com/storytold/materialcraft
cd materialcraft
cargo run --release -p materialcraft # desktop app (opens the brick wall sample)
cargo run --release -p materialcraft # desktop app (opens an empty graph)
cargo run --release -p materialcraft -- package.mcraft # open a package
cargo run --release -p materialcraft -- --control 7878 # + JSON control channel
cargo run --release -p materialcraft-cli -- mcp # MCP server (stdio)
@@ -144,7 +144,7 @@ cd apps/materialcraft-web && trunk serve # web build
cargo xtask ci # fmt, clippy, tests, layering, wasm
```
You need Rust 1.95 or newer. The app opens with the brick wall sample; **File ▸ New ▸ Graph...**
You need Rust 1.95 or newer. The app opens with an empty graph in a new package; **File ▸ New ▸ Graph...**
starts from a PBR template or one of the sample materials.
### Use it from Claude Code and other agents
+2 -2
View File
@@ -38,8 +38,8 @@ pub fn start() {
async fn run(canvas: web_sys::HtmlCanvasElement) -> Result<(), String> {
// The WebAssembly has downloaded and compiled when this runs.
step(0.5, "Preparing the sample package…").await;
let session = materialcraft_engine::sample_session();
step(0.5, "Preparing a new package…").await;
let session = materialcraft_engine::new_session();
step(0.8, "Starting the renderer…").await;
let mut options = eframe::WebOptions::default();
if query().contains("webgl")
+35 -10
View File
@@ -26,11 +26,10 @@ fn version() -> String {
struct Args {
file: Option<String>,
control: Option<u16>,
empty: bool,
}
fn parse_args() -> Result<Option<Args>, String> {
let mut args = Args { file: None, control: None, empty: false };
let mut args = Args { file: None, control: None };
let mut it = std::env::args().skip(1);
while let Some(a) = it.next() {
match a.as_str() {
@@ -46,7 +45,8 @@ fn parse_args() -> Result<Option<Args>, String> {
let p = it.next().ok_or("--control needs a port")?;
args.control = Some(p.parse().map_err(|_| format!("invalid port `{p}`"))?);
}
"--empty" => args.empty = true,
// Kept for scripts written before empty startup became the default.
"--empty" => {}
f if !f.starts_with('-') => args.file = Some(f.to_string()),
other => return Err(format!("unknown option `{other}`")),
}
@@ -55,13 +55,7 @@ fn parse_args() -> Result<Option<Args>, String> {
}
fn initial_session(args: &Args) -> Session {
let mut s = if args.empty {
let mut pkg = materialcraft_engine::core::Package::new("Untitled package");
pkg.graphs.push(materialcraft_engine::samples::template("pbr", "New_graph", 10));
Session::new(pkg)
} else {
materialcraft_engine::sample_session()
};
let mut s = materialcraft_engine::new_session();
if let Some(f) = &args.file {
match std::fs::read(f)
.map_err(|e| e.to_string())
@@ -121,3 +115,34 @@ fn main() {
std::process::exit(1);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn startup_without_a_file_has_one_empty_graph() {
let s = initial_session(&Args { file: None, control: None });
assert_eq!(s.pkg.name, "Untitled package");
assert_eq!(s.pkg.graphs.len(), 1);
let g = s.graph().unwrap();
assert_eq!(g.name, "New_graph");
assert!(g.nodes.is_empty());
assert!(g.connections.is_empty());
assert!(s.path.is_none());
assert!(!s.modified);
assert!(s.history().is_empty());
}
#[test]
fn startup_opens_the_requested_package() {
let path = std::env::temp_dir().join(format!("mc-startup-{}.mcraft", std::process::id()));
let original = materialcraft_engine::sample_session();
std::fs::write(&path, materialcraft_engine::file_cmds::save_bytes(&original).unwrap()).unwrap();
let s = initial_session(&Args { file: Some(path.to_string_lossy().into_owned()), control: None });
assert_eq!(s.pkg.graphs, original.pkg.graphs);
assert_eq!(s.path.as_deref(), path.to_str());
assert!(!s.modified);
std::fs::remove_file(path).unwrap();
}
}
+7
View File
@@ -48,6 +48,13 @@ pub type Result<T> = std::result::Result<T, EngineError>;
/// Version string shown in About, `--version` and window titles.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// A fresh, unsaved package with one empty graph, used at desktop and browser startup.
pub fn new_session() -> Session {
let mut pkg = core::Package::new("Untitled package");
pkg.graphs.push(samples::template("empty", "New_graph", 10));
Session::new(pkg)
}
/// A session on the built-in sample package (the brick wall).
pub fn sample_session() -> Session {
Session::new(samples::sample_package())
+29 -1
View File
@@ -5,7 +5,7 @@
use std::time::Duration;
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::{Harness, kittest::Queryable};
use materialcraft_ui_egui::{MaterialCraftApp, UiState};
use serde_json::json;
@@ -33,6 +33,34 @@ fn save(h: &mut Harness<'static, MaterialCraftApp>, dir: &std::path::Path, name:
println!("wrote {}", dir.join(format!("{name}.png")).display());
}
/// The startup graph stays empty through real UI frames; templates are still available on demand.
#[test]
#[ignore = "requires an offscreen GPU; run explicitly with --ignored"]
fn empty_startup_can_create_a_template() {
let app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default());
let mut h = harness(app, 1600.0, 960.0);
settle(&mut h, 40);
assert_eq!(h.state().session.pkg.graphs.len(), 1);
assert!(h.state().session.graph().unwrap().nodes.is_empty());
assert!(h.state().graph.rects.is_empty());
if let Some(dir) = std::env::var_os("MC_SNAPSHOT_DIR").map(std::path::PathBuf::from) {
std::fs::create_dir_all(&dir).unwrap();
save(&mut h, &dir, "empty-startup");
}
h.get_by_label("New graph...").click();
settle(&mut h, 4);
h.get_by_label("Brick wall").click();
settle(&mut h, 4);
h.get_by_label("Create").click();
settle(&mut h, 40);
assert_eq!(h.state().session.pkg.graphs.len(), 2);
assert!(!h.state().session.graph().unwrap().nodes.is_empty());
assert!(!h.state().graph.rects.is_empty());
if let Some(dir) = std::env::var_os("MC_SNAPSHOT_DIR").map(std::path::PathBuf::from) {
save(&mut h, &dir, "chosen-template");
}
}
#[test]
#[ignore]
fn snapshots() {