#[rustfmt::skip] use bevy::{ pbr::NotShadowCaster, prelude::*, reflect::{TypePath, TypeUuid}, render::render_resource::{AsBindGroup, ShaderRef}, }; use crate::orbit_cam::{self, OrbitCam}; pub struct TestScenePlugin; impl Plugin for TestScenePlugin { fn build(&self, app: &mut App) { app.insert_resource(ClearColor(Color::hsl(0.0, 0.0, 0.25))) .add_plugins(MaterialPlugin::::default()) .add_systems(Startup, spawn_scene) .add_systems( PostUpdate, move_plane_with_camera.after(orbit_cam::pan_orbit_camera), ); } } #[derive(Component)] struct WorldGridPlane; fn spawn_scene( mut cmd: Commands, mut meshes: ResMut>, mut std_mats: ResMut>, mut mat_proc_grid: ResMut>, ) { // Sphere cmd.spawn(PbrBundle { mesh: meshes.add(Mesh::try_from(shape::Icosphere::default()).unwrap()), material: std_mats.add(StandardMaterial { base_color: Color::WHITE, ..default() }), transform: Transform::IDENTITY, ..default() }); // Plane let plane = meshes.add(Mesh::from(shape::Plane { size: 200.0, subdivisions: 0, })); let proc_grid = mat_proc_grid.add(ProceduralGridMaterial {}); cmd.spawn(( SpatialBundle::from_transform(Transform::IDENTITY), WorldGridPlane, )) .with_children(|parent| { parent.spawn(( MaterialMeshBundle { mesh: plane.clone(), material: proc_grid.clone(), transform: Transform::IDENTITY, ..default() }, NotShadowCaster, )); parent.spawn(( MaterialMeshBundle { mesh: plane, material: proc_grid, transform: Transform::IDENTITY.looking_to(Vec3::Z, Vec3::NEG_Y), ..default() }, NotShadowCaster, )); }); } fn move_plane_with_camera( mut plane: Query<(&WorldGridPlane, &mut Transform)>, camera: Query<&OrbitCam, Changed>, ) { if let Ok(&OrbitCam { focus, .. }) = camera.get_single() { let (_, mut xform) = plane.single_mut(); xform.translation.x = focus.x; xform.translation.z = focus.z; } } #[derive(AsBindGroup, Clone, TypePath, TypeUuid)] #[uuid = "CE331A13-F247-4F5B-A75E-670B2BD5776A"] struct ProceduralGridMaterial {} impl Material for ProceduralGridMaterial { fn fragment_shader() -> ShaderRef { "shaders/procedural_grid.wgsl".into() } fn alpha_mode(&self) -> AlphaMode { AlphaMode::Blend } }