Files
IceSentry 88f9fdf738 Upstream bevy_infinite_grid (#23482)
# Objective

- Bevy is now at a point where multiple people are experimenting with
editors and pretty much all of them need an infinite grid
- There are various techniques that can be used to render an infinite
grid without artifacts. This one fades out the lines that are too far
from the camera. The general idea is that the grid is rendered in a
fullscreen pass.
- [bevy_infinite_grid](https://github.com/fslabs/bevy_infinite_grid) is
maintained by foresight spatial labs. It has been used by foresight and
other third party projects in production for many years at this point.
This PR upstreams that crate with full permission from foresight.

## Solution

- Upstream bevy_infinite_grid
- A few minor changes were done to bring it in line with the rest of
bevy
- In the process of upstreaming I noticed a few minor issues that I
fixed like using a fullscreen triangle instead of a quad and using
bevy's View instead of custom one.
- The infinite_grid is currently part of bevy_dev_tools since we don't
have an editor crate but I suspect we'll want to move it to an editor
crate down the line. Although I'm sure projects will want to use this
even if they aren't using the official editor.

## Testing

- I tested the example to confirm it works

---

## Showcase

<img width="1280" height="720" alt="infinite_grid_cSJj0G02fP"
src="https://github.com/user-attachments/assets/cacddc5e-9a54-454b-aefa-b7829c34227a"
/>

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2026-03-24 19:31:17 +00:00

69 lines
2.0 KiB
Rust

//! A simple example to show how to spawn an infinite grid.
//!
//! Infinite grids are useful as the ground plane in editor-like applications,
//! as they provide a consistent reference for the orientation of objects
//! and an evenly spaced grid to judge relative scale.
use bevy::{
camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
dev_tools::infinite_grid::{InfiniteGrid, InfiniteGridPlugin, InfiniteGridSettings},
prelude::*,
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
FreeCameraPlugin,
// Make sure to add the plugin
InfiniteGridPlugin,
))
.add_systems(Startup, setup_system)
.run();
}
fn setup_system(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
// You need to spawn an entity with this component
InfiniteGrid,
// Optional component you can use to configure the grid
InfiniteGridSettings::default(),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-12.5, 5.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
FreeCamera::default(),
));
commands.spawn((
DirectionalLight { ..default() },
Transform::from_translation(Vec3::X * 15. + Vec3::Y * 20.).looking_at(Vec3::ZERO, Vec3::Y),
));
// cube
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(
standard_materials.add(StandardMaterial::from_color(Color::srgba(
1.0, 1.0, 1.0, 0.5,
))),
),
Transform::from_xyz(0.0, 2.0, 0.0),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(
standard_materials.add(StandardMaterial::from_color(Color::srgba(
1.0, 1.0, 1.0, 0.5,
))),
),
Transform::from_xyz(0.0, -2.0, 0.0),
));
}