Skip to content

Commit ffb6faa

Browse files
Use Direction3d for gizmos.circle normal (#11422)
# Objective Fix weird visuals when drawing a gizmo with a non-normed normal. Fixes #11401 ## Solution Just normalize right before we draw. Could do it when constructing the builder but that seems less consistent. ## Changelog - gizmos.circle normal is now a Direction3d instead of a Vec3. ## Migration Guide - Pass a Direction3d for gizmos.circle normal, eg. `Direction3d::new(vec).unwrap_or(default)` or potentially `Direction3d::new_unchecked(vec)` if you know your vec is definitely normalized.
1 parent 259fb68 commit ffb6faa

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

crates/bevy_gizmos/src/circles.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! and assorted support items.
55
66
use crate::prelude::{GizmoConfigGroup, Gizmos};
7-
use bevy_math::{Quat, Vec2, Vec3};
7+
use bevy_math::{primitives::Direction3d, Quat, Vec2, Vec3};
88
use bevy_render::color::Color;
99
use std::f32::consts::TAU;
1010

@@ -28,12 +28,12 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
2828
/// # use bevy_render::prelude::*;
2929
/// # use bevy_math::prelude::*;
3030
/// fn system(mut gizmos: Gizmos) {
31-
/// gizmos.circle(Vec3::ZERO, Vec3::Z, 1., Color::GREEN);
31+
/// gizmos.circle(Vec3::ZERO, Direction3d::Z, 1., Color::GREEN);
3232
///
3333
/// // Circles have 32 line-segments by default.
3434
/// // You may want to increase this for larger circles.
3535
/// gizmos
36-
/// .circle(Vec3::ZERO, Vec3::Z, 5., Color::RED)
36+
/// .circle(Vec3::ZERO, Direction3d::Z, 5., Color::RED)
3737
/// .segments(64);
3838
/// }
3939
/// # bevy_ecs::system::assert_is_system(system);
@@ -42,7 +42,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
4242
pub fn circle(
4343
&mut self,
4444
position: Vec3,
45-
normal: Vec3,
45+
normal: Direction3d,
4646
radius: f32,
4747
color: Color,
4848
) -> CircleBuilder<'_, 'w, 's, T> {
@@ -97,7 +97,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
9797
pub struct CircleBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
9898
gizmos: &'a mut Gizmos<'w, 's, T>,
9999
position: Vec3,
100-
normal: Vec3,
100+
normal: Direction3d,
101101
radius: f32,
102102
color: Color,
103103
segments: usize,
@@ -116,7 +116,7 @@ impl<T: GizmoConfigGroup> Drop for CircleBuilder<'_, '_, '_, T> {
116116
if !self.gizmos.enabled {
117117
return;
118118
}
119-
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal);
119+
let rotation = Quat::from_rotation_arc(Vec3::Z, *self.normal);
120120
let positions = circle_inner(self.radius, self.segments)
121121
.map(|vec2| self.position + rotation * vec2.extend(0.));
122122
self.gizmos.linestrip(positions, self.color);

crates/bevy_gizmos/src/gizmos.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy_ecs::{
88
system::{Deferred, ReadOnlySystemParam, Res, Resource, SystemBuffer, SystemMeta, SystemParam},
99
world::{unsafe_world_cell::UnsafeWorldCell, World},
1010
};
11-
use bevy_math::{Mat2, Quat, Vec2, Vec3};
11+
use bevy_math::{primitives::Direction3d, Mat2, Quat, Vec2, Vec3};
1212
use bevy_render::color::Color;
1313
use bevy_transform::TransformPoint;
1414

@@ -618,7 +618,12 @@ impl<T: GizmoConfigGroup> Drop for SphereBuilder<'_, '_, '_, T> {
618618
}
619619
for axis in Vec3::AXES {
620620
self.gizmos
621-
.circle(self.position, self.rotation * axis, self.radius, self.color)
621+
.circle(
622+
self.position,
623+
Direction3d::new_unchecked(self.rotation * axis),
624+
self.radius,
625+
self.color,
626+
)
622627
.segments(self.circle_segments);
623628
}
624629
}

examples/3d/3d_gizmos.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::f32::consts::PI;
44

5+
use bevy::math::primitives::Direction3d;
56
use bevy::prelude::*;
67

78
fn main() {
@@ -96,10 +97,10 @@ fn system(mut gizmos: Gizmos, mut my_gizmos: Gizmos<MyRoundGizmos>, time: Res<Ti
9697
}
9798

9899
// Circles have 32 line-segments by default.
99-
my_gizmos.circle(Vec3::ZERO, Vec3::Y, 3., Color::BLACK);
100+
my_gizmos.circle(Vec3::ZERO, Direction3d::Y, 3., Color::BLACK);
100101
// You may want to increase this for larger circles or spheres.
101102
my_gizmos
102-
.circle(Vec3::ZERO, Vec3::Y, 3.1, Color::NAVY)
103+
.circle(Vec3::ZERO, Direction3d::Y, 3.1, Color::NAVY)
103104
.segments(64);
104105
my_gizmos
105106
.sphere(Vec3::ZERO, Quat::IDENTITY, 3.2, Color::BLACK)

examples/3d/3d_viewport_to_world.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! This example demonstrates how to use the `Camera::viewport_to_world` method.
22
3+
use bevy::math::primitives::Direction3d;
34
use bevy::prelude::*;
45

56
fn main() {
@@ -36,7 +37,12 @@ fn draw_cursor(
3637
let point = ray.get_point(distance);
3738

3839
// Draw a circle just above the ground plane at that position.
39-
gizmos.circle(point + ground.up() * 0.01, ground.up(), 0.2, Color::WHITE);
40+
gizmos.circle(
41+
point + ground.up() * 0.01,
42+
Direction3d::new_unchecked(ground.up()), // Up vector is already normalized.
43+
0.2,
44+
Color::WHITE,
45+
);
4046
}
4147

4248
#[derive(Component)]

0 commit comments

Comments
 (0)