MegaFFD.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 
  2. using UnityEngine;
  3. using System;
  4. public class MegaFFD : MegaModifier
  5. {
  6. public float KnotSize = 0.1f;
  7. public bool inVol = false;
  8. public Vector3[] pt = new Vector3[64];
  9. [HideInInspector]
  10. public float EPSILON = 0.001f;
  11. [HideInInspector]
  12. public Vector3 lsize = Vector3.one;
  13. [HideInInspector]
  14. public Vector3 bsize = new Vector3();
  15. [HideInInspector]
  16. public Vector3 bcenter = new Vector3();
  17. public virtual int GridSize() { return 1; }
  18. public virtual int GridIndex(int i, int j, int k) { return 0; }
  19. public override string GetHelpURL() { return "?page_id=199"; }
  20. public override void PostCopy(MegaModifier src)
  21. {
  22. MegaFFD ffd = (MegaFFD)src;
  23. pt = new Vector3[64];
  24. for ( int c = 0; c < 64; c++ )
  25. {
  26. pt[c] = ffd.pt[c];
  27. }
  28. }
  29. public Vector3 LatticeSize()
  30. {
  31. Vector3 size = bsize;
  32. if ( size.x == 0.0f ) size.x = 0.001f;
  33. if ( size.y == 0.0f ) size.y = 0.001f;
  34. if ( size.z == 0.0f ) size.z = 0.001f;
  35. return size;
  36. }
  37. void Init()
  38. {
  39. lsize = LatticeSize();
  40. int size = GridSize();
  41. float fsize = size - 1.0f;
  42. for ( int i = 0; i < size; i++ ) // TODO: dim for all ffd
  43. {
  44. for ( int j = 0; j < size; j++ )
  45. {
  46. for ( int k = 0; k < size; k++ )
  47. {
  48. int c = GridIndex(i, j, k);
  49. pt[c].x = (float)(i) / fsize;
  50. pt[c].y = (float)(j) / fsize;
  51. pt[c].z = (float)(k) / fsize;
  52. }
  53. }
  54. }
  55. }
  56. public override bool ModLateUpdate(MegaModContext mc)
  57. {
  58. return Prepare(mc);
  59. }
  60. public override bool Prepare(MegaModContext mc)
  61. {
  62. Vector3 s = LatticeSize();
  63. for ( int i = 0; i < 3; i++ )
  64. {
  65. if ( s[i] == 0.0f )
  66. s[i] = 1.0f;
  67. else
  68. s[i] = 1.0f / s[i];
  69. }
  70. Vector3 c = MegaMatrix.GetTrans(ref tm);
  71. MegaMatrix.SetTrans(ref tm, c - bbox.min - Offset);
  72. MegaMatrix.Scale(ref tm, s, false);
  73. invtm = tm.inverse;
  74. return true;
  75. }
  76. public Vector3 GetPoint(int i)
  77. {
  78. Vector3 p = pt[i];
  79. p.x -= 0.5f;
  80. p.y -= 0.5f;
  81. p.z -= 0.5f;
  82. return Vector3.Scale(p, lsize) + bcenter;
  83. }
  84. public Vector3 GetPoint(int i, int j, int k)
  85. {
  86. Vector3 p = pt[GridIndex(i, j, k)];
  87. p.x -= 0.5f;
  88. p.y -= 0.5f;
  89. p.z -= 0.5f;
  90. return Vector3.Scale(p, lsize) + bcenter;
  91. }
  92. public void SetPoint(int i, int j, int k, Vector3 pos)
  93. {
  94. Vector3 lpos = transform.worldToLocalMatrix.MultiplyPoint(pos);
  95. SetPointLocal(i, j, k, lpos);
  96. }
  97. public void SetPointLocal(int i, int j, int k, Vector3 lpos)
  98. {
  99. Vector3 size = lsize;
  100. Vector3 osize = lsize;
  101. osize.x = 1.0f / size.x;
  102. osize.y = 1.0f / size.y;
  103. osize.z = 1.0f / size.z;
  104. lpos -= bcenter;
  105. Vector3 p = Vector3.Scale(lpos, osize);
  106. p.x += 0.5f;
  107. p.y += 0.5f;
  108. p.z += 0.5f;
  109. pt[GridIndex(i, j, k)] = p;
  110. }
  111. public void SetPoint(int index, Vector3 pos)
  112. {
  113. Vector3 lpos = transform.worldToLocalMatrix.MultiplyPoint(pos);
  114. SetPointLocal(index, lpos);
  115. }
  116. public void SetPointLocal(int index, Vector3 lpos)
  117. {
  118. Vector3 size = lsize;
  119. Vector3 osize = lsize;
  120. osize.x = 1.0f / size.x;
  121. osize.y = 1.0f / size.y;
  122. osize.z = 1.0f / size.z;
  123. lpos -= bcenter;
  124. Vector3 p = Vector3.Scale(lpos, osize);
  125. p.x += 0.5f;
  126. p.y += 0.5f;
  127. p.z += 0.5f;
  128. pt[index] = p;
  129. }
  130. public void MovePoint(int x, int y, int z, Vector3 localmove)
  131. {
  132. Vector3 p = GetPoint(x, y, z);
  133. p += localmove;
  134. SetPointLocal(x, y, z, p);
  135. }
  136. void Reset()
  137. {
  138. MegaModifyObject modobj = (MegaModifyObject)gameObject.GetComponent<MegaModifyObject>();
  139. if ( modobj != null )
  140. modobj.ModReset(this);
  141. Renderer rend = GetComponent<Renderer>();
  142. if ( rend != null )
  143. {
  144. Mesh ms = MegaUtils.GetSharedMesh(gameObject);
  145. if ( ms != null )
  146. {
  147. Bounds b = ms.bounds;
  148. Offset = -b.center;
  149. bbox.min = b.center - b.extents;
  150. bbox.max = b.center + b.extents;
  151. }
  152. }
  153. if ( modobj.selection != null )
  154. {
  155. Bounds bb = new Bounds();
  156. for ( int i = 0; i < modobj.verts.Length; i++ )
  157. {
  158. if ( modobj.selection[i] > 0.001f )
  159. bb.Encapsulate(modobj.verts[i]);
  160. }
  161. Offset = -bb.center;
  162. bbox.min = bb.center - bb.extents;
  163. bbox.max = bb.center + bb.extents;
  164. }
  165. bsize = bbox.Size();
  166. bcenter = bbox.center;
  167. Init();
  168. }
  169. [ContextMenu("Fit FFD to Selection")]
  170. public void FitFFD()
  171. {
  172. Reset();
  173. }
  174. [ContextMenu("Fit FFD to Mesh")]
  175. public void FitFFDToMesh()
  176. {
  177. Renderer rend = GetComponent<Renderer>();
  178. if ( rend != null )
  179. {
  180. Mesh ms = MegaUtils.GetSharedMesh(gameObject);
  181. if ( ms != null )
  182. {
  183. Bounds b = ms.bounds;
  184. Offset = -b.center;
  185. bbox.min = b.center - b.extents;
  186. bbox.max = b.center + b.extents;
  187. }
  188. }
  189. bsize = bbox.Size();
  190. bcenter = bbox.center;
  191. Init();
  192. }
  193. public override bool InitMod(MegaModifiers mc)
  194. {
  195. bsize = mc.bbox.size;
  196. bcenter = mc.bbox.center;
  197. Init();
  198. return true;
  199. }
  200. static MegaFFD Create(GameObject go, int type)
  201. {
  202. switch ( type )
  203. {
  204. case 0: return go.AddComponent<MegaFFD2x2x2>();
  205. case 1: return go.AddComponent<MegaFFD3x3x3>();
  206. case 2: return go.AddComponent<MegaFFD4x4x4>();
  207. }
  208. return null;
  209. }
  210. public override void DrawGizmo(MegaModContext context)
  211. {
  212. //Gizmos.DrawCube(transform.position, new Vector3(bsize.x * 0.1f, bsize.y * 0.1f, bsize.z * 0.1f));
  213. }
  214. }