diff --git a/.cargo/config.toml b/.cargo/config.toml index c302a44..3b57381 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,4 @@ [target.wasm32-unknown-unknown] -runner = "wasm-server-runner" \ No newline at end of file +runner = "wasm-server-runner" +# Needed for getrandom/uuid: https://github.com/uuid-rs/uuid/issues/792 +rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 8a1452a..cbf629f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ members = [ "crates/wgsparkl-testbed3d", "crates/wgsparkl2d", "crates/wgsparkl3d", + "crates/text_to_collider", + "examples3d", + "examples2d", ] resolver = "2" @@ -23,6 +26,31 @@ rust.unexpected_cfgs = { level = "warn", check-cfg = [ ] } [patch.crates-io] +# parry2d = { path = "../parry/crates/parry2d" } +# parry3d = { path = "../parry/crates/parry3d" } +# encase = { path = "../encase" } +# wgcore = { path = "../wgmath/crates/wgcore" } +# wgebra = { path = "../wgmath/crates/wgebra" } +# wgparry2d = { path = "../wgmath/crates/wgparry/crates/wgparry2d" } +# wgparry3d = { path = "../wgmath/crates/wgparry/crates/wgparry3d" } +# wgrapier2d = { path = "../wgmath/crates/wgrapier/crates/wgrapier2d" } +# wgrapier3d = { path = "../wgmath/crates/wgrapier/crates/wgrapier3d" } + +parry2d = { git = "https://github.com/dimforge/parry.git", rev = "407fb449504dde7ba0a3d80a8de7e37ac991a77a" } +parry3d = { git = "https://github.com/dimforge/parry.git", rev = "407fb449504dde7ba0a3d80a8de7e37ac991a77a" } +encase = { git = "https://github.com/sebcrozet/encase.git", rev = "6755c1414c225cbeac9fde1ff9394b70d3b05404" } +# wgcore = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" } +wgcore = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" } +wgcore-derive = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" } +wgebra = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" } +# wgparry2d = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" } +wgparry3d = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" } +# wgrapier2d = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" } +wgrapier3d = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" } +rapier3d = { git = "https://github.com/vrixyz/rapier.git", branch = "uber_physics_context" } [profile.release] opt-level = 'z' + +[profile.dev] +opt-level = 1 diff --git a/build_wasm.sh b/build_wasm.sh index 8b2f27d..42cdf7e 100755 --- a/build_wasm.sh +++ b/build_wasm.sh @@ -1,11 +1,11 @@ #!/bin/sh -cargo build --example elasticity2 --release --target wasm32-unknown-unknown --features dim2 -cargo build --example elasticity3 --release --target wasm32-unknown-unknown --features dim3 -wasm-bindgen --no-typescript --target web --out-dir dist2d --out-name elasticity2 ./target/wasm32-unknown-unknown/release/examples/elasticity2.wasm -wasm-bindgen --no-typescript --target web --out-dir dist3d --out-name elasticity3 ./target/wasm32-unknown-unknown/release/examples/elasticity3.wasm -wasm-opt -Oz -o ./dist2d/opt.wasm ./dist2d/elasticity2_bg.wasm && mv ./dist2d/opt.wasm ./dist2d/elasticity2_bg.wasm -wasm-opt -Oz -o ./dist3d/opt.wasm ./dist3d/elasticity3_bg.wasm && mv ./dist3d/opt.wasm ./dist3d/elasticity3_bg.wasm +# cargo build -p wgsparkl-examples-2d --release --target wasm32-unknown-unknown +cargo build -p wgsparkl-examples-3d --release --target wasm32-unknown-unknown +# wasm-bindgen --no-typescript --target web --out-dir dist2d --out-name wgsparkl-examples-2d ./target/wasm32-unknown-unknown/release/wgsparkl-examples-2d.wasm +wasm-bindgen --no-typescript --target web --out-dir dist3d --out-name wgsparkl-examples-3d ./target/wasm32-unknown-unknown/release/wgsparkl-examples-3d.wasm +# wasm-opt -Oz -o ./dist2d/opt.wasm ./dist2d/wgsparkl-examples-2d_bg.wasm && mv ./dist2d/opt.wasm ./dist2d/wgsparkl-examples-2d_bg.wasm +wasm-opt -Oz -o ./dist3d/opt.wasm ./dist3d/wgsparkl-examples-3d_bg.wasm && mv ./dist3d/opt.wasm ./dist3d/wgsparkl-examples-3d_bg.wasm -brotli ./dist2d/elasticity2_bg.wasm && mv ./dist2d/elasticity2_bg.wasm.br ./dist2d/elasticity2_bg.wasm -brotli ./dist3d/elasticity3_bg.wasm && mv ./dist3d/elasticity3_bg.wasm.br ./dist3d/elasticity3_bg.wasm \ No newline at end of file +# brotli ./dist2d/wgsparkl-examples-2d_bg.wasm && mv ./dist2d/wgsparkl-examples-2d_bg.wasm.br ./dist2d/wgsparkl-examples-2d_bg.wasm +brotli ./dist3d/wgsparkl-examples-3d_bg.wasm && mv ./dist3d/wgsparkl-examples-3d_bg.wasm.br ./dist3d/wgsparkl-examples-3d_bg.wasm \ No newline at end of file diff --git a/crates/text_to_collider/Cargo.toml b/crates/text_to_collider/Cargo.toml new file mode 100644 index 0000000..5e797ee --- /dev/null +++ b/crates/text_to_collider/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "text_to_collider" +version = "0.1.0" +license = "Apache-2.0 OR Custom" +edition = "2021" + +[lints] +workspace = true + +[dependencies] +env_logger = "*" +cosmic-text = "*" +image = "*" +parry3d = "*" + +[dev-dependencies] +bevy = "0.15" +rkyv = "0.8" diff --git a/crates/text_to_collider/examples/color_positions.rs b/crates/text_to_collider/examples/color_positions.rs new file mode 100644 index 0000000..136c272 --- /dev/null +++ b/crates/text_to_collider/examples/color_positions.rs @@ -0,0 +1,33 @@ +use std::fs; + +use bevy::math::Vec2; +use cosmic_text::Color; +use parry3d::{na::Vector2, shape::Cuboid}; +use text_to_collider::{get_rects_for, Rect, HEIGHT, WIDTH}; + +fn main() { + let rects = get_rects_for("Wgsparkl 🌊✨"); + let positions = std::fs::read("particles.bin") + .expect("Could not read particles.bin, generate it by running example sand3."); + let positions = rkyv::from_bytes::, rkyv::rancor::Error>(&positions).unwrap(); + let detection_radius = 2f32; + let colors = positions + .iter() + .map(|p| { + // dismiss the y coordinate + let pos_to_check = + Vec2::new(p[0], -p[2] + 20f32) * 8f32 + Vec2::new(WIDTH, 0f32) / 2f32; + if let Some(c) = rects.iter().find(|c| { + let c1 = Vec2::new(c.x as f32, c.y as f32); + c1.distance_squared(pos_to_check) < detection_radius + }) { + let color = c.color.as_rgba(); + [color[0], color[1], color[2]] + } else { + [0, 55, 200] + } + }) + .collect::>(); + let particle_bytes = rkyv::to_bytes::(&colors).unwrap(); + std::fs::write("particles_colors.bin", &particle_bytes).unwrap(); +} diff --git a/crates/text_to_collider/examples/display.rs b/crates/text_to_collider/examples/display.rs new file mode 100644 index 0000000..381bf06 --- /dev/null +++ b/crates/text_to_collider/examples/display.rs @@ -0,0 +1,69 @@ +use bevy::{input::common_conditions::input_toggle_active, prelude::*}; +use cosmic_text::Color; +use parry3d::{na::Vector2, shape::Cuboid}; +use text_to_collider::{get_rects_for, Rect}; + +fn main() { + let rects = get_rects_for("Wgsparkl 🌊✨"); + + App::new() + .add_plugins(DefaultPlugins) + .insert_resource(Cuboids(rects.iter().map(|r| r.to_cuboid(2f32)).collect())) + .insert_resource(RectsStorage(rects)) + .add_systems(Startup, setup) + .add_systems( + Update, + show_rects.run_if(input_toggle_active(true, KeyCode::Space)), + ) + .add_systems(Update, show_cuboids) + .run(); +} + +#[derive(Resource)] +pub struct RectsStorage(pub Vec); + +#[derive(Resource)] +pub struct Cuboids(pub Vec<(Cuboid, Vector2, Color)>); + +pub fn setup(mut commands: Commands) { + commands.spawn((Camera::default(), Camera2d)); +} + +pub fn show_rects(mut g: Gizmos, rects: Res) { + for r in rects.0.iter() { + let color = r.color.as_rgba(); + g.rect( + Isometry3d::from_translation(Vec3::new(r.x as f32, r.y as f32, 0f32)), + Vec2::new(r.width as f32, r.height as f32), + bevy::prelude::Color::srgba_u8(color[0], color[1], color[2], color[3]), + ); + } +} + +pub fn show_cuboids(time: Res