merge shading.rs into load.rs

This commit is contained in:
yuni 2024-05-10 10:27:52 +02:00
parent 3c52a10a6d
commit 932a54b460
5 changed files with 85 additions and 99 deletions

View file

@ -15,7 +15,7 @@ use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use bevy::math::DVec3; use bevy::math::DVec3;
use bevy::pbr::{NotShadowCaster, NotShadowReceiver}; 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 regex::Regex;
use std::f32::consts::PI; use std::f32::consts::PI;
use std::f64::consts::PI as PI64; use std::f64::consts::PI as PI64;
@ -518,7 +518,7 @@ fn spawn_entities(
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut materials_jupiter: ResMut<Assets<shading::JupitersRing>>, mut materials_jupiter: ResMut<Assets<load::JupitersRing>>,
mut id2pos: ResMut<actor::Id2Pos>, mut id2pos: ResMut<actor::Id2Pos>,
settings: Res<var::Settings>, settings: Res<var::Settings>,
) { ) {
@ -818,7 +818,7 @@ fn spawn_entities(
world::DespawnOnPlayerDeath, world::DespawnOnPlayerDeath,
MaterialMeshBundle { MaterialMeshBundle {
mesh: meshes.add(Mesh::from(Cylinder::new(nature::JUPITER_RING_RADIUS as f32, 1.0))), 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, alpha_mode: AlphaMode::Blend,
ring_radius: nature::JUPITER_RING_RADIUS as f32, ring_radius: nature::JUPITER_RING_RADIUS as f32,
jupiter_radius: nature::JUPITER_RADIUS as f32, jupiter_radius: nature::JUPITER_RADIUS as f32,

View file

@ -11,8 +11,19 @@
// This module manages asset loading. // This module manages asset loading.
use bevy::ecs::system::EntityCommands; use bevy::ecs::system::EntityCommands;
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod};
use bevy::prelude::*; use bevy::prelude::*;
pub struct LoadPlugin;
impl Plugin for LoadPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(MaterialPlugin::<JupitersRing>::default());
app.add_plugins(MaterialPlugin::<SkyBox>::default());
app.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, AsteroidSurface, >>::default());
}
}
pub fn asset_name_to_path(name: &str) -> &'static str { pub fn asset_name_to_path(name: &str) -> &'static str {
match name { match name {
"suitv2" => "models/suit_v2/suit_v2.glb#Scene0", "suitv2" => "models/suit_v2/suit_v2.glb#Scene0",
@ -64,3 +75,68 @@ pub fn load_scene_by_path(
asset_server.load(&path_string) 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<StandardMaterial, AsteroidSurface> {
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,
}
}
}

View file

@ -18,7 +18,6 @@ mod chat;
mod commands; mod commands;
mod effects; mod effects;
mod hud; mod hud;
mod shading;
mod load; mod load;
mod var; mod var;
mod world; mod world;
@ -131,7 +130,7 @@ impl Plugin for OutFlyPlugin {
commands::CommandsPlugin, commands::CommandsPlugin,
effects::EffectsPlugin, effects::EffectsPlugin,
hud::HudPlugin, hud::HudPlugin,
shading::ShadingPlugin, load::LoadPlugin,
world::WorldPlugin, world::WorldPlugin,
)); ));
} }
@ -183,7 +182,7 @@ fn debug(
settings: Res<var::Settings>, settings: Res<var::Settings>,
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands, mut commands: Commands,
mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, shading::AsteroidSurface>>>, mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, load::AsteroidSurface>>>,
materials: Query<(Entity, Option<&Name>, &Handle<Mesh>)>, materials: Query<(Entity, Option<&Name>, &Handle<Mesh>)>,
) { ) {
if settings.dev_mode && keyboard_input.pressed(KeyCode::KeyP) { if settings.dev_mode && keyboard_input.pressed(KeyCode::KeyP) {
@ -191,7 +190,7 @@ fn debug(
dbg!(mesh); dbg!(mesh);
let mut entity = commands.entity(entity); let mut entity = commands.entity(entity);
entity.remove::<Handle<StandardMaterial>>(); entity.remove::<Handle<StandardMaterial>>();
let material = extended_materials.add(shading::AsteroidSurface::material()); let material = extended_materials.add(load::AsteroidSurface::material());
entity.insert(material); entity.insert(material);
} }
} }

View file

@ -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::<JupitersRing>::default());
app.add_plugins(MaterialPlugin::<SkyBox>::default());
app.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, AsteroidSurface, >>::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<StandardMaterial, AsteroidSurface> {
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,
}
}
}

View file

@ -10,7 +10,7 @@
// //
// This module populates the world with stars and asteroids. // 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::prelude::*;
use bevy::math::{DVec3, I64Vec3}; use bevy::math::{DVec3, I64Vec3};
use bevy::scene::{InstanceId, SceneInstance}; use bevy::scene::{InstanceId, SceneInstance};
@ -74,7 +74,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_skybox: ResMut<Assets<shading::SkyBox>>, mut materials_skybox: ResMut<Assets<load::SkyBox>>,
) { ) {
// Generate starmap // Generate starmap
let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(16, 16)); let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(16, 16));
@ -158,7 +158,7 @@ pub fn setup(
} }
commands.spawn(MaterialMeshBundle { commands.spawn(MaterialMeshBundle {
mesh: meshes.add(mesh), 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)), transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..default() ..default()
}); });