Add double end arrow to gizmos (#11890)
# Objective - Implement double ended arrows, suggestion of #9400 ## Solution - Creation of new field and method to the `ArrowBuilder` --- ## Changelog ### Added - New field `ArrowBuilder::double_ended` - New method `ArrowBuilder::with_double_end` to redefine the double_ended field ## Additional I added this in the 3d_gizmos example, that's the result:  I added this arrow in the 2d_gizmos example too:  --------- Co-authored-by: Afonso Lage <lage.afonso@gmail.com> Co-authored-by: Pablo Reinhardt <pabloreinhardt@gmail.com>
This commit is contained in:
		
							parent
							
								
									1141e731ff
								
							
						
					
					
						commit
						6b0e3fa572
					
				| @ -17,6 +17,7 @@ pub struct ArrowBuilder<'a, 'w, 's, T: GizmoConfigGroup> { | |||||||
|     start: Vec3, |     start: Vec3, | ||||||
|     end: Vec3, |     end: Vec3, | ||||||
|     color: Color, |     color: Color, | ||||||
|  |     double_ended: bool, | ||||||
|     tip_length: f32, |     tip_length: f32, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -41,6 +42,13 @@ impl<T: GizmoConfigGroup> ArrowBuilder<'_, '_, '_, T> { | |||||||
|         self.tip_length = length; |         self.tip_length = length; | ||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Adds another tip to the arrow, appended in the start point.
 | ||||||
|  |     /// the default is only one tip at the end point.
 | ||||||
|  |     pub fn with_double_end(mut self) -> Self { | ||||||
|  |         self.double_ended = true; | ||||||
|  |         self | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> { | impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> { | ||||||
| @ -53,8 +61,8 @@ impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> { | |||||||
|         self.gizmos.line(self.start, self.end, self.color); |         self.gizmos.line(self.start, self.end, self.color); | ||||||
|         // now the hard part is to draw the head in a sensible way
 |         // now the hard part is to draw the head in a sensible way
 | ||||||
|         // put us in a coordinate system where the arrow is pointing towards +x and ends at the origin
 |         // put us in a coordinate system where the arrow is pointing towards +x and ends at the origin
 | ||||||
|         let pointing = (self.end - self.start).normalize(); |         let pointing_end = (self.end - self.start).normalize(); | ||||||
|         let rotation = Quat::from_rotation_arc(Vec3::X, pointing); |         let rotation_end = Quat::from_rotation_arc(Vec3::X, pointing_end); | ||||||
|         let tips = [ |         let tips = [ | ||||||
|             Vec3::new(-1., 1., 0.), |             Vec3::new(-1., 1., 0.), | ||||||
|             Vec3::new(-1., 0., 1.), |             Vec3::new(-1., 0., 1.), | ||||||
| @ -64,11 +72,21 @@ impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> { | |||||||
|         // - extend the vectors so their length is `tip_length`
 |         // - extend the vectors so their length is `tip_length`
 | ||||||
|         // - rotate the world so +x is facing in the same direction as the arrow
 |         // - rotate the world so +x is facing in the same direction as the arrow
 | ||||||
|         // - translate over to the tip of the arrow
 |         // - translate over to the tip of the arrow
 | ||||||
|         let tips = tips.map(|v| rotation * (v.normalize() * self.tip_length) + self.end); |         let tips_end = tips.map(|v| rotation_end * (v.normalize() * self.tip_length) + self.end); | ||||||
|         for v in tips { |         for v in tips_end { | ||||||
|             // then actually draw the tips
 |             // then actually draw the tips
 | ||||||
|             self.gizmos.line(self.end, v, self.color); |             self.gizmos.line(self.end, v, self.color); | ||||||
|         } |         } | ||||||
|  |         if self.double_ended { | ||||||
|  |             let pointing_start = (self.start - self.end).normalize(); | ||||||
|  |             let rotation_start = Quat::from_rotation_arc(Vec3::X, pointing_start); | ||||||
|  |             let tips_start = | ||||||
|  |                 tips.map(|v| rotation_start * (v.normalize() * self.tip_length) + self.start); | ||||||
|  |             for v in tips_start { | ||||||
|  |                 // draw the start points tips
 | ||||||
|  |                 self.gizmos.line(self.start, v, self.color); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -100,6 +118,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { | |||||||
|             start, |             start, | ||||||
|             end, |             end, | ||||||
|             color: color.into(), |             color: color.into(), | ||||||
|  |             double_ended: false, | ||||||
|             tip_length: length / 10., |             tip_length: length / 10., | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -89,6 +89,12 @@ fn draw_example_collection( | |||||||
|         Vec2::from_angle(sin / -10. + PI / 2.) * 50., |         Vec2::from_angle(sin / -10. + PI / 2.) * 50., | ||||||
|         YELLOW, |         YELLOW, | ||||||
|     ); |     ); | ||||||
|  | 
 | ||||||
|  |     // You can create more complex arrows using the arrow builder.
 | ||||||
|  |     gizmos | ||||||
|  |         .arrow_2d(Vec2::ZERO, Vec2::from_angle(sin / -10.) * 50., GREEN) | ||||||
|  |         .with_double_end() | ||||||
|  |         .with_tip_length(10.); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn update_config( | fn update_config( | ||||||
|  | |||||||
| @ -137,6 +137,12 @@ fn draw_example_collection( | |||||||
|         .circle_segments(64); |         .circle_segments(64); | ||||||
| 
 | 
 | ||||||
|     gizmos.arrow(Vec3::ZERO, Vec3::ONE * 1.5, YELLOW); |     gizmos.arrow(Vec3::ZERO, Vec3::ONE * 1.5, YELLOW); | ||||||
|  | 
 | ||||||
|  |     // You can create more complex arrows using the arrow builder.
 | ||||||
|  |     gizmos | ||||||
|  |         .arrow(Vec3::new(2., 0., 2.), Vec3::new(2., 2., 2.), ORANGE_RED) | ||||||
|  |         .with_double_end() | ||||||
|  |         .with_tip_length(0.5); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn update_config( | fn update_config( | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Pablo Reinhardt
						Pablo Reinhardt