From fc01b6808604fe021cde2de46583b03231eb6ec5 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 16 Apr 2024 16:27:17 +0200 Subject: [PATCH] add shading.rs --- src/main.rs | 1 + src/shading.rs | 21 +++++++++++++++++++++ src/world.rs | 27 ++++----------------------- 3 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 src/shading.rs diff --git a/src/main.rs b/src/main.rs index ad0c8d2..775a476 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod chat; mod commands; mod effects; mod hud; +mod shading; mod var; mod world; diff --git a/src/shading.rs b/src/shading.rs new file mode 100644 index 0000000..e1da827 --- /dev/null +++ b/src/shading.rs @@ -0,0 +1,21 @@ +use bevy::prelude::*; +use bevy::render::render_resource::{AsBindGroup, ShaderRef}; + +#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +pub struct JupitersRing { + pub alpha_mode: AlphaMode, + #[uniform(0)] + pub ring_radius: f32, + #[uniform(1)] + pub jupiter_radius: f32, +} + +impl Material for JupitersRing { + fn fragment_shader() -> ShaderRef { + "shaders/jupiters_rings.wgsl".into() + } + fn alpha_mode(&self) -> AlphaMode { + self.alpha_mode + } +} + diff --git a/src/world.rs b/src/world.rs index 321fdd3..096a051 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,6 +1,5 @@ -use crate::{actor, audio, hud, nature, var}; +use crate::{actor, audio, hud, nature, shading, var}; use bevy::prelude::*; -use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::math::{DVec3, I64Vec3}; use bevy::scene::{InstanceId, SceneInstance}; use bevy_xpbd_3d::prelude::*; @@ -53,7 +52,7 @@ impl Plugin for WorldPlugin { app.add_systems(PostUpdate, handle_despawn); app.add_systems(Update, spawn_despawn_asteroids); app.add_plugins(PhysicsPlugins::default()); - app.add_plugins(MaterialPlugin::::default()); + app.add_plugins(MaterialPlugin::::default()); //app.add_plugins(PhysicsDebugPlugin::default()); app.insert_resource(Gravity(DVec3::splat(0.0))); app.insert_resource(AsteroidUpdateTimer( @@ -97,24 +96,6 @@ pub struct DespawnEvent { origin: I64Vec3, } -#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] -pub struct RingMaterial { - alpha_mode: AlphaMode, - #[uniform(0)] - ring_radius: f32, - #[uniform(1)] - jupiter_radius: f32, -} - -impl Material for RingMaterial { - fn fragment_shader() -> ShaderRef { - "shaders/jupiters_rings.wgsl".into() - } - fn alpha_mode(&self) -> AlphaMode { - self.alpha_mode - } -} - #[derive(Component)] pub struct Star; @@ -122,7 +103,7 @@ pub fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, - mut materials_custom: ResMut>, + mut materials_custom: ResMut>, asset_server: Res, ) { // Load assets @@ -205,7 +186,7 @@ pub fn setup( commands.spawn(( MaterialMeshBundle { mesh: meshes.add(Mesh::from(Cylinder::new(ring_radius, 1.0))), - material: materials_custom.add(RingMaterial { + material: materials_custom.add(shading::JupitersRing { alpha_mode: AlphaMode::Blend, ring_radius: ring_radius, jupiter_radius: jupiter_radius,