Advertisement
Staggart

Scale Box Colliders to River Width

Jul 26th, 2024
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Splines;
  3.  
  4. namespace sc.modeling.river.runtime
  5. {
  6.     [ExecuteAlways]
  7.     public class ScaleRiverBoxColliders : MonoBehaviour
  8.     {
  9.         public RiverModeler river;
  10.         [Min(0)]
  11.         public int splineIndex = 0;
  12.  
  13.         public Vector3 baseScale = Vector3.one;
  14.        
  15.         private void Reset()
  16.         {
  17.             river = GetComponent<RiverModeler>();
  18.         }
  19.        
  20.         private void OnEnable()
  21.         {
  22.             //Subscribe
  23.             RiverModeler.onPostRebuildRiver += OnPostRebuild;
  24.         }
  25.  
  26.         private void OnPostRebuild(RiverModeler instance, RiverModeler.ChangeFlags changeFlags)
  27.         {
  28.             //Is the instance being rebuild the one we want to work with
  29.             if (instance == river)
  30.             {
  31.                 if (changeFlags.HasFlag(RiverModeler.ChangeFlags.Spline) || changeFlags.HasFlag(RiverModeler.ChangeFlags.Scale) || changeFlags.HasFlag(RiverModeler.ChangeFlags.Geometry))
  32.                 {
  33.                     UnityEngine.Splines.Interpolators.LerpFloat3 float3Interpolator = new UnityEngine.Splines.Interpolators.LerpFloat3();
  34.                     BoxCollider[] colliders = gameObject.GetComponentsInChildren<BoxCollider>();
  35.                     Debug.Log(colliders.Length);
  36.  
  37.                     foreach (BoxCollider box in colliders)
  38.                     {
  39.                         //Position of box collider in spline's local-space
  40.                         Vector3 samplePos = river.splineContainer.transform.InverseTransformPoint(box.transform.position + box.center);
  41.                         //Find the position on the spline that's nearest to the box's center
  42.                         SplineUtility.GetNearestPoint(river.splineContainer.Splines[splineIndex], samplePos, out var nearestPoint, out float t, SplineUtility.PickResolutionMin, 2);
  43.      
  44.                         //Convert the normalized t-index to the distances on the spline
  45.                         t = river.splineContainer.Splines[splineIndex].ConvertIndexUnit(t, PathIndexUnit.Normalized, PathIndexUnit.Distance);
  46.                        
  47.                         //Sample the scale data on the spline at the calculated distance
  48.                         Vector3 outputScale = river.ScaleData[splineIndex].Evaluate(river.splineContainer.Splines[splineIndex], t, river.ScaleData[splineIndex].PathIndexUnit, float3Interpolator);
  49.                         outputScale.x += river.settings.shape.width * 0.5f;
  50.  
  51.                         //Apply scale
  52.                         box.transform.localScale = Vector3.Scale(baseScale, outputScale);
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.  
  58.         private void OnDisable()
  59.         {
  60.             //Unsubscribe
  61.             RiverModeler.onPostRebuildRiver -= OnPostRebuild;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement