Skip to content

Commit

Permalink
Dedicated primitive example (#11697)
Browse files Browse the repository at this point in the history
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: #11699
  • Loading branch information
RobWalt authored Feb 14, 2024
1 parent 8cf3447 commit b446374
Show file tree
Hide file tree
Showing 5 changed files with 681 additions and 374 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
151 changes: 1 addition & 150 deletions examples/2d/2d_gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,24 @@ use bevy::prelude::*;

fn main() {
App::new()
.init_state::<PrimitiveState>()
.add_plugins(DefaultPlugins)
.init_gizmo_group::<MyRoundGizmos>()
.add_systems(Startup, setup)
.add_systems(Update, (draw_example_collection, update_config))
.add_systems(Update, (draw_primitives, update_primitives))
.run();
}

// We can create our own gizmo config group!
#[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<AssetServer>) {
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.,
Expand Down Expand Up @@ -130,92 +80,6 @@ fn draw_example_collection(
);
}

fn draw_primitives(
mut gizmos: Gizmos,
time: Res<Time>,
primitive_state: Res<State<PrimitiveState>>,
) {
let angle = time.elapsed_seconds();
let rotation = Mat2::from_angle(angle);
let position = rotation * Vec2::X;
let color = Color::WHITE;

const SIZE: f32 = 50.0;
match primitive_state.get() {
PrimitiveState::Nothing => {}
PrimitiveState::Circle => {
gizmos.primitive_2d(Circle { radius: SIZE }, position, angle, color);
}
PrimitiveState::Ellipse => gizmos.primitive_2d(
Ellipse {
half_size: Vec2::new(SIZE, SIZE * 0.5),
},
position,
angle,
color,
),
PrimitiveState::Capsule => gizmos.primitive_2d(
Capsule2d {
radius: SIZE * 0.5,
half_length: SIZE,
},
position,
angle,
color,
),
PrimitiveState::Line => drop(gizmos.primitive_2d(
Line2d {
direction: Direction2d::X,
},
position,
angle,
color,
)),
PrimitiveState::Plane => gizmos.primitive_2d(
Plane2d {
normal: Direction2d::Y,
},
position,
angle,
color,
),
PrimitiveState::Segment => drop(gizmos.primitive_2d(
Segment2d {
direction: Direction2d::X,
half_length: SIZE * 0.5,
},
position,
angle,
color,
)),
PrimitiveState::Triangle => gizmos.primitive_2d(
Triangle2d {
vertices: [Vec2::ZERO, Vec2::Y, Vec2::X].map(|p| p * SIZE * 0.5),
},
position,
angle,
color,
),
PrimitiveState::Rectangle => gizmos.primitive_2d(
Rectangle {
half_size: Vec2::splat(SIZE * 0.5),
},
position,
angle,
color,
),
PrimitiveState::RegularPolygon => gizmos.primitive_2d(
RegularPolygon {
circumcircle: Circle { radius: SIZE * 0.5 },
sides: 5,
},
position,
angle,
color,
),
}
}

fn update_config(
mut config_store: ResMut<GizmoConfigStore>,
keyboard: Res<ButtonInput<KeyCode>>,
Expand Down Expand Up @@ -247,16 +111,3 @@ fn update_config(
my_config.enabled ^= true;
}
}

fn update_primitives(
keyboard: Res<ButtonInput<KeyCode>>,
mut next_primitive_state: ResMut<NextState<PrimitiveState>>,
primitive_state: Res<State<PrimitiveState>>,
) {
if keyboard.just_pressed(KeyCode::KeyJ) {
next_primitive_state.set(primitive_state.get().last());
}
if keyboard.just_pressed(KeyCode::KeyK) {
next_primitive_state.set(primitive_state.get().next());
}
}
Loading

0 comments on commit b446374

Please sign in to comment.