I MADE A GRID USING WGSL
This commit is contained in:
parent
5adc76505d
commit
f43cf6728b
8 changed files with 264 additions and 163 deletions
61
src/infinite_grid.rs
Normal file
61
src/infinite_grid.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy::reflect::TypeUuid;
|
||||
use bevy::render::render_resource::AsBindGroup;
|
||||
use bevy::sprite::{MaterialMesh2dBundle, Material2d};
|
||||
|
||||
use crate::MainCamera;
|
||||
|
||||
#[derive(AsBindGroup, TypeUuid, Debug, Clone)]
|
||||
#[uuid = "b56a4075-b836-4866-a6f6-3e34ef3ae44f"]
|
||||
pub struct GridMaterial {
|
||||
#[uniform(0)]
|
||||
width: f32,
|
||||
#[uniform(1)]
|
||||
size: Vec2,
|
||||
#[uniform(2)]
|
||||
visible: u32,
|
||||
}
|
||||
|
||||
impl Material2d for GridMaterial {
|
||||
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
|
||||
"shaders/grid.wgsl".into()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_grid(
|
||||
mut coms: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut mats: ResMut<Assets<GridMaterial>>,
|
||||
) {
|
||||
|
||||
coms.spawn().insert_bundle(MaterialMesh2dBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1000.0 })).into(),
|
||||
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
material: mats.add(GridMaterial { width: 2.0, size: Vec2::splat(15.0), visible: 1 }),
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_grid_shader(
|
||||
snap_grid: Res<crate::SnapGrid>,
|
||||
handles: Query<&Handle<GridMaterial>>,
|
||||
mut mats: ResMut<Assets<GridMaterial>>,
|
||||
) {
|
||||
for h in handles.iter() {
|
||||
if let Some(mut m) = mats.get_mut(h) {
|
||||
m.size = Vec2::new(snap_grid.width, snap_grid.height);
|
||||
m.visible = snap_grid.visible as u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_grid_position(
|
||||
cam: Query<&GlobalTransform, With<MainCamera>>,
|
||||
mut grid: Query<&mut Transform, With<Handle<GridMaterial>>>,
|
||||
) {
|
||||
let mut cam_pos = cam.get_single().map(|c| c.translation()).unwrap_or(Vec3::ZERO);
|
||||
cam_pos.z = 0.0;
|
||||
for mut t in grid.iter_mut() {
|
||||
t.translation = cam_pos;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
#![feature(let_chains)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_egui::egui;
|
||||
|
||||
|
@ -7,6 +5,7 @@ pub mod create;
|
|||
pub mod helpers;
|
||||
pub mod modify;
|
||||
pub mod ui;
|
||||
pub mod infinite_grid;
|
||||
pub use modify::modify_sys;
|
||||
pub use create::create_sys;
|
||||
pub use helpers::*;
|
||||
|
@ -17,11 +16,12 @@ pub struct SnapGrid {
|
|||
pub height: f32,
|
||||
pub snap: bool,
|
||||
pub offset: Vec2,
|
||||
pub visible: bool,
|
||||
}
|
||||
impl SnapGrid {
|
||||
pub fn snap_to_grid(&self, v: Vec2) -> Vec2 {
|
||||
if self.snap {
|
||||
let w = v - self.offset;
|
||||
let w = v - self.offset + 0.5 * Vec2::new(self.width.copysign(v.x), self.height.copysign(v.y));
|
||||
let snapped = Vec2::new(w.x - w.x % self.width, w.y - w.y % self.height);
|
||||
|
||||
snapped + self.offset
|
||||
|
@ -33,7 +33,7 @@ impl SnapGrid {
|
|||
}
|
||||
impl Default for SnapGrid {
|
||||
fn default() -> Self {
|
||||
Self { width: 15.0, height: 15.0, snap: false, offset: Vec2::ZERO }
|
||||
Self { width: 15.0, height: 15.0, snap: false, offset: Vec2::ZERO, visible: true }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use bevy::ecs::schedule::ShouldRun;
|
||||
use bevy::input::mouse::{MouseMotion, MouseWheel};
|
||||
use bevy::math::Vec3Swizzles;
|
||||
use bevy::sprite::Material2dPlugin;
|
||||
use bevy::{prelude::*, window::PresentMode, winit::WinitSettings};
|
||||
use bevy_egui::{egui, EguiContext, EguiPlugin};
|
||||
use bevy_prototype_lyon::prelude::*;
|
||||
|
@ -31,11 +32,16 @@ fn main() {
|
|||
.add_plugins(DefaultPlugins)
|
||||
.add_plugin(EguiPlugin)
|
||||
.add_plugin(ShapePlugin)
|
||||
.add_plugin(Material2dPlugin::<infinite_grid::GridMaterial>::default())
|
||||
;
|
||||
|
||||
app
|
||||
.add_startup_system(configure_visuals)
|
||||
.add_startup_system(basic_setup_sys)
|
||||
.add_startup_system(infinite_grid::spawn_grid)
|
||||
;
|
||||
|
||||
app
|
||||
.add_system(create_sys.with_run_criteria(|state: Res<UiState>, mut ec: ResMut<EguiContext>|
|
||||
if !ec.ctx_mut().is_pointer_over_area() && state.current_action == Action::Create { ShouldRun::Yes } else { ShouldRun::No }
|
||||
))
|
||||
|
@ -51,6 +57,8 @@ fn main() {
|
|||
.add_system(drag_camera_sys)
|
||||
.add_system(zoom_camera_sys)
|
||||
.add_system(scale_points)
|
||||
.add_system(infinite_grid::update_grid_shader)
|
||||
.add_system(infinite_grid::update_grid_position)
|
||||
;
|
||||
|
||||
app.run();
|
||||
|
|
|
@ -88,7 +88,11 @@ pub fn grid_window_sys(
|
|||
.title_bar(true)
|
||||
.resizable(false)
|
||||
.show(egui_ctx.ctx_mut(), |ui| {
|
||||
ui.checkbox(&mut grid.snap, "Snap Enabled");
|
||||
ui.horizontal(|hui| {
|
||||
hui.checkbox(&mut grid.snap, "Snap Enabled");
|
||||
hui.checkbox(&mut grid.visible, "Visible");
|
||||
});
|
||||
|
||||
ui.label("Grid size:");
|
||||
ui.horizontal(|hui| {
|
||||
hui.label("Width:");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue