MegaFFDWarpEditor.cs 13 KB

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