1use bevy::prelude::*;
8use wde_pbr::prelude::*;
9use wde_renderer::prelude::{WdeColor, *};
10
11use crate::{
12 data::{GizmoLineData, GizmoLinesBinding, GizmoQuadData, GizmoQuadsBinding},
13 extract::{
14 ExtractedGizmoLines, ExtractedGizmoQuads, GizmoDrawCount, GizmoQuadDrawCount,
15 extract_gizmo_lines, update_gizmo_buffer, update_gizmo_quad_buffer
16 },
17 pipeline::{GizmoQuadRenderPipeline, GizmoRenderPipeline},
18 subpass::{SubRenderPassGizmoQuads, SubRenderPassGizmos}
19};
20
21mod data;
22mod extract;
23mod pipeline;
24mod subpass;
25
26#[doc(hidden)]
27pub mod prelude {
28 pub use crate::{Gizmos, data::MAX_GIZMO_LINES};
29}
30
31#[derive(Resource, Default)]
32pub struct Gizmos {
33 lines: Vec<(Vec3, Vec3, WdeColor)>,
34 quads: Vec<([Vec3; 4], WdeColor)>
35}
36impl Gizmos {
37 pub fn line(&mut self, start: Vec3, end: Vec3, color: WdeColor) {
38 self.lines.push((start, end, color));
39 }
40
41 pub fn quad(&mut self, corners: [Vec3; 4], color: WdeColor) {
44 self.quads.push((corners, color));
45 }
46
47 pub fn cube(&mut self, transform: Transform, color: WdeColor) {
48 let half_size = Vec3::splat(0.5);
49 let vertices = [
50 Vec3::new(-half_size.x, -half_size.y, -half_size.z),
51 Vec3::new(half_size.x, -half_size.y, -half_size.z),
52 Vec3::new(half_size.x, half_size.y, -half_size.z),
53 Vec3::new(-half_size.x, half_size.y, -half_size.z),
54 Vec3::new(-half_size.x, -half_size.y, half_size.z),
55 Vec3::new(half_size.x, -half_size.y, half_size.z),
56 Vec3::new(half_size.x, half_size.y, half_size.z),
57 Vec3::new(-half_size.x, half_size.y, half_size.z)
58 ];
59
60 let edges = [
61 (0, 1),
62 (1, 2),
63 (2, 3),
64 (3, 0), (4, 5),
66 (5, 6),
67 (6, 7),
68 (7, 4), (0, 4),
70 (1, 5),
71 (2, 6),
72 (3, 7) ];
74
75 for &(start_idx, end_idx) in &edges {
76 let start = transform.transform_point(vertices[start_idx]);
77 let end = transform.transform_point(vertices[end_idx]);
78 self.line(start, end, color);
79 }
80 }
81
82 pub fn circle(&mut self, center: Vec3, normal: Vec3, radius: f32, color: WdeColor) {
84 const SEGMENTS: usize = 64;
85 let (u, v) = normal.normalize().any_orthonormal_pair();
86 let point = |i: usize| {
87 let (sin, cos) = (i as f32 / SEGMENTS as f32 * std::f32::consts::TAU).sin_cos();
88 center + (u * cos + v * sin) * radius
89 };
90 for i in 0..SEGMENTS {
91 self.line(point(i), point(i + 1), color);
92 }
93 }
94
95 pub fn sphere(&mut self, center: Vec3, rotation: Quat, radius: f32, color: WdeColor) {
97 for axis in [Vec3::X, Vec3::Y, Vec3::Z] {
98 self.circle(center, rotation * axis, radius, color);
99 }
100 }
101
102 fn take_lines(&mut self) -> Vec<(Vec3, Vec3, WdeColor)> {
104 std::mem::take(&mut self.lines)
105 }
106
107 fn take_quads(&mut self) -> Vec<([Vec3; 4], WdeColor)> {
109 std::mem::take(&mut self.quads)
110 }
111}
112
113pub struct GizmosPlugin;
114impl Plugin for GizmosPlugin {
115 fn build(&self, app: &mut App) {
116 app.init_resource::<Gizmos>();
117
118 app.add_plugins((
119 RenderDataRegisterPlugin::<GizmoLineData>::default(),
120 RenderBindingRegisterPlugin::<GizmoLinesBinding>::default(),
121 RenderPipelineRegisterPlugin::<GizmoRenderPipeline>::default(),
122 RenderDataRegisterPlugin::<GizmoQuadData>::default(),
123 RenderBindingRegisterPlugin::<GizmoQuadsBinding>::default(),
124 RenderPipelineRegisterPlugin::<GizmoQuadRenderPipeline>::default()
125 ));
126
127 app.get_sub_app_mut(RenderApp)
128 .unwrap()
129 .init_resource::<ExtractedGizmoLines>()
130 .init_resource::<ExtractedGizmoQuads>()
131 .init_resource::<GizmoDrawCount>()
132 .init_resource::<GizmoQuadDrawCount>()
133 .add_systems(ExtractSchedule, extract_gizmo_lines)
134 .add_systems(
135 Render,
136 (update_gizmo_quad_buffer, update_gizmo_buffer).in_set(RenderSet::Prepare)
137 );
138 }
139
140 fn finish(&self, app: &mut App) {
141 app.get_sub_app_mut(RenderApp)
143 .unwrap()
144 .world_mut()
145 .get_resource_mut::<RenderGraph>()
146 .unwrap()
147 .add_sub_pass::<SubRenderPassGizmoQuads, RenderPassTransparent>()
148 .add_sub_pass::<SubRenderPassGizmos, RenderPassTransparent>();
149 }
150}