diff --git a/res/shader/terrain/voxel-bake.wgsl b/res/shader/terrain/voxel-bake.wgsl index f9494e5..cc60a6e 100644 --- a/res/shader/terrain/voxel-bake.wgsl +++ b/res/shader/terrain/voxel-bake.wgsl @@ -2,7 +2,7 @@ struct VoxelData { lod_count: vec4, - lods: array, + lods: array, 16>, occupancy: array>, } @group(0) @binding(0) var b_VoxelGrid: VoxelData; diff --git a/res/shader/terrain/voxel-draw.wgsl b/res/shader/terrain/voxel-draw.wgsl index 92f873b..0fdb7e6 100644 --- a/res/shader/terrain/voxel-draw.wgsl +++ b/res/shader/terrain/voxel-draw.wgsl @@ -10,7 +10,7 @@ struct VoxelConstants { struct VoxelData { lod_count: vec4, - lods: array, + lods: array, 16>, occupancy: array, } @@ -19,9 +19,9 @@ struct VoxelData { fn check_occupancy(coordinates: vec3, lod: u32) -> bool { let lod_info = b_VoxelGrid.lods[lod]; - let ramainder = coordinates % lod_info.dim; + let ramainder = coordinates % lod_info.xyz; // see https://github.com/gfx-rs/naga/issues/2122 - let sanitized = ramainder + select(lod_info.dim, vec3(0), ramainder >= vec3(0)); + let sanitized = ramainder + select(lod_info.xyz, vec3(0), ramainder >= vec3(0)); let addr = linearize(vec3(sanitized), lod_info); return (b_VoxelGrid.occupancy[addr.offset] & addr.mask) != 0u; } @@ -171,7 +171,8 @@ fn cast_ray_through_voxels(base: vec3, dir: vec3) -> CastPoint { let a = vec3((lod_voxel_pos + 0) * lod_voxel_size); let b = vec3((lod_voxel_pos + 1) * lod_voxel_size); // "tc" is the distance to each of the walls of the box - let tc = (select(a, b, dir > vec3(0.0)) - pos) / dir; + let c = mix(a, b, step(vec3(0.0), dir)); + let tc = (c - pos) / dir; // "t" is the closest distance to the boundary let t = min(tc.x, min(tc.y, tc.z)); let new_pos = pos + t * dir; @@ -274,7 +275,7 @@ fn vert_bound( var index = i32(inst_index); var coord = vec3(0); while (lod < b_VoxelGrid.lod_count.x) { - let dim = b_VoxelGrid.lods[lod].dim; + let dim = b_VoxelGrid.lods[lod].xyz; let total = i32(dim.x * dim.y * dim.z); if (index < total) { coord = vec3(index % dim.x, (index / dim.x) % dim.y, index / (dim.x * dim.y)); diff --git a/res/shader/terrain/voxel.inc.wgsl b/res/shader/terrain/voxel.inc.wgsl index b6991c6..193ba14 100644 --- a/res/shader/terrain/voxel.inc.wgsl +++ b/res/shader/terrain/voxel.inc.wgsl @@ -1,21 +1,16 @@ -struct VoxelLod { - dim: vec3, - offset: u32, -} - struct BitAddress { offset: u32, mask: u32, } -fn linearize(coords: vec3, vlod: VoxelLod) -> BitAddress { +fn linearize(coords: vec3, vlod: vec4) -> BitAddress { let words_per_tile = `morton_tile_size` * `morton_tile_size` * `morton_tile_size` / 32u; - let tile_counts = vec3(vlod.dim - 1) / vec3(`morton_tile_size`) + 1u; + let tile_counts = vec3(vlod.xyz - 1) / vec3(`morton_tile_size`) + 1u; let bit_index = encode_morton3(coords % vec3(`morton_tile_size`)); let tile_coord = coords / vec3(`morton_tile_size`); let tile_index = (tile_coord.z * tile_counts.y + tile_coord.y) * tile_counts.x + tile_coord.x; var addr: BitAddress; - addr.offset = vlod.offset + tile_index * words_per_tile + bit_index / 32u; + addr.offset = u32(vlod.w) + tile_index * words_per_tile + bit_index / 32u; addr.mask = 1u << (bit_index & 31u); return addr; }