From b446374392adc70aceb92621b080d1a6cf7a7392 Mon Sep 17 00:00:00 2001 From: Robert Walter <26892280+RobWalt@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:55:44 +0000 Subject: [PATCH] Dedicated primitive example (#11697) I just implemented this to record a video for the new blog post, but I figured it would also make a good dedicated example. This also allows us to remove a lot of code from the 2d/3d gizmo examples since it supersedes this portion of code. Depends on: https://github.com/bevyengine/bevy/pull/11699 --- Cargo.toml | 11 + examples/2d/2d_gizmos.rs | 151 +------ examples/3d/3d_gizmos.rs | 224 ---------- examples/README.md | 7 + examples/math/render_primitives.rs | 662 +++++++++++++++++++++++++++++ 5 files changed, 681 insertions(+), 374 deletions(-) create mode 100644 examples/math/render_primitives.rs diff --git a/Cargo.toml b/Cargo.toml index fb180cf432..af1aedec06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2644,6 +2644,17 @@ description = "Demonstrates creating and using custom Ui materials" category = "UI (User Interface)" wasm = true +[[example]] +name = "render_primitives" +path = "examples/math/render_primitives.rs" +doc-scrape-examples = true + +[package.metadata.example.render_primitives] +name = "Rendering Primitives" +description = "Shows off rendering for all math primitives as both Meshes and Gizmos" +category = "Math" +wasm = true + [profile.wasm-release] inherits = "release" opt-level = "z" diff --git a/examples/2d/2d_gizmos.rs b/examples/2d/2d_gizmos.rs index 1e7129f519..fb414201ed 100644 --- a/examples/2d/2d_gizmos.rs +++ b/examples/2d/2d_gizmos.rs @@ -6,12 +6,10 @@ use bevy::prelude::*; fn main() { App::new() - .init_state::() .add_plugins(DefaultPlugins) .init_gizmo_group::() .add_systems(Startup, setup) .add_systems(Update, (draw_example_collection, update_config)) - .add_systems(Update, (draw_primitives, update_primitives)) .run(); } @@ -19,61 +17,13 @@ fn main() { #[derive(Default, Reflect, GizmoConfigGroup)] struct MyRoundGizmos {} -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)] -enum PrimitiveState { - #[default] - Nothing, - Circle, - Ellipse, - Capsule, - Line, - Plane, - Segment, - Triangle, - Rectangle, - RegularPolygon, -} - -impl PrimitiveState { - const ALL: [Self; 10] = [ - Self::Nothing, - Self::Circle, - Self::Ellipse, - Self::Capsule, - Self::Line, - Self::Plane, - Self::Segment, - Self::Triangle, - Self::Rectangle, - Self::RegularPolygon, - ]; - fn next(self) -> Self { - Self::ALL - .into_iter() - .cycle() - .skip_while(|&x| x != self) - .nth(1) - .unwrap() - } - fn last(self) -> Self { - Self::ALL - .into_iter() - .rev() - .cycle() - .skip_while(|&x| x != self) - .nth(1) - .unwrap() - } -} - fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle::default()); // text commands.spawn(TextBundle::from_section( "Hold 'Left' or 'Right' to change the line width of straight gizmos\n\ Hold 'Up' or 'Down' to change the line width of round gizmos\n\ - Press '1' or '2' to toggle the visibility of straight gizmos or round gizmos\n\ - Press 'K' or 'J' to cycle through primitives rendered with gizmos", + Press '1' or '2' to toggle the visibility of straight gizmos or round gizmos", TextStyle { font: asset_server.load("fonts/FiraMono-Medium.ttf"), font_size: 24., @@ -130,92 +80,6 @@ fn draw_example_collection( ); } -fn draw_primitives( - mut gizmos: Gizmos, - time: Res