pub struct Heightfield {
pub origin: Vec3,
pub cell: f64,
pub nx: usize,
pub ny: usize,
pub heights: Vec<f32>,
}Expand description
A regular grid of terrain heights, sampled bilinearly.
Node (ix, iy) sits at world (origin.x + ix·cell, origin.y + iy·cell)
with surface height origin.z + heights[iy·nx + ix]. Between nodes the
surface is the bilinear patch through the four surrounding nodes; outside
the grid the query is clamped to the border, so the terrain extends
flatly (per-edge) to infinity rather than falling away to zero. That
clamp is also what makes a 1×1 field an exact infinite plane.
Heights are stored as f32 deliberately: the GPU contact shader consumes
this buffer verbatim, and keeping one representation means the CPU and
GPU paths sample identical terrain instead of terrain that agrees to
rounding. Construction is a couple of allocations, cheap enough to draw a
fresh field per training iteration.
Fields§
§origin: Vec3World position of node (0, 0); origin.z is added to every height.
cell: f64Grid spacing in metres, same in x and y. Must be positive.
nx: usizeNumber of nodes along x. At least 1.
ny: usizeNumber of nodes along y. At least 1.
heights: Vec<f32>Row-major heights, ny rows of nx: heights[iy * nx + ix].
Implementations§
Source§impl Heightfield
impl Heightfield
Sourcepub fn new(origin: Vec3, cell: f64, nx: usize, ny: usize) -> Self
pub fn new(origin: Vec3, cell: f64, nx: usize, ny: usize) -> Self
A heightfield of nx × ny nodes spaced cell apart, all at height
zero above origin.
§Panics
If nx or ny is zero or cell is not a positive finite number.
Sourcepub fn flat(height: f64) -> Self
pub fn flat(height: f64) -> Self
The infinite flat plane at z = height: a single node whose border
clamp covers the whole world.
Sourcepub fn height(&self, x: f64, y: f64) -> f64
pub fn height(&self, x: f64, y: f64) -> f64
Surface height at world (x, y), bilinear between nodes, clamped to
the border outside the grid.
Sourcepub fn normal(&self, x: f64, y: f64) -> Vec3
pub fn normal(&self, x: f64, y: f64) -> Vec3
Unit surface normal at world (x, y).
The analytic normal of the bilinear patch: (-∂h/∂x, -∂h/∂y, 1)
normalized. Outside the grid the clamp makes the surface flat, so the
normal degrades to +ẑ there, and a 1×1 field reports +ẑ
everywhere — exactly the flat-plane contact normal.
Trait Implementations§
Source§impl Clone for Heightfield
impl Clone for Heightfield
Source§fn clone(&self) -> Heightfield
fn clone(&self) -> Heightfield
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more