From 932a54b460f7817b1166ea6c4ac863a55e3a1725 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 10 May 2024 10:27:52 +0200 Subject: [PATCH] merge shading.rs into load.rs --- src/commands.rs | 6 ++-- src/load.rs | 76 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 ++-- src/shading.rs | 89 ------------------------------------------------- src/world.rs | 6 ++-- 5 files changed, 85 insertions(+), 99 deletions(-) delete mode 100644 src/shading.rs diff --git a/src/commands.rs b/src/commands.rs index ffee5e0..30b2ae4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -15,7 +15,7 @@ use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; use bevy::math::DVec3; use bevy::pbr::{NotShadowCaster, NotShadowReceiver}; -use crate::{actor, camera, chat, hud, load, nature, shading, var, world}; +use crate::{actor, camera, chat, hud, load, nature, var, world}; use regex::Regex; use std::f32::consts::PI; use std::f64::consts::PI as PI64; @@ -518,7 +518,7 @@ fn spawn_entities( asset_server: Res, mut meshes: ResMut>, mut materials: ResMut>, - mut materials_jupiter: ResMut>, + mut materials_jupiter: ResMut>, mut id2pos: ResMut, settings: Res, ) { @@ -818,7 +818,7 @@ fn spawn_entities( world::DespawnOnPlayerDeath, MaterialMeshBundle { mesh: meshes.add(Mesh::from(Cylinder::new(nature::JUPITER_RING_RADIUS as f32, 1.0))), - material: materials_jupiter.add(shading::JupitersRing { + material: materials_jupiter.add(load::JupitersRing { alpha_mode: AlphaMode::Blend, ring_radius: nature::JUPITER_RING_RADIUS as f32, jupiter_radius: nature::JUPITER_RADIUS as f32, diff --git a/src/load.rs b/src/load.rs index 1afd168..3c98304 100644 --- a/src/load.rs +++ b/src/load.rs @@ -11,8 +11,19 @@ // This module manages asset loading. use bevy::ecs::system::EntityCommands; +use bevy::render::render_resource::{AsBindGroup, ShaderRef}; +use bevy::pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod}; use bevy::prelude::*; +pub struct LoadPlugin; +impl Plugin for LoadPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(MaterialPlugin::::default()); + app.add_plugins(MaterialPlugin::::default()); + app.add_plugins(MaterialPlugin::>::default()); + } +} + pub fn asset_name_to_path(name: &str) -> &'static str { match name { "suitv2" => "models/suit_v2/suit_v2.glb#Scene0", @@ -64,3 +75,68 @@ pub fn load_scene_by_path( asset_server.load(&path_string) } } + +// Jupiter's Ring +#[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 + } +} + +// Sky Box +#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +pub struct SkyBox {} +impl Material for SkyBox { + fn fragment_shader() -> ShaderRef { + "shaders/skybox.wgsl".into() + } +} + +// Asteroid Surface +#[derive(Asset, Reflect, AsBindGroup, Debug, Clone)] +pub struct AsteroidSurface { + #[uniform(100)] + quantize_steps: u32 +} + +impl MaterialExtension for AsteroidSurface { + fn fragment_shader() -> ShaderRef { + "shaders/material_asteroid.wgsl".into() + } + fn deferred_fragment_shader() -> ShaderRef { + "shaders/material_asteroid.wgsl".into() + } +} + +impl AsteroidSurface { + pub fn material() -> ExtendedMaterial { + ExtendedMaterial { + base: StandardMaterial { + base_color: Color::rgb(0.29, 0.255, 0.208), + perceptual_roughness: 1.0, + opaque_render_method: OpaqueRendererMethod::Auto, + ..default() + }, + extension: Self::default(), + } + } +} +impl Default for AsteroidSurface { + fn default() -> Self { + Self { + quantize_steps: 3, + } + } +} diff --git a/src/main.rs b/src/main.rs index eae243e..6f8b3f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,6 @@ mod chat; mod commands; mod effects; mod hud; -mod shading; mod load; mod var; mod world; @@ -131,7 +130,7 @@ impl Plugin for OutFlyPlugin { commands::CommandsPlugin, effects::EffectsPlugin, hud::HudPlugin, - shading::ShadingPlugin, + load::LoadPlugin, world::WorldPlugin, )); } @@ -183,7 +182,7 @@ fn debug( settings: Res, keyboard_input: Res>, mut commands: Commands, - mut extended_materials: ResMut>>, + mut extended_materials: ResMut>>, materials: Query<(Entity, Option<&Name>, &Handle)>, ) { if settings.dev_mode && keyboard_input.pressed(KeyCode::KeyP) { @@ -191,7 +190,7 @@ fn debug( dbg!(mesh); let mut entity = commands.entity(entity); entity.remove::>(); - let material = extended_materials.add(shading::AsteroidSurface::material()); + let material = extended_materials.add(load::AsteroidSurface::material()); entity.insert(material); } } diff --git a/src/shading.rs b/src/shading.rs deleted file mode 100644 index bbe72ae..0000000 --- a/src/shading.rs +++ /dev/null @@ -1,89 +0,0 @@ -// ▄████████▄ + ███ + ▄█████████ ███ + -// ███▀ ▀███ + + ███ ███▀ + ███ + + -// ███ + ███ ███ ███ █████████ ███ ███ ███ ███ -// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███ -// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███ -// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███ -// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████ -// + + + ███ -// + ▀████████████████████████████████████████████████████▀ -// -// This module manages graphics shaders. - -use bevy::prelude::*; -use bevy::render::render_resource::{AsBindGroup, ShaderRef}; -use bevy::pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod}; - -pub struct ShadingPlugin; -impl Plugin for ShadingPlugin { - fn build(&self, app: &mut App) { - app.add_plugins(MaterialPlugin::::default()); - app.add_plugins(MaterialPlugin::::default()); - app.add_plugins(MaterialPlugin::>::default()); - } -} - -// Jupiter's Ring -#[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 - } -} - -// Sky Box -#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] -pub struct SkyBox {} -impl Material for SkyBox { - fn fragment_shader() -> ShaderRef { - "shaders/skybox.wgsl".into() - } -} - -// Asteroid Surface -#[derive(Asset, Reflect, AsBindGroup, Debug, Clone)] -pub struct AsteroidSurface { - #[uniform(100)] - quantize_steps: u32 -} - -impl MaterialExtension for AsteroidSurface { - fn fragment_shader() -> ShaderRef { - "shaders/material_asteroid.wgsl".into() - } - fn deferred_fragment_shader() -> ShaderRef { - "shaders/material_asteroid.wgsl".into() - } -} - -impl AsteroidSurface { - pub fn material() -> ExtendedMaterial { - ExtendedMaterial { - base: StandardMaterial { - base_color: Color::rgb(0.29, 0.255, 0.208), - perceptual_roughness: 1.0, - opaque_render_method: OpaqueRendererMethod::Auto, - ..default() - }, - extension: Self::default(), - } - } -} -impl Default for AsteroidSurface { - fn default() -> Self { - Self { - quantize_steps: 3, - } - } -} diff --git a/src/world.rs b/src/world.rs index f9db8d8..db7de0d 100644 --- a/src/world.rs +++ b/src/world.rs @@ -10,7 +10,7 @@ // // This module populates the world with stars and asteroids. -use crate::{actor, hud, load, nature, shading}; +use crate::{actor, hud, load, nature}; use bevy::prelude::*; use bevy::math::{DVec3, I64Vec3}; use bevy::scene::{InstanceId, SceneInstance}; @@ -74,7 +74,7 @@ pub fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, - mut materials_skybox: ResMut>, + mut materials_skybox: ResMut>, ) { // Generate starmap let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(16, 16)); @@ -158,7 +158,7 @@ pub fn setup( } commands.spawn(MaterialMeshBundle { mesh: meshes.add(mesh), - material: materials_skybox.add(shading::SkyBox {}), + material: materials_skybox.add(load::SkyBox {}), transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)), ..default() });