Skip to content

Commit

Permalink
Fix terrain.wgsl
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Feb 20, 2024
1 parent 741990d commit 845f592
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
19 changes: 2 additions & 17 deletions assets/shaders/terrain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ struct Rectangles {
count: u32,
};

@group(1) @binding(0)
@group(1) @binding(100)
var<uniform> circles: KdTree;
@group(1) @binding(1)
@group(1) @binding(101)
var<uniform> rectangles: Rectangles;
@group(1) @binding(2)
var terrain_texture: texture_2d<f32>;
@group(1) @binding(3)
var terrain_sampler: sampler;

fn mix_colors(base: vec4<f32>, cover: vec4<f32>) -> vec4<f32> {
let alpha = base.a * cover.a;
let rgb = base.rgb * cover.a + cover.rgb * (1. - cover.a);
Expand Down Expand Up @@ -176,16 +171,6 @@ 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
Expand Down
10 changes: 7 additions & 3 deletions crates/terrain/src/marker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::{
pbr::ExtendedMaterial,
prelude::*,
render::{
primitives::{Aabb as BevyAabb, Frustum},
Expand Down Expand Up @@ -115,10 +116,13 @@ impl Marker for RectangleMarker {

// TODO test this
fn update_markers<M>(
mut materials: ResMut<Assets<TerrainMaterial>>,
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, TerrainMaterial>>>,
solids: SolidObjects,
camera: Query<(&Transform, &Frustum), With<Camera3d>>,
terrains: Query<(&ViewVisibility, &Handle<TerrainMaterial>)>,
terrains: Query<(
&ViewVisibility,
&Handle<ExtendedMaterial<StandardMaterial, TerrainMaterial>>,
)>,
markers: Query<(
&ObjectTypeComponent,
&ViewVisibility,
Expand Down Expand Up @@ -177,6 +181,6 @@ fn update_markers<M>(
}

let material = materials.get_mut(material).unwrap();
M::apply_to_material(material, shapes.clone());
M::apply_to_material(&mut material.extension, shapes.clone());
}
}
31 changes: 21 additions & 10 deletions crates/terrain/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -17,7 +18,9 @@ pub(crate) struct TerrainPlugin;

impl Plugin for TerrainPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(MaterialPlugin::<TerrainMaterial>::default())
app.add_plugins(MaterialPlugin::<
ExtendedMaterial<StandardMaterial, TerrainMaterial>,
>::default())
.add_systems(OnEnter(AppState::InGame), load)
.add_systems(OnExit(AppState::InGame), cleanup)
.add_systems(
Expand Down Expand Up @@ -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()
}
Expand All @@ -83,14 +85,23 @@ fn setup_textures(
fn init(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<TerrainMaterial>>,
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, TerrainMaterial>>>,
textures: Res<Textures>,
uninitialized: Query<(Entity, &Terrain, &Transform), Without<Handle<Mesh>>>,
) {
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()
});
Expand Down
13 changes: 5 additions & 8 deletions crates/terrain/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<Image>,
}

impl TerrainMaterial {
pub(crate) fn new(texture: Handle<Image>) -> Self {
pub(crate) fn new() -> Self {
Self {
circles: KdTree::empty(),
rectangles: Rectangles::default(),
texture,
}
}

Expand All @@ -45,7 +42,7 @@ impl TerrainMaterial {
}
}

impl Material for TerrainMaterial {
impl MaterialExtension for TerrainMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/terrain.wgsl".into()
}
Expand Down

0 comments on commit 845f592

Please sign in to comment.