Skip to content

Commit

Permalink
add heightmap override system
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 4, 2024
1 parent 0e17c41 commit 69f88d3
Show file tree
Hide file tree
Showing 19 changed files with 455 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: rust-build
on:
push:
branches:
- master
- '*'
tags-ignore:
- v* # don't run on tags since release does that
pull_request:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wgpu::{
VertexAttribute, VertexBufferLayout,
};

use geom::{vec2, vec3, Camera, Intersect3, Matrix4, Vec2, AABB3};
use geom::{vec2, vec3, Camera, HeightmapChunk, Intersect3, Matrix4, Vec2, AABB3};

use crate::{
bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext,
Expand All @@ -26,7 +26,7 @@ pub struct TerrainChunk {

/// CSIZE is the size of a chunk in meters
/// CRESOLUTION is the resolution of a chunk, in vertices, at the chunk data level (not LOD0 since we upsample)
pub struct TerrainRender<const CSIZE: usize, const CRESOLUTION: usize> {
pub struct TerrainRender<const CSIZE: u32, const CRESOLUTION: usize> {
terrain_tex: Arc<Texture>,
normal_tex: Arc<Texture>,

Expand All @@ -48,7 +48,7 @@ pub struct TerrainPrepared {
instances: [(PBuffer, u32); LOD],
}

impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUTION> {
impl<const CSIZE: u32, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUTION> {
const LOD0_RESOLUTION: usize = CRESOLUTION * (1 << UPSCALE_LOD);

pub fn new(gfx: &mut GfxContext, w: u32, h: u32) -> Self {
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
&mut self,
gfx: &mut GfxContext,
cell: (u32, u32),
chunk: &[[f32; CRESOLUTION]; CRESOLUTION],
chunk: &HeightmapChunk<CRESOLUTION, CSIZE>,
) {
fn pack(height: f32) -> [u8; 2] {
let h_encoded = ((height.clamp(MIN_HEIGHT, MAX_HEIGHT) - MIN_HEIGHT)
Expand All @@ -172,9 +172,9 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT

let mut contents = Vec::with_capacity(CRESOLUTION * CRESOLUTION * 2);

for ys in chunk.iter() {
for &v in ys {
contents.extend(pack(v));
for y in 0..CRESOLUTION {
for x in 0..CRESOLUTION {
contents.extend(pack(chunk.height_idx(x, y).unwrap()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions engine_demo/src/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use geom::{vec2, Camera, Heightmap, HeightmapChunk, LinearColor, Vec3};

use crate::DemoElement;

const CSIZE: usize = 512;
const CSIZE: u32 = 512;
const CRESO: usize = 16;
const MAP_SIZE: usize = 50;

Expand Down Expand Up @@ -58,7 +58,7 @@ impl DemoElement for Terrain {
terrain.update_chunk(
gfx,
(x as u32, y as u32),
h.get_chunk((x as u16, y as u16)).unwrap().heights(),
h.get_chunk((x as u16, y as u16)).unwrap(),
);
}
}
Expand Down
22 changes: 22 additions & 0 deletions geom/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ impl AABB {
}
}

#[inline]
pub fn intersection(self, other: AABB) -> AABB {
let v = AABB {
ll: self.ll.max(other.ll),
ur: self.ur.min(other.ur),
};
if v.ll.x > v.ur.x || v.ll.y > v.ur.y {
AABB::zero()
} else {
v
}
}

#[inline]
pub fn offset(self, offset: Vec2) -> AABB {
AABB {
ll: self.ll + offset,
ur: self.ur + offset,
}
}

#[inline]
pub fn size(&self) -> Vec2 {
self.ur - self.ll
}
Expand Down
8 changes: 8 additions & 0 deletions geom/src/aabb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ impl AABB3 {
}
}

#[inline]
pub fn flatten(self) -> AABB {
AABB {
ll: self.ll.xy(),
ur: self.ur.xy(),
}
}

#[inline]
pub fn w(&self) -> f32 {
self.ur.x - self.ll.x
Expand Down
6 changes: 6 additions & 0 deletions geom/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub struct Circle {
pub radius: f32,
}

impl Circle {
pub fn contains(&self, p: Vec2) -> bool {
self.center.is_close(p, self.radius)
}
}

impl Circle {
#[inline]
pub fn new(center: Vec2, radius: f32) -> Self {
Expand Down
Loading

0 comments on commit 69f88d3

Please sign in to comment.