MegaUtils.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using UnityEngine;
  2. using System;
  3. //using System.IO;
  4. using System.Collections.Generic;
  5. //public delegate bool ParseBinCallbackType(BinaryReader br, string id);
  6. //public delegate void ParseClassCallbackType(string classname, BinaryReader br);
  7. public class MegaModBut
  8. {
  9. public MegaModBut() { }
  10. public MegaModBut(string _but, string tooltip, System.Type _classname, Color _col)
  11. {
  12. name = _but;
  13. color = _col;
  14. classname = _classname;
  15. content = new GUIContent(_but, tooltip);
  16. }
  17. public string name;
  18. public Color color;
  19. public System.Type classname;
  20. public GUIContent content;
  21. }
  22. public enum MegaAxis
  23. {
  24. X = 0,
  25. Y = 1,
  26. Z = 2,
  27. };
  28. public enum MegaRepeatMode
  29. {
  30. Loop,
  31. Clamp,
  32. PingPong,
  33. None,
  34. };
  35. public class MegaUtils
  36. {
  37. #if false
  38. static public void Bez3D(out Vector3 b, ref Vector3[] p, float u)
  39. {
  40. Vector3 t01 = p[0] + (p[1] - p[0]) * u;
  41. Vector3 t12 = p[1] + (p[2] - p[1]) * u;
  42. Vector3 t02 = t01 + (t12 - t01) * u;
  43. t01 = p[2] + (p[3] - p[2]) * u;
  44. Vector3 t13 = t12 + (t01 - t12) * u;
  45. b = t02 + (t13 - t02) * u;
  46. }
  47. #else
  48. static public void Bez3D(out Vector3 b, ref Vector3[] p, float u)
  49. {
  50. Vector3 t01 = p[0];
  51. t01.x += (p[1].x - p[0].x) * u;
  52. t01.y += (p[1].y - p[0].y) * u;
  53. t01.z += (p[1].z - p[0].z) * u;
  54. Vector3 t12 = p[1];
  55. t12.x += (p[2].x - p[1].x) * u;
  56. t12.y += (p[2].y - p[1].y) * u;
  57. t12.z += (p[2].z - p[1].z) * u;
  58. Vector3 t02 = t01 + (t12 - t01) * u;
  59. t01.x = p[2].x + (p[3].x - p[2].x) * u;
  60. t01.y = p[2].y + (p[3].y - p[2].y) * u;
  61. t01.z = p[2].z + (p[3].z - p[2].z) * u;
  62. t01.x = t12.x + (t01.x - t12.x) * u;
  63. t01.y = t12.y + (t01.y - t12.y) * u;
  64. t01.z = t12.z + (t01.z - t12.z) * u;
  65. b.x = t02.x + (t01.x - t02.x) * u;
  66. b.y = t02.y + (t01.y - t02.y) * u;
  67. b.z = t02.z + (t01.z - t02.z) * u;
  68. }
  69. #endif
  70. static public float WaveFunc(float radius, float t, float amp, float waveLen, float phase, float decay)
  71. {
  72. if ( waveLen == 0.0f )
  73. waveLen = 0.0000001f;
  74. float ang = Mathf.PI * 2.0f * (radius / waveLen + phase);
  75. return amp * Mathf.Sin(ang) * Mathf.Exp(-decay * Mathf.Abs(radius));
  76. }
  77. static public Mesh GetMesh(GameObject go)
  78. {
  79. if ( !Application.isPlaying )
  80. return GetSharedMesh(go);
  81. //return GetSharedMesh(go);
  82. MeshFilter meshFilter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
  83. // Mesh mesh;
  84. if ( meshFilter != null )
  85. return meshFilter.mesh; //sharedMesh; //sharedMesh;
  86. else
  87. {
  88. SkinnedMeshRenderer smesh = (SkinnedMeshRenderer)go.GetComponent(typeof(SkinnedMeshRenderer));
  89. if ( smesh != null )
  90. return smesh.sharedMesh;
  91. }
  92. return null;
  93. }
  94. static public Mesh GetSharedMesh(GameObject go)
  95. {
  96. //if ( Application.isPlaying )
  97. //return GetMesh(go);
  98. MeshFilter meshFilter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
  99. // Mesh mesh;
  100. if ( meshFilter != null )
  101. return meshFilter.sharedMesh;
  102. else
  103. {
  104. SkinnedMeshRenderer smesh = (SkinnedMeshRenderer)go.GetComponent(typeof(SkinnedMeshRenderer));
  105. if ( smesh != null )
  106. return smesh.sharedMesh;
  107. }
  108. return null;
  109. }
  110. // All the parse stuff in here
  111. #if false
  112. static public Vector3 ReadP3(BinaryReader br)
  113. {
  114. Vector3 v = Vector3.zero;
  115. v.x = br.ReadSingle();
  116. v.y = br.ReadSingle();
  117. v.z = br.ReadSingle();
  118. return v;
  119. }
  120. static public string ReadString(BinaryReader br)
  121. {
  122. int len = br.ReadInt32();
  123. string str = new string(br.ReadChars(len - 1));
  124. br.ReadChar();
  125. return str;
  126. }
  127. static public string ReadStr(BinaryReader br)
  128. {
  129. string str = "";
  130. while ( true )
  131. {
  132. char c = br.ReadChar();
  133. if ( c == 0 )
  134. break;
  135. str += c;
  136. }
  137. return str;
  138. }
  139. static public Vector3[] ReadP3v(BinaryReader br)
  140. {
  141. int count = br.ReadInt32();
  142. Vector3[] tab = new Vector3[count];
  143. for ( int i = 0; i < count; i++ )
  144. {
  145. tab[i].x = br.ReadSingle();
  146. tab[i].y = br.ReadSingle();
  147. tab[i].z = br.ReadSingle();
  148. }
  149. return tab;
  150. }
  151. static public List<Vector3> ReadP3l(BinaryReader br)
  152. {
  153. int count = br.ReadInt32();
  154. List<Vector3> tab = new List<Vector3>(count);
  155. Vector3 p = Vector3.zero;
  156. for ( int i = 0; i < count; i++ )
  157. {
  158. p.x = br.ReadSingle();
  159. p.y = br.ReadSingle();
  160. p.z = br.ReadSingle();
  161. tab.Add(p);
  162. }
  163. return tab;
  164. }
  165. static public float ReadMotFloat(BinaryReader br)
  166. {
  167. byte[] floatBytes = br.ReadBytes(4);
  168. // swap the bytes
  169. Array.Reverse(floatBytes);
  170. // get the float from the byte array
  171. return BitConverter.ToSingle(floatBytes, 0);
  172. }
  173. static public double ReadMotDouble(BinaryReader br)
  174. {
  175. byte[] floatBytes = br.ReadBytes(8);
  176. // swap the bytes
  177. Array.Reverse(floatBytes);
  178. // get the float from the byte array
  179. return BitConverter.ToDouble(floatBytes, 0);
  180. }
  181. static public int ReadMotInt(BinaryReader br)
  182. {
  183. byte[] floatBytes = br.ReadBytes(4);
  184. // swap the bytes
  185. Array.Reverse(floatBytes);
  186. // get the float from the byte array
  187. return BitConverter.ToInt32(floatBytes, 0);
  188. }
  189. //public delegate bool ParseBinCallbackType(BinaryReader br, string id);
  190. //public delegate void ParseClassCallbackType(string classname, BinaryReader br);
  191. static public void Parse(BinaryReader br, ParseBinCallbackType cb)
  192. {
  193. bool readchunk = true;
  194. while ( readchunk )
  195. {
  196. string id = MegaUtils.ReadString(br);
  197. if ( id == "eoc" )
  198. break;
  199. int skip = br.ReadInt32();
  200. long fpos = br.BaseStream.Position;
  201. if ( !cb(br, id) )
  202. {
  203. Debug.Log("Error Loading chunk id " + id);
  204. readchunk = false; // done
  205. break;
  206. }
  207. br.BaseStream.Position = fpos + skip;
  208. }
  209. }
  210. #endif
  211. static public int LargestComponent(Vector3 p)
  212. {
  213. if ( p.x > p.y )
  214. return (p.x > p.z) ? 0 : 2;
  215. else
  216. return (p.y > p.z) ? 1 : 2;
  217. }
  218. static public float LargestValue(Vector3 p)
  219. {
  220. if ( p.x > p.y )
  221. return (p.x > p.z) ? p.x : p.z;
  222. else
  223. return (p.y > p.z) ? p.y : p.z;
  224. }
  225. static public float LargestValue1(Vector3 p)
  226. {
  227. if ( Mathf.Abs(p.x) > Mathf.Abs(p.y) )
  228. return (Mathf.Abs(p.x) > Mathf.Abs(p.z)) ? p.x : p.z;
  229. else
  230. return (Mathf.Abs(p.y) > Mathf.Abs(p.z)) ? p.y : p.z;
  231. }
  232. static public int SmallestComponent(Vector3 p)
  233. {
  234. if ( p.x < p.y )
  235. return (p.x < p.z) ? 0 : 2;
  236. else
  237. return (p.y < p.z) ? 1 : 2;
  238. }
  239. static public float SmallestValue(Vector3 p)
  240. {
  241. if ( p.x < p.y )
  242. return (p.x < p.z) ? p.x : p.z;
  243. else
  244. return (p.y < p.z) ? p.y : p.z;
  245. }
  246. static public float SmallestLargestValueAbs(Vector3 p)
  247. {
  248. if ( Mathf.Abs(p.x) < Mathf.Abs(p.y) )
  249. return (Mathf.Abs(p.x) < Mathf.Abs(p.z)) ? p.x : p.z;
  250. else
  251. return (Mathf.Abs(p.y) < Mathf.Abs(p.z)) ? p.y : p.z;
  252. }
  253. // These two are utils so can remove from here and old morpher
  254. static public Vector3 Extents(Vector3[] verts, out Vector3 min, out Vector3 max)
  255. {
  256. Vector3 extent = Vector3.zero;
  257. min = Vector3.zero;
  258. max = Vector3.zero;
  259. if ( verts != null && verts.Length > 0 )
  260. {
  261. min = verts[0];
  262. max = verts[0];
  263. for ( int i = 1; i < verts.Length; i++ )
  264. {
  265. if ( verts[i].x < min.x ) min.x = verts[i].x;
  266. if ( verts[i].y < min.y ) min.y = verts[i].y;
  267. if ( verts[i].z < min.z ) min.z = verts[i].z;
  268. if ( verts[i].x > max.x ) max.x = verts[i].x;
  269. if ( verts[i].y > max.y ) max.y = verts[i].y;
  270. if ( verts[i].z > max.z ) max.z = verts[i].z;
  271. }
  272. extent = max - min;
  273. }
  274. return extent;
  275. }
  276. static public Vector3 Extents(List<Vector3> verts, out Vector3 min, out Vector3 max)
  277. {
  278. Vector3 extent = Vector3.zero;
  279. min = Vector3.zero;
  280. max = Vector3.zero;
  281. if ( verts != null && verts.Count > 0 )
  282. {
  283. min = verts[0];
  284. max = verts[0];
  285. for ( int i = 1; i < verts.Count; i++ )
  286. {
  287. if ( verts[i].x < min.x ) min.x = verts[i].x;
  288. if ( verts[i].y < min.y ) min.y = verts[i].y;
  289. if ( verts[i].z < min.z ) min.z = verts[i].z;
  290. if ( verts[i].x > max.x ) max.x = verts[i].x;
  291. if ( verts[i].y > max.y ) max.y = verts[i].y;
  292. if ( verts[i].z > max.z ) max.z = verts[i].z;
  293. }
  294. extent = max - min;
  295. }
  296. return extent;
  297. }
  298. static public int FindVert(Vector3 vert, List<Vector3> verts, float tolerance, float scl, bool flipyz, bool negx, int vn)
  299. {
  300. int find = 0;
  301. if ( negx )
  302. vert.x = -vert.x;
  303. if ( flipyz )
  304. {
  305. float z = vert.z;
  306. vert.z = vert.y;
  307. vert.y = z;
  308. }
  309. vert /= scl;
  310. float closest = Vector3.SqrMagnitude(verts[0] - vert);
  311. for ( int i = 0; i < verts.Count; i++ )
  312. {
  313. float dif = Vector3.SqrMagnitude(verts[i] - vert);
  314. if ( dif < closest )
  315. {
  316. closest = dif;
  317. find = i;
  318. }
  319. }
  320. if ( closest > tolerance ) //0.0001f ) // not exact
  321. return -1;
  322. return find; //0;
  323. }
  324. static public void BuildTangents(Mesh mesh)
  325. {
  326. int triangleCount = mesh.triangles.Length;
  327. int vertexCount = mesh.vertices.Length;
  328. Vector3[] tan1 = new Vector3[vertexCount];
  329. Vector3[] tan2 = new Vector3[vertexCount];
  330. Vector4[] tangents = new Vector4[vertexCount];
  331. Vector3[] verts = mesh.vertices;
  332. Vector2[] uvs = mesh.uv;
  333. Vector3[] norms = mesh.normals;
  334. int[] tris = mesh.triangles;
  335. for ( int a = 0; a < triangleCount; a += 3 )
  336. {
  337. long i1 = tris[a];
  338. long i2 = tris[a + 1];
  339. long i3 = tris[a + 2];
  340. Vector3 v1 = verts[i1];
  341. Vector3 v2 = verts[i2];
  342. Vector3 v3 = verts[i3];
  343. Vector2 w1 = uvs[i1];
  344. Vector2 w2 = uvs[i2];
  345. Vector2 w3 = uvs[i3];
  346. float x1 = v2.x - v1.x;
  347. float x2 = v3.x - v1.x;
  348. float y1 = v2.y - v1.y;
  349. float y2 = v3.y - v1.y;
  350. float z1 = v2.z - v1.z;
  351. float z2 = v3.z - v1.z;
  352. float s1 = w2.x - w1.x;
  353. float s2 = w3.x - w1.x;
  354. float t1 = w2.y - w1.y;
  355. float t2 = w3.y - w1.y;
  356. float r = 1.0f / (s1 * t2 - s2 * t1);
  357. Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
  358. Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
  359. tan1[i1] += sdir;
  360. tan1[i2] += sdir;
  361. tan1[i3] += sdir;
  362. tan2[i1] += tdir;
  363. tan2[i2] += tdir;
  364. tan2[i3] += tdir;
  365. }
  366. for ( int a = 0; a < vertexCount; a++ )
  367. {
  368. Vector3 n = norms[a];
  369. Vector3 t = tan1[a];
  370. Vector3.OrthoNormalize(ref n, ref t);
  371. tangents[a].x = t.x;
  372. tangents[a].y = t.y;
  373. tangents[a].z = t.z;
  374. tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
  375. }
  376. mesh.tangents = tangents;
  377. }
  378. }