Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Splines;
- namespace sc.modeling.river.runtime
- {
- [ExecuteAlways]
- public class ScaleRiverBoxColliders : MonoBehaviour
- {
- public RiverModeler river;
- [Min(0)]
- public int splineIndex = 0;
- public Vector3 baseScale = Vector3.one;
- private void Reset()
- {
- river = GetComponent<RiverModeler>();
- }
- private void OnEnable()
- {
- //Subscribe
- RiverModeler.onPostRebuildRiver += OnPostRebuild;
- }
- private void OnPostRebuild(RiverModeler instance, RiverModeler.ChangeFlags changeFlags)
- {
- //Is the instance being rebuild the one we want to work with
- if (instance == river)
- {
- if (changeFlags.HasFlag(RiverModeler.ChangeFlags.Spline) || changeFlags.HasFlag(RiverModeler.ChangeFlags.Scale) || changeFlags.HasFlag(RiverModeler.ChangeFlags.Geometry))
- {
- UnityEngine.Splines.Interpolators.LerpFloat3 float3Interpolator = new UnityEngine.Splines.Interpolators.LerpFloat3();
- BoxCollider[] colliders = gameObject.GetComponentsInChildren<BoxCollider>();
- Debug.Log(colliders.Length);
- foreach (BoxCollider box in colliders)
- {
- //Position of box collider in spline's local-space
- Vector3 samplePos = river.splineContainer.transform.InverseTransformPoint(box.transform.position + box.center);
- //Find the position on the spline that's nearest to the box's center
- SplineUtility.GetNearestPoint(river.splineContainer.Splines[splineIndex], samplePos, out var nearestPoint, out float t, SplineUtility.PickResolutionMin, 2);
- //Convert the normalized t-index to the distances on the spline
- t = river.splineContainer.Splines[splineIndex].ConvertIndexUnit(t, PathIndexUnit.Normalized, PathIndexUnit.Distance);
- //Sample the scale data on the spline at the calculated distance
- Vector3 outputScale = river.ScaleData[splineIndex].Evaluate(river.splineContainer.Splines[splineIndex], t, river.ScaleData[splineIndex].PathIndexUnit, float3Interpolator);
- outputScale.x += river.settings.shape.width * 0.5f;
- //Apply scale
- box.transform.localScale = Vector3.Scale(baseScale, outputScale);
- }
- }
- }
- }
- private void OnDisable()
- {
- //Unsubscribe
- RiverModeler.onPostRebuildRiver -= OnPostRebuild;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement