2024-01-18 15:52:50 +00:00
|
|
|
//! A module for the [`GizmoConfig<T>`] [`Resource`].
|
|
|
|
|
|
|
|
use crate as bevy_gizmos;
|
|
|
|
pub use bevy_gizmos_macros::GizmoConfigGroup;
|
|
|
|
|
2024-02-25 01:57:44 +00:00
|
|
|
use bevy_ecs::{component::Component, reflect::ReflectResource, system::Resource};
|
|
|
|
use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath};
|
2024-02-03 23:47:04 +00:00
|
|
|
use bevy_utils::TypeIdMap;
|
2024-01-18 15:52:50 +00:00
|
|
|
use core::panic;
|
|
|
|
use std::{
|
|
|
|
any::TypeId,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
};
|
|
|
|
|
Gizmo line joints (#12252)
# Objective
- Adds gizmo line joints, suggestion of #9400
## Solution
- Adds `line_joints: GizmoLineJoint` to `GizmoConfig`. Currently the
following values are supported:
- `GizmoLineJoint::None`: does not draw line joints, same behaviour as
previously
- `GizmoLineJoint::Bevel`: draws a single triangle between the lines
- `GizmoLineJoint::Miter` / 'spiky joints': draws two triangles between
the lines extending them until they meet at a (miter) point.
- NOTE: for very small angles between the lines, which happens
frequently in 3d, the miter point will be very far away from the point
at which the lines meet.
- `GizmoLineJoint::Round(resolution)`: Draw a circle arc between the
lines. The circle is a triangle fan of `resolution` triangles.
---
## Changelog
- Added `GizmoLineJoint`, use that in `GizmoConfig` and added necessary
pipelines and draw commands.
- Added a new `line_joints.wgsl` shader containing three vertex shaders
`vertex_bevel`, `vertex_miter` and `vertex_round` as well as a basic
`fragment` shader.
## Migration Guide
Any manually created `GizmoConfig`s must now set the `.line_joints`
field.
## Known issues
- The way we currently create basic closed shapes like rectangles,
circles, triangles or really any closed 2d shape means that one of the
corners will not be drawn with joints, although that would probably be
expected. (see the triangle in the 2d image)
- This could be somewhat mitigated by introducing line caps or fixed by
adding another segment overlapping the first of the strip. (Maybe in a
followup PR?)
- 3d shapes can look 'off' with line joints (especially bevel) because
wherever 3 or more lines meet one of them may stick out beyond the joint
drawn between the other 2.
- Adding additional lines so that there is a joint between every line at
a corner would fix this but would probably be too computationally
expensive.
- Miter joints are 'unreasonably long' for very small angles between the
lines (the angle is the angle between the lines in screen space). This
is technically correct but distracting and does not feel right,
especially in 3d contexts. I think limiting the length of the miter to
the point at which the lines meet might be a good idea.
- The joints may be drawn with a different gizmo in-between them and
their corresponding lines in 2d. Some sort of z-ordering would probably
be good here, but I believe this may be out of scope for this PR.
## Additional information
Some pretty images :)
<img width="1175" alt="Screenshot 2024-03-02 at 04 53 50"
src="https://github.com/bevyengine/bevy/assets/62256001/58df7e63-9376-4430-8871-32adba0cb53b">
- Note that the top vertex does not have a joint drawn.
<img width="1440" alt="Screenshot 2024-03-02 at 05 03 55"
src="https://github.com/bevyengine/bevy/assets/62256001/137a00cf-cbd4-48c2-a46f-4b47492d4fd9">
Now for a weird video:
https://github.com/bevyengine/bevy/assets/62256001/93026f48-f1d6-46fe-9163-5ab548a3fce4
- The black lines shooting out from the cube are miter joints that get
very long because the lines between which they are drawn are (almost)
collinear in screen space.
---------
Co-authored-by: Pablo Reinhardt <126117294+pablo-lua@users.noreply.github.com>
2024-03-11 19:21:32 +00:00
|
|
|
/// An enum configuring how line joints will be drawn.
|
|
|
|
#[derive(Debug, Default, Copy, Clone, Reflect, PartialEq, Eq, Hash)]
|
|
|
|
pub enum GizmoLineJoint {
|
|
|
|
/// Does not draw any line joints.
|
|
|
|
#[default]
|
|
|
|
None,
|
|
|
|
/// Extends both lines at the joining point until they meet in a sharp point.
|
|
|
|
Miter,
|
|
|
|
/// Draws a round corner with the specified resolution between the two lines.
|
|
|
|
///
|
|
|
|
/// The resolution determines the amount of triangles drawn per joint,
|
|
|
|
/// e.g. `GizmoLineJoint::Round(4)` will draw 4 triangles at each line joint.
|
|
|
|
Round(u32),
|
|
|
|
/// Draws a bevel, a straight line in this case, to connect the ends of both lines.
|
|
|
|
Bevel,
|
|
|
|
}
|
|
|
|
|
2024-03-25 19:10:45 +00:00
|
|
|
/// An enum used to configure the style of gizmo lines, similar to CSS line-style
|
|
|
|
#[derive(Copy, Clone, Debug, Default, Hash, PartialEq, Eq, Reflect)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum GizmoLineStyle {
|
|
|
|
/// A solid line without any decorators
|
|
|
|
#[default]
|
|
|
|
Solid,
|
|
|
|
/// A dotted line
|
|
|
|
Dotted,
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
/// A trait used to create gizmo configs groups.
|
|
|
|
///
|
|
|
|
/// Here you can store additional configuration for you gizmo group not covered by [`GizmoConfig`]
|
|
|
|
///
|
|
|
|
/// Make sure to derive [`Default`] + [`Reflect`] and register in the app using `app.init_gizmo_group::<T>()`
|
|
|
|
pub trait GizmoConfigGroup: Reflect + TypePath + Default {}
|
|
|
|
|
|
|
|
/// The default gizmo config group.
|
|
|
|
#[derive(Default, Reflect, GizmoConfigGroup)]
|
|
|
|
pub struct DefaultGizmoConfigGroup;
|
|
|
|
|
|
|
|
/// A [`Resource`] storing [`GizmoConfig`] and [`GizmoConfigGroup`] structs
|
|
|
|
///
|
|
|
|
/// Use `app.init_gizmo_group::<T>()` to register a custom config group.
|
2024-02-25 01:57:44 +00:00
|
|
|
#[derive(Reflect, Resource, Default)]
|
|
|
|
#[reflect(Resource, Default)]
|
2024-01-18 15:52:50 +00:00
|
|
|
pub struct GizmoConfigStore {
|
|
|
|
// INVARIANT: must map TypeId::of::<T>() to correct type T
|
2024-02-25 01:57:44 +00:00
|
|
|
#[reflect(ignore)]
|
2024-02-03 23:47:04 +00:00
|
|
|
store: TypeIdMap<(GizmoConfig, Box<dyn Reflect>)>,
|
2024-01-18 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GizmoConfigStore {
|
|
|
|
/// Returns [`GizmoConfig`] and [`GizmoConfigGroup`] associated with [`TypeId`] of a [`GizmoConfigGroup`]
|
|
|
|
pub fn get_config_dyn(&self, config_type_id: &TypeId) -> Option<(&GizmoConfig, &dyn Reflect)> {
|
|
|
|
let (config, ext) = self.store.get(config_type_id)?;
|
|
|
|
Some((config, ext.deref()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns [`GizmoConfig`] and [`GizmoConfigGroup`] associated with [`GizmoConfigGroup`] `T`
|
|
|
|
pub fn config<T: GizmoConfigGroup>(&self) -> (&GizmoConfig, &T) {
|
|
|
|
let Some((config, ext)) = self.get_config_dyn(&TypeId::of::<T>()) else {
|
|
|
|
panic!("Requested config {} does not exist in `GizmoConfigStore`! Did you forget to add it using `app.init_gizmo_group<T>()`?", T::type_path());
|
|
|
|
};
|
|
|
|
// hash map invariant guarantees that &dyn Reflect is of correct type T
|
|
|
|
let ext = ext.as_any().downcast_ref().unwrap();
|
|
|
|
(config, ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns mutable [`GizmoConfig`] and [`GizmoConfigGroup`] associated with [`TypeId`] of a [`GizmoConfigGroup`]
|
|
|
|
pub fn get_config_mut_dyn(
|
|
|
|
&mut self,
|
|
|
|
config_type_id: &TypeId,
|
|
|
|
) -> Option<(&mut GizmoConfig, &mut dyn Reflect)> {
|
|
|
|
let (config, ext) = self.store.get_mut(config_type_id)?;
|
|
|
|
Some((config, ext.deref_mut()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns mutable [`GizmoConfig`] and [`GizmoConfigGroup`] associated with [`GizmoConfigGroup`] `T`
|
|
|
|
pub fn config_mut<T: GizmoConfigGroup>(&mut self) -> (&mut GizmoConfig, &mut T) {
|
|
|
|
let Some((config, ext)) = self.get_config_mut_dyn(&TypeId::of::<T>()) else {
|
|
|
|
panic!("Requested config {} does not exist in `GizmoConfigStore`! Did you forget to add it using `app.init_gizmo_group<T>()`?", T::type_path());
|
|
|
|
};
|
|
|
|
// hash map invariant guarantees that &dyn Reflect is of correct type T
|
|
|
|
let ext = ext.as_any_mut().downcast_mut().unwrap();
|
|
|
|
(config, ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over all [`GizmoConfig`]s.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&TypeId, &GizmoConfig, &dyn Reflect)> + '_ {
|
|
|
|
self.store
|
|
|
|
.iter()
|
|
|
|
.map(|(id, (config, ext))| (id, config, ext.deref()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over all [`GizmoConfig`]s, by mutable reference.
|
|
|
|
pub fn iter_mut(
|
|
|
|
&mut self,
|
|
|
|
) -> impl Iterator<Item = (&TypeId, &mut GizmoConfig, &mut dyn Reflect)> + '_ {
|
|
|
|
self.store
|
|
|
|
.iter_mut()
|
|
|
|
.map(|(id, (config, ext))| (id, config, ext.deref_mut()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts [`GizmoConfig`] and [`GizmoConfigGroup`] replacing old values
|
|
|
|
pub fn insert<T: GizmoConfigGroup>(&mut self, config: GizmoConfig, ext_config: T) {
|
|
|
|
// INVARIANT: hash map must correctly map TypeId::of::<T>() to &dyn Reflect of type T
|
|
|
|
self.store
|
|
|
|
.insert(TypeId::of::<T>(), (config, Box::new(ext_config)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn register<T: GizmoConfigGroup>(&mut self) {
|
|
|
|
self.insert(GizmoConfig::default(), T::default());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct that stores configuration for gizmos.
|
|
|
|
#[derive(Clone, Reflect)]
|
|
|
|
pub struct GizmoConfig {
|
|
|
|
/// Set to `false` to stop drawing gizmos.
|
|
|
|
///
|
|
|
|
/// Defaults to `true`.
|
|
|
|
pub enabled: bool,
|
|
|
|
/// Line width specified in pixels.
|
|
|
|
///
|
|
|
|
/// If `line_perspective` is `true` then this is the size in pixels at the camera's near plane.
|
|
|
|
///
|
|
|
|
/// Defaults to `2.0`.
|
|
|
|
pub line_width: f32,
|
|
|
|
/// Apply perspective to gizmo lines.
|
|
|
|
///
|
|
|
|
/// This setting only affects 3D, non-orthographic cameras.
|
|
|
|
///
|
|
|
|
/// Defaults to `false`.
|
|
|
|
pub line_perspective: bool,
|
2024-03-25 19:10:45 +00:00
|
|
|
/// Determine the style of gizmo lines.
|
|
|
|
pub line_style: GizmoLineStyle,
|
2024-01-18 15:52:50 +00:00
|
|
|
/// How closer to the camera than real geometry the line should be.
|
|
|
|
///
|
|
|
|
/// In 2D this setting has no effect and is effectively always -1.
|
|
|
|
///
|
|
|
|
/// Value between -1 and 1 (inclusive).
|
|
|
|
/// * 0 means that there is no change to the line position when rendering
|
|
|
|
/// * 1 means it is furthest away from camera as possible
|
|
|
|
/// * -1 means that it will always render in front of other things.
|
|
|
|
///
|
|
|
|
/// This is typically useful if you are drawing wireframes on top of polygons
|
|
|
|
/// and your wireframe is z-fighting (flickering on/off) with your main model.
|
|
|
|
/// You would set this value to a negative number close to 0.
|
|
|
|
pub depth_bias: f32,
|
|
|
|
/// Describes which rendering layers gizmos will be rendered to.
|
|
|
|
///
|
|
|
|
/// Gizmos will only be rendered to cameras with intersecting layers.
|
2024-08-06 13:09:10 +00:00
|
|
|
#[cfg(feature = "bevy_render")]
|
|
|
|
pub render_layers: bevy_render::view::RenderLayers,
|
Gizmo line joints (#12252)
# Objective
- Adds gizmo line joints, suggestion of #9400
## Solution
- Adds `line_joints: GizmoLineJoint` to `GizmoConfig`. Currently the
following values are supported:
- `GizmoLineJoint::None`: does not draw line joints, same behaviour as
previously
- `GizmoLineJoint::Bevel`: draws a single triangle between the lines
- `GizmoLineJoint::Miter` / 'spiky joints': draws two triangles between
the lines extending them until they meet at a (miter) point.
- NOTE: for very small angles between the lines, which happens
frequently in 3d, the miter point will be very far away from the point
at which the lines meet.
- `GizmoLineJoint::Round(resolution)`: Draw a circle arc between the
lines. The circle is a triangle fan of `resolution` triangles.
---
## Changelog
- Added `GizmoLineJoint`, use that in `GizmoConfig` and added necessary
pipelines and draw commands.
- Added a new `line_joints.wgsl` shader containing three vertex shaders
`vertex_bevel`, `vertex_miter` and `vertex_round` as well as a basic
`fragment` shader.
## Migration Guide
Any manually created `GizmoConfig`s must now set the `.line_joints`
field.
## Known issues
- The way we currently create basic closed shapes like rectangles,
circles, triangles or really any closed 2d shape means that one of the
corners will not be drawn with joints, although that would probably be
expected. (see the triangle in the 2d image)
- This could be somewhat mitigated by introducing line caps or fixed by
adding another segment overlapping the first of the strip. (Maybe in a
followup PR?)
- 3d shapes can look 'off' with line joints (especially bevel) because
wherever 3 or more lines meet one of them may stick out beyond the joint
drawn between the other 2.
- Adding additional lines so that there is a joint between every line at
a corner would fix this but would probably be too computationally
expensive.
- Miter joints are 'unreasonably long' for very small angles between the
lines (the angle is the angle between the lines in screen space). This
is technically correct but distracting and does not feel right,
especially in 3d contexts. I think limiting the length of the miter to
the point at which the lines meet might be a good idea.
- The joints may be drawn with a different gizmo in-between them and
their corresponding lines in 2d. Some sort of z-ordering would probably
be good here, but I believe this may be out of scope for this PR.
## Additional information
Some pretty images :)
<img width="1175" alt="Screenshot 2024-03-02 at 04 53 50"
src="https://github.com/bevyengine/bevy/assets/62256001/58df7e63-9376-4430-8871-32adba0cb53b">
- Note that the top vertex does not have a joint drawn.
<img width="1440" alt="Screenshot 2024-03-02 at 05 03 55"
src="https://github.com/bevyengine/bevy/assets/62256001/137a00cf-cbd4-48c2-a46f-4b47492d4fd9">
Now for a weird video:
https://github.com/bevyengine/bevy/assets/62256001/93026f48-f1d6-46fe-9163-5ab548a3fce4
- The black lines shooting out from the cube are miter joints that get
very long because the lines between which they are drawn are (almost)
collinear in screen space.
---------
Co-authored-by: Pablo Reinhardt <126117294+pablo-lua@users.noreply.github.com>
2024-03-11 19:21:32 +00:00
|
|
|
|
|
|
|
/// Describe how lines should join
|
|
|
|
pub line_joints: GizmoLineJoint,
|
2024-01-18 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GizmoConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
enabled: true,
|
|
|
|
line_width: 2.,
|
|
|
|
line_perspective: false,
|
2024-03-25 19:10:45 +00:00
|
|
|
line_style: GizmoLineStyle::Solid,
|
2024-01-18 15:52:50 +00:00
|
|
|
depth_bias: 0.,
|
2024-08-06 13:09:10 +00:00
|
|
|
#[cfg(feature = "bevy_render")]
|
2024-01-18 15:52:50 +00:00
|
|
|
render_layers: Default::default(),
|
Gizmo line joints (#12252)
# Objective
- Adds gizmo line joints, suggestion of #9400
## Solution
- Adds `line_joints: GizmoLineJoint` to `GizmoConfig`. Currently the
following values are supported:
- `GizmoLineJoint::None`: does not draw line joints, same behaviour as
previously
- `GizmoLineJoint::Bevel`: draws a single triangle between the lines
- `GizmoLineJoint::Miter` / 'spiky joints': draws two triangles between
the lines extending them until they meet at a (miter) point.
- NOTE: for very small angles between the lines, which happens
frequently in 3d, the miter point will be very far away from the point
at which the lines meet.
- `GizmoLineJoint::Round(resolution)`: Draw a circle arc between the
lines. The circle is a triangle fan of `resolution` triangles.
---
## Changelog
- Added `GizmoLineJoint`, use that in `GizmoConfig` and added necessary
pipelines and draw commands.
- Added a new `line_joints.wgsl` shader containing three vertex shaders
`vertex_bevel`, `vertex_miter` and `vertex_round` as well as a basic
`fragment` shader.
## Migration Guide
Any manually created `GizmoConfig`s must now set the `.line_joints`
field.
## Known issues
- The way we currently create basic closed shapes like rectangles,
circles, triangles or really any closed 2d shape means that one of the
corners will not be drawn with joints, although that would probably be
expected. (see the triangle in the 2d image)
- This could be somewhat mitigated by introducing line caps or fixed by
adding another segment overlapping the first of the strip. (Maybe in a
followup PR?)
- 3d shapes can look 'off' with line joints (especially bevel) because
wherever 3 or more lines meet one of them may stick out beyond the joint
drawn between the other 2.
- Adding additional lines so that there is a joint between every line at
a corner would fix this but would probably be too computationally
expensive.
- Miter joints are 'unreasonably long' for very small angles between the
lines (the angle is the angle between the lines in screen space). This
is technically correct but distracting and does not feel right,
especially in 3d contexts. I think limiting the length of the miter to
the point at which the lines meet might be a good idea.
- The joints may be drawn with a different gizmo in-between them and
their corresponding lines in 2d. Some sort of z-ordering would probably
be good here, but I believe this may be out of scope for this PR.
## Additional information
Some pretty images :)
<img width="1175" alt="Screenshot 2024-03-02 at 04 53 50"
src="https://github.com/bevyengine/bevy/assets/62256001/58df7e63-9376-4430-8871-32adba0cb53b">
- Note that the top vertex does not have a joint drawn.
<img width="1440" alt="Screenshot 2024-03-02 at 05 03 55"
src="https://github.com/bevyengine/bevy/assets/62256001/137a00cf-cbd4-48c2-a46f-4b47492d4fd9">
Now for a weird video:
https://github.com/bevyengine/bevy/assets/62256001/93026f48-f1d6-46fe-9163-5ab548a3fce4
- The black lines shooting out from the cube are miter joints that get
very long because the lines between which they are drawn are (almost)
collinear in screen space.
---------
Co-authored-by: Pablo Reinhardt <126117294+pablo-lua@users.noreply.github.com>
2024-03-11 19:21:32 +00:00
|
|
|
|
|
|
|
line_joints: GizmoLineJoint::None,
|
2024-01-18 15:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:09:10 +00:00
|
|
|
#[cfg(feature = "bevy_render")]
|
2024-01-18 15:52:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub(crate) struct GizmoMeshConfig {
|
|
|
|
pub line_perspective: bool,
|
2024-03-25 19:10:45 +00:00
|
|
|
pub line_style: GizmoLineStyle,
|
2024-08-06 13:09:10 +00:00
|
|
|
pub render_layers: bevy_render::view::RenderLayers,
|
2024-01-18 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:09:10 +00:00
|
|
|
#[cfg(feature = "bevy_render")]
|
2024-01-18 15:52:50 +00:00
|
|
|
impl From<&GizmoConfig> for GizmoMeshConfig {
|
|
|
|
fn from(item: &GizmoConfig) -> Self {
|
|
|
|
GizmoMeshConfig {
|
|
|
|
line_perspective: item.line_perspective,
|
2024-03-25 19:10:45 +00:00
|
|
|
line_style: item.line_style,
|
2024-05-16 16:15:47 +00:00
|
|
|
render_layers: item.render_layers.clone(),
|
2024-01-18 15:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|