Guest User

Untitled

a guest
May 7th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. using SplineMesh;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using static Absum.Helper.MeshHelpers;
  7.  
  8. namespace Absum.Helper
  9. {
  10. [CustomEditor(typeof(SplineFromMesh))]
  11. public class SplineFromMeshEditor : Editor
  12. {
  13. private const int EDGES_MAX = 32;
  14. private float _progress;
  15. private bool _running = false;
  16. private SplineFromMesh _s;
  17.  
  18. private Spline _splineScript;
  19. public GameObject GO_S => Selection.activeGameObject;
  20. public Mesh Mesh_S => MeshFilter_S.sharedMesh;
  21. public MeshFilter MeshFilter_S => _s.meshfilter;
  22. public Spline SS { get => _splineScript; set => _splineScript = value; }
  23. public int[] Tris => Mesh_S.triangles;
  24. private List<CubicBezierCurve> Curves_S => SS.curves;
  25. private List<SplineNode> Nodes_S => SS.nodes;
  26.  
  27. public override void OnInspectorGUI()
  28. {
  29. _s = (SplineFromMesh)target;
  30.  
  31. DrawDefaultInspector();
  32.  
  33. EditorGUILayout.BeginVertical();
  34.  
  35. EditorGUILayout.HelpBox("The button adds node by mesh", MessageType.Info);
  36.  
  37. //EditorGUILayout.ObjectField("Mesh", )
  38.  
  39. if (_progress > 0f && _progress < 1f)
  40. {
  41. if (GUILayout.Button("Cancel", GUILayout.ExpandWidth(true)))
  42. {
  43. _running = false;
  44. _progress = 0f;
  45. }
  46.  
  47. ProgressBar("Generating nodes...", _progress);
  48. }
  49. else
  50. {
  51. if (GUILayout.Button("Generate nodes from mesh", GUILayout.ExpandWidth(true)))
  52. {
  53. _running = true;
  54. GenerateNodes();
  55. }
  56. }
  57.  
  58. //EditorUtility.DisplayProgressBar("Node-Generation", "Generating nodes", _progress);
  59.  
  60. EditorGUILayout.EndVertical();
  61. }
  62.  
  63. private void ClearNodesOneByOne()
  64. {
  65. // clear and add new nodes
  66. for (int i = 0; i < Nodes_S.Count; i++)
  67. {
  68. if (!_running)
  69. {
  70. Debug.LogError("Aborted!");
  71. break;
  72. }
  73.  
  74. SplineNode theNode = Nodes_S[i];
  75.  
  76. SS.RemoveNode(theNode); // TODO use real direction
  77. _progress += 1f / Nodes_S.Count;
  78. new WaitForEndOfFrame();
  79. new WaitForSeconds(0.1f);
  80. }
  81. }
  82.  
  83. private Vector3 EdgeTranslate(Vector3 edgePos)
  84. {
  85. Vector3 result = edgePos;
  86. // transform
  87. result += MeshFilter_S.transform.position;
  88. //res.Scale = MeshFilter.transform.localScale;
  89. return result;
  90. }
  91.  
  92. private void GenerateNodes()
  93. {
  94. if (!MeshFilter_S)
  95. {
  96. Debug.LogError("No MeshFilter assigned!", SS.gameObject);
  97. return;
  98. }
  99.  
  100. // reset progressbar value
  101. _progress = 0f;
  102.  
  103. // clear old nodes and curves
  104. Nodes_S.Clear();
  105. Curves_S.Clear();
  106.  
  107. // iterate through mesh nodes
  108. var edges = MeshHelpers.GetEdges(Tris).FindBoundary().SortEdges();
  109. Debug.Log("Found " + edges.Count + " edges");
  110.  
  111. // add first edge position to end of list
  112. // so the spline will be closed
  113. edges.Add(edges[0]);
  114.  
  115. for (int i = 0; i < edges.Count; i++)
  116. {
  117. if (!_running)
  118. {
  119. Debug.LogError("Aborted!");
  120. break;
  121. }
  122.  
  123. // determine edge position
  124. Edge edge = edges[i];
  125. Vector3 edgePos = Mesh_S.vertices[edge.v1];
  126. Debug.Log("Edge [" + i + "] at " + edgePos);
  127.  
  128. // determine following edge position
  129. int nextEdgeIndex = i + 1;
  130. if (nextEdgeIndex >= edges.Count)
  131. nextEdgeIndex = 0;
  132.  
  133. var nextEdge = edges[nextEdgeIndex];
  134. Vector3 nextEdgePos = Mesh_S.vertices[nextEdge.v1];
  135. Debug.Log("- Next [" + nextEdgeIndex + "] at " + nextEdgePos);
  136.  
  137. //Vector3 direction = nextEdgePos;
  138. //Vector3 direction = Vector3.zero;
  139. Vector3 direction = edgePos;
  140.  
  141. // add new nodes
  142. var sn = new SplineNode(edgePos, direction); // TODO use real direction
  143.  
  144. SS.AddNode(sn);
  145. _progress += 1f / edges.Count;
  146. new WaitForEndOfFrame();
  147. new WaitForSeconds(0.1f);
  148.  
  149. if (i > 32)
  150. {
  151. _running = false;
  152. Debug.LogError("Too many edges to be created! Max is " + EDGES_MAX + ".");
  153. break;
  154. }
  155. }
  156.  
  157. SS.RefreshCurves();
  158.  
  159. // set position of parent GO
  160. GO_S.transform.position = MeshFilter_S.transform.position;
  161. GO_S.transform.rotation = MeshFilter_S.transform.rotation;
  162. GO_S.transform.localScale = MeshFilter_S.transform.localScale;
  163.  
  164. // finish progress
  165. _progress = 1f;
  166. }
  167.  
  168. private void OnEnable()
  169. {
  170. SS = GO_S.GetComponent<Spline>();
  171. }
  172.  
  173. private void ProgressBar(string label, float value)
  174. {
  175. // Get a rect for the progress bar using the same margins as a textfield:
  176. Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
  177. EditorGUI.ProgressBar(rect, value, label);
  178. EditorGUILayout.Space();
  179. }
  180. }
  181. }
Add Comment
Please, Sign In to add comment