SourceMesh.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace SplineMesh {
  6. /// <summary>
  7. /// This class returns a transformed version of a given source mesh, plus others
  8. /// informations to help bending the mesh along a curve.
  9. /// It is imutable to ensure better performances.
  10. ///
  11. /// To obtain an instance, call the static method <see cref="Build(Mesh)"/>.
  12. /// The building is made in a fluent way.
  13. /// </summary>
  14. public struct SourceMesh {
  15. private Vector3 translation;
  16. private Quaternion rotation;
  17. private Vector3 scale;
  18. internal Mesh Mesh { get; }
  19. private List<MeshVertex> vertices;
  20. internal List<MeshVertex> Vertices {
  21. get {
  22. if (vertices == null) BuildData();
  23. return vertices;
  24. }
  25. }
  26. private int[] triangles;
  27. internal int[] Triangles {
  28. get {
  29. if (vertices == null) BuildData();
  30. return triangles;
  31. }
  32. }
  33. private float minX;
  34. internal float MinX {
  35. get {
  36. if (vertices == null) BuildData();
  37. return minX;
  38. }
  39. }
  40. private float length;
  41. internal float Length {
  42. get {
  43. if (vertices == null) BuildData();
  44. return length;
  45. }
  46. }
  47. /// <summary>
  48. /// constructor is private to enable fluent builder pattern.
  49. /// Use <see cref="Build(Mesh)"/> to obtain an instance.
  50. /// </summary>
  51. /// <param name="mesh"></param>
  52. private SourceMesh(Mesh mesh) {
  53. Mesh = mesh;
  54. translation = default(Vector3);
  55. rotation = default(Quaternion);
  56. scale = default(Vector3);
  57. vertices = null;
  58. triangles = null;
  59. minX = 0;
  60. length = 0;
  61. }
  62. /// <summary>
  63. /// copy constructor
  64. /// </summary>
  65. /// <param name="other"></param>
  66. private SourceMesh(SourceMesh other) {
  67. Mesh = other.Mesh;
  68. translation = other.translation;
  69. rotation = other.rotation;
  70. scale = other.scale;
  71. vertices = null;
  72. triangles = null;
  73. minX = 0;
  74. length = 0;
  75. }
  76. public static SourceMesh Build(Mesh mesh) {
  77. return new SourceMesh(mesh);
  78. }
  79. public SourceMesh Translate(Vector3 translation) {
  80. var res = new SourceMesh(this) {
  81. translation = translation
  82. };
  83. return res;
  84. }
  85. public SourceMesh Translate(float x, float y, float z) {
  86. return Translate(new Vector3(x, y, z));
  87. }
  88. public SourceMesh Rotate(Quaternion rotation) {
  89. var res = new SourceMesh(this) {
  90. rotation = rotation
  91. };
  92. return res;
  93. }
  94. public SourceMesh Scale(Vector3 scale) {
  95. var res = new SourceMesh(this) {
  96. scale = scale
  97. };
  98. return res;
  99. }
  100. public SourceMesh Scale(float x, float y, float z) {
  101. return Scale(new Vector3(x, y, z));
  102. }
  103. private void BuildData() {
  104. // if the mesh is reversed by scale, we must change the culling of the faces by inversing all triangles.
  105. // the mesh is reverse only if the number of resersing axes is impair.
  106. bool reversed = scale.x < 0;
  107. if (scale.y < 0) reversed = !reversed;
  108. if (scale.z < 0) reversed = !reversed;
  109. triangles = reversed ? MeshUtility.GetReversedTriangles(Mesh) : Mesh.triangles;
  110. // we transform the source mesh vertices according to rotation/translation/scale
  111. int i = 0;
  112. vertices = new List<MeshVertex>(Mesh.vertexCount);
  113. foreach (Vector3 vert in Mesh.vertices) {
  114. var transformed = new MeshVertex(vert, Mesh.normals[i++]);
  115. // application of rotation
  116. if (rotation != Quaternion.identity) {
  117. transformed.position = rotation * transformed.position;
  118. transformed.normal = rotation * transformed.normal;
  119. }
  120. if (scale != Vector3.one) {
  121. transformed.position = Vector3.Scale(transformed.position, scale);
  122. transformed.normal = Vector3.Scale(transformed.normal, scale);
  123. }
  124. if (translation != Vector3.zero) {
  125. transformed.position += translation;
  126. }
  127. vertices.Add(transformed);
  128. }
  129. // find the bounds along x
  130. minX = float.MaxValue;
  131. float maxX = float.MinValue;
  132. foreach (var vert in vertices) {
  133. Vector3 p = vert.position;
  134. maxX = Math.Max(maxX, p.x);
  135. minX = Math.Min(minX, p.x);
  136. }
  137. length = Math.Abs(maxX - minX);
  138. }
  139. public override bool Equals(object obj) {
  140. if (obj == null || GetType() != obj.GetType()) {
  141. return false;
  142. }
  143. var other = (SourceMesh)obj;
  144. return Mesh == other.Mesh &&
  145. translation == other.translation &&
  146. rotation == other.rotation &&
  147. scale == other.scale;
  148. }
  149. public override int GetHashCode() {
  150. return base.GetHashCode();
  151. }
  152. public static bool operator ==(SourceMesh sm1, SourceMesh sm2) {
  153. return sm1.Equals(sm2);
  154. }
  155. public static bool operator !=(SourceMesh sm1, SourceMesh sm2) {
  156. return sm1.Equals(sm2);
  157. }
  158. }
  159. }