Skip to main content

RiverGenerator

A class in Root.Township, Alta.Forests.

Part of a procedural forest: works out the river network from the forest's spaces and terrain heights, carves it into the terrain, paints the river bed and spawns the river visuals. The result is cached per forest seed.

Constructors​

MemberWhat it is
RiverGenerator()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

29 private and internal fields
MemberWhat it is
private Task<RiverGeneration> calculateTaskThe cached river generation task, started by GetGenerationAsync.
private float curveMaxWidthCap on the part of a river's radius that gets the depth curve and width scaling. Defaults to 3.
private float curveTangentMultiplierTangent strength for the spline that curves split segments. Defaults to 2.
private float decorationMultiplierPassed to the edge segment evaluation for river decorations.
private AnimationCurve depthCurveCurve from distance across the river (0 at the centre) to depth, scaled by the river's radius.
private float distanceNoiseRandom extra width added per terrain point, as a fraction of the radius, to roughen the banks.
private List<IRiverDecorationSegment> edgeSegmentsBacking field for EdgeSegments, filled by Calculate.
private ForestGenerator forestBacking field for Forest.
private RiverIntersections intersectionsBacking field for Intersections, built on first use.
private static Logger loggerNLog logger for this class.
private AnimationCurve lowerStrengthCurveHow strongly terrain above the river is carved down to it, by distance across.
private float maxVolumeLargest river volume, passed to the river generation.
private float nodeWidthMultiplierWidth multiplier passed to the river generation.
private float overgroundVolumeThresholdVolume a river needs to run overground, passed to the river generation. Defaults to 2.5.
private AnimationCurve raiseStrengthCurveHow strongly terrain below the river is raised to it, by distance across.
private float[,] riverDepthsPer terrain point river depth from Modify, used to paint the riverbed.
private int riverSegmentMinimumRivers with fewer overground segments than this upstream are marked too small. Defaults to 3.
private float[,] riverValuesPer terrain point river strength (0 to 1) from Modify, used for splat painting.
private List<IRiverDecorationSegment> segmentsBacking field for Segments, filled by Calculate.
private int splitDistanceLength each river path is split into, in metres; 0 or less means no split.
private float splitPositionJitterRandom offset radius for each split point. Defaults to 0.5.
private float[] staticWaterfallVolumeMultiplierVolume multipliers for the nodes after a waterfall's bottom.
private AnimationCurve volumePerNodeCurve from a space's biome wetness to the water volume it adds.
private float waterfallAlignmentThresholdA threshold set in the inspector. Not read in the part of the class shown in the server build.
private float waterfallCliffStrengthExtra weight a waterfall top's height gets when blending terrain heights. Defaults to 5.
private float waterfallSlopeSlope (height over length) above which a river segment becomes a waterfall. Defaults to 1.
private float waterfallVolumeMultiplierVolume multiplier for the node a waterfall drops into. Defaults to 1.2.
private BiomeAttribute wetnessAttributeBiome attribute read as wetness for each space's water volume.
private float widthMultiplierScales river width when carving terrain. Defaults to 1.

Properties​

MemberWhat it is
IEnumerable<IRiverDecorationSegment> EdgeSegments { get; }Decoration segments at river ends, filled when the river is worked out.
ForestGenerator Forest { get; }The forest generator this river belongs to.
RiverIntersections Intersections { get; }Where rivers cross things, worked out on first use. Needs the river to be generated first.
IEnumerable<IRiverDecorationSegment> Segments { get; }Decoration segments along the rivers.

Methods​

MemberWhat it is
float Distance(Vector2 point, bool isIncludingRadius)Distance from a point to the nearest overground river, optionally measured from the river's edge.
Task<RiverGeneration> GetGenerationAsync(ForestGeneration forest, TerrainAccess access)Works out the river network, or loads it from the cache. The same task is reused after the first call.
async Task Modify(ForestGeneration forest, TerrainAccess access)Carves the rivers and waterfalls into a terrain height map, in parallel per column.
void Paint(ForestGeneration forest, Random rowRandom, TerrainAccess access, int x, int z, Vector2 position, ref SplatData data)Paints river splat layers onto a terrain cell from the carved river values.
async Task SpawnVisuals(ForestGeneration forest)Client: has every IRiverVisual child build its visuals from the river network. Does nothing on a headless server.
RiverGeneration TryGetGenerated(bool isError = true)The river network if it's finished, otherwise null (logging an error unless told not to).
async Task Wipe(ForestGeneration forest)Deletes the cached river network for a forest.
3 private and internal methods
MemberWhat it is
private async Task<RiverGeneration> Calculate(ForestGeneration forest, TerrainAccess access)Builds the river network from the forest's spaces: a node per space, flow downhill, drop short rivers, split segments and add waterfalls. Cached by seed.
private Vector2 GetCatmullRomPosition(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float tangentStrength)Catmull-Rom spline point between two nodes, with adjustable tangent strength, used to curve split river segments.
private void OnDrawGizmos()Draws the generated river paths: blue overground, green waterfalls, red underground.

Used in​

Members of other types that take, return or hold a RiverGenerator: