MegaFFDWarp.cs 5.8 KB

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