Skip to content

Commit

Permalink
dont handle edge of terrain properly
Browse files Browse the repository at this point in the history
it's ok, it shouldn't be seen anyway and simplifies the code a lot, also makes a nice texture size that's divisible by 2 enough
  • Loading branch information
Uriopass committed Dec 11, 2023
1 parent 3ae46d7 commit c2a2248
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
52 changes: 23 additions & 29 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext,
IndexType, PipelineBuilder, RenderParams, Texture, Uniform, TL,
IndexType, PipelineBuilder, RenderParams, Texture, TextureBuilder, Uniform, TL,
};
use geom::{vec2, vec3, Camera, InfiniteFrustrum, Intersect3, Matrix4, Vec2, AABB3};
use std::sync::Arc;
Expand Down Expand Up @@ -38,23 +38,33 @@ pub struct TerrainPrepared {

impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUTION> {
pub fn new(gfx: &mut GfxContext, w: u32, h: u32, grass: Arc<Texture>) -> Self {
debug_assert!(
CRESOLUTION >= 1 << LOD,
"TERRAIN RESOLUTION must be >= {}",
1 << LOD
);

let indices = Self::generate_indices_mesh(gfx);
let mut tex = Texture::create_fbo(
&gfx.device,
(w * CRESOLUTION as u32 + 1, h * CRESOLUTION as u32 + 1),

let tex = TextureBuilder::empty(
w * CRESOLUTION as u32,
h * CRESOLUTION as u32,
1,
TextureFormat::Rg16Uint,
TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
None,
);
tex.sampler = gfx.device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("texture sampler"),
)
.with_usage(TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING)
.with_fixed_mipmaps(LOD as u32)
.with_sampler(wgpu::SamplerDescriptor {
label: Some("terrain sampler"),
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
..Default::default()
});
})
.with_no_anisotropy()
.build(&gfx.device, &gfx.queue);

let mut bgs = vec![];
for lod in 0..LOD {
Expand Down Expand Up @@ -126,12 +136,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
[a[0], a[1], b, c]
}

// Need to add one more vertex on the edge of the map because when rendering a chunk
// we render "to the next chunk", which doesn't exist on the edge
let extrax = cell.0 + 1 == self.w;
let extray = cell.1 + 1 == self.h;
let mut contents =
Vec::with_capacity((CRESOLUTION + extrax as usize) * (CRESOLUTION + extray as usize));
let mut contents = Vec::with_capacity(CRESOLUTION * CRESOLUTION);

let mut holder_y_edge: [f32; CRESOLUTION] = [0.0; CRESOLUTION];
let mut j = 0;
Expand Down Expand Up @@ -161,23 +166,12 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
contents.extend(pack(height, dh_x, dh_y));
last_height = height;
}
if extrax {
contents.extend(pack(ys[ys.len() - 1], 0.0, 0.0));
}

last_ys = ys;
}
if extray {
for i in 0..CRESOLUTION {
contents.extend(pack(chunk[CRESOLUTION - 1][i], 0.0, 0.0));
}
if extrax {
contents.extend(pack(chunk[CRESOLUTION - 1][CRESOLUTION - 1], 0.0, 0.0));
}
}

let w = CRESOLUTION as u32 + extrax as u32;
let h = CRESOLUTION as u32 + extray as u32;
let h = CRESOLUTION as u32;
let w = CRESOLUTION as u32;

gfx.queue.write_texture(
ImageCopyTexture {
Expand Down
25 changes: 22 additions & 3 deletions engine/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ pub struct TextureBuilder<'a> {
mipmaps: Option<CompiledModule>,
mipmaps_no_gen: bool,
fixed_mipmaps: Option<u32>,
usage: TextureUsages,
no_anisotropy: bool,
}

impl<'a> TextureBuilder<'a> {
Expand All @@ -295,11 +297,21 @@ impl<'a> TextureBuilder<'a> {
self
}

pub fn with_usage(mut self, usage: TextureUsages) -> Self {
self.usage = usage;
self
}

pub fn with_sampler(mut self, sampler: SamplerDescriptor<'static>) -> Self {
self.sampler = sampler;
self
}

pub fn with_no_anisotropy(mut self) -> Self {
self.no_anisotropy = true;
self
}

pub fn with_srgb(mut self, srgb: bool) -> Self {
self.srgb = srgb;
self
Expand Down Expand Up @@ -377,6 +389,10 @@ impl<'a> TextureBuilder<'a> {
mipmaps: None,
mipmaps_no_gen: false,
fixed_mipmaps: None,
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
no_anisotropy: false,
}
}

Expand All @@ -391,6 +407,10 @@ impl<'a> TextureBuilder<'a> {
mipmaps: None,
mipmaps_no_gen: false,
fixed_mipmaps: None,
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
no_anisotropy: false,
}
}

Expand Down Expand Up @@ -461,9 +481,7 @@ impl<'a> TextureBuilder<'a> {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
usage: self.usage,
view_formats: &[],
});

Expand Down Expand Up @@ -512,6 +530,7 @@ impl<'a> TextureBuilder<'a> {
if mip_level_count > 1
&& (sampl.min_filter == wgpu::FilterMode::Linear
|| sampl.mag_filter == wgpu::FilterMode::Linear)
&& !self.no_anisotropy
{
sampl.anisotropy_clamp = 16;
}
Expand Down

0 comments on commit c2a2248

Please sign in to comment.