MegaWarp.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. public class MegaWarp : MonoBehaviour
  4. {
  5. public float Width = 1.0f;
  6. public float Height = 1.0f;
  7. public float Length = 1.0f;
  8. public float Decay;
  9. public bool Enabled = true;
  10. public bool DisplayGizmo = true;
  11. public Color GizCol1 = Color.yellow;
  12. public Color GizCol2 = Color.green;
  13. [System.NonSerialized]
  14. public Matrix4x4 tm = new Matrix4x4();
  15. [System.NonSerialized]
  16. public Matrix4x4 invtm = new Matrix4x4();
  17. Vector3 Offset = Vector3.zero;
  18. int steps = 50; // How many steps for the gizmo boxes
  19. [System.NonSerialized]
  20. public float totaldecay;
  21. [HideInInspector]
  22. public Vector3[] corners = new Vector3[8]; // Make static
  23. public virtual string WarpName() { return "None"; }
  24. public virtual string GetHelpURL() { return "Warp.htm"; }
  25. public virtual Vector3 Map(int i, Vector3 p) { return p; }
  26. public virtual bool Prepare(float decay) { return true; }
  27. public virtual string GetIcon() { return "MegaWave icon.png"; }
  28. [ContextMenu("Help")]
  29. public void Help()
  30. {
  31. Application.OpenURL("http://www.west-racing.com/mf/" + GetHelpURL());
  32. }
  33. public virtual void SetAxis(Matrix4x4 tmAxis)
  34. {
  35. Matrix4x4 itm = tmAxis.inverse;
  36. tm = tmAxis * tm;
  37. invtm = invtm * itm;
  38. }
  39. public void DrawEdge(Vector3 p1, Vector3 p2)
  40. {
  41. Vector3 last = Map(-1, p1);
  42. Vector3 pos = Vector3.zero;
  43. for ( int i = 1; i <= steps; i++ )
  44. {
  45. pos = p1 + ((p2 - p1) * ((float)i / (float)steps));
  46. pos = Map(-1, pos);
  47. if ( (i & 4) == 0 )
  48. Gizmos.color = gCol1; //GizCol1;
  49. else
  50. Gizmos.color = gCol2; //GizCol2;
  51. Gizmos.DrawLine(last, pos);
  52. last = pos;
  53. }
  54. Gizmos.color = gCol1; //GizCol1;
  55. }
  56. public void DrawEdgeCol(Vector3 p1, Vector3 p2)
  57. {
  58. Vector3 last = Map(-1, p1);
  59. Vector3 pos = Vector3.zero;
  60. for ( int i = 1; i <= steps; i++ )
  61. {
  62. pos = p1 + ((p2 - p1) * ((float)i / (float)steps));
  63. pos = Map(-1, pos);
  64. Gizmos.DrawLine(last, pos);
  65. last = pos;
  66. }
  67. }
  68. public static Color gCol1;
  69. public static Color gCol2;
  70. public void SetGizCols(float a)
  71. {
  72. gCol1 = GizCol1;
  73. gCol1.a *= a;
  74. gCol2 = GizCol2;
  75. gCol2.a *= a;
  76. }
  77. public virtual void DrawGizmo(Color col)
  78. {
  79. SetGizCols(col.a);
  80. tm = Matrix4x4.identity;
  81. invtm = tm.inverse;
  82. if ( !Prepare(0.0f) )
  83. return;
  84. tm = tm * transform.localToWorldMatrix; // * tm;
  85. invtm = tm.inverse;
  86. Vector3 min = new Vector3(-Width * 0.5f, 0.0f, -Length * 0.5f);
  87. Vector3 max = new Vector3(Width * 0.5f, Height, Length * 0.5f);
  88. Gizmos.matrix = transform.localToWorldMatrix;
  89. corners[0] = new Vector3(min.x, min.y, min.z);
  90. corners[1] = new Vector3(min.x, max.y, min.z);
  91. corners[2] = new Vector3(max.x, max.y, min.z);
  92. corners[3] = new Vector3(max.x, min.y, min.z);
  93. corners[4] = new Vector3(min.x, min.y, max.z);
  94. corners[5] = new Vector3(min.x, max.y, max.z);
  95. corners[6] = new Vector3(max.x, max.y, max.z);
  96. corners[7] = new Vector3(max.x, min.y, max.z);
  97. DrawEdge(corners[0], corners[1]);
  98. DrawEdge(corners[1], corners[2]);
  99. DrawEdge(corners[2], corners[3]);
  100. DrawEdge(corners[3], corners[0]);
  101. DrawEdge(corners[4], corners[5]);
  102. DrawEdge(corners[5], corners[6]);
  103. DrawEdge(corners[6], corners[7]);
  104. DrawEdge(corners[7], corners[4]);
  105. DrawEdge(corners[0], corners[4]);
  106. DrawEdge(corners[1], corners[5]);
  107. DrawEdge(corners[2], corners[6]);
  108. DrawEdge(corners[3], corners[7]);
  109. ExtraGizmo();
  110. }
  111. public virtual void ExtraGizmo()
  112. {
  113. }
  114. public void DrawFromTo(MegaAxis axis, float from, float to)
  115. {
  116. Vector3 min = new Vector3(-Width * 0.5f, 0.0f, -Length * 0.5f);
  117. Vector3 max = new Vector3(Width * 0.5f, Height, Length * 0.5f);
  118. switch ( axis )
  119. {
  120. case MegaAxis.X:
  121. corners[0] = new Vector3(-from, min.y, min.z);
  122. corners[1] = new Vector3(-from, max.y, min.z);
  123. corners[2] = new Vector3(-from, max.y, max.z);
  124. corners[3] = new Vector3(-from, min.y, max.z);
  125. corners[4] = new Vector3(-to, min.y, min.z);
  126. corners[5] = new Vector3(-to, max.y, min.z);
  127. corners[6] = new Vector3(-to, max.y, max.z);
  128. corners[7] = new Vector3(-to, min.y, max.z);
  129. break;
  130. case MegaAxis.Y:
  131. corners[0] = new Vector3(min.x, min.y, -from);
  132. corners[1] = new Vector3(min.x, max.y, -from);
  133. corners[2] = new Vector3(max.x, max.y, -from);
  134. corners[3] = new Vector3(max.x, min.y, -from);
  135. corners[4] = new Vector3(min.x, min.y, -to);
  136. corners[5] = new Vector3(min.x, max.y, -to);
  137. corners[6] = new Vector3(max.x, max.y, -to);
  138. corners[7] = new Vector3(max.x, min.y, -to);
  139. break;
  140. case MegaAxis.Z:
  141. corners[0] = new Vector3(min.x, from, min.z);
  142. corners[1] = new Vector3(min.x, from, max.z);
  143. corners[2] = new Vector3(max.x, from, max.z);
  144. corners[3] = new Vector3(max.x, from, min.z);
  145. corners[4] = new Vector3(min.x, to, min.z);
  146. corners[5] = new Vector3(min.x, to, max.z);
  147. corners[6] = new Vector3(max.x, to, max.z);
  148. corners[7] = new Vector3(max.x, to, min.z);
  149. break;
  150. }
  151. Color c = Color.red;
  152. c.a = gCol1.a;
  153. Gizmos.color = c;
  154. DrawEdgeCol(corners[0] - Offset, corners[1] - Offset);
  155. DrawEdgeCol(corners[1] - Offset, corners[2] - Offset);
  156. DrawEdgeCol(corners[2] - Offset, corners[3] - Offset);
  157. DrawEdgeCol(corners[3] - Offset, corners[0] - Offset);
  158. c = Color.green;
  159. c.a = gCol1.a;
  160. Gizmos.color = c;
  161. DrawEdgeCol(corners[4] - Offset, corners[5] - Offset);
  162. DrawEdgeCol(corners[5] - Offset, corners[6] - Offset);
  163. DrawEdgeCol(corners[6] - Offset, corners[7] - Offset);
  164. DrawEdgeCol(corners[7] - Offset, corners[4] - Offset);
  165. }
  166. }