bevy_gizmos_render 0.20.1

Provides gizmos rendering for Bevy Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Mesh-based rendering for the transform gizmo.
//!
//! Uses [`StandardMaterial`] with `unlit: true` and a dedicated overlay camera
//! on a separate [`RenderLayers`] to render gizmo meshes always-on-top.

use bevy_app::{App, Plugin, PostUpdate, Startup};
use bevy_asset::{Assets, Handle};
use bevy_camera::{
    visibility::{RenderLayers, Visibility},
    Camera, Camera3d,
};
use bevy_color::Color;
use bevy_ecs::{
    component::Component,
    hierarchy::ChildOf,
    query::{Or, With, Without},
    resource::Resource,
    schedule::IntoScheduleConfigs,
    system::{Commands, Query, Res, ResMut},
};
use bevy_math::{Quat, Vec3};
use bevy_mesh::{Mesh, Mesh3d, MeshBuilder, Meshable};
use bevy_pbr::{MeshMaterial3d, StandardMaterial};
use bevy_shape::{Cone, Cuboid, Cylinder, Torus};
use bevy_transform::{
    components::{GlobalTransform, Transform},
    systems::propagate_transforms_for,
};

use bevy_gizmos::transform_gizmo::{
    TransformGizmoAxis, TransformGizmoCamera, TransformGizmoFocus, TransformGizmoMeshMarker,
    TransformGizmoMode, TransformGizmoRoot, TransformGizmoSettings, TransformGizmoState,
    AXIS_START_OFFSET, COLOR_VIEW, COLOR_X, COLOR_Y, COLOR_Z, CONE_HEIGHT, CONE_RADIUS,
    INACTIVE_ALPHA, ROTATE_RING_RADIUS, SCALE_CUBE_SIZE, SHAFT_LENGTH, SHAFT_RADIUS,
    VIEW_CIRCLE_MAJOR, VIEW_CIRCLE_MINOR, VIEW_RING_MAJOR, VIEW_RING_MINOR,
};

/// The render layer used exclusively for gizmo meshes.
const GIZMO_RENDER_LAYER: usize = 15;

/// Marker for the internal overlay camera that renders gizmo meshes.
#[derive(Component)]
struct GizmoOverlayCamera;

#[derive(Resource)]
struct TransformGizmoMaterials {
    normal_colors: [Color; 4],
    highlight_colors: [Color; 4],
    inactive_colors: [Color; 4],
}

impl TransformGizmoMaterials {
    fn axis_index(axis: TransformGizmoAxis) -> usize {
        match axis {
            TransformGizmoAxis::X => 0,
            TransformGizmoAxis::Y => 1,
            TransformGizmoAxis::Z => 2,
            TransformGizmoAxis::View => 3,
        }
    }

    fn color(&self, axis: TransformGizmoAxis, highlight: bool, inactive: bool) -> Color {
        let i = Self::axis_index(axis);
        if highlight {
            self.highlight_colors[i]
        } else if inactive {
            self.inactive_colors[i]
        } else {
            self.normal_colors[i]
        }
    }
}

/// Plugin that adds mesh-based rendering for the transform gizmo.
///
/// Requires [`bevy_gizmos::transform_gizmo::TransformGizmoPlugin`] to be added first
/// for the interaction logic (hover, drag, state).
pub struct TransformGizmoRenderPlugin;

impl Plugin for TransformGizmoRenderPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(
            Startup,
            spawn_gizmo_meshes.run_if(
                bevy_ecs::schedule::common_conditions::resource_exists::<TransformGizmoSettings>,
            ),
        )
        .add_systems(
            PostUpdate,
            (
                update_gizmo_meshes,
                propagate_transforms_for::<
                    Or<(
                        With<TransformGizmoRoot>,
                        With<GizmoOverlayCamera>,
                        With<TransformGizmoMeshMarker>,
                    )>,
                >
                    .ambiguous_with_all(),
            )
                .chain()
                .after(bevy_transform::TransformSystems::Propagate)
                .after(bevy_camera::visibility::VisibilitySystems::VisibilityPropagate)
                .after(bevy_render::camera::camera_system)
                .after(bevy_camera::visibility::check_visibility_cpu_culling)
                .in_set(bevy_app::TransformGizmoRenderStep),
        );
    }
}

fn axis_vec(axis: TransformGizmoAxis) -> Vec3 {
    match axis {
        TransformGizmoAxis::X => Vec3::X,
        TransformGizmoAxis::Y => Vec3::Y,
        TransformGizmoAxis::Z => Vec3::Z,
        TransformGizmoAxis::View => Vec3::ZERO,
    }
}

fn make_unlit_material(color: Color) -> StandardMaterial {
    StandardMaterial {
        base_color: color,
        unlit: true,
        cull_mode: None,
        ..Default::default()
    }
}

fn highlight_color(color: Color) -> Color {
    let srgba = color.to_srgba();
    Color::srgba(
        (srgba.red * 1.4).min(1.0),
        (srgba.green * 1.4).min(1.0),
        (srgba.blue * 1.4).min(1.0),
        srgba.alpha,
    )
}

fn inactive_color(color: Color) -> Color {
    let srgba = color.to_srgba();
    Color::srgba(
        srgba.red * INACTIVE_ALPHA,
        srgba.green * INACTIVE_ALPHA,
        srgba.blue * INACTIVE_ALPHA,
        1.0,
    )
}

fn spawn_gizmo_meshes(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    let gizmo_layer = RenderLayers::layer(GIZMO_RENDER_LAYER);

    let colors = [COLOR_X, COLOR_Y, COLOR_Z, COLOR_VIEW];
    let mat_res = TransformGizmoMaterials {
        normal_colors: colors,
        highlight_colors: colors.map(highlight_color),
        inactive_colors: colors.map(inactive_color),
    };

    // Helper: create a unique unlit material for a given axis
    let mut make_mat = |axis: TransformGizmoAxis| {
        materials.add(make_unlit_material(
            colors[TransformGizmoMaterials::axis_index(axis)],
        ))
    };

    // Pre-create meshes
    let shaft_mesh = meshes.add(Cylinder::new(SHAFT_RADIUS, SHAFT_LENGTH).mesh().build());
    let cone_mesh = meshes.add(Cone::new(CONE_RADIUS, CONE_HEIGHT).mesh().build());
    let scale_cube_mesh = meshes.add(
        Cuboid::new(SCALE_CUBE_SIZE, SCALE_CUBE_SIZE, SCALE_CUBE_SIZE)
            .mesh()
            .build(),
    );
    let rotate_torus_mesh = meshes.add(
        Torus {
            minor_radius: 0.015,
            major_radius: ROTATE_RING_RADIUS,
        }
        .mesh()
        .build(),
    );
    let view_circle_mesh = meshes.add(
        Torus {
            minor_radius: VIEW_CIRCLE_MINOR,
            major_radius: VIEW_CIRCLE_MAJOR,
        }
        .mesh()
        .build(),
    );
    let view_ring_mesh = meshes.add(
        Torus {
            minor_radius: VIEW_RING_MINOR,
            major_radius: VIEW_RING_MAJOR,
        }
        .mesh()
        .build(),
    );
    let view_scale_mesh = meshes.add(
        Cuboid::new(
            SCALE_CUBE_SIZE * 1.5,
            SCALE_CUBE_SIZE * 1.5,
            SCALE_CUBE_SIZE * 1.5,
        )
        .mesh()
        .build(),
    );

    // Axis rotations: cylinder default is Y-up
    let axis_rotation = |axis: TransformGizmoAxis| -> Quat {
        match axis {
            TransformGizmoAxis::X => Quat::from_rotation_z(-core::f32::consts::FRAC_PI_2),
            TransformGizmoAxis::Y | TransformGizmoAxis::View => Quat::IDENTITY,
            TransformGizmoAxis::Z => Quat::from_rotation_x(core::f32::consts::FRAC_PI_2),
        }
    };

    // Spawn root
    let root_entity = commands
        .spawn((TransformGizmoRoot, Transform::IDENTITY, Visibility::Hidden))
        .id();

    // Helper: spawn a child mesh on the gizmo render layer
    let spawn_child = |commands: &mut Commands,
                       mesh: Handle<Mesh>,
                       material: Handle<StandardMaterial>,
                       transform: Transform,
                       axis: TransformGizmoAxis,
                       mode: TransformGizmoMode| {
        let child = commands
            .spawn((
                Mesh3d(mesh),
                MeshMaterial3d(material),
                transform,
                TransformGizmoMeshMarker { axis, mode },
                Visibility::Hidden,
                gizmo_layer.clone(),
            ))
            .id();
        commands.entity(child).insert(ChildOf(root_entity));
    };

    // --- Translate mode ---
    for axis in [
        TransformGizmoAxis::X,
        TransformGizmoAxis::Y,
        TransformGizmoAxis::Z,
    ] {
        let mat = make_mat(axis);
        spawn_child(
            &mut commands,
            shaft_mesh.clone(),
            mat.clone(),
            Transform::from_translation(axis_vec(axis) * (AXIS_START_OFFSET + SHAFT_LENGTH / 2.0))
                .with_rotation(axis_rotation(axis)),
            axis,
            TransformGizmoMode::Translate,
        );
        spawn_child(
            &mut commands,
            cone_mesh.clone(),
            mat,
            Transform::from_translation(
                axis_vec(axis) * (AXIS_START_OFFSET + SHAFT_LENGTH + CONE_HEIGHT / 2.0),
            )
            .with_rotation(axis_rotation(axis)),
            axis,
            TransformGizmoMode::Translate,
        );
    }

    // View-plane circle (translate)
    spawn_child(
        &mut commands,
        view_circle_mesh,
        make_mat(TransformGizmoAxis::View),
        Transform::IDENTITY,
        TransformGizmoAxis::View,
        TransformGizmoMode::Translate,
    );

    // --- Rotate mode ---
    for axis in [
        TransformGizmoAxis::X,
        TransformGizmoAxis::Y,
        TransformGizmoAxis::Z,
    ] {
        let mat = make_mat(axis);
        let torus_rot = match axis {
            TransformGizmoAxis::X => Quat::from_rotation_z(core::f32::consts::FRAC_PI_2),
            TransformGizmoAxis::Y | TransformGizmoAxis::View => Quat::IDENTITY,
            TransformGizmoAxis::Z => Quat::from_rotation_x(core::f32::consts::FRAC_PI_2),
        };
        spawn_child(
            &mut commands,
            rotate_torus_mesh.clone(),
            mat,
            Transform::from_rotation(torus_rot),
            axis,
            TransformGizmoMode::Rotate,
        );
    }

    // View-axis ring (rotate)
    spawn_child(
        &mut commands,
        view_ring_mesh,
        make_mat(TransformGizmoAxis::View),
        Transform::IDENTITY,
        TransformGizmoAxis::View,
        TransformGizmoMode::Rotate,
    );

    // --- Scale mode ---
    for axis in [
        TransformGizmoAxis::X,
        TransformGizmoAxis::Y,
        TransformGizmoAxis::Z,
    ] {
        let mat = make_mat(axis);
        spawn_child(
            &mut commands,
            shaft_mesh.clone(),
            mat.clone(),
            Transform::from_translation(axis_vec(axis) * (AXIS_START_OFFSET + SHAFT_LENGTH / 2.0))
                .with_rotation(axis_rotation(axis)),
            axis,
            TransformGizmoMode::Scale,
        );
        spawn_child(
            &mut commands,
            scale_cube_mesh.clone(),
            mat,
            Transform::from_translation(
                axis_vec(axis) * (AXIS_START_OFFSET + SHAFT_LENGTH + CONE_HEIGHT / 2.0),
            ),
            axis,
            TransformGizmoMode::Scale,
        );
    }

    // View-axis cube (scale)
    spawn_child(
        &mut commands,
        view_scale_mesh,
        make_mat(TransformGizmoAxis::View),
        Transform::IDENTITY,
        TransformGizmoAxis::View,
        TransformGizmoMode::Scale,
    );

    // --- Overlay camera ---
    // This camera renders only the gizmo layer, after the main camera (order: 1),
    // without clearing the color buffer — so gizmo meshes appear on top of everything.
    commands.spawn((
        Camera3d::default(),
        Camera {
            order: 1,
            ..Default::default()
        },
        GizmoOverlayCamera,
        RenderLayers::layer(GIZMO_RENDER_LAYER),
        Transform::default(),
    ));

    commands.insert_resource(mat_res);
}

fn update_gizmo_meshes(
    focus: Option<bevy_ecs::system::Single<&GlobalTransform, With<TransformGizmoFocus>>>,
    marked_cameras: Query<
        (&GlobalTransform, &Camera),
        (
            With<TransformGizmoCamera>,
            With<Camera>,
            Without<GizmoOverlayCamera>,
        ),
    >,
    all_cameras: Query<
        (&GlobalTransform, &Camera),
        (
            Without<GizmoOverlayCamera>,
            Without<TransformGizmoRoot>,
            With<Camera>,
        ),
    >,
    settings: Option<Res<TransformGizmoSettings>>,
    state: Option<Res<TransformGizmoState>>,
    materials_res: Option<Res<TransformGizmoMaterials>>,
    mut root_query: Query<
        (&mut Transform, &mut Visibility),
        (With<TransformGizmoRoot>, Without<TransformGizmoMeshMarker>),
    >,
    mut handle_query: Query<
        (
            &TransformGizmoMeshMarker,
            &mut Visibility,
            &MeshMaterial3d<StandardMaterial>,
        ),
        Without<TransformGizmoRoot>,
    >,
    mut std_materials: ResMut<Assets<StandardMaterial>>,
    mut overlay_cam: Query<
        (&mut Transform, &mut Camera),
        (
            With<GizmoOverlayCamera>,
            Without<TransformGizmoRoot>,
            Without<TransformGizmoMeshMarker>,
        ),
    >,
) {
    let (Some(materials_res), Some(settings), Some(state)) = (materials_res, settings, state)
    else {
        return;
    };

    let Ok((mut root_tf, mut root_vis)) = root_query.single_mut() else {
        return;
    };

    let Some(global_tf) = focus else {
        *root_vis = Visibility::Hidden;
        return;
    };
    let Some((cam_tf, cam)) = bevy_gizmos::resolve_gizmo_camera!(marked_cameras, all_cameras)
    else {
        *root_vis = Visibility::Hidden;
        return;
    };

    // Copy main camera transform to overlay camera
    if let Ok((mut overlay_tf, mut overlay_cam)) = overlay_cam.single_mut() {
        *overlay_tf = cam_tf.compute_transform();
        overlay_cam.viewport = cam.viewport.clone();
    }

    *root_vis = Visibility::Inherited;
    let pos = global_tf.translation();

    let space = bevy_gizmos::transform_gizmo::effective_space(&settings);
    let rotation = bevy_gizmos::transform_gizmo::gizmo_rotation(*global_tf, space);

    let scale = if settings.screen_scale_factor > 0.0 {
        (cam_tf.translation() - pos).length() * settings.screen_scale_factor
    } else {
        1.0
    };

    root_tf.translation = pos;
    root_tf.rotation = rotation;
    root_tf.scale = Vec3::splat(scale);

    let active_axis = if state.active {
        state.axis
    } else {
        state.hovered_axis
    };
    let dragging = state.active;

    for (handle, mut vis, mat) in &mut handle_query {
        if handle.mode != settings.mode {
            *vis = Visibility::Hidden;
            continue;
        }
        *vis = Visibility::Inherited;

        // Update the material color in-place (avoids writing MeshMaterial3d)
        let is_active = active_axis == Some(handle.axis);
        let desired_color = materials_res.color(handle.axis, is_active, dragging && !is_active);
        if let Some(mut material) = std_materials.get_mut(&mat.0)
            && material.base_color != desired_color
        {
            material.base_color = desired_color;
        }
    }
}