pub struct HeightMap { /* private fields */ }Expand description
A struct representing a height map.
Implementations§
Source§impl HeightMap
impl HeightMap
Sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Returns a new height map with the given width and height. Initially, all the values of the
height map are 0.0.
§Panics
If the width or the height is 0.
Sourcepub fn new_with_values(width: usize, height: usize, values: &[f32]) -> Self
pub fn new_with_values(width: usize, height: usize, values: &[f32]) -> Self
Returns a new height map with the given width and height, and a set of values.
§Panics
- If the
widthor theheightis 0. - If the length of
valuesis notwidth * height.
Sourcepub fn values_mut(&mut self) -> &mut [f32]
pub fn values_mut(&mut self) -> &mut [f32]
Returns the values of the height map.
Sourcepub fn value(&self, position: UPosition) -> f32
pub fn value(&self, position: UPosition) -> f32
Returns the value of the height map at the given position.
§Panics
If the position is outside the range of the height map.
Sourcepub fn set_value(&mut self, position: UPosition, value: f32)
pub fn set_value(&mut self, position: UPosition, value: f32)
Sets the value of the height map at the given position.
§Panics
If the position is outside the range of the height map.
Sourcepub fn interpolated_value(&self, position: FPosition) -> f32
pub fn interpolated_value(&self, position: FPosition) -> f32
Interpolates the value of the height map at the given position.
§Panics
If the position is outside the range of the height map.
Sourcepub fn slope(&self, position: UPosition) -> f32
pub fn slope(&self, position: UPosition) -> f32
Calculates the slope at the given position.
§Panics
If the position is outside the range of the height map.
Sourcepub fn normal(&self, position: FPosition, water_level: f32) -> [f32; 3]
pub fn normal(&self, position: FPosition, water_level: f32) -> [f32; 3]
Calculates the normal at the given position.
§Panics
If the position is outside the range of the height map.
Sourcepub fn count_cells(&self, min: f32, max: f32) -> usize
pub fn count_cells(&self, min: f32, max: f32) -> usize
Returns the number of cells that have a height between min and max, inclusive.
Sourcepub fn has_land_on_border(&self, water_level: f32) -> bool
pub fn has_land_on_border(&self, water_level: f32) -> bool
Returns whether there is any land along the edge of the height map. A result of false
implies that the map is an island.
Sourcepub fn normalize(&mut self, min: f32, max: f32)
pub fn normalize(&mut self, min: f32, max: f32)
Normalizes the values in the height map by scaling them proportionally such that the map’s
current smallest value will be set to min, and its largest value will be set to max, and
all values in-between will be the same as they were, relative to these new end points.
§Panics
If max > min.
§Examples
let mut hm = HeightMap::new_with_values(2, 5,
&[-25.0, -15.0, -10.0, -5.0, 0.0, 10.0, 20.0, 30.0, 40.0, 50.0]);
hm.normalize(-25.0, 20.0);
assert_eq!(hm.values(), [
-25.0, -19.0, -16.0, -13.0, -10.0, -4.0, 2.0, 8.0, 14.0, 20.0,
]);Sourcepub fn lerp(&self, other: &Self, coefficient: f32) -> Self
pub fn lerp(&self, other: &Self, coefficient: f32) -> Self
Linearly interpolate two height maps together.
Sourcepub fn add_hill(&mut self, position: FPosition, radius: f32, height: f32)
pub fn add_hill(&mut self, position: FPosition, radius: f32, height: f32)
Adds a hill (a half spheroid) at the given position, with a radius and a height.
If height == radius or -radius, the hill will be a half-sphere.
Sourcepub fn dig_hill(&mut self, position: FPosition, radius: f32, height: f32)
pub fn dig_hill(&mut self, position: FPosition, radius: f32, height: f32)
Takes the highest value (if height > 0) or the lowest (if height < 0) between the map
and the hill. Its main goal is to carve things into maps (like rivers) by digging hills
along a curve.
Sourcepub fn dig_bezier(
&mut self,
positions: [UPosition; 4],
start_radius: f32,
start_depth: f32,
end_radius: f32,
end_depth: f32,
)
pub fn dig_bezier( &mut self, positions: [UPosition; 4], start_radius: f32, start_depth: f32, end_radius: f32, end_depth: f32, )
Carves a path along a cubic Bezier curve using the dig_hill method. Could be used for
generating roads, rivers, etc. Both radius and depth can vary linearly along the path. The
four positions are the 4 Bezier control points.
Sourcepub fn rain_erosion<A: RandomAlgorithm>(
&mut self,
drops: u32,
erosion_coefficient: f32,
aggregation_coefficient: f32,
random: &mut Random<A>,
)
pub fn rain_erosion<A: RandomAlgorithm>( &mut self, drops: u32, erosion_coefficient: f32, aggregation_coefficient: f32, random: &mut Random<A>, )
Simulates the effect of rain drops on the terrain, resulting in erosion patterns.
§Parameters
drops- The number of rain drops to simulate. Should be at leastwidth * height.erosion_coefficient- The amount of ground eroded on the drop’s path.aggregation_coefficient- The amount of ground deposited when the drops stops to flow.random- The random number generator to use.
Sourcepub fn kernel_transform(
&mut self,
cells: &[NeighborCell],
min_level: f32,
max_level: f32,
)
pub fn kernel_transform( &mut self, cells: &[NeighborCell], min_level: f32, max_level: f32, )
Apply a generic transformation on the height map, so that each resulting cell value is the weighted sum of several neighbour cells. This can be used to, e.g. smooth/sharpen the map.
§Examples
Do simple horizontal smoothing with direct neighbor cells.
let mut hm =
HeightMap::new_with_values(3, 3, &[3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0]);
let cells = [
NeighborCell { relative_position: Position::new(-1, 0), weight: 0.33 },
NeighborCell { relative_position: Position::new(0, 0), weight: 0.33 },
NeighborCell { relative_position: Position::new(1, 0), weight: 0.33 },
];
hm.kernel_transform(&cells, 0.0, 100.0);
assert_eq!(hm.values(), &[4.5, 6.5, 7.75, 13.5, 15.5, 16.75, 22.5, 24.5, 25.75])Sourcepub fn add_voronoi<A: RandomAlgorithm>(
&mut self,
sites: usize,
coefficients: &[f32],
random: &mut Random<A>,
)
pub fn add_voronoi<A: RandomAlgorithm>( &mut self, sites: usize, coefficients: &[f32], random: &mut Random<A>, )
Adds values from a Voronoi diagram to the height map.
Sourcepub fn mid_point_displacement<A: RandomAlgorithm>(
&mut self,
random: &mut Random<A>,
roughness: f32,
)
pub fn mid_point_displacement<A: RandomAlgorithm>( &mut self, random: &mut Random<A>, roughness: f32, )
Generates a height map with mid-point displacement.
The mid-point displacement algorithm generates a realistic fractal height map using the diamond-square (aka random midpoint displacement) algorithm.
The roughness range should be comprised between 0.4 and 0.6.
§Panics
If the width or the height is 0.
Sourcepub fn add_fbm<A: NoiseAlgorithm>(
&mut self,
noise: &mut Noise<A>,
octaves: f32,
coordinates: FbmCoordinateParameters,
delta: f32,
scale: f32,
)
pub fn add_fbm<A: NoiseAlgorithm>( &mut self, noise: &mut Noise<A>, octaves: f32, coordinates: FbmCoordinateParameters, delta: f32, scale: f32, )
Add an FBM to the height map.
The noise value for map cell (x, y) is (x + add_x) * mul_x / width and
(y + add_y) * mul_y / height, respectively. Those values allow you to scale and translate
the noise function over the height map.
§Panics
If the noise provided isn’t 2D.
Sourcepub fn scale_fbm<A: NoiseAlgorithm>(
&mut self,
noise: &mut Noise<A>,
coordinates: FbmCoordinateParameters,
octaves: f32,
delta: f32,
scale: f32,
)
pub fn scale_fbm<A: NoiseAlgorithm>( &mut self, noise: &mut Noise<A>, coordinates: FbmCoordinateParameters, octaves: f32, delta: f32, scale: f32, )
Scale the map by an FBM.
The noise coordinate for map cell (x, y) is (x + add_x) * mul_x / width and
(y + add_y) * mul_y / height, respectively. Those values allow you to scale and translate
the noise function over the height map.
The value multiplied with the height map is delta + noise * scale.
§Panics
If the noise generator provided isn’t 2D.
Trait Implementations§
Source§impl AddAssign<f32> for HeightMap
impl AddAssign<f32> for HeightMap
Source§fn add_assign(&mut self, rhs: f32)
fn add_assign(&mut self, rhs: f32)
+= operation. Read more