MegaHoseNewEditor.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(MegaHoseNew))]
  4. public class MegaHoseNewEditor : Editor
  5. {
  6. [MenuItem("GameObject/Create Other/MegaShape/Hose New")]
  7. static void CreatePageMesh()
  8. {
  9. Vector3 pos = Vector3.zero;
  10. if ( UnityEditor.SceneView.lastActiveSceneView )
  11. pos = UnityEditor.SceneView.lastActiveSceneView.pivot;
  12. GameObject go = new GameObject("Hose New");
  13. MeshFilter mf = go.AddComponent<MeshFilter>();
  14. mf.sharedMesh = new Mesh();
  15. MeshRenderer mr = go.AddComponent<MeshRenderer>();
  16. Material[] mats = new Material[3];
  17. mr.sharedMaterials = mats;
  18. MegaHoseNew pm = go.AddComponent<MegaHoseNew>();
  19. go.transform.position = pos;
  20. Selection.activeObject = go;
  21. pm.rebuildcross = true;
  22. pm.updatemesh = true;
  23. //pm.Rebuild();
  24. }
  25. public override void OnInspectorGUI()
  26. {
  27. MegaHoseNew mod = (MegaHoseNew)target;
  28. #if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019 && !UNITY_2020
  29. EditorGUIUtility.LookLikeControls();
  30. #endif
  31. mod.dolateupdate = EditorGUILayout.Toggle("Do Late Update", mod.dolateupdate);
  32. mod.InvisibleUpdate = EditorGUILayout.Toggle("Invisible Update", mod.InvisibleUpdate);
  33. mod.wiretype = (MegaHoseType)EditorGUILayout.EnumPopup("Wire Type", mod.wiretype);
  34. mod.segments = EditorGUILayout.IntField("Segments", mod.segments);
  35. mod.capends = EditorGUILayout.Toggle("Cap Ends", mod.capends);
  36. mod.calcnormals = EditorGUILayout.Toggle("Calc Normals", mod.calcnormals);
  37. mod.calctangents = EditorGUILayout.Toggle("Calc Tangents", mod.calctangents);
  38. #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  39. #else
  40. mod.optimize = EditorGUILayout.Toggle("Optimize", mod.optimize);
  41. #endif
  42. mod.recalcCollider = EditorGUILayout.Toggle("Calc Collider", mod.recalcCollider);
  43. switch ( mod.wiretype )
  44. {
  45. case MegaHoseType.Round:
  46. mod.rnddia = EditorGUILayout.FloatField("Diameter", mod.rnddia);
  47. mod.rndsides = EditorGUILayout.IntField("Sides", mod.rndsides);
  48. mod.rndrot = EditorGUILayout.FloatField("Rotate", mod.rndrot);
  49. break;
  50. case MegaHoseType.Rectangle:
  51. mod.rectwidth = EditorGUILayout.FloatField("Width", mod.rectwidth);
  52. mod.rectdepth = EditorGUILayout.FloatField("Depth", mod.rectdepth);
  53. mod.rectfillet = EditorGUILayout.FloatField("Fillet", mod.rectfillet);
  54. mod.rectfilletsides = EditorGUILayout.IntField("Fillet Sides", mod.rectfilletsides);
  55. mod.rectrotangle = EditorGUILayout.FloatField("Rotate", mod.rectrotangle);
  56. break;
  57. case MegaHoseType.DSection:
  58. mod.dsecwidth = EditorGUILayout.FloatField("Width", mod.dsecwidth);
  59. mod.dsecdepth = EditorGUILayout.FloatField("Depth", mod.dsecdepth);
  60. mod.dsecrndsides = EditorGUILayout.IntField("Rnd Sides", mod.dsecrndsides);
  61. mod.dsecfillet = EditorGUILayout.FloatField("Fillet", mod.dsecfillet);
  62. mod.dsecfilletsides = EditorGUILayout.IntField("Fillet Sides", mod.dsecfilletsides);
  63. mod.dsecrotangle = EditorGUILayout.FloatField("Rotate", mod.dsecrotangle);
  64. break;
  65. }
  66. mod.uvscale = EditorGUILayout.Vector2Field("UV Scale", mod.uvscale);
  67. if ( GUI.changed )
  68. {
  69. mod.updatemesh = true;
  70. mod.rebuildcross = true;
  71. }
  72. mod.custnode = (GameObject)EditorGUILayout.ObjectField("Start Object", mod.custnode, typeof(GameObject), true);
  73. mod.offset = EditorGUILayout.Vector3Field("Offset", mod.offset);
  74. mod.rotate = EditorGUILayout.Vector3Field("Rotate", mod.rotate);
  75. //mod.scale = EditorGUILayout.Vector3Field("Scale", mod.scale);
  76. mod.custnode2 = (GameObject)EditorGUILayout.ObjectField("End Object", mod.custnode2, typeof(GameObject), true);
  77. mod.offset1 = EditorGUILayout.Vector3Field("Offset", mod.offset1);
  78. mod.rotate1 = EditorGUILayout.Vector3Field("Rotate", mod.rotate1);
  79. //mod.scale1 = EditorGUILayout.Vector3Field("Scale", mod.scale1);
  80. mod.flexon = EditorGUILayout.BeginToggleGroup("Flex On", mod.flexon);
  81. mod.flexstart = EditorGUILayout.Slider("Start", mod.flexstart, 0.0f, 1.0f);
  82. mod.flexstop = EditorGUILayout.Slider("Stop", mod.flexstop, 0.0f, 1.0f);
  83. if ( mod.flexstart > mod.flexstop )
  84. mod.flexstart = mod.flexstop;
  85. if ( mod.flexstop < mod.flexstart )
  86. mod.flexstop = mod.flexstart;
  87. mod.flexcycles = EditorGUILayout.IntField("Cycles", mod.flexcycles);
  88. mod.flexdiameter = EditorGUILayout.FloatField("Diameter", mod.flexdiameter);
  89. EditorGUILayout.EndToggleGroup();
  90. mod.usebulgecurve = EditorGUILayout.BeginToggleGroup("Use Bulge Curve", mod.usebulgecurve);
  91. mod.bulge = EditorGUILayout.CurveField("Bulge", mod.bulge);
  92. mod.bulgeamount = EditorGUILayout.FloatField("Bulge Amount", mod.bulgeamount);
  93. mod.bulgeoffset = EditorGUILayout.FloatField("Bulge Offset", mod.bulgeoffset);
  94. mod.animatebulge = EditorGUILayout.BeginToggleGroup("Animate", mod.animatebulge);
  95. mod.bulgespeed = EditorGUILayout.FloatField("Speed", mod.bulgespeed);
  96. mod.minbulge = EditorGUILayout.FloatField("Min", mod.minbulge);
  97. mod.maxbulge = EditorGUILayout.FloatField("Max", mod.maxbulge);
  98. EditorGUILayout.EndToggleGroup();
  99. EditorGUILayout.EndToggleGroup();
  100. mod.usesizecurve = EditorGUILayout.BeginToggleGroup("Use Size Curve", mod.usesizecurve);
  101. mod.size = EditorGUILayout.CurveField("Size", mod.size);
  102. EditorGUILayout.EndToggleGroup();
  103. mod.tension1 = EditorGUILayout.FloatField("Tension Start", mod.tension1);
  104. mod.tension2 = EditorGUILayout.FloatField("Tension End", mod.tension2);
  105. mod.freecreate = EditorGUILayout.BeginToggleGroup("Free Create", mod.freecreate);
  106. mod.noreflength = EditorGUILayout.FloatField("Free Length", mod.noreflength);
  107. EditorGUILayout.EndToggleGroup();
  108. mod.up = EditorGUILayout.Vector3Field("Up", mod.up);
  109. mod.displayspline = EditorGUILayout.Toggle("Display Spline", mod.displayspline);
  110. if ( GUI.changed ) //rebuild )
  111. {
  112. mod.updatemesh = true;
  113. mod.Rebuild();
  114. }
  115. }
  116. #if false
  117. public void OnSceneGUI()
  118. {
  119. MegaHoseNew hose = (MegaHoseNew)target;
  120. if ( hose.calcnormals )
  121. {
  122. Handles.matrix = hose.transform.localToWorldMatrix;
  123. Handles.color = Color.red;
  124. for ( int i = 0; i < hose.verts.Length; i++ )
  125. {
  126. //Gizmos.DrawRay(hose.verts[i], hose.normals[i] * 2.0f);
  127. Handles.DrawLine(hose.verts[i], hose.verts[i] + (hose.normals[i] * 0.5f));
  128. }
  129. }
  130. }
  131. #endif
  132. #if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  133. [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.Pickable | GizmoType.InSelectionHierarchy)]
  134. #else
  135. [DrawGizmo(GizmoType.NotSelected | GizmoType.Pickable | GizmoType.SelectedOrChild)]
  136. #endif
  137. static void RenderGizmo(MegaHoseNew hose, GizmoType gizmoType)
  138. {
  139. if ( (gizmoType & GizmoType.Active) != 0 && Selection.activeObject == hose.gameObject )
  140. {
  141. if ( !hose.displayspline )
  142. return;
  143. if ( hose.custnode == null || hose.custnode2 == null )
  144. return;
  145. DrawGizmos(hose, new Color(1.0f, 1.0f, 1.0f, 1.0f));
  146. Color col = Color.yellow;
  147. col.a = 0.5f; //0.75f;
  148. Gizmos.color = col; //Color.yellow;
  149. Matrix4x4 RingTM = Matrix4x4.identity;
  150. hose.CalcMatrix(ref RingTM, 0.0f);
  151. RingTM = hose.transform.localToWorldMatrix * RingTM;
  152. float gsize = 0.0f;
  153. switch ( hose.wiretype )
  154. {
  155. case MegaHoseType.Round: gsize = hose.rnddia; break;
  156. case MegaHoseType.Rectangle: gsize = (hose.rectdepth + hose.rectwidth) * 0.5f; break;
  157. case MegaHoseType.DSection: gsize = (hose.dsecdepth + hose.dsecwidth) * 0.5f; break;
  158. }
  159. gsize *= 0.1f;
  160. for ( int p = 0; p < hose.hosespline.knots.Count; p++ )
  161. {
  162. Vector3 p1 = RingTM.MultiplyPoint(hose.hosespline.knots[p].p);
  163. Vector3 p2 = RingTM.MultiplyPoint(hose.hosespline.knots[p].invec);
  164. Vector3 p3 = RingTM.MultiplyPoint(hose.hosespline.knots[p].outvec);
  165. Gizmos.color = Color.black;
  166. Gizmos.DrawLine(p2, p1);
  167. Gizmos.DrawLine(p3, p1);
  168. Gizmos.color = Color.green;
  169. Gizmos.DrawSphere(p1, gsize);
  170. Gizmos.color = Color.red;
  171. Gizmos.DrawSphere(p2, gsize);
  172. Gizmos.DrawSphere(p3, gsize);
  173. }
  174. }
  175. }
  176. static void DrawGizmos(MegaHoseNew hose, Color modcol1)
  177. {
  178. Matrix4x4 RingTM = Matrix4x4.identity;
  179. Matrix4x4 tm = hose.transform.localToWorldMatrix;
  180. float ldist = 1.0f * 0.1f;
  181. if ( ldist < 0.01f )
  182. ldist = 0.01f;
  183. Color modcol = modcol1;
  184. if ( hose.hosespline.length / ldist > 500.0f )
  185. ldist = hose.hosespline.length / 500.0f;
  186. float ds = hose.hosespline.length / (hose.hosespline.length / ldist);
  187. if ( ds > hose.hosespline.length )
  188. ds = hose.hosespline.length;
  189. int c = 0;
  190. int k = -1;
  191. int lk = -1;
  192. Vector3 first = hose.hosespline.Interpolate(0.0f, true, ref lk);
  193. hose.CalcMatrix(ref RingTM, 0.0f);
  194. RingTM = tm * RingTM;
  195. for ( float dist = ds; dist < hose.hosespline.length; dist += ds )
  196. {
  197. float alpha = dist / hose.hosespline.length;
  198. Vector3 pos = hose.hosespline.Interpolate(alpha, true, ref k);
  199. if ( (c & 1) == 1 )
  200. Gizmos.color = Color.black * modcol;
  201. else
  202. Gizmos.color = Color.yellow * modcol;
  203. if ( k != lk )
  204. {
  205. for ( lk = lk + 1; lk <= k; lk++ )
  206. {
  207. Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(hose.hosespline.knots[lk].p));
  208. first = hose.hosespline.knots[lk].p;
  209. }
  210. }
  211. lk = k;
  212. Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(pos));
  213. c++;
  214. first = pos;
  215. }
  216. if ( (c & 1) == 1 )
  217. Gizmos.color = Color.blue * modcol;
  218. else
  219. Gizmos.color = Color.yellow * modcol;
  220. Vector3 lastpos;
  221. if ( hose.hosespline.closed )
  222. lastpos = hose.hosespline.Interpolate(0.0f, true, ref k);
  223. else
  224. lastpos = hose.hosespline.Interpolate(1.0f, true, ref k);
  225. Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(lastpos));
  226. }
  227. }