From b0297c53f2bcf5de4365f8e6b61d040073c57d2a Mon Sep 17 00:00:00 2001 From: Matthew Deville Date: Sun, 22 Feb 2026 15:41:34 +0100 Subject: [PATCH] wip --- Cargo.lock | 147 ++++++------------------------------------------ Cargo.toml | 19 ++++++- app/Cargo.toml | 4 +- app/src/main.rs | 37 ++---------- build-wasm.sh | 14 +++-- flake.nix | 2 + justfile | 35 ++++++++++++ web/index.html | 4 +- 8 files changed, 88 insertions(+), 174 deletions(-) create mode 100644 justfile diff --git a/Cargo.lock b/Cargo.lock index c7339c5..0136123 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -373,12 +373,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "base64" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" - [[package]] name = "bevy" version = "0.18.0" @@ -423,7 +417,6 @@ dependencies = [ "bevy_derive", "bevy_ecs", "bevy_math", - "bevy_mesh", "bevy_platform", "bevy_reflect", "bevy_time", @@ -779,7 +772,6 @@ dependencies = [ "bevy_color", "bevy_ecs", "bevy_gizmos_macros", - "bevy_light", "bevy_math", "bevy_reflect", "bevy_time", @@ -813,7 +805,6 @@ dependencies = [ "bevy_image", "bevy_math", "bevy_mesh", - "bevy_pbr", "bevy_render", "bevy_shader", "bevy_sprite_render", @@ -823,42 +814,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "bevy_gltf" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08372f222676dba313061fc71128209b82f9711e7c5cba222b5c34bf1c5c70fe" -dependencies = [ - "async-lock", - "base64", - "bevy_animation", - "bevy_app", - "bevy_asset", - "bevy_camera", - "bevy_color", - "bevy_ecs", - "bevy_image", - "bevy_light", - "bevy_math", - "bevy_mesh", - "bevy_pbr", - "bevy_platform", - "bevy_reflect", - "bevy_render", - "bevy_scene", - "bevy_tasks", - "bevy_transform", - "fixedbitset", - "gltf", - "itertools 0.14.0", - "percent-encoding", - "serde", - "serde_json", - "smallvec", - "thiserror 2.0.18", - "tracing", -] - [[package]] name = "bevy_image" version = "0.18.0" @@ -879,9 +834,7 @@ dependencies = [ "guillotiere", "half", "image", - "ktx2", "rectangle-pack", - "ruzstd", "serde", "thiserror 2.0.18", "tracing", @@ -945,11 +898,9 @@ dependencies = [ "bevy_gilrs", "bevy_gizmos", "bevy_gizmos_render", - "bevy_gltf", "bevy_image", "bevy_input", "bevy_input_focus", - "bevy_light", "bevy_log", "bevy_math", "bevy_mesh", @@ -2637,42 +2588,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "gltf" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3ce1918195723ce6ac74e80542c5a96a40c2b26162c1957a5cd70799b8cacf7" -dependencies = [ - "byteorder", - "gltf-json", - "lazy_static", - "serde_json", -] - -[[package]] -name = "gltf-derive" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14070e711538afba5d6c807edb74bcb84e5dbb9211a3bf5dea0dfab5b24f4c51" -dependencies = [ - "inflections", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "gltf-json" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6176f9d60a7eab0a877e8e96548605dedbde9190a7ae1e80bbcc1c9af03ab14" -dependencies = [ - "gltf-derive", - "serde", - "serde_derive", - "serde_json", -] - [[package]] name = "glutin_wgl_sys" version = "0.6.1" @@ -2962,12 +2877,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "inflections" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" - [[package]] name = "inotify" version = "0.11.0" @@ -3055,9 +2964,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.85" +version = "0.3.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +checksum = "93f0862381daaec758576dcc22eb7bbf4d7efd67328553f3b45a412a51a3fb21" dependencies = [ "once_cell", "wasm-bindgen", @@ -3080,15 +2989,6 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" -[[package]] -name = "ktx2" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff7f53bdf698e7aa7ec916411bbdc8078135da11b66db5182675b2227f6c0d07" -dependencies = [ - "bitflags 2.11.0", -] - [[package]] name = "lazy_static" version = "1.5.0" @@ -4296,15 +4196,6 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" -[[package]] -name = "ruzstd" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ff0cc5e135c8870a775d3320910cd9b564ec036b4dc0b8741629020be63f01" -dependencies = [ - "twox-hash", -] - [[package]] name = "ryu" version = "1.0.23" @@ -4618,9 +4509,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.116" +version = "2.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3df424c70518695237746f84cede799c9c58fcb37450d7b23716568cc8bc69cb" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" dependencies = [ "proc-macro2", "quote", @@ -4985,12 +4876,6 @@ dependencies = [ "core_maths", ] -[[package]] -name = "twox-hash" -version = "2.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" - [[package]] name = "typeid" version = "1.0.3" @@ -5128,9 +5013,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.108" +version = "0.2.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +checksum = "1de241cdc66a9d91bd84f097039eb140cdc6eec47e0cdbaf9d932a1dd6c35866" dependencies = [ "cfg-if", "once_cell", @@ -5141,9 +5026,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.58" +version = "0.4.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f" +checksum = "a42e96ea38f49b191e08a1bab66c7ffdba24b06f9995b39a9dd60222e5b6f1da" dependencies = [ "cfg-if", "futures-util", @@ -5155,9 +5040,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.108" +version = "0.2.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +checksum = "e12fdf6649048f2e3de6d7d5ff3ced779cdedee0e0baffd7dff5cdfa3abc8a52" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5165,9 +5050,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.108" +version = "0.2.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +checksum = "0e63d1795c565ac3462334c1e396fd46dbf481c40f51f5072c310717bc4fb309" dependencies = [ "bumpalo", "proc-macro2", @@ -5178,9 +5063,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.108" +version = "0.2.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +checksum = "e9f9cdac23a5ce71f6bf9f8824898a501e511892791ea2a0c6b8568c68b9cb53" dependencies = [ "unicode-ident", ] @@ -5329,9 +5214,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.85" +version = "0.3.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598" +checksum = "f2c7c5718134e770ee62af3b6b4a84518ec10101aad610c024b64d6ff29bb1ff" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index e2fddd7..27935b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,21 @@ members = ["app", "server"] resolver = "3" +# Enable more optimization in the release profile at the cost of compile time. [profile.release] - lto = "thin" - opt-level = 'z' + # Compile the entire crate as one unit. + # Slows compile times, marginal improvements. + codegen-units = 1 + # Do a second optimization pass over the entire program, including dependencies. + # Slows compile times, marginal improvements. + lto = true + +# Optimize for size in the wasm-release profile to reduce load times and bandwidth usage on web. +[profile.wasm-release] + # Default to release profile values. + inherits = "release" + # Optimize with size in mind (also try "z", sometimes it is better). + # Slightly slows compile times, great improvements to file size and runtime performance. + opt-level = "z" + # Strip all debugging information from the binary to slightly reduce file size. + strip = "debuginfo" diff --git a/app/Cargo.toml b/app/Cargo.toml index 1f04abc..dc95fc9 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -5,8 +5,8 @@ [dependencies] bevy = { version = "0.18", default-features = false, features = [ - "3d", - "tonemapping_luts", + "2d", + "keyboard", "web", "webgl2", ] } diff --git a/app/src/main.rs b/app/src/main.rs index cc9a054..e1097c8 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -1,47 +1,18 @@ use bevy::prelude::*; +const ALIVE_COLOR: [u8; 4] = [0, 255, 136, 255]; +const DEAD_COLOR: [u8; 4] = [18, 18, 32, 255]; + fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { + title: "Game of Life".into(), fit_canvas_to_parent: true, - prevent_default_event_handling: false, canvas: Some("#bevy-canvas".into()), ..default() }), ..default() })) - .add_systems(Startup, setup) .run(); } - -fn setup( - mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, -) { - commands.spawn(( - Mesh3d(meshes.add(Circle::new(4.0))), - MeshMaterial3d(materials.add(Color::WHITE)), - Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), - )); - - commands.spawn(( - Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))), - MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))), - Transform::from_xyz(0.0, 0.5, 0.0), - )); - - commands.spawn(( - PointLight { - shadows_enabled: true, - ..default() - }, - Transform::from_xyz(4.0, 8.0, 4.0), - )); - - commands.spawn(( - Camera3d::default(), - Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y), - )); -} diff --git a/build-wasm.sh b/build-wasm.sh index 782d020..9a4bbfa 100755 --- a/build-wasm.sh +++ b/build-wasm.sh @@ -1,21 +1,27 @@ #!/usr/bin/env bash set -euo pipefail -PROFILE="${1:-release}" +PROFILE="${1:-wasm-release}" if [ "$PROFILE" = "dev" ]; then PROFILE_DIR="debug" CARGO_FLAGS="" else - PROFILE_DIR="release" - CARGO_FLAGS="--release" + PROFILE_DIR=$PROFILE + CARGO_FLAGS="--profile wasm-release" fi cargo build $CARGO_FLAGS --target wasm32-unknown-unknown -p app mkdir -p static -wasm-bindgen --out-name 3dscene \ +wasm-bindgen --out-name app \ --out-dir static \ --target web \ "target/wasm32-unknown-unknown/$PROFILE_DIR/app.wasm" + +# Optimize the WASM file for size +if [ "$PROFILE" = "wasm-release" ]; then + wasm-opt -Oz -o static/app_bg.wasm static/app_bg.wasm +fi + cp web/index.html static/ diff --git a/flake.nix b/flake.nix index 46d943f..6315ed2 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,8 @@ buildInputs = [ rustToolchain pkgs.wasm-bindgen-cli + pkgs.binaryen + pkgs.just ]; }; } diff --git a/justfile b/justfile new file mode 100644 index 0000000..11ebb84 --- /dev/null +++ b/justfile @@ -0,0 +1,35 @@ +# Default: build WASM app for release +default: + just build + +# Build the WASM app (default: release) +build profile="wasm-release": + ./build-wasm.sh {{ profile }} + +# Build for development (faster compile, no wasm-opt) +build-dev: + ./build-wasm.sh dev + +# Build the server binary +build-server: + cargo build --release -p server + +# Build everything: WASM app + server +build-all profile="wasm-release": + ./build-wasm.sh {{ profile }} + cargo build --release -p server + +# Serve the site locally (builds WASM first, then runs server on :3000) +serve profile="wasm-release": + ./build-wasm.sh {{ profile }} + cargo run --release -p server + +# Development: build WASM (dev profile) and run server with hot reload +dev: + ./build-wasm.sh dev + cargo run --release -p server + +# Clean build artifacts +clean: + cargo clean + rm -rf static/* diff --git a/web/index.html b/web/index.html index debdce5..eae0b6f 100644 --- a/web/index.html +++ b/web/index.html @@ -4,7 +4,7 @@ - 3D Scene + Bevy App