Skip to content

Commit

Permalink
simpler stitching code
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 11, 2023
1 parent 04e86bb commit 3ae46d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
34 changes: 12 additions & 22 deletions assets/shaders/terrain.vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct VertexOutput {

struct ChunkData {
lod: u32, // 0 = most details, 1 = half details, 2 = quarter details, etc.
lod_pow2: f32, // 2^lod
lod_pow2: u32, // 2^lod
resolution: u32, // number of vertices per side
distance_lod_cutoff: f32, // max distance at which to switch to the next lod to have smooth transitions
cell_size: f32, // size of a cell in world space at lod0
Expand Down Expand Up @@ -58,39 +58,29 @@ fn vert(@builtin(vertex_index) vid: u32,
let idx_x: u32 = vid % cdata.resolution;
let idx_y: u32 = vid / cdata.resolution;

var in_position: vec2<f32> = vec2(f32(idx_x), f32(idx_y)) * cdata.cell_size * cdata.lod_pow2;
var in_position: vec2<i32> = vec2(i32(idx_x), i32(idx_y));

let stitch_x_pos: bool = (stitch_dir_flags & 1u) != 0u;
let stitch_y_pos: bool = (stitch_dir_flags & 2u) != 0u;
let stitch_x_neg: bool = (stitch_dir_flags & 4u) != 0u;
let stitch_y_neg: bool = (stitch_dir_flags & 8u) != 0u;

if (stitch_x_pos && idx_x == cdata.resolution - 1u && (idx_y % 2u) == 1u) {
in_position = in_position - vec2<f32>(0.0, cdata.cell_size * cdata.lod_pow2);
if (idx_x == 0u) { // x_neg
in_position.y &= -1 << ((stitch_dir_flags & 4u) >> 2u);
}
if (stitch_x_neg && idx_x == 0u && (idx_y % 2u) == 1u) {
in_position = in_position - vec2<f32>(0.0, cdata.cell_size * cdata.lod_pow2);
else if (idx_x == cdata.resolution - 1u) { // x_pos
in_position.y &= -1 << (stitch_dir_flags & 1u);
}
if (stitch_y_pos && (vid >= (cdata.resolution * (cdata.resolution - 1u))) && ((idx_x) % 2u) == 1u) {
in_position = in_position - vec2<f32>(cdata.cell_size * cdata.lod_pow2, 0.0);
if (idx_y == 0u) { // y_neg
in_position.x &= -1 << ((stitch_dir_flags & 8u) >> 3u);
}
if (stitch_y_neg && (vid < cdata.resolution) && ((idx_x) % 2u) == 1u) {
in_position = in_position - vec2<f32>(cdata.cell_size * cdata.lod_pow2, 0.0);
else if (idx_y == cdata.resolution - 1u) { // y_pos
in_position.x &= -1 << ((stitch_dir_flags & 2u) >> 1u);
}

let tpos: vec2<i32> = vec2<i32>((in_position + in_off) * cdata.inv_cell_size);
let tpos: vec2<i32> = in_position * i32(cdata.lod_pow2) + vec2<i32>(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(cdata.lod)));

let zf_off: vec2<i32> = vec2( step * (i32(idx_x) % 2),
step * (i32(idx_y) % 2));

let world_pos: vec3<f32> = vec3(in_position + in_off, height);
let world_pos: vec3<f32> = vec3(vec2<f32>(in_position * i32(cdata.lod_pow2)) * cdata.cell_size + in_off, height);

//let dist_to_cam: f32 = length(params.cam_pos.xyz - vec3(pos.xy, 0.0));
//let transition_alpha: f32 = smoothstep(cdata.distance_lod_cutoff * 0.8, cdata.distance_lod_cutoff, dist_to_cam);
Expand Down
4 changes: 2 additions & 2 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
let uni = Uniform::new(
TerrainChunkData {
lod: lod as u32,
lod_pow2: scale as f32,
lod_pow2: scale,
resolution: 1 + CRESOLUTION as u32 / scale,
distance_lod_cutoff: 2.0f32.powf(1.0 + LOD_MIN_DIST_LOG2 + lod as f32)
- std::f32::consts::SQRT_2 * CSIZE as f32,
Expand Down Expand Up @@ -338,7 +338,7 @@ u8slice_impl!(TerrainInstance);
#[repr(C)]
pub struct TerrainChunkData {
lod: u32, // 0 = highest resolution, 1 = half resolution, etc.*
lod_pow2: f32, // 2^lod
lod_pow2: u32, // 2^lod
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,
Expand Down

0 comments on commit 3ae46d7

Please sign in to comment.