ExampleTentacle.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SplineMesh {
  6. /// <summary>
  7. /// Example of component to bend a mesh along a spline with some interpolation of scales and rolls. This component can be used as-is but will most likely be a base for your own component.
  8. ///
  9. /// For explanations of the base component, <see cref="ExamplePipe"/>
  10. ///
  11. /// In this component, we have added properties to make scale and roll vary between spline start and end.
  12. /// Intermediate scale and roll values are calculated at each spline node accordingly to the distance, then given to the MeshBenders component.
  13. /// MeshBender applies scales and rolls values by interpollation if they differ from strat to end of the curve.
  14. ///
  15. /// You can easily imagine a list of scales to apply to each node independantly to create your own variation.
  16. /// </summary>
  17. [DisallowMultipleComponent]
  18. public class ExampleTentacle : MonoBehaviour {
  19. private Spline spline { get => GetComponent<Spline>(); }
  20. public float startScale = 1, endScale = 1;
  21. public float startRoll = 0, endRoll = 0;
  22. private void OnValidate() {
  23. // apply scale and roll at each node
  24. float currentLength = 0;
  25. foreach (CubicBezierCurve curve in spline.GetCurves()) {
  26. float startRate = currentLength / spline.Length;
  27. currentLength += curve.Length;
  28. float endRate = currentLength / spline.Length;
  29. curve.n1.Scale = Vector2.one * (startScale + (endScale - startScale) * startRate);
  30. curve.n2.Scale = Vector2.one * (startScale + (endScale - startScale) * endRate);
  31. curve.n1.Roll = startRoll + (endRoll - startRoll) * startRate;
  32. curve.n2.Roll = startRoll + (endRoll - startRoll) * endRate;
  33. }
  34. }
  35. }
  36. }