MegaUtils.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. public class MegaModBut
  5. {
  6. public MegaModBut() { }
  7. public MegaModBut(string _but, string tooltip, System.Type _classname, Color _col)
  8. {
  9. name = _but;
  10. color = _col;
  11. classname = _classname;
  12. content = new GUIContent(_but, tooltip);
  13. }
  14. public string name;
  15. public Color color;
  16. public System.Type classname;
  17. public GUIContent content;
  18. }
  19. public enum MegaAxis
  20. {
  21. X = 0,
  22. Y = 1,
  23. Z = 2,
  24. };
  25. public enum MegaRepeatMode
  26. {
  27. Loop,
  28. Clamp,
  29. PingPong,
  30. };
  31. public class MegaUtils
  32. {
  33. static public void Bez3D(out Vector3 b, ref Vector3[] p, float u)
  34. {
  35. Vector3 t01 = p[0];
  36. t01.x += (p[1].x - p[0].x) * u;
  37. t01.y += (p[1].y - p[0].y) * u;
  38. t01.z += (p[1].z - p[0].z) * u;
  39. Vector3 t12 = p[1];
  40. t12.x += (p[2].x - p[1].x) * u;
  41. t12.y += (p[2].y - p[1].y) * u;
  42. t12.z += (p[2].z - p[1].z) * u;
  43. Vector3 t02 = t01 + (t12 - t01) * u;
  44. t01.x = p[2].x + (p[3].x - p[2].x) * u;
  45. t01.y = p[2].y + (p[3].y - p[2].y) * u;
  46. t01.z = p[2].z + (p[3].z - p[2].z) * u;
  47. t01.x = t12.x + (t01.x - t12.x) * u;
  48. t01.y = t12.y + (t01.y - t12.y) * u;
  49. t01.z = t12.z + (t01.z - t12.z) * u;
  50. b.x = t02.x + (t01.x - t02.x) * u;
  51. b.y = t02.y + (t01.y - t02.y) * u;
  52. b.z = t02.z + (t01.z - t02.z) * u;
  53. }
  54. static public float WaveFunc(float radius, float t, float amp, float waveLen, float phase, float decay)
  55. {
  56. if ( waveLen == 0.0f )
  57. waveLen = 0.0000001f;
  58. float ang = Mathf.PI * 2.0f * (radius / waveLen + phase);
  59. return amp * Mathf.Sin(ang) * Mathf.Exp(-decay * Mathf.Abs(radius));
  60. }
  61. static public int LargestComponent(Vector3 p)
  62. {
  63. if ( p.x > p.y )
  64. return (p.x > p.z) ? 0 : 2;
  65. else
  66. return (p.y > p.z) ? 1 : 2;
  67. }
  68. static public float LargestValue(Vector3 p)
  69. {
  70. if ( p.x > p.y )
  71. return (p.x > p.z) ? p.x : p.z;
  72. else
  73. return (p.y > p.z) ? p.y : p.z;
  74. }
  75. static public float LargestValue1(Vector3 p)
  76. {
  77. if ( Mathf.Abs(p.x) > Mathf.Abs(p.y) )
  78. return (Mathf.Abs(p.x) > Mathf.Abs(p.z)) ? p.x : p.z;
  79. else
  80. return (Mathf.Abs(p.y) > Mathf.Abs(p.z)) ? p.y : p.z;
  81. }
  82. static public int SmallestComponent(Vector3 p)
  83. {
  84. if ( p.x < p.y )
  85. return (p.x < p.z) ? 0 : 2;
  86. else
  87. return (p.y < p.z) ? 1 : 2;
  88. }
  89. static public float SmallestValue(Vector3 p)
  90. {
  91. if ( p.x < p.y )
  92. return (p.x < p.z) ? p.x : p.z;
  93. else
  94. return (p.y < p.z) ? p.y : p.z;
  95. }
  96. static public float SmallestLargestValueAbs(Vector3 p)
  97. {
  98. if ( Mathf.Abs(p.x) < Mathf.Abs(p.y) )
  99. return (Mathf.Abs(p.x) < Mathf.Abs(p.z)) ? p.x : p.z;
  100. else
  101. return (Mathf.Abs(p.y) < Mathf.Abs(p.z)) ? p.y : p.z;
  102. }
  103. // These two are utils so can remove from here and old morpher
  104. static public Vector3 Extents(Vector3[] verts, out Vector3 min, out Vector3 max)
  105. {
  106. Vector3 extent = Vector3.zero;
  107. min = Vector3.zero;
  108. max = Vector3.zero;
  109. if ( verts != null && verts.Length > 0 )
  110. {
  111. min = verts[0];
  112. max = verts[0];
  113. for ( int i = 1; i < verts.Length; i++ )
  114. {
  115. if ( verts[i].x < min.x ) min.x = verts[i].x;
  116. if ( verts[i].y < min.y ) min.y = verts[i].y;
  117. if ( verts[i].z < min.z ) min.z = verts[i].z;
  118. if ( verts[i].x > max.x ) max.x = verts[i].x;
  119. if ( verts[i].y > max.y ) max.y = verts[i].y;
  120. if ( verts[i].z > max.z ) max.z = verts[i].z;
  121. }
  122. extent = max - min;
  123. }
  124. return extent;
  125. }
  126. static public Vector3 Extents(List<Vector3> verts, out Vector3 min, out Vector3 max)
  127. {
  128. Vector3 extent = Vector3.zero;
  129. min = Vector3.zero;
  130. max = Vector3.zero;
  131. if ( verts != null && verts.Count > 0 )
  132. {
  133. min = verts[0];
  134. max = verts[0];
  135. for ( int i = 1; i < verts.Count; i++ )
  136. {
  137. if ( verts[i].x < min.x ) min.x = verts[i].x;
  138. if ( verts[i].y < min.y ) min.y = verts[i].y;
  139. if ( verts[i].z < min.z ) min.z = verts[i].z;
  140. if ( verts[i].x > max.x ) max.x = verts[i].x;
  141. if ( verts[i].y > max.y ) max.y = verts[i].y;
  142. if ( verts[i].z > max.z ) max.z = verts[i].z;
  143. }
  144. extent = max - min;
  145. }
  146. return extent;
  147. }
  148. static public int FindVert(Vector3 vert, List<Vector3> verts, float tolerance, float scl, bool flipyz, bool negx, int vn)
  149. {
  150. int find = 0;
  151. if ( negx )
  152. vert.x = -vert.x;
  153. if ( flipyz )
  154. {
  155. float z = vert.z;
  156. vert.z = vert.y;
  157. vert.y = z;
  158. }
  159. vert /= scl;
  160. float closest = Vector3.SqrMagnitude(verts[0] - vert);
  161. for ( int i = 0; i < verts.Count; i++ )
  162. {
  163. float dif = Vector3.SqrMagnitude(verts[i] - vert);
  164. if ( dif < closest )
  165. {
  166. closest = dif;
  167. find = i;
  168. }
  169. }
  170. if ( closest > tolerance ) //0.0001f ) // not exact
  171. return -1;
  172. return find; //0;
  173. }
  174. static public void BuildTangents(Mesh mesh)
  175. {
  176. int triangleCount = mesh.triangles.Length;
  177. int vertexCount = mesh.vertices.Length;
  178. Vector3[] tan1 = new Vector3[vertexCount];
  179. Vector3[] tan2 = new Vector3[vertexCount];
  180. Vector4[] tangents = new Vector4[vertexCount];
  181. Vector3[] verts = mesh.vertices;
  182. Vector2[] uvs = mesh.uv;
  183. Vector3[] norms = mesh.normals;
  184. int[] tris = mesh.triangles;
  185. for ( int a = 0; a < triangleCount; a += 3 )
  186. {
  187. long i1 = tris[a];
  188. long i2 = tris[a + 1];
  189. long i3 = tris[a + 2];
  190. Vector3 v1 = verts[i1];
  191. Vector3 v2 = verts[i2];
  192. Vector3 v3 = verts[i3];
  193. Vector2 w1 = uvs[i1];
  194. Vector2 w2 = uvs[i2];
  195. Vector2 w3 = uvs[i3];
  196. float x1 = v2.x - v1.x;
  197. float x2 = v3.x - v1.x;
  198. float y1 = v2.y - v1.y;
  199. float y2 = v3.y - v1.y;
  200. float z1 = v2.z - v1.z;
  201. float z2 = v3.z - v1.z;
  202. float s1 = w2.x - w1.x;
  203. float s2 = w3.x - w1.x;
  204. float t1 = w2.y - w1.y;
  205. float t2 = w3.y - w1.y;
  206. float r = 1.0f / (s1 * t2 - s2 * t1);
  207. Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
  208. Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
  209. tan1[i1] += sdir;
  210. tan1[i2] += sdir;
  211. tan1[i3] += sdir;
  212. tan2[i1] += tdir;
  213. tan2[i2] += tdir;
  214. tan2[i3] += tdir;
  215. }
  216. for ( int a = 0; a < vertexCount; a++ )
  217. {
  218. Vector3 n = norms[a];
  219. Vector3 t = tan1[a];
  220. Vector3.OrthoNormalize(ref n, ref t);
  221. tangents[a].x = t.x;
  222. tangents[a].y = t.y;
  223. tangents[a].z = t.z;
  224. tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
  225. }
  226. mesh.tangents = tangents;
  227. }
  228. static public Mesh GetMesh(GameObject go)
  229. {
  230. if ( !Application.isPlaying )
  231. return GetSharedMesh(go);
  232. MeshFilter meshFilter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
  233. // Mesh mesh;
  234. if ( meshFilter != null )
  235. return meshFilter.mesh; //sharedMesh; //sharedMesh;
  236. else
  237. {
  238. SkinnedMeshRenderer smesh = (SkinnedMeshRenderer)go.GetComponent(typeof(SkinnedMeshRenderer));
  239. if ( smesh != null )
  240. return smesh.sharedMesh;
  241. }
  242. return null;
  243. }
  244. static public Mesh GetSharedMesh(GameObject go)
  245. {
  246. MeshFilter meshFilter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
  247. // Mesh mesh;
  248. if ( meshFilter != null )
  249. return meshFilter.sharedMesh;
  250. else
  251. {
  252. SkinnedMeshRenderer smesh = (SkinnedMeshRenderer)go.GetComponent(typeof(SkinnedMeshRenderer));
  253. if ( smesh != null )
  254. return smesh.sharedMesh;
  255. }
  256. return null;
  257. }
  258. }