diff --git a/assets/shaders/terrain.wgsl b/assets/shaders/terrain.wgsl index 132f17f3..c75fba3e 100644 --- a/assets/shaders/terrain.wgsl +++ b/assets/shaders/terrain.wgsl @@ -2,18 +2,10 @@ pbr_fragment::pbr_input_from_standard_material, pbr_functions::alpha_discard, } - -#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 // How large (in meters) is a texture. const TEXTURE_SIZE = 16.; @@ -43,15 +35,10 @@ struct Rectangles { count: u32, }; -@group(1) @binding(0) +@group(1) @binding(100) var circles: KdTree; -@group(1) @binding(1) +@group(1) @binding(101) var rectangles: Rectangles; -@group(1) @binding(2) -var terrain_texture: texture_2d; -@group(1) @binding(3) -var terrain_sampler: sampler; - fn mix_colors(base: vec4, cover: vec4) -> vec4 { let alpha = base.a * cover.a; let rgb = base.rgb * cover.a + cover.rgb * (1. - cover.a); @@ -176,22 +163,8 @@ fn fragment( @builtin(front_facing) is_front: bool, ) -> FragmentOutput { var pbr_input = pbr_input_from_standard_material(in, is_front); - - pbr_input.material.perceptual_roughness = 0.8; - pbr_input.material.metallic = 0.23; - pbr_input.material.reflectance = 0.06; - - pbr_input.material.base_color = textureSample( - terrain_texture, - terrain_sampler, - in.uv / TEXTURE_SIZE - ); pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color); -#ifdef PREPASS_PIPELINE - // TODO remove this if deffered mode is not used - let out = deferred_output(in, pbr_input); -#else var out: FragmentOutput; out.color = apply_pbr_lighting(pbr_input); @@ -199,7 +172,6 @@ fn fragment( out.color = draw_rectangles(out.color, in.uv); out.color = main_pass_post_lighting_processing(pbr_input, out.color); -#endif return out; } diff --git a/crates/terrain/src/marker.rs b/crates/terrain/src/marker.rs index 3083ed92..316a40fc 100644 --- a/crates/terrain/src/marker.rs +++ b/crates/terrain/src/marker.rs @@ -1,4 +1,5 @@ use bevy::{ + pbr::ExtendedMaterial, prelude::*, render::{ primitives::{Aabb as BevyAabb, Frustum}, @@ -115,10 +116,13 @@ impl Marker for RectangleMarker { // TODO test this fn update_markers( - mut materials: ResMut>, + mut materials: ResMut>>, solids: SolidObjects, camera: Query<(&Transform, &Frustum), With>, - terrains: Query<(&ViewVisibility, &Handle)>, + terrains: Query<( + &ViewVisibility, + &Handle>, + )>, markers: Query<( &ObjectTypeComponent, &ViewVisibility, @@ -177,6 +181,6 @@ fn update_markers( } let material = materials.get_mut(material).unwrap(); - M::apply_to_material(material, shapes.clone()); + M::apply_to_material(&mut material.extension, shapes.clone()); } } diff --git a/crates/terrain/src/plugin.rs b/crates/terrain/src/plugin.rs index 19745889..ec95a0ae 100644 --- a/crates/terrain/src/plugin.rs +++ b/crates/terrain/src/plugin.rs @@ -1,9 +1,10 @@ use bevy::{ asset::{AssetPath, LoadState}, + pbr::ExtendedMaterial, prelude::*, render::{ render_resource::{AddressMode, SamplerDescriptor}, - texture::ImageSampler, + texture::{ImageAddressMode, ImageSampler, ImageSamplerDescriptor}, }, }; use de_core::{gamestate::GameState, state::AppState}; @@ -17,7 +18,9 @@ pub(crate) struct TerrainPlugin; impl Plugin for TerrainPlugin { fn build(&self, app: &mut App) { - app.add_plugins(MaterialPlugin::::default()) + app.add_plugins(MaterialPlugin::< + ExtendedMaterial, + >::default()) .add_systems(OnEnter(AppState::InGame), load) .add_systems(OnExit(AppState::InGame), cleanup) .add_systems( @@ -65,12 +68,11 @@ fn setup_textures( // // https://github.com/bevyengine/bevy/discussions/3972 let image = images.get_mut(&textures.0).unwrap(); - // TODO - // image.sampler_descriptor = ImageSampler::Descriptor(SamplerDescriptor { - // address_mode_u: AddressMode::Repeat, - // address_mode_v: AddressMode::Repeat, - // ..Default::default() - // }); + image.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor { + address_mode_u: ImageAddressMode::Repeat, + address_mode_v: ImageAddressMode::Repeat, + ..Default::default() + }); true.into() } @@ -83,14 +85,23 @@ fn setup_textures( fn init( mut commands: Commands, mut meshes: ResMut>, - mut materials: ResMut>, + mut materials: ResMut>>, textures: Res, uninitialized: Query<(Entity, &Terrain, &Transform), Without>>, ) { for (entity, terrain, transform) in uninitialized.iter() { commands.entity(entity).insert(MaterialMeshBundle { mesh: meshes.add(terrain.generate_mesh(transform.translation)), - material: materials.add(TerrainMaterial::new(textures.0.clone())), + material: materials.add(ExtendedMaterial { + base: StandardMaterial { + base_color_texture: Some(textures.0.clone()), + perceptual_roughness: 0.8, + metallic: 0.23, + reflectance: 0.06, + ..default() + }, + extension: TerrainMaterial::new(), + }), transform: *transform, ..Default::default() }); diff --git a/crates/terrain/src/shader.rs b/crates/terrain/src/shader.rs index 7372b6fc..0593b36d 100644 --- a/crates/terrain/src/shader.rs +++ b/crates/terrain/src/shader.rs @@ -2,6 +2,7 @@ use std::{cmp::Ordering, ops::Range}; use bevy::{ asset::Asset, + pbr::MaterialExtension, prelude::{Handle, Image, Material}, reflect::{TypePath, TypeUuid}, render::render_resource::{AsBindGroup, ShaderRef, ShaderType}, @@ -18,21 +19,17 @@ pub(crate) const RECTANGLE_CAPACITY: usize = 31; #[derive(Asset, AsBindGroup, TypeUuid, TypePath, Debug, Clone)] #[uuid = "9e124e04-fdf1-4836-b82d-fa2f01fddb62"] pub struct TerrainMaterial { - #[uniform(0)] + #[uniform(100)] circles: KdTree, - #[uniform(1)] + #[uniform(101)] rectangles: Rectangles, - #[texture(2)] - #[sampler(3)] - texture: Handle, } impl TerrainMaterial { - pub(crate) fn new(texture: Handle) -> Self { + pub(crate) fn new() -> Self { Self { circles: KdTree::empty(), rectangles: Rectangles::default(), - texture, } } @@ -45,7 +42,7 @@ impl TerrainMaterial { } } -impl Material for TerrainMaterial { +impl MaterialExtension for TerrainMaterial { fn fragment_shader() -> ShaderRef { "shaders/terrain.wgsl".into() }