Skip to content

Commit

Permalink
Move gridconfig to plugin, hide grass using out of viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkponk committed Jan 23, 2023
1 parent 208459e commit 9181f0b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"cwd": "${workspaceFolder}"
}
]
}
}
9 changes: 7 additions & 2 deletions assets/shaders/grass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ fn vertex(vertex: Vertex,
out.world_position.z = out.world_position.z*growth;

// "Hide" grass under map (This can be done better, probably by sampling 5x times and adjusting nr_instances based on texture sum over chunk)
// Randomaly hide some in order to get a smooth transition from grass to ground
if growth<0.5 && growth/0.5<rand(vec2<f32>(f32(vertex.instance_index), 18.956724*cos(f32(vertex.instance_index)*2.9515))){
out.world_position.z = out.world_position.z+(-10.0);
out.clip_position = mesh_position_world_to_clip(out.world_position);
// Hide under map:
// out.world_position.z = out.world_position.z+(-10.0);
// out.clip_position = mesh_position_world_to_clip(out.world_position);

// Remove from view: (no perforamance gain compared to hiding under map)
out.clip_position = vec4<f32>(-2.0,-2.0,-2.0,-2.0);
return out;
}

Expand Down
19 changes: 10 additions & 9 deletions examples/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ fn main() {
.add_plugin(OrbitCameraPlugin)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.insert_resource(GrowthTextures::default())
.insert_resource(GridConfig {
grid_center_xy: [0.0, 0.0],
grid_half_extents: [
NR_SIDE_CHUNKS as f32 * CHUNK_SIZE / 2.0,
NR_SIDE_CHUNKS as f32 * CHUNK_SIZE / 2.0,
],
})
.add_plugin(ChunkInstancingPlugin)
.add_plugin(ChunkGrassPlugin)
.add_enter_system(GameState::InGame, setup)
Expand All @@ -113,9 +105,18 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
mut meshes: ResMut<Assets<Mesh>>,
grid_config: Res<GridConfig>,
mut grid_config: ResMut<GridConfig>,
mut growth_texture: ResMut<GrowthTextures>,
) {
//Set map size
*grid_config = GridConfig {
grid_center_xy: [0.0, 0.0],
grid_half_extents: [
NR_SIDE_CHUNKS as f32 * CHUNK_SIZE / 2.0,
NR_SIDE_CHUNKS as f32 * CHUNK_SIZE / 2.0,
],
};

//Growth Textures
*growth_texture = GrowthTextures::new(&mut images);

Expand Down
9 changes: 5 additions & 4 deletions src/rendering/chunk_grass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ use bytemuck::{Pod, Zeroable};

use noise::{NoiseFn, Perlin, Seedable};

use crate::camera::orbit::OrbitCamera;

use super::{Chunk, DistanceCulling};

//Bundle
Expand Down Expand Up @@ -114,6 +112,8 @@ impl Plugin for ChunkGrassPlugin {
app.add_plugin(ExtractComponentPlugin::<ChunkGrass>::extract_visible());
app.add_plugin(ExtractResourcePlugin::<GrowthTextures>::default());
app.add_plugin(ExtractResourcePlugin::<GridConfig>::default());
app.insert_resource(GridConfig::default());
app.insert_resource(GrowthTextures::default());
app.add_system(update_time_for_custom_material);
app.add_system(grass_chunk_distance_culling);

Expand Down Expand Up @@ -150,6 +150,7 @@ impl GrowthTextures {
let noise =
perlin.get([x as f64 * pattern_scale, y as f64 * pattern_scale]) as f32;
let value = (noise + 1.0) / 2.0 * scale;
// value = (value - 100.0).max(0.0); //Truncate short grass to 0.0
data.push(value as u8)
}
}
Expand All @@ -172,7 +173,7 @@ impl GrowthTextures {
}
}

#[derive(Clone)]
#[derive(Clone, Default)]
pub struct GridConfig {
pub grid_center_xy: [f32; 2], //Assume axis aligned grid otherwise need to calc homogenous coordinate matrix
pub grid_half_extents: [f32; 2],
Expand Down Expand Up @@ -397,7 +398,7 @@ fn prepare_grid_config_bind_group(
grid_config: Res<GridConfig>,
custom_pipeline: Res<CustomPipeline>,
) {
if !grid_config_bind_group_res.grid_config_bind_group.is_some() {
if grid_config.is_changed() {
let grid_config_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
label: Some("grid_config_buffer"),
contents: bytemuck::cast_slice(&[grid_config.to_raw()]),
Expand Down
2 changes: 0 additions & 2 deletions src/rendering/chunk_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use bevy::{

use rand::Rng;

use crate::camera::orbit::OrbitCamera;

use super::{Chunk, DistanceCulling};

//Bundle
Expand Down

0 comments on commit 9181f0b

Please sign in to comment.