add basic grainy texture to material_asteroid

This commit is contained in:
yuni 2024-04-16 18:28:15 +02:00
parent 8afd150223
commit c880a8fb97
3 changed files with 108 additions and 15 deletions

View file

@ -1,9 +1,77 @@
#import bevy_pbr::{ #import bevy_pbr::{
mesh_view_bindings::globals, mesh_view_bindings::view,
forward_io::VertexOutput, pbr_fragment::pbr_input_from_standard_material,
pbr_functions::alpha_discard,
utils::coords_to_viewport_uv,
} }
@fragment #ifdef PREPASS_PIPELINE
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> { #import bevy_pbr::{
return vec4<f32>(0.0, 0.4, 0.0, 1.0); 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<uniform> 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<f32>(vec4<u32>(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<f32>(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>) -> f32 {
return clamp(sin(uv[0]+uv[2])+cos(uv[1]), 0.0, 1.0);
} }

View file

@ -16,6 +16,7 @@ use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode};
use bevy::diagnostic::FrameTimeDiagnosticsPlugin; use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode}; use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
use bevy::pbr::ExtendedMaterial;
use std::env; use std::env;
fn main() { fn main() {
@ -102,17 +103,15 @@ 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 materials_custom: ResMut<Assets<shading::AsteroidSurface>>, mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, shading::AsteroidSurface>>>,
materials: Query<(Entity, &Name, &Handle<StandardMaterial>)>, 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) {
for (entity, name, mat) in &materials { for (entity, _name, mesh) in &materials {
dbg!(name); dbg!(mesh);
dbg!(mat);
let mut entity = commands.entity(entity); let mut entity = commands.entity(entity);
entity.remove::<Handle<StandardMaterial>>(); entity.remove::<Handle<StandardMaterial>>();
let material = materials_custom.add(shading::AsteroidSurface { let material = extended_materials.add(shading::AsteroidSurface::material());
});
entity.insert(material); entity.insert(material);
} }
} }

View file

@ -1,11 +1,12 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod};
pub struct ShadingPlugin; pub struct ShadingPlugin;
impl Plugin for ShadingPlugin { impl Plugin for ShadingPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(MaterialPlugin::<JupitersRing>::default()); app.add_plugins(MaterialPlugin::<JupitersRing>::default());
app.add_plugins(MaterialPlugin::<AsteroidSurface>::default()); app.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, AsteroidSurface, >>::default());
} }
} }
@ -28,13 +29,38 @@ impl Material for JupitersRing {
} }
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] #[derive(Asset, Reflect, AsBindGroup, Debug, Clone)]
pub struct AsteroidSurface { pub struct AsteroidSurface {
#[uniform(100)]
quantize_steps: u32
} }
impl Material for AsteroidSurface { impl MaterialExtension for AsteroidSurface {
fn fragment_shader() -> ShaderRef { fn fragment_shader() -> ShaderRef {
"shaders/material_asteroid.wgsl".into() "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,
}
}
}