add shading.rs

This commit is contained in:
yuni 2024-04-16 16:27:17 +02:00
parent 8248d43463
commit fc01b68086
3 changed files with 26 additions and 23 deletions

View file

@ -5,6 +5,7 @@ mod chat;
mod commands; mod commands;
mod effects; mod effects;
mod hud; mod hud;
mod shading;
mod var; mod var;
mod world; mod world;

21
src/shading.rs Normal file
View file

@ -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
}
}

View file

@ -1,6 +1,5 @@
use crate::{actor, audio, hud, nature, var}; use crate::{actor, audio, hud, nature, shading, var};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::math::{DVec3, I64Vec3}; use bevy::math::{DVec3, I64Vec3};
use bevy::scene::{InstanceId, SceneInstance}; use bevy::scene::{InstanceId, SceneInstance};
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
@ -53,7 +52,7 @@ impl Plugin for WorldPlugin {
app.add_systems(PostUpdate, handle_despawn); app.add_systems(PostUpdate, handle_despawn);
app.add_systems(Update, spawn_despawn_asteroids); app.add_systems(Update, spawn_despawn_asteroids);
app.add_plugins(PhysicsPlugins::default()); app.add_plugins(PhysicsPlugins::default());
app.add_plugins(MaterialPlugin::<RingMaterial>::default()); app.add_plugins(MaterialPlugin::<shading::JupitersRing>::default());
//app.add_plugins(PhysicsDebugPlugin::default()); //app.add_plugins(PhysicsDebugPlugin::default());
app.insert_resource(Gravity(DVec3::splat(0.0))); app.insert_resource(Gravity(DVec3::splat(0.0)));
app.insert_resource(AsteroidUpdateTimer( app.insert_resource(AsteroidUpdateTimer(
@ -97,24 +96,6 @@ pub struct DespawnEvent {
origin: I64Vec3, 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)] #[derive(Component)]
pub struct Star; pub struct Star;
@ -122,7 +103,7 @@ pub fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut materials_custom: ResMut<Assets<RingMaterial>>, mut materials_custom: ResMut<Assets<shading::JupitersRing>>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
) { ) {
// Load assets // Load assets
@ -205,7 +186,7 @@ pub fn setup(
commands.spawn(( commands.spawn((
MaterialMeshBundle { MaterialMeshBundle {
mesh: meshes.add(Mesh::from(Cylinder::new(ring_radius, 1.0))), 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, alpha_mode: AlphaMode::Blend,
ring_radius: ring_radius, ring_radius: ring_radius,
jupiter_radius: jupiter_radius, jupiter_radius: jupiter_radius,