| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // River Modeler
- // Staggart Creations (http://staggart.xyz)
- // Copyright protected under Unity Asset Store EULA
- // Copying or referencing source code for the production of new asset store content is strictly prohibited.
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- #if MATHEMATICS
- using Unity.Mathematics;
- #endif
- #if SPLINES
- using UnityEngine.Splines;
- #endif
- #if MICROVERSE_SPLINES
- using SplinePath = JBooth.MicroVerseCore.SplinePath;
- #endif
- namespace sc.modeling.river.runtime
- {
- public partial class RiverModeler
- {
- //Unused code warnings
- #pragma warning disable CS0162
- //A bit of padding to ensure that the river geometry at least clips through the terrain
- private const float MV_WIDTH_PADDING = 0.5f;
- private const float MV_MIN_SMOOTHNESS = 1f;
-
- [Serializable]
- public class MicroVerseRiverSettings
- {
- [Header("River Bank (Height)")]
- [Min(0f)]
- public float bankHeight = 0.5f;
- [Min(0.01f)]
- public float bankWidth = 5f;
-
- [Space]
- [Min(0f)]
- public float bankFalloff;
-
- [Space]
-
- [Header("River Bank (Texture)")]
- public float bankSplatWidth;
- public float bankSplatSmoothness = 3f;
-
- [Space]
- [Header("River Bed")]
- [Min(0f)]
- public float bedDepth = 2f;
- [Min(0f)]
- public float bedSmoothness = 0f;
- [Space]
- public float offset;
- }
- public MicroVerseRiverSettings microVerseSettings;
-
- private readonly Keyframe[] heightCurveKeyframes = new Keyframe[3];
- #pragma warning restore CS0162
-
- public partial void UpdateMicroVerseSpline()
- {
- #if UNITY_EDITOR && MICROVERSE_SPLINES && SPLINES && MATHEMATICS
- if (!splineContainer) return;
- SplinePath splinePath = splineContainer.GetComponent<SplinePath>();
-
- if (splinePath)
- {
- if (splinePath.modifyHeightMap == false && splinePath.modifyHeightMap == false)
- {
- return;
- }
-
- var riverWidth = this.settings.shape.width;
- var m_offset = microVerseSettings.offset;
- var m_bankWidth = microVerseSettings.bankWidth;
-
- if (splinePath.modifyHeightMap)
- {
- heightCurveKeyframes[0].time = 0f;
- heightCurveKeyframes[0].value = microVerseSettings.bedDepth;
- heightCurveKeyframes[1].time = math.max(0.02f, microVerseSettings.bedSmoothness / riverWidth);
- heightCurveKeyframes[1].value = -microVerseSettings.bankHeight;
- heightCurveKeyframes[2].time = 1f;
- heightCurveKeyframes[2].value = 0f;
-
- splinePath.trench = 0f;
- splinePath.useTrenchCurve = true;
-
- splinePath.trenchCurve = new AnimationCurve(heightCurveKeyframes);
- splinePath.ClearCachedSplineTrenchCurve();
- m_bankWidth += microVerseSettings.bedSmoothness;
- splinePath.width = m_bankWidth;
- splinePath.smoothness = MV_MIN_SMOOTHNESS + microVerseSettings.bankFalloff;
- //Counter so that the smoothness only appears to go inwards
- m_offset -= microVerseSettings.bedSmoothness;
- }
- if (splinePath.modifySplatMap)
- {
- splinePath.splatWidth = microVerseSettings.bankSplatWidth;
- splinePath.splatSmoothness = microVerseSettings.bankSplatSmoothness;
- }
- //Copy the Scale (Width+Displacement) data into the spline's width data
- splinePath.splineWidths = new List<SplinePath.SplineWidthData>();
- float widthScale = (riverWidth * 0.5f) - MV_WIDTH_PADDING - -m_offset;
- foreach (var s in splineContainer.Splines)
- {
- SplinePath.SplineWidthData outWidth = new SplinePath.SplineWidthData();
- outWidth.widthData.PathIndexUnit = PathIndexUnit.Distance;
-
- foreach (SplineData<float3> scale in ScaleData)
- {
- for (int i = 0; i < scale.Count; i++)
- {
- outWidth.widthData.Add(scale[i].Index, scale[i].Value.x * widthScale);
- }
-
- splinePath.splineWidths.Add(outWidth);
- }
- }
-
- //Force an update
- splinePath.OnMoved();
- }
- #endif
- }
- }
- }
|