ExampleGrowingRoot.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace SplineMesh {
  6. /// <summary>
  7. /// Example of component to show the deformation of the mesh on a changing
  8. /// interval and changing spline nodes.
  9. ///
  10. /// In this example, as the MeshBender is working on spline space, it will update
  11. /// the mesh if one of the curve change. Each change make the MeshBender "dirty" and
  12. /// it will compute the mesh only once on it's next update call.
  13. ///
  14. /// This component is only for demo purpose and is not intended to be used as-is.
  15. /// </summary>
  16. [ExecuteInEditMode]
  17. [RequireComponent(typeof(Spline))]
  18. public class ExampleGrowingRoot : MonoBehaviour {
  19. private GameObject generated;
  20. private Spline spline;
  21. private float rate = 0;
  22. private MeshBender meshBender;
  23. public Mesh mesh;
  24. public Material material;
  25. public Vector3 rotation;
  26. public Vector3 scale;
  27. public float startScale = 1;
  28. public float DurationInSecond;
  29. private void OnEnable() {
  30. rate = 0;
  31. Init();
  32. #if UNITY_EDITOR
  33. EditorApplication.update += EditorUpdate;
  34. #endif
  35. }
  36. void OnDisable() {
  37. #if UNITY_EDITOR
  38. EditorApplication.update -= EditorUpdate;
  39. #endif
  40. }
  41. private void OnValidate() {
  42. Init();
  43. }
  44. private void Update() {
  45. EditorUpdate();
  46. }
  47. void EditorUpdate() {
  48. rate += Time.deltaTime / DurationInSecond;
  49. if (rate > 1) {
  50. rate --;
  51. }
  52. Contort();
  53. }
  54. private void Contort() {
  55. float nodeDistance = 0;
  56. int i = 0;
  57. foreach (var n in spline.nodes) {
  58. float nodeDistanceRate = nodeDistance / spline.Length;
  59. float nodeScale = startScale * (rate - nodeDistanceRate);
  60. n.Scale = new Vector2(nodeScale, nodeScale);
  61. if (i < spline.curves.Count) {
  62. nodeDistance += spline.curves[i++].Length;
  63. }
  64. }
  65. if (generated != null) {
  66. meshBender.SetInterval(spline, 0, spline.Length * rate);
  67. meshBender.ComputeIfNeeded();
  68. }
  69. }
  70. private void Init() {
  71. string generatedName = "generated by " + GetType().Name;
  72. var generatedTranform = transform.Find(generatedName);
  73. generated = generatedTranform != null ? generatedTranform.gameObject : UOUtility.Create(generatedName, gameObject,
  74. typeof(MeshFilter),
  75. typeof(MeshRenderer),
  76. typeof(MeshBender));
  77. generated.GetComponent<MeshRenderer>().material = material;
  78. meshBender = generated.GetComponent<MeshBender>();
  79. spline = GetComponent<Spline>();
  80. meshBender.Source = SourceMesh.Build(mesh)
  81. .Rotate(Quaternion.Euler(rotation))
  82. .Scale(scale);
  83. meshBender.Mode = MeshBender.FillingMode.StretchToInterval;
  84. meshBender.SetInterval(spline, 0, 0.01f);
  85. }
  86. }
  87. }