RiverModeler.MicroVerse.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // River Modeler
  2. // Staggart Creations (http://staggart.xyz)
  3. // Copyright protected under Unity Asset Store EULA
  4. // Copying or referencing source code for the production of new asset store content is strictly prohibited.
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. #if MATHEMATICS
  9. using Unity.Mathematics;
  10. #endif
  11. #if SPLINES
  12. using UnityEngine.Splines;
  13. #endif
  14. #if MICROVERSE_SPLINES
  15. using SplinePath = JBooth.MicroVerseCore.SplinePath;
  16. #endif
  17. namespace sc.modeling.river.runtime
  18. {
  19. public partial class RiverModeler
  20. {
  21. //Unused code warnings
  22. #pragma warning disable CS0162
  23. //A bit of padding to ensure that the river geometry at least clips through the terrain
  24. private const float MV_WIDTH_PADDING = 0.5f;
  25. private const float MV_MIN_SMOOTHNESS = 1f;
  26. [Serializable]
  27. public class MicroVerseRiverSettings
  28. {
  29. [Header("River Bank (Height)")]
  30. [Min(0f)]
  31. public float bankHeight = 0.5f;
  32. [Min(0.01f)]
  33. public float bankWidth = 5f;
  34. [Space]
  35. [Min(0f)]
  36. public float bankFalloff;
  37. [Space]
  38. [Header("River Bank (Texture)")]
  39. public float bankSplatWidth;
  40. public float bankSplatSmoothness = 3f;
  41. [Space]
  42. [Header("River Bed")]
  43. [Min(0f)]
  44. public float bedDepth = 2f;
  45. [Min(0f)]
  46. public float bedSmoothness = 0f;
  47. [Space]
  48. public float offset;
  49. }
  50. public MicroVerseRiverSettings microVerseSettings;
  51. private readonly Keyframe[] heightCurveKeyframes = new Keyframe[3];
  52. #pragma warning restore CS0162
  53. public partial void UpdateMicroVerseSpline()
  54. {
  55. #if UNITY_EDITOR && MICROVERSE_SPLINES && SPLINES && MATHEMATICS
  56. if (!splineContainer) return;
  57. SplinePath splinePath = splineContainer.GetComponent<SplinePath>();
  58. if (splinePath)
  59. {
  60. if (splinePath.modifyHeightMap == false && splinePath.modifyHeightMap == false)
  61. {
  62. return;
  63. }
  64. var riverWidth = this.settings.shape.width;
  65. var m_offset = microVerseSettings.offset;
  66. var m_bankWidth = microVerseSettings.bankWidth;
  67. if (splinePath.modifyHeightMap)
  68. {
  69. heightCurveKeyframes[0].time = 0f;
  70. heightCurveKeyframes[0].value = microVerseSettings.bedDepth;
  71. heightCurveKeyframes[1].time = math.max(0.02f, microVerseSettings.bedSmoothness / riverWidth);
  72. heightCurveKeyframes[1].value = -microVerseSettings.bankHeight;
  73. heightCurveKeyframes[2].time = 1f;
  74. heightCurveKeyframes[2].value = 0f;
  75. splinePath.trench = 0f;
  76. splinePath.useTrenchCurve = true;
  77. splinePath.trenchCurve = new AnimationCurve(heightCurveKeyframes);
  78. splinePath.ClearCachedSplineTrenchCurve();
  79. m_bankWidth += microVerseSettings.bedSmoothness;
  80. splinePath.width = m_bankWidth;
  81. splinePath.smoothness = MV_MIN_SMOOTHNESS + microVerseSettings.bankFalloff;
  82. //Counter so that the smoothness only appears to go inwards
  83. m_offset -= microVerseSettings.bedSmoothness;
  84. }
  85. if (splinePath.modifySplatMap)
  86. {
  87. splinePath.splatWidth = microVerseSettings.bankSplatWidth;
  88. splinePath.splatSmoothness = microVerseSettings.bankSplatSmoothness;
  89. }
  90. //Copy the Scale (Width+Displacement) data into the spline's width data
  91. splinePath.splineWidths = new List<SplinePath.SplineWidthData>();
  92. float widthScale = (riverWidth * 0.5f) - MV_WIDTH_PADDING - -m_offset;
  93. foreach (var s in splineContainer.Splines)
  94. {
  95. SplinePath.SplineWidthData outWidth = new SplinePath.SplineWidthData();
  96. outWidth.widthData.PathIndexUnit = PathIndexUnit.Distance;
  97. foreach (SplineData<float3> scale in ScaleData)
  98. {
  99. for (int i = 0; i < scale.Count; i++)
  100. {
  101. outWidth.widthData.Add(scale[i].Index, scale[i].Value.x * widthScale);
  102. }
  103. splinePath.splineWidths.Add(outWidth);
  104. }
  105. }
  106. //Force an update
  107. splinePath.OnMoved();
  108. }
  109. #endif
  110. }
  111. }
  112. }