Skip to content

Commit

Permalink
rename LevelData to ChunkData
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 10, 2023
1 parent f0838a9 commit daf0eb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions assets/shaders/terrain.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct FragmentOutput {
@location(0) out_color: vec4<f32>,
}

struct LevelData {
struct ChunkData {
lod: u32,
resolution: u32,
}
Expand All @@ -17,7 +17,7 @@ struct LevelData {
@group(2) @binding(1) var s_terraindata: sampler;
@group(2) @binding(2) var t_grass: texture_2d<f32>;
@group(2) @binding(3) var s_grass: sampler;
@group(2) @binding(4) var<uniform> ldata: LevelData;
@group(2) @binding(4) var<uniform> cdata: ChunkData;

@group(3) @binding(0) var t_ssao: texture_2d<f32>;
@group(3) @binding(1) var s_ssao: sampler;
Expand Down
18 changes: 9 additions & 9 deletions assets/shaders/terrain.vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct VertexOutput {
@builtin(position) member: vec4<f32>,
}

struct LevelData {
struct ChunkData {
lod: u32,
resolution: u32,
distance_lod_cutoff: f32, // max distance at which to switch to the next lod to have smooth transitions
Expand All @@ -27,7 +27,7 @@ struct LevelData {

@group(2) @binding(0) var t_terraindata: texture_2d<u32>;
@group(2) @binding(1) var s_terraindata: sampler;
@group(2) @binding(4) var<uniform> ldata: LevelData;
@group(2) @binding(4) var<uniform> cdata: ChunkData;

/*
normal: vec3(self.cell_size * scale as f32, 0.0, hx - height)
Expand All @@ -51,29 +51,29 @@ fn unpack_diffs(v: u32) -> vec2<f32> {

@vertex
fn vert(@builtin(vertex_index) vid: u32, @location(0) in_position: vec2<f32>, @location(1) in_off: vec2<f32>) -> VertexOutput {
let tpos: vec2<i32> = vec2<i32>((in_position + in_off) * ldata.inv_cell_size);
let tpos: vec2<i32> = vec2<i32>((in_position + in_off) * cdata.inv_cell_size);

let texLoad: vec2<u32> = textureLoad(t_terraindata, tpos, 0).rg;

let height: f32 = unpack_height(texLoad.r);
let diffs: vec2<f32> = unpack_diffs(texLoad.g);

let step: i32 = i32(pow(2.0, f32(ldata.lod)));
let step: i32 = i32(pow(2.0, f32(cdata.lod)));

let zf_off: vec2<i32> = vec2( step * (i32(vid % ldata.resolution) % 2),
step * (i32(vid / ldata.resolution) % 2));
let zf_off: vec2<i32> = vec2( step * (i32(vid % cdata.resolution) % 2),
step * (i32(vid / cdata.resolution) % 2));

let world_pos: vec3<f32> = vec3(in_position + in_off, height);

//let dist_to_cam: f32 = length(params.cam_pos.xyz - vec3(pos.xy, 0.0));
//let transition_alpha: f32 = smoothstep(ldata.distance_lod_cutoff * 0.8, ldata.distance_lod_cutoff, dist_to_cam);
//let transition_alpha: f32 = smoothstep(cdata.distance_lod_cutoff * 0.8, cdata.distance_lod_cutoff, dist_to_cam);

var out_normal: vec3<f32> = normalize(vec3(diffs.x, diffs.y, ldata.cell_size * 2.0)); // https://stackoverflow.com/questions/49640250/calculate-normals-from-heightmap
var out_normal: vec3<f32> = normalize(vec3(diffs.x, diffs.y, cdata.cell_size * 2.0)); // https://stackoverflow.com/questions/49640250/calculate-normals-from-heightmap

let position: vec4<f32> = global.u_view_proj * vec4(world_pos, 1.0);

#ifdef DEBUG
var debug = f32(ldata.lod);
var debug = f32(cdata.lod);

if(height >= MAX_HEIGHT) {
debug = diffs.x;
Expand Down
8 changes: 4 additions & 4 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
let mut bgs = vec![];
for lod in 0..LOD {
let uni = Uniform::new(
LevelData {
TerrainChunkData {
lod: lod as u32,
resolution: 1 + CRESOLUTION as u32 / (1 << lod as u32),
distance_lod_cutoff: 2.0f32.powf(1.0 + LOD_MIN_DIST_LOG2 + lod as f32)
Expand Down Expand Up @@ -317,14 +317,14 @@ u8slice_impl!(TerrainInstance);

#[derive(Copy, Clone)]
#[repr(C)]
pub(crate) struct LevelData {
pub struct TerrainChunkData {
lod: u32, // 0 = highest resolution, 1 = half resolution, etc.
resolution: u32, // width of the vertex grid
distance_lod_cutoff: f32, // max distance at which to switch to the next lod to have smooth transitions
cell_size: f32,
inv_cell_size: f32,
}
u8slice_impl!(LevelData);
u8slice_impl!(TerrainChunkData);

const ATTRS: &[VertexAttribute] = &wgpu::vertex_attr_array![1 => Float32x2];

Expand Down Expand Up @@ -369,7 +369,7 @@ impl PipelineBuilder for TerrainPipeline {
.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &Texture::bindgroup_layout_entries(0, [TL::UInt, TL::Float].into_iter())
.chain(std::iter::once(
Uniform::<LevelData>::bindgroup_layout_entry(4),
Uniform::<TerrainChunkData>::bindgroup_layout_entry(4),
))
.collect::<Vec<_>>(),
label: Some("terrain bindgroup layout"),
Expand Down

0 comments on commit daf0eb3

Please sign in to comment.