Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SplineMesh;
- using System;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- using static Absum.Helper.MeshHelpers;
- namespace Absum.Helper
- {
- [CustomEditor(typeof(SplineFromMesh))]
- public class SplineFromMeshEditor : Editor
- {
- private const int EDGES_MAX = 32;
- private float _progress;
- private bool _running = false;
- private SplineFromMesh _s;
- private Spline _splineScript;
- public GameObject GO_S => Selection.activeGameObject;
- public Mesh Mesh_S => MeshFilter_S.sharedMesh;
- public MeshFilter MeshFilter_S => _s.meshfilter;
- public Spline SS { get => _splineScript; set => _splineScript = value; }
- public int[] Tris => Mesh_S.triangles;
- private List<CubicBezierCurve> Curves_S => SS.curves;
- private List<SplineNode> Nodes_S => SS.nodes;
- public override void OnInspectorGUI()
- {
- _s = (SplineFromMesh)target;
- DrawDefaultInspector();
- EditorGUILayout.BeginVertical();
- EditorGUILayout.HelpBox("The button adds node by mesh", MessageType.Info);
- //EditorGUILayout.ObjectField("Mesh", )
- if (_progress > 0f && _progress < 1f)
- {
- if (GUILayout.Button("Cancel", GUILayout.ExpandWidth(true)))
- {
- _running = false;
- _progress = 0f;
- }
- ProgressBar("Generating nodes...", _progress);
- }
- else
- {
- if (GUILayout.Button("Generate nodes from mesh", GUILayout.ExpandWidth(true)))
- {
- _running = true;
- GenerateNodes();
- }
- }
- //EditorUtility.DisplayProgressBar("Node-Generation", "Generating nodes", _progress);
- EditorGUILayout.EndVertical();
- }
- private void ClearNodesOneByOne()
- {
- // clear and add new nodes
- for (int i = 0; i < Nodes_S.Count; i++)
- {
- if (!_running)
- {
- Debug.LogError("Aborted!");
- break;
- }
- SplineNode theNode = Nodes_S[i];
- SS.RemoveNode(theNode); // TODO use real direction
- _progress += 1f / Nodes_S.Count;
- new WaitForEndOfFrame();
- new WaitForSeconds(0.1f);
- }
- }
- private Vector3 EdgeTranslate(Vector3 edgePos)
- {
- Vector3 result = edgePos;
- // transform
- result += MeshFilter_S.transform.position;
- //res.Scale = MeshFilter.transform.localScale;
- return result;
- }
- private void GenerateNodes()
- {
- if (!MeshFilter_S)
- {
- Debug.LogError("No MeshFilter assigned!", SS.gameObject);
- return;
- }
- // reset progressbar value
- _progress = 0f;
- // clear old nodes and curves
- Nodes_S.Clear();
- Curves_S.Clear();
- // iterate through mesh nodes
- var edges = MeshHelpers.GetEdges(Tris).FindBoundary().SortEdges();
- Debug.Log("Found " + edges.Count + " edges");
- // add first edge position to end of list
- // so the spline will be closed
- edges.Add(edges[0]);
- for (int i = 0; i < edges.Count; i++)
- {
- if (!_running)
- {
- Debug.LogError("Aborted!");
- break;
- }
- // determine edge position
- Edge edge = edges[i];
- Vector3 edgePos = Mesh_S.vertices[edge.v1];
- Debug.Log("Edge [" + i + "] at " + edgePos);
- // determine following edge position
- int nextEdgeIndex = i + 1;
- if (nextEdgeIndex >= edges.Count)
- nextEdgeIndex = 0;
- var nextEdge = edges[nextEdgeIndex];
- Vector3 nextEdgePos = Mesh_S.vertices[nextEdge.v1];
- Debug.Log("- Next [" + nextEdgeIndex + "] at " + nextEdgePos);
- //Vector3 direction = nextEdgePos;
- //Vector3 direction = Vector3.zero;
- Vector3 direction = edgePos;
- // add new nodes
- var sn = new SplineNode(edgePos, direction); // TODO use real direction
- SS.AddNode(sn);
- _progress += 1f / edges.Count;
- new WaitForEndOfFrame();
- new WaitForSeconds(0.1f);
- if (i > 32)
- {
- _running = false;
- Debug.LogError("Too many edges to be created! Max is " + EDGES_MAX + ".");
- break;
- }
- }
- SS.RefreshCurves();
- // set position of parent GO
- GO_S.transform.position = MeshFilter_S.transform.position;
- GO_S.transform.rotation = MeshFilter_S.transform.rotation;
- GO_S.transform.localScale = MeshFilter_S.transform.localScale;
- // finish progress
- _progress = 1f;
- }
- private void OnEnable()
- {
- SS = GO_S.GetComponent<Spline>();
- }
- private void ProgressBar(string label, float value)
- {
- // Get a rect for the progress bar using the same margins as a textfield:
- Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
- EditorGUI.ProgressBar(rect, value, label);
- EditorGUILayout.Space();
- }
- }
- }
Add Comment
Please, Sign In to add comment