MegaMeshPage.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. // TODO: Help as a popup window of text
  4. // This will be a basic box mesh but need to have different uvs for front and back, and maybe different mtlids
  5. // if have thickness then again different mtlid maybe and or uvs
  6. // first version ignore thickness
  7. // each mesh should have an ption to set pivot, or centre it, also rotate
  8. // should derive from SimpleMesh or some class as will be adding a few of these
  9. [AddComponentMenu("MegaShapes/Page Mesh")]
  10. [RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer))]
  11. public class MegaMeshPage : MonoBehaviour
  12. {
  13. public float Width = 1.0f;
  14. public float Length = 1.41f;
  15. public float Height = 0.1f;
  16. public int WidthSegs = 10;
  17. public int LengthSegs = 10;
  18. public int HeightSegs = 1;
  19. public bool genUVs = true;
  20. public float rotate = 0.0f;
  21. public bool PivotBase = false;
  22. public bool PivotEdge = true;
  23. public bool tangents = false;
  24. public bool optimize = false;
  25. [ContextMenu("Help")]
  26. public void Help()
  27. {
  28. Application.OpenURL("http://www.west-racing.com/mf/?page_id=853");
  29. }
  30. void Reset()
  31. {
  32. Rebuild();
  33. }
  34. public void Rebuild()
  35. {
  36. MeshFilter mf = GetComponent<MeshFilter>();
  37. if ( mf != null )
  38. {
  39. Mesh mesh = mf.sharedMesh; //Utils.GetMesh(gameObject);
  40. if ( mesh == null )
  41. {
  42. mesh = new Mesh();
  43. mf.sharedMesh = mesh;
  44. }
  45. if ( mesh != null )
  46. {
  47. BuildMesh(mesh);
  48. MegaModifyObject mo = GetComponent<MegaModifyObject>();
  49. if ( mo != null )
  50. {
  51. mo.MeshUpdated();
  52. }
  53. }
  54. }
  55. }
  56. static void MakeQuad1(List<int> f, int a, int b, int c, int d)
  57. {
  58. f.Add(a);
  59. f.Add(b);
  60. f.Add(c);
  61. f.Add(c);
  62. f.Add(d);
  63. f.Add(a);
  64. }
  65. // Put in utils
  66. int MaxComponent(Vector3 v)
  67. {
  68. if ( Mathf.Abs(v.x) > Mathf.Abs(v.y) )
  69. {
  70. if ( Mathf.Abs(v.x) > Mathf.Abs(v.z) )
  71. return 0;
  72. else
  73. return 2;
  74. }
  75. else
  76. {
  77. if ( Mathf.Abs(v.y) > Mathf.Abs(v.z) )
  78. return 1;
  79. else
  80. return 2;
  81. }
  82. }
  83. void BuildMesh(Mesh mesh)
  84. {
  85. Width = Mathf.Clamp(Width, 0.0f, float.MaxValue);
  86. Length = Mathf.Clamp(Length, 0.0f, float.MaxValue);
  87. Height = Mathf.Clamp(Height, 0.0f, float.MaxValue);
  88. LengthSegs = Mathf.Clamp(LengthSegs, 1, 200);
  89. HeightSegs = Mathf.Clamp(HeightSegs, 1, 200);
  90. WidthSegs = Mathf.Clamp(WidthSegs, 1, 200);
  91. Vector3 vb = new Vector3(Width, Height, Length) / 2.0f;
  92. Vector3 va = -vb;
  93. if ( PivotBase )
  94. {
  95. va.y = 0.0f;
  96. vb.y = Height;
  97. }
  98. if ( PivotEdge )
  99. {
  100. va.x = 0.0f;
  101. vb.x = Width;
  102. }
  103. float dx = Width / (float)WidthSegs;
  104. float dy = Height / (float)HeightSegs;
  105. float dz = Length / (float)LengthSegs;
  106. Vector3 p = va;
  107. // Lists should be static, clear out to reuse
  108. List<Vector3> verts = new List<Vector3>();
  109. List<Vector2> uvs = new List<Vector2>();
  110. List<int> tris = new List<int>();
  111. List<int> tris1 = new List<int>();
  112. List<int> tris2 = new List<int>();
  113. Vector2 uv = Vector2.zero;
  114. // Do we have top and bottom
  115. if ( Width > 0.0f && Length > 0.0f )
  116. {
  117. Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one);
  118. Vector3 uv1 = Vector3.zero;
  119. p.y = vb.y;
  120. for ( int iz = 0; iz <= LengthSegs; iz++ )
  121. {
  122. p.x = va.x;
  123. for ( int ix = 0; ix <= WidthSegs; ix++ )
  124. {
  125. verts.Add(p);
  126. if ( genUVs )
  127. {
  128. //uv.x = Mathf.Repeat((p.x + vb.x) / Width, 1.0f);
  129. uv.x = (p.x - va.x) / Width;
  130. uv.y = (p.z + vb.z) / Length;
  131. uv1.x = uv.x - 0.5f;
  132. uv1.y = 0.0f;
  133. uv1.z = uv.y - 0.5f;
  134. uv1 = tm1.MultiplyPoint3x4(uv1);
  135. uv.x = 0.5f + uv1.x;
  136. uv.y = 0.5f + uv1.z;
  137. uvs.Add(uv);
  138. }
  139. p.x += dx;
  140. }
  141. p.z += dz;
  142. }
  143. for ( int iz = 0; iz < LengthSegs; iz++ )
  144. {
  145. int kv = iz * (WidthSegs + 1);
  146. for ( int ix = 0; ix < WidthSegs; ix++ )
  147. {
  148. MakeQuad1(tris, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
  149. kv++;
  150. }
  151. }
  152. int index = verts.Count;
  153. p.y = va.y;
  154. p.z = va.z;
  155. for ( int iy = 0; iy <= LengthSegs; iy++ )
  156. {
  157. p.x = va.x;
  158. for ( int ix = 0; ix <= WidthSegs; ix++ )
  159. {
  160. verts.Add(p);
  161. if ( genUVs )
  162. {
  163. uv.x = 1.0f - ((p.x + vb.x) / Width);
  164. uv.y = ((p.z + vb.z) / Length);
  165. uv1.x = uv.x - 0.5f;
  166. uv1.y = 0.0f;
  167. uv1.z = uv.y - 0.5f;
  168. uv1 = tm1.MultiplyPoint3x4(uv1);
  169. uv.x = 0.5f + uv1.x;
  170. uv.y = 0.5f + uv1.z;
  171. uvs.Add(uv);
  172. }
  173. p.x += dx;
  174. }
  175. p.z += dz;
  176. }
  177. for ( int iy = 0; iy < LengthSegs; iy++ )
  178. {
  179. int kv = iy * (WidthSegs + 1) + index;
  180. for ( int ix = 0; ix < WidthSegs; ix++ )
  181. {
  182. MakeQuad1(tris1, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
  183. kv++;
  184. }
  185. }
  186. }
  187. // Front back
  188. if ( Width > 0.0f && Height > 0.0f )
  189. {
  190. int index = verts.Count;
  191. p.z = va.z;
  192. p.y = va.y;
  193. for ( int iz = 0; iz <= HeightSegs; iz++ )
  194. {
  195. p.x = va.x;
  196. for ( int ix = 0; ix <= WidthSegs; ix++ )
  197. {
  198. verts.Add(p);
  199. if ( genUVs )
  200. {
  201. uv.x = (p.x + vb.x) / Width;
  202. uv.y = (p.y + vb.y) / Height;
  203. uvs.Add(uv);
  204. }
  205. p.x += dx;
  206. }
  207. p.y += dy;
  208. }
  209. for ( int iz = 0; iz < HeightSegs; iz++ )
  210. {
  211. int kv = iz * (WidthSegs + 1) + index;
  212. for ( int ix = 0; ix < WidthSegs; ix++ )
  213. {
  214. MakeQuad1(tris2, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
  215. kv++;
  216. }
  217. }
  218. index = verts.Count;
  219. p.z = vb.z;
  220. p.y = va.y;
  221. for ( int iy = 0; iy <= HeightSegs; iy++ )
  222. {
  223. p.x = va.x;
  224. for ( int ix = 0; ix <= WidthSegs; ix++ )
  225. {
  226. verts.Add(p);
  227. if ( genUVs )
  228. {
  229. uv.x = (p.x + vb.x) / Width;
  230. uv.y = (p.y + vb.y) / Height;
  231. uvs.Add(uv);
  232. }
  233. p.x += dx;
  234. }
  235. p.y += dy;
  236. }
  237. for ( int iy = 0; iy < HeightSegs; iy++ )
  238. {
  239. int kv = iy * (WidthSegs + 1) + index;
  240. for ( int ix = 0; ix < WidthSegs; ix++ )
  241. {
  242. MakeQuad1(tris2, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
  243. kv++;
  244. }
  245. }
  246. }
  247. // Left Right
  248. if ( Length > 0.0f && Height > 0.0f )
  249. {
  250. int index = verts.Count;
  251. p.x = vb.x;
  252. p.y = va.y;
  253. for ( int iz = 0; iz <= HeightSegs; iz++ )
  254. {
  255. p.z = va.z;
  256. for ( int ix = 0; ix <= LengthSegs; ix++ )
  257. {
  258. verts.Add(p);
  259. if ( genUVs )
  260. {
  261. uv.x = (p.z + vb.z) / Length;
  262. uv.y = (p.y + vb.y) / Height;
  263. uvs.Add(uv);
  264. }
  265. p.z += dz;
  266. }
  267. p.y += dy;
  268. }
  269. for ( int iz = 0; iz < HeightSegs; iz++ )
  270. {
  271. int kv = iz * (LengthSegs + 1) + index;
  272. for ( int ix = 0; ix < LengthSegs; ix++ )
  273. {
  274. MakeQuad1(tris2, kv, kv + LengthSegs + 1, kv + LengthSegs + 2, kv + 1);
  275. kv++;
  276. }
  277. }
  278. index = verts.Count;
  279. p.x = va.x;
  280. p.y = va.y;
  281. for ( int iy = 0; iy <= HeightSegs; iy++ )
  282. {
  283. p.z = va.z;
  284. for ( int ix = 0; ix <= LengthSegs; ix++ )
  285. {
  286. verts.Add(p);
  287. if ( genUVs )
  288. {
  289. uv.x = (p.z + vb.z) / Length;
  290. uv.y = (p.y + vb.y) / Height;
  291. uvs.Add(uv);
  292. }
  293. p.z += dz;
  294. }
  295. p.y += dy;
  296. }
  297. for ( int iy = 0; iy < HeightSegs; iy++ )
  298. {
  299. int kv = iy * (LengthSegs + 1) + index;
  300. for ( int ix = 0; ix < LengthSegs; ix++ )
  301. {
  302. MakeQuad1(tris2, kv, kv + 1, kv + LengthSegs + 2, kv + LengthSegs + 1);
  303. kv++;
  304. }
  305. }
  306. }
  307. mesh.Clear();
  308. mesh.subMeshCount = 3;
  309. mesh.vertices = verts.ToArray();
  310. mesh.uv = uvs.ToArray();
  311. mesh.SetTriangles(tris.ToArray(), 0);
  312. mesh.SetTriangles(tris1.ToArray(), 1);
  313. mesh.SetTriangles(tris2.ToArray(), 2);
  314. mesh.RecalculateNormals();
  315. if ( tangents )
  316. MegaUtils.BuildTangents(mesh);
  317. #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  318. #else
  319. if ( optimize )
  320. mesh.Optimize();
  321. #endif
  322. mesh.RecalculateBounds();
  323. }
  324. void BuildMeshOld(Mesh mesh)
  325. {
  326. Width = Mathf.Clamp(Width, 0.0f, float.MaxValue);
  327. Length = Mathf.Clamp(Length, 0.0f, float.MaxValue);
  328. Height = Mathf.Clamp(Height, 0.0f, float.MaxValue);
  329. LengthSegs = Mathf.Clamp(LengthSegs, 1, 200);
  330. HeightSegs = Mathf.Clamp(HeightSegs, 1, 200);
  331. WidthSegs = Mathf.Clamp(WidthSegs, 1, 200);
  332. Vector3 vb = new Vector3(Width, Height, Length) / 2.0f;
  333. Vector3 va = -vb;
  334. if ( PivotBase )
  335. {
  336. va.y = 0.0f;
  337. vb.y = Height;
  338. }
  339. if ( PivotEdge )
  340. {
  341. va.x = 0.0f;
  342. vb.x = Width;
  343. }
  344. float dx = Width / (float)WidthSegs;
  345. float dy = Height / (float)HeightSegs;
  346. float dz = Length / (float)LengthSegs;
  347. Vector3 p = va;
  348. // Lists should be static, clear out to reuse
  349. List<Vector3> verts = new List<Vector3>();
  350. List<Vector2> uvs = new List<Vector2>();
  351. List<int> tris = new List<int>();
  352. List<int> tris1 = new List<int>();
  353. List<int> tris2 = new List<int>();
  354. Vector2 uv = Vector2.zero;
  355. // Do we have top and bottom
  356. if ( Width > 0.0f && Length > 0.0f )
  357. {
  358. //Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one);
  359. p.y = vb.y;
  360. for ( int iz = 0; iz <= LengthSegs; iz++ )
  361. {
  362. p.x = va.x;
  363. for ( int ix = 0; ix <= WidthSegs; ix++ )
  364. {
  365. verts.Add(p);
  366. if ( genUVs )
  367. {
  368. uv.x = (p.x + vb.x) / Width;
  369. uv.y = (p.z + vb.z) / Length;
  370. uvs.Add(uv);
  371. }
  372. p.x += dx;
  373. }
  374. p.z += dz;
  375. }
  376. for ( int iz = 0; iz < LengthSegs; iz++ )
  377. {
  378. int kv = iz * (WidthSegs + 1);
  379. for ( int ix = 0; ix < WidthSegs; ix++ )
  380. {
  381. MakeQuad1(tris, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
  382. kv++;
  383. }
  384. }
  385. int index = verts.Count;
  386. p.y = va.y;
  387. p.z = va.z;
  388. for ( int iy = 0; iy <= LengthSegs; iy++ )
  389. {
  390. p.x = va.x;
  391. for ( int ix = 0; ix <= WidthSegs; ix++ )
  392. {
  393. verts.Add(p);
  394. if ( genUVs )
  395. {
  396. uv.x = 1.0f - ((p.x + vb.x) / Width);
  397. uv.y = ((p.z + vb.z) / Length);
  398. uvs.Add(uv);
  399. }
  400. p.x += dx;
  401. }
  402. p.z += dz;
  403. }
  404. for ( int iy = 0; iy < LengthSegs; iy++ )
  405. {
  406. int kv = iy * (WidthSegs + 1) + index;
  407. for ( int ix = 0; ix < WidthSegs; ix++ )
  408. {
  409. MakeQuad1(tris1, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
  410. kv++;
  411. }
  412. }
  413. }
  414. // Front back
  415. if ( Width > 0.0f && Height > 0.0f )
  416. {
  417. int index = verts.Count;
  418. p.z = va.z;
  419. p.y = va.y;
  420. for ( int iz = 0; iz <= HeightSegs; iz++ )
  421. {
  422. p.x = va.x;
  423. for ( int ix = 0; ix <= WidthSegs; ix++ )
  424. {
  425. verts.Add(p);
  426. if ( genUVs )
  427. {
  428. uv.x = (p.x + vb.x) / Width;
  429. uv.y = (p.y + vb.y) / Height;
  430. uvs.Add(uv);
  431. }
  432. p.x += dx;
  433. }
  434. p.y += dy;
  435. }
  436. for ( int iz = 0; iz < HeightSegs; iz++ )
  437. {
  438. int kv = iz * (WidthSegs + 1) + index;
  439. for ( int ix = 0; ix < WidthSegs; ix++ )
  440. {
  441. MakeQuad1(tris2, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
  442. kv++;
  443. }
  444. }
  445. index = verts.Count;
  446. p.z = vb.z;
  447. p.y = va.y;
  448. for ( int iy = 0; iy <= HeightSegs; iy++ )
  449. {
  450. p.x = va.x;
  451. for ( int ix = 0; ix <= WidthSegs; ix++ )
  452. {
  453. verts.Add(p);
  454. if ( genUVs )
  455. {
  456. uv.x = (p.x + vb.x) / Width;
  457. uv.y = (p.y + vb.y) / Height;
  458. uvs.Add(uv);
  459. }
  460. p.x += dx;
  461. }
  462. p.y += dy;
  463. }
  464. for ( int iy = 0; iy < HeightSegs; iy++ )
  465. {
  466. int kv = iy * (WidthSegs + 1) + index;
  467. for ( int ix = 0; ix < WidthSegs; ix++ )
  468. {
  469. MakeQuad1(tris2, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
  470. kv++;
  471. }
  472. }
  473. }
  474. // Left Right
  475. if ( Length > 0.0f && Height > 0.0f )
  476. {
  477. int index = verts.Count;
  478. p.x = vb.x;
  479. p.y = va.y;
  480. for ( int iz = 0; iz <= HeightSegs; iz++ )
  481. {
  482. p.z = va.z;
  483. for ( int ix = 0; ix <= LengthSegs; ix++ )
  484. {
  485. verts.Add(p);
  486. if ( genUVs )
  487. {
  488. uv.x = (p.z + vb.z) / Length;
  489. uv.y = (p.y + vb.y) / Height;
  490. uvs.Add(uv);
  491. }
  492. p.z += dz;
  493. }
  494. p.y += dy;
  495. }
  496. for ( int iz = 0; iz < HeightSegs; iz++ )
  497. {
  498. int kv = iz * (LengthSegs + 1) + index;
  499. for ( int ix = 0; ix < LengthSegs; ix++ )
  500. {
  501. MakeQuad1(tris2, kv, kv + LengthSegs + 1, kv + LengthSegs + 2, kv + 1);
  502. kv++;
  503. }
  504. }
  505. index = verts.Count;
  506. p.x = va.x;
  507. p.y = va.y;
  508. for ( int iy = 0; iy <= HeightSegs; iy++ )
  509. {
  510. p.z = va.z;
  511. for ( int ix = 0; ix <= LengthSegs; ix++ )
  512. {
  513. verts.Add(p);
  514. if ( genUVs )
  515. {
  516. uv.x = (p.z + vb.z) / Length;
  517. uv.y = (p.y + vb.y) / Height;
  518. uvs.Add(uv);
  519. }
  520. p.z += dz;
  521. }
  522. p.y += dy;
  523. }
  524. for ( int iy = 0; iy < HeightSegs; iy++ )
  525. {
  526. int kv = iy * (LengthSegs + 1) + index;
  527. for ( int ix = 0; ix < LengthSegs; ix++ )
  528. {
  529. MakeQuad1(tris2, kv, kv + 1, kv + LengthSegs + 2, kv + LengthSegs + 1);
  530. kv++;
  531. }
  532. }
  533. }
  534. mesh.Clear();
  535. mesh.subMeshCount = 3;
  536. mesh.vertices = verts.ToArray();
  537. mesh.uv = uvs.ToArray();
  538. mesh.SetTriangles(tris.ToArray(), 0);
  539. mesh.SetTriangles(tris1.ToArray(), 1);
  540. mesh.SetTriangles(tris2.ToArray(), 2);
  541. mesh.RecalculateNormals();
  542. if ( tangents )
  543. MegaUtils.BuildTangents(mesh);
  544. #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  545. #else
  546. if ( optimize )
  547. mesh.Optimize();
  548. #endif
  549. mesh.RecalculateBounds();
  550. }
  551. }