Stylized Water 3 - River Modeler (Extension)
4.Scripting
Scripts are in the StylizedWater3.RiverModeler namespace.
Events
RiverModeler.onPreRebuildRiver += OnRiverRebuild;
RiverModeler.onPostRebuildRiver += OnRiverRebuild;
These callbacks returns the Instance being modified and the ChangeFlags (specifying which aspect prompted the rebuild, if any).
Physics Trigger callbacks
You can then, in any other script, subscribe to these static events:
RiverSegment.onTriggerEnter(RiverSegment segment, Collider other);RiverSegment.onTriggerStay(RiverSegment segment, Collider other);RiverSegment.onTriggerExit(RiverSegment segment, Collider other);
These events are excellent to handle behaviour specific to rivers, such as detecting the nearest position to the spline (eg. finding a drinking spot).
Note that you can access the river the segment belongs to through its River property. Which in turn has a Owner property (the River Modeler) component. Traversing the hierarchy up this way allows you to even access the Spline it was made with.
Sampling the river surface
Generally, you can sample the same spline used by a river to get its position/direction/normal using Spline.Evalutate(t);. But the River Modeler component contains a helper function that simplifies this:
RiverModeler.FindNearestPointOnRiver(float3 worldPosition, out float3 point, out float3 direction, out float3 normal, out float t, float sampleDistance = 1f);
This is excellent to use for controlling particle effects, swimming behaviour or buoyancy physics!
Procedural Splines
If you’re looking to generate a spline from a set of points, that is certainly possible. Unity’s Splines API contains this function which can be used for it:
FitSplineToPoints(List<float3> positions, float errorThreshold, bool closed, out Spline spline);
If you have the Spline Extensions asset installed, you can use this multithreaded variant that’s x26 times faster:
CreateSplineFromPoints.Execute(List<float3> positions, float errorThreshold, bool closed, out Spline spline, float3 up = default);
