MegaFFDEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using UnityEngine;
  2. using UnityEditor;
  3. public class MegaFFDEditor : MegaModifierEditor
  4. {
  5. Vector3 pm = new Vector3();
  6. public override Texture LoadImage() { return (Texture)EditorGUIUtility.LoadRequired("MegaFiers\\ffd_help.png"); }
  7. bool showpoints = false;
  8. public override bool Inspector()
  9. {
  10. MegaFFD mod = (MegaFFD)target;
  11. #if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019 && !UNITY_2020
  12. EditorGUIUtility.LookLikeControls();
  13. #endif
  14. mod.KnotSize = EditorGUILayout.FloatField("Knot Size", mod.KnotSize);
  15. mod.inVol = EditorGUILayout.Toggle("In Vol", mod.inVol);
  16. handles = EditorGUILayout.Toggle("Handles", handles);
  17. handleSize = EditorGUILayout.Slider("Size", handleSize, 0.0f, 1.0f);
  18. mirrorX = EditorGUILayout.Toggle("Mirror X", mirrorX);
  19. mirrorY = EditorGUILayout.Toggle("Mirror Y", mirrorY);
  20. mirrorZ = EditorGUILayout.Toggle("Mirror Z", mirrorZ);
  21. showpoints = EditorGUILayout.Foldout(showpoints, "Points");
  22. if ( showpoints )
  23. {
  24. int gs = mod.GridSize();
  25. //int num = gs * gs * gs;
  26. for ( int x = 0; x < gs; x++ )
  27. {
  28. for ( int y = 0; y < gs; y++ )
  29. {
  30. for ( int z = 0; z < gs; z++ )
  31. {
  32. int i = (x * gs * gs) + (y * gs) + z;
  33. mod.pt[i] = EditorGUILayout.Vector3Field("p[" + x + "," + y + "," + z + "]", mod.pt[i]);
  34. }
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. static public float handleSize = 0.5f;
  41. static public bool handles = true;
  42. static public bool mirrorX = false;
  43. static public bool mirrorY = false;
  44. static public bool mirrorZ = false;
  45. Vector3 CircleCap(int id, Vector3 pos, Quaternion rot, float size)
  46. {
  47. #if UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  48. return Handles.FreeMoveHandle(pos, rot, size, Vector3.zero, Handles.CircleHandleCap);
  49. #else
  50. return Handles.FreeMoveHandle(pos, rot, size, Vector3.zero, Handles.CircleCap);
  51. #endif
  52. }
  53. public override void DrawSceneGUI()
  54. {
  55. MegaFFD ffd = (MegaFFD)target;
  56. bool snapshot = false;
  57. if ( ffd.DisplayGizmo )
  58. {
  59. MegaModifiers context = ffd.GetComponent<MegaModifiers>();
  60. if ( context && context.DrawGizmos )
  61. {
  62. Vector3 size = ffd.lsize;
  63. Vector3 osize = ffd.lsize;
  64. osize.x = 1.0f / size.x;
  65. osize.y = 1.0f / size.y;
  66. osize.z = 1.0f / size.z;
  67. Matrix4x4 tm1 = Matrix4x4.identity;
  68. Quaternion rot = Quaternion.Euler(ffd.gizmoRot);
  69. tm1.SetTRS(-(ffd.gizmoPos + ffd.Offset), rot, ffd.gizmoScale); //Vector3.one);
  70. Matrix4x4 tm = Matrix4x4.identity;
  71. Handles.matrix = Matrix4x4.identity;
  72. if ( context != null && context.sourceObj != null )
  73. tm = context.sourceObj.transform.localToWorldMatrix * tm1;
  74. else
  75. tm = ffd.transform.localToWorldMatrix * tm1;
  76. DrawGizmos(ffd, tm); //Handles.matrix);
  77. Handles.color = Color.yellow;
  78. #if false
  79. int pc = ffd.GridSize();
  80. pc = pc * pc * pc;
  81. for ( int i = 0; i < pc; i++ )
  82. {
  83. Vector3 p = ffd.GetPoint(i); // + ffd.bcenter;
  84. //pm = Handles.PositionHandle(p, Quaternion.identity);
  85. pm = Handles.FreeMoveHandle(p, Quaternion.identity, ffd.KnotSize * 0.1f, Vector3.zero, Handles.CircleCap);
  86. pm -= ffd.bcenter;
  87. p = Vector3.Scale(pm, osize);
  88. p.x += 0.5f;
  89. p.y += 0.5f;
  90. p.z += 0.5f;
  91. ffd.pt[i] = p;
  92. }
  93. #endif
  94. int gs = ffd.GridSize();
  95. //int i = 0;
  96. Vector3 ttp = Vector3.zero;
  97. for ( int z = 0; z < gs; z++ )
  98. {
  99. for ( int y = 0; y < gs; y++ )
  100. {
  101. for ( int x = 0; x < gs; x++ )
  102. {
  103. int index = ffd.GridIndex(x, y, z);
  104. //Vector3 p = ffd.GetPoint(i); // + ffd.bcenter;
  105. Vector3 lp = ffd.GetPoint(index);
  106. Vector3 p = lp; //tm.MultiplyPoint(lp); //ffdi); // + ffd.bcenter;
  107. Vector3 tp = tm.MultiplyPoint(p);
  108. if ( handles )
  109. {
  110. //ttp = Handles.PositionHandle(tp, Quaternion.identity);
  111. //pm = tm.inverse.MultiplyPoint(Handles.PositionHandle(tm.MultiplyPoint(p), Quaternion.identity));
  112. ttp = PositionHandle(tp, Quaternion.identity, handleSize, ffd.gizCol1.a);
  113. }
  114. else
  115. {
  116. //ttp = Handles.FreeMoveHandle(tp, Quaternion.identity, ffd.KnotSize * 0.1f, Vector3.zero, Handles.CircleCap);
  117. ttp = CircleCap(0, tp, Quaternion.identity, ffd.KnotSize * 0.1f);
  118. }
  119. if ( ttp != tp )
  120. {
  121. if ( !snapshot )
  122. {
  123. MegaUndo.SetSnapshotTarget(ffd, "FFD Lattice Move");
  124. snapshot = true;
  125. }
  126. }
  127. pm = tm.inverse.MultiplyPoint(ttp);
  128. Vector3 delta = pm - p;
  129. //pm = lp + delta;
  130. //ffd.SetPoint(x, y, z, pm);
  131. pm -= ffd.bcenter;
  132. p = Vector3.Scale(pm, osize);
  133. p.x += 0.5f;
  134. p.y += 0.5f;
  135. p.z += 0.5f;
  136. ffd.pt[index] = p;
  137. if ( mirrorX )
  138. {
  139. int y1 = y - (gs - 1);
  140. if ( y1 < 0 )
  141. y1 = -y1;
  142. if ( y != y1 )
  143. {
  144. Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x, y1, z)); // + ffd.bcenter;
  145. delta.y = -delta.y;
  146. p1 += delta;
  147. p1 -= ffd.bcenter;
  148. p = Vector3.Scale(p1, osize);
  149. p.x += 0.5f;
  150. p.y += 0.5f;
  151. p.z += 0.5f;
  152. ffd.pt[ffd.GridIndex(x, y1, z)] = p;
  153. }
  154. }
  155. if ( mirrorY )
  156. {
  157. int z1 = z - (gs - 1);
  158. if ( z1 < 0 )
  159. z1 = -z1;
  160. if ( z != z1 )
  161. {
  162. Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x, y, z1)); // + ffd.bcenter;
  163. delta.z = -delta.z;
  164. p1 += delta;
  165. p1 -= ffd.bcenter;
  166. p = Vector3.Scale(p1, osize);
  167. p.x += 0.5f;
  168. p.y += 0.5f;
  169. p.z += 0.5f;
  170. ffd.pt[ffd.GridIndex(x, y, z1)] = p;
  171. }
  172. }
  173. if ( mirrorZ )
  174. {
  175. int x1 = x - (gs - 1);
  176. if ( x1 < 0 )
  177. x1 = -x1;
  178. if ( x != x1 )
  179. {
  180. Vector3 p1 = ffd.GetPoint(ffd.GridIndex(x1, y, z)); // + ffd.bcenter;
  181. delta.x = -delta.x;
  182. p1 += delta;
  183. p1 -= ffd.bcenter;
  184. p = Vector3.Scale(p1, osize);
  185. p.x += 0.5f;
  186. p.y += 0.5f;
  187. p.z += 0.5f;
  188. ffd.pt[ffd.GridIndex(x1, y, z)] = p;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. Handles.matrix = Matrix4x4.identity;
  195. if ( GUI.changed && snapshot )
  196. {
  197. MegaUndo.CreateSnapshot();
  198. MegaUndo.RegisterSnapshot();
  199. EditorUtility.SetDirty(target);
  200. }
  201. MegaUndo.ClearSnapshotTarget();
  202. }
  203. }
  204. }
  205. #if UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  206. public static Vector3 PositionHandle(Vector3 position, Quaternion rotation, float size, float alpha)
  207. {
  208. float handlesize = HandleUtility.GetHandleSize(position) * size;
  209. Color color = Handles.color;
  210. Color col = Color.red;
  211. col.a = alpha;
  212. Handles.color = col; //Color.red; //Handles..xAxisColor;
  213. //position = Handles.Slider(position, rotation * Vector3.right, handlesize, new Handles.DrawCapFunction(Handles.ArrowHandleCap), 0.0f); //SnapSettings.move.x);
  214. position = Handles.Slider(position, rotation * Vector3.right, handlesize, Handles.ArrowHandleCap, 0.0f); // new Handles.DrawCapFunction(Handles.ArrowHandleCap), 0.0f); //SnapSettings.move.x);
  215. col = Color.green;
  216. col.a = alpha;
  217. Handles.color = col; //Color.green; //Handles.yAxisColor;
  218. position = Handles.Slider(position, rotation * Vector3.up, handlesize, Handles.ArrowHandleCap, 0.0f); //SnapSettings.move.y);
  219. col = Color.blue;
  220. col.a = alpha;
  221. Handles.color = col; //Color.blue; //Handles.zAxisColor;
  222. position = Handles.Slider(position, rotation * Vector3.forward, handlesize, Handles.ArrowHandleCap, 0.0f); //SnapSettings.move.z);
  223. col = Color.yellow;
  224. col.a = alpha;
  225. Handles.color = col; //Color.yellow; //Handles.centerColor;
  226. position = Handles.FreeMoveHandle(position, rotation, handlesize * 0.15f, Vector3.zero, Handles.RectangleHandleCap);
  227. Handles.color = color;
  228. return position;
  229. }
  230. #else
  231. public static Vector3 PositionHandle(Vector3 position, Quaternion rotation, float size, float alpha)
  232. {
  233. float handlesize = HandleUtility.GetHandleSize(position) * size;
  234. Color color = Handles.color;
  235. Color col = Color.red;
  236. col.a = alpha;
  237. Handles.color = col; //Color.red; //Handles..xAxisColor;
  238. position = Handles.Slider(position, rotation * Vector3.right, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.x);
  239. col = Color.green;
  240. col.a = alpha;
  241. Handles.color = col; //Color.green; //Handles.yAxisColor;
  242. position = Handles.Slider(position, rotation * Vector3.up, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.y);
  243. col = Color.blue;
  244. col.a = alpha;
  245. Handles.color = col; //Color.blue; //Handles.zAxisColor;
  246. position = Handles.Slider(position, rotation * Vector3.forward, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.z);
  247. col = Color.yellow;
  248. col.a = alpha;
  249. Handles.color = col; //Color.yellow; //Handles.centerColor;
  250. position = Handles.FreeMoveHandle(position, rotation, handlesize * 0.15f, Vector3.zero, new Handles.DrawCapFunction(Handles.RectangleCap));
  251. Handles.color = color;
  252. return position;
  253. }
  254. #endif
  255. #if false
  256. public static Vector3 PositionHandle(Vector3 position, Quaternion rotation, float size, float alpha)
  257. {
  258. float handlesize = handleSize; //HandleUtility.GetHandleSize(position) * size;
  259. Color color = Handles.color;
  260. Color col = Color.red;
  261. col.a = alpha;
  262. Handles.color = col; //Color.red; //Handles..xAxisColor;
  263. position = Handles.Slider(position, rotation * Vector3.right, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.x);
  264. col = Color.green;
  265. col.a = alpha;
  266. Handles.color = col; //Color.green; //Handles.yAxisColor;
  267. position = Handles.Slider(position, rotation * Vector3.up, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.y);
  268. col = Color.blue;
  269. col.a = alpha;
  270. Handles.color = col; //Color.blue; //Handles.zAxisColor;
  271. position = Handles.Slider(position, rotation * Vector3.forward, handlesize, new Handles.DrawCapFunction(Handles.ArrowCap), 0.0f); //SnapSettings.move.z);
  272. col = Color.yellow;
  273. col.a = alpha;
  274. Handles.color = col; //Color.yellow; //Handles.centerColor;
  275. position = Handles.FreeMoveHandle(position, rotation, handlesize * 0.15f, Vector3.zero, new Handles.DrawCapFunction(Handles.RectangleCap));
  276. Handles.color = color;
  277. return position;
  278. }
  279. #endif
  280. Vector3[] pp3 = new Vector3[3];
  281. #if false
  282. public void DrawGizmos(MegaFFD ffd, Matrix4x4 tm)
  283. {
  284. Handles.color = ffd.gizCol1; //Color.red;
  285. int pc = ffd.GridSize();
  286. for ( int i = 0; i < pc; i++ )
  287. {
  288. for ( int j = 0; j < pc; j++ )
  289. {
  290. for ( int k = 0; k < pc; k++ )
  291. {
  292. //pp3[0] = tm.MultiplyPoint(ffd.GetPoint(i, j, k)); // + ffd.bcenter);
  293. pp3[0] = ffd.GetPoint(i, j, k); // + ffd.bcenter);
  294. if ( i < pc - 1 )
  295. {
  296. //pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i + 1, j, k)); // + ffd.bcenter);
  297. pp3[1] = ffd.GetPoint(i + 1, j, k); // + ffd.bcenter);
  298. Handles.DrawLine(pp3[0], pp3[1]);
  299. }
  300. if ( j < pc - 1 )
  301. {
  302. //pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i, j + 1, k)); // + ffd.bcenter);
  303. pp3[1] = ffd.GetPoint(i, j + 1, k); // + ffd.bcenter);
  304. Handles.DrawLine(pp3[0], pp3[1]);
  305. }
  306. if ( k < pc - 1 )
  307. {
  308. //pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i, j, k + 1)); // + ffd.bcenter);
  309. pp3[1] = ffd.GetPoint(i, j, k + 1); // + ffd.bcenter);
  310. Handles.DrawLine(pp3[0], pp3[1]);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. #else
  317. public void DrawGizmos(MegaFFD ffd, Matrix4x4 tm)
  318. {
  319. Handles.color = ffd.gizCol1; //Color.red;
  320. int pc = ffd.GridSize();
  321. for ( int i = 0; i < pc; i++ )
  322. {
  323. for ( int j = 0; j < pc; j++ )
  324. {
  325. for ( int k = 0; k < pc; k++ )
  326. {
  327. pp3[0] = tm.MultiplyPoint(ffd.GetPoint(i, j, k)); // + ffd.bcenter);
  328. //pp3[0] = ffd.GetPoint(i, j, k); // + ffd.bcenter);
  329. if ( i < pc - 1 )
  330. {
  331. pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i + 1, j, k)); // + ffd.bcenter);
  332. //pp3[1] = ffd.GetPoint(i + 1, j, k); // + ffd.bcenter);
  333. Handles.DrawLine(pp3[0], pp3[1]);
  334. }
  335. if ( j < pc - 1 )
  336. {
  337. pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i, j + 1, k)); // + ffd.bcenter);
  338. //pp3[1] = ffd.GetPoint(i, j + 1, k); // + ffd.bcenter);
  339. Handles.DrawLine(pp3[0], pp3[1]);
  340. }
  341. if ( k < pc - 1 )
  342. {
  343. pp3[1] = tm.MultiplyPoint(ffd.GetPoint(i, j, k + 1)); // + ffd.bcenter);
  344. //pp3[1] = ffd.GetPoint(i, j, k + 1); // + ffd.bcenter);
  345. Handles.DrawLine(pp3[0], pp3[1]);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. #endif
  352. }