diff --git a/assets/shaders/material_asteroid.wgsl b/assets/shaders/material_asteroid.wgsl index 5a935ae..c2cfef3 100644 --- a/assets/shaders/material_asteroid.wgsl +++ b/assets/shaders/material_asteroid.wgsl @@ -1,9 +1,77 @@ #import bevy_pbr::{ - mesh_view_bindings::globals, - forward_io::VertexOutput, + mesh_view_bindings::view, + pbr_fragment::pbr_input_from_standard_material, + pbr_functions::alpha_discard, + utils::coords_to_viewport_uv, } -@fragment -fn fragment(in: VertexOutput) -> @location(0) vec4 { - return vec4(0.0, 0.4, 0.0, 1.0); +#ifdef PREPASS_PIPELINE +#import bevy_pbr::{ + prepass_io::{VertexOutput, FragmentOutput}, + pbr_deferred_functions::deferred_output, +} +#else +#import bevy_pbr::{ + forward_io::{VertexOutput, FragmentOutput}, + pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing}, +} +#endif + +struct MyExtendedMaterial { + quantize_steps: u32, +} + +@group(2) @binding(100) +var my_extended_material: MyExtendedMaterial; + +@fragment +fn fragment( + in: VertexOutput, + @builtin(front_facing) is_front: bool, +) -> FragmentOutput { + // generate a PbrInput struct from the StandardMaterial bindings + var pbr_input = pbr_input_from_standard_material(in, is_front); + + // we can optionally modify the input before lighting and alpha_discard is applied + //pbr_input.material.base_color.b = pbr_input.material.base_color.r; + + // alpha discard + pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color); + +#ifdef PREPASS_PIPELINE + // in deferred mode we can't modify anything after that, as lighting is run in a separate fullscreen shader. + let out = deferred_output(in, pbr_input); +#else + var out: FragmentOutput; + // apply lighting + out.color = apply_pbr_lighting(pbr_input); + + // we can optionally modify the lit color before post-processing is applied + //out.color = vec4(vec4(out.color * f32(my_extended_material.quantize_steps))) / f32(my_extended_material.quantize_steps); + + // apply in-shader post processing (fog, alpha-premultiply, and also tonemapping, debanding if the camera is non-hdr) + // note this does not include fullscreen postprocessing effects like bloom. + out.color = main_pass_post_lighting_processing(pbr_input, out.color); + + // we can optionally modify the final result here + //out.color = out.color * 2.0; + +//#ifdef VERTEX_UV +// out.color = out.color * grain(in.u, in.v); +// out.color = vec4(0.0, 1.0, 0.0, 1.0); +//#else +// // Why am I not getting in.uv?? +// let viewport_uv = coords_to_viewport_uv(in.position.xy, view.viewport); +// out.color = out.color * grain(viewport_uv); +//#endif + out.color = out.color * grain(in.position.xyz); + +#endif + + + return out; +} + +fn grain(uv: vec3) -> f32 { + return clamp(sin(uv[0]+uv[2])+cos(uv[1]), 0.0, 1.0); } diff --git a/src/main.rs b/src/main.rs index 51864dc..12a05fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode}; use bevy::diagnostic::FrameTimeDiagnosticsPlugin; use bevy::prelude::*; use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode}; +use bevy::pbr::ExtendedMaterial; use std::env; fn main() { @@ -102,17 +103,15 @@ fn debug( settings: Res, keyboard_input: Res>, mut commands: Commands, - mut materials_custom: ResMut>, - materials: Query<(Entity, &Name, &Handle)>, + mut extended_materials: ResMut>>, + materials: Query<(Entity, Option<&Name>, &Handle)>, ) { if settings.dev_mode && keyboard_input.pressed(KeyCode::KeyP) { - for (entity, name, mat) in &materials { - dbg!(name); - dbg!(mat); + for (entity, _name, mesh) in &materials { + dbg!(mesh); let mut entity = commands.entity(entity); entity.remove::>(); - let material = materials_custom.add(shading::AsteroidSurface { - }); + let material = extended_materials.add(shading::AsteroidSurface::material()); entity.insert(material); } } diff --git a/src/shading.rs b/src/shading.rs index c69cb7b..3de398a 100644 --- a/src/shading.rs +++ b/src/shading.rs @@ -1,11 +1,12 @@ 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()); } } @@ -28,13 +29,38 @@ impl Material for JupitersRing { } -#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +#[derive(Asset, Reflect, AsBindGroup, Debug, Clone)] pub struct AsteroidSurface { + #[uniform(100)] + quantize_steps: u32 } -impl Material for AsteroidSurface { +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, + } + } +}