Skip to main content

infinite_grid/
infinite_grid.rs

1//! A simple example to show how to spawn an infinite grid.
2//!
3//! Infinite grids are useful as the ground plane in editor-like applications,
4//! as they provide a consistent reference for the orientation of objects
5//! and an evenly spaced grid to judge relative scale.
6
7use bevy::{
8    camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
9    dev_tools::infinite_grid::{InfiniteGrid, InfiniteGridPlugin, InfiniteGridSettings},
10    prelude::*,
11};
12
13fn main() {
14    App::new()
15        .add_plugins((
16            DefaultPlugins,
17            FreeCameraPlugin,
18            // Make sure to add the plugin
19            InfiniteGridPlugin,
20        ))
21        .add_systems(Startup, setup_system)
22        .run();
23}
24
25fn setup_system(
26    mut commands: Commands,
27    mut meshes: ResMut<Assets<Mesh>>,
28    mut standard_materials: ResMut<Assets<StandardMaterial>>,
29) {
30    commands.spawn((
31        // You need to spawn an entity with this component
32        InfiniteGrid,
33        // Optional component you can use to configure the grid
34        InfiniteGridSettings::default(),
35    ));
36
37    commands.spawn((
38        Camera3d::default(),
39        Transform::from_xyz(-12.5, 5.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
40        FreeCamera::default(),
41    ));
42
43    commands.spawn((
44        DirectionalLight { ..default() },
45        Transform::from_translation(Vec3::X * 15. + Vec3::Y * 20.).looking_at(Vec3::ZERO, Vec3::Y),
46    ));
47
48    // cube
49    commands.spawn((
50        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
51        MeshMaterial3d(
52            standard_materials.add(StandardMaterial::from_color(Color::srgba(
53                1.0, 1.0, 1.0, 0.5,
54            ))),
55        ),
56        Transform::from_xyz(0.0, 2.0, 0.0),
57    ));
58
59    commands.spawn((
60        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
61        MeshMaterial3d(
62            standard_materials.add(StandardMaterial::from_color(Color::srgba(
63                1.0, 1.0, 1.0, 0.5,
64            ))),
65        ),
66        Transform::from_xyz(0.0, -2.0, 0.0),
67    ));
68}