public void Init(SLO_Edge edge, SLO_Join attachedJoin, SLO_Join lookAtJoin, float maxLength, float joinerMaxPercentageOfEdge) { m_Edge = edge; m_AttachedJoin = attachedJoin; m_LookAtJoin = lookAtJoin; m_SleeveLength = maxLength; m_JoinerMaxPercentageOfEdge = joinerMaxPercentageOfEdge; m_TubeMesh = new GameObject("Tube Mesh").AddComponent <ProceduralMesh_Tube>(); m_TubeMesh.transform.SetParent(transform); m_TubeMesh.Init(); m_TubeMesh.transform.localScale = Vector3.one; m_TubeMesh.transform.localRotation = Quaternion.Euler(90, 0, 0); m_TubeMesh.m_Renderer.material = SLOResourceManager.Instance.m_MatJoiner; if (edge.m_Join0 == m_AttachedJoin) { edge.m_JoinArm0 = this; } else { edge.m_JoinArm1 = this; } transform.LookAt(LookAt); UpdateArm(); }
public void RemoveEdge(SLO_Edge edge) { // remove edge from list m_ConnectedEdges.Remove(edge); if (m_Arms.Contains(edge.m_JoinArm0)) { m_Arms.Remove(edge.m_JoinArm0); Destroy(edge.m_JoinArm0.gameObject); } else if (m_Arms.Contains(edge.m_JoinArm1)) { m_Arms.Remove(edge.m_JoinArm1); Destroy(edge.m_JoinArm1.gameObject); } print("Removing edge from " + name); }
public void AddEdge(SLO_Edge edge, float maxJoinLength) { m_ConnectedEdges.Add(edge); SLO_Join lookAtJoin = edge.m_Join0; if (lookAtJoin.transform.position == transform.position) { lookAtJoin = edge.m_Join1; } // Create new arm SLO_Join_Arm newArm = new GameObject("Join Arm").AddComponent <SLO_Join_Arm>(); newArm.name = "Join Arm"; newArm.transform.SetParent(transform); newArm.Init(edge, this, lookAtJoin, maxJoinLength, m_MaxLengthPercentage); m_Arms.Add(newArm); }
// Update is called once per frame void Update() { // hax if (m_SelectedElement != null) { if (m_SelectedElement.GetComponent <SLO_Face>()) { SLO_Face face = m_SelectedElement.GetComponent <SLO_Face>(); m_UITxt_Tri.text = "Face: #" + face.m_Index + "\n" + "Edge 0 Length: " + (face.Edge0Length * 1000).ToString("###") + "\n" + "Edge 01 Angle: " + face.Angle01.ToString("##.#") + "\n" + "Edge 1 Length: " + (face.Edge1Length * 1000).ToString("###") + "\n" + "Edge 12 Angle: " + face.Angle12.ToString("##.#") + "\n" + "Edge 2 Length: " + (face.Edge2Length * 1000).ToString("###") + "\n" + "Edge 20 Angle: " + face.Angle20.ToString("##.#") + "\n"; } else if (m_SelectedElement.transform.parent.transform.parent.GetComponent <SLO_Edge>()) { SLO_Edge edge = m_SelectedElement.transform.parent.transform.parent.GetComponent <SLO_Edge>(); m_UITxt_Tri.text = "Edge: #" + edge.m_Index + "\n" + "Length: " + (edge.Length * 1000).ToString("###") + "\n" + "Connected to Joiners: " + edge.m_Join0.Index + " - " + edge.m_Join1.Index; } } // Update GUI if (SelectedObj != null) { m_TextEdgeLength.text = "Edge Length: " + (SelectedObj.TotalEdgeLength * 1000f).ToString("##"); m_TextAreaTotal.text = "Area: " + (SelectedObj.TotalArea * 1000f).ToString("##"); m_TextJointCount.text = "Joint count: " + SelectedObj.JointCount; } }
void Update() { if (Input.GetKeyDown(KeyCode.C)) { m_SelectedSLOObject = m_MeshConvertor.ConvertMesh(m_MeshFilterTest, true, true); } // Testing if (Input.GetKeyDown(KeyCode.Delete)) { if (m_SelectedComponentType == ComponentType.Face) { SLO_Face face = m_SelectedComponent.GetComponent <SLO_Face>(); m_SelectedSLOObject.Faces.Remove(face); // TODO: Remove tabs? Destroy(face.gameObject); m_GUI.m_SelectedElement = null; } else if (m_SelectedComponentType == ComponentType.Edge) { SLO_Edge edge = m_SelectedComponent.GetComponent <SLO_Edge>(); // Remove form list m_SelectedSLOObject.Edges.Remove(edge); edge.Delete(); // set gui back to null m_GUI.m_SelectedElement = null; } } }
public SLOObject ConvertMesh(MeshFilter meshfilter, bool addOuterTabs, bool addInnerTabs) { // Create new object SLOObject sloObj = new GameObject("SLO Obj - ").AddComponent <SLOObject>(); sloObj.transform.position = Vector3.zero; float m_EdgeDiameter = PlayerPrefs.GetFloat("m_EdgeDiameter", 9.5f); float m_JoinerLength = PlayerPrefs.GetFloat("m_JoinerLength", 20); float WallThickness = PlayerPrefs.GetFloat("WallThickness", 2); Mesh mesh = meshfilter.mesh; // Create array of edges for (int i = 0; i < mesh.triangles.Length / 3; i++) { // Create edge for 0 - 1 sloObj.CreateEdge(mesh.vertices[mesh.triangles[(i * 3)]], mesh.vertices[mesh.triangles[(i * 3) + 1]]); // Create edge for 1 - 2 sloObj.CreateEdge(mesh.vertices[mesh.triangles[(i * 3) + 1]], mesh.vertices[mesh.triangles[(i * 3) + 2]]); // Create edge for 2 - 0 sloObj.CreateEdge(mesh.vertices[mesh.triangles[(i * 3) + 2]], mesh.vertices[mesh.triangles[(i * 3)]]); } #region Find and remove duplicate edges // Check if any edges are the same and remove copies // If edges share a vertex add them to the appropriate connection list List <SLO_Edge> edgesToRemove = new List <SLO_Edge>(); for (int i = 0; i < sloObj.Edges.Count; i++) { SLO_Edge baseEdge = sloObj.Edges[i]; if (edgesToRemove.Contains(baseEdge)) { continue; } for (int j = 0; j < sloObj.Edges.Count; j++) { if (j == i) { continue; } SLO_Edge compareEdge = sloObj.Edges[j]; if (edgesToRemove.Contains(compareEdge)) { continue; } // If not same length then continue if (baseEdge.Length != compareEdge.Length) { continue; } else if (baseEdge.m_VertexPos0 == compareEdge.m_VertexPos0 && baseEdge.m_VertexPos1 == compareEdge.m_VertexPos1) { edgesToRemove.Add(baseEdge); } else if (baseEdge.m_VertexPos0 == compareEdge.m_VertexPos1 && baseEdge.m_VertexPos1 == compareEdge.m_VertexPos0) { edgesToRemove.Add(baseEdge); } } } // Remove duplicate edges foreach (SLO_Edge e in edgesToRemove) { if (e != null) { sloObj.Edges.Remove(e); DestroyImmediate(e.gameObject); } } #endregion // Index the remaining edges for (int i = 0; i < sloObj.Edges.Count; i++) { sloObj.Edges[i].m_Index = i; sloObj.Edges[i].name = "Edge " + i; } // Find all unique verts List <Vector3> uniqueVertPositions = new List <Vector3>(); for (int i = 0; i < mesh.vertices.Length; i++) { if (!uniqueVertPositions.Contains(mesh.vertices[i])) { uniqueVertPositions.Add(mesh.vertices[i]); } } // Set to the transform of the mesh sloObj.transform.position = transform.position; // Create joiners for each unique vert for (int i = 0; i < uniqueVertPositions.Count; i++) { sloObj.CreateJoiner(meshfilter.transform.TransformPoint(uniqueVertPositions[i])); } // Assign joiners to edges for (int i = 0; i < sloObj.Edges.Count; i++) { for (int j = 0; j < sloObj.Joiners.Count; j++) { if (meshfilter.transform.TransformPoint(sloObj.Edges[i].m_VertexPos0) == sloObj.Joiners[j].transform.position) { sloObj.Edges[i].m_Join0 = sloObj.Joiners[j]; } if (meshfilter.transform.TransformPoint(sloObj.Edges[i].m_VertexPos1) == sloObj.Joiners[j].transform.position) { sloObj.Edges[i].m_Join1 = sloObj.Joiners[j]; } } } // Assign joiners to edges for (int i = 0; i < sloObj.Edges.Count; i++) { sloObj.Edges[i].m_Join0.AddEdge(sloObj.Edges[i], m_JoinerLength); sloObj.Edges[i].m_Join1.AddEdge(sloObj.Edges[i], m_JoinerLength); } // Initalize edges for (int i = 0; i < sloObj.Edges.Count; i++) { sloObj.Edges[i].transform.position = sloObj.Edges[i].m_Join0.transform.position; sloObj.Edges[i].transform.LookAt(sloObj.Edges[i].m_Join1.transform.position); sloObj.Edges[i].transform.SetParent(sloObj.transform); sloObj.Edges[i].AddEdgeMesh(); // sloObj.Edges[i].SetMaterial(SLOManager.Instance.m_MatEdge); } // Create faces for (int i = 0; i < mesh.triangles.Length / 3f; i++) { int triIndex = (i * 3); Vector3 v0 = meshfilter.transform.TransformPoint(mesh.vertices[mesh.triangles[triIndex]]); Vector3 v1 = meshfilter.transform.TransformPoint(mesh.vertices[mesh.triangles[triIndex + 1]]); Vector3 v2 = meshfilter.transform.TransformPoint(mesh.vertices[mesh.triangles[triIndex + 2]]); SLO_Join j0 = sloObj.FindJoinFromPos(v0); SLO_Join j1 = sloObj.FindJoinFromPos(v1); SLO_Join j2 = sloObj.FindJoinFromPos(v2); sloObj.CreateFace(j0, j1, j2); } for (int i = 0; i < sloObj.Faces.Count; i++) { int triIndex = (i * 3); SLO_Join j0 = sloObj.Faces[i].Join0; // m_MeshParent.TransformPoint(m_Mesh.vertices[m_Mesh.triangles[triIndex]]); SLO_Join j1 = sloObj.Faces[i].Join1; //m_MeshParent.TransformPoint(m_Mesh.vertices[m_Mesh.triangles[triIndex + 1]]); SLO_Join j2 = sloObj.Faces[i].Join2; //m_MeshParent.TransformPoint(m_Mesh.vertices[m_Mesh.triangles[triIndex + 2]]); // Tri 1 sloObj.CreateTab(j0, j1, j2); sloObj.CreateTab(j1, j2, j0); sloObj.CreateTab(j2, j0, j1); } sloObj.CreateCaps(); sloObj.RecalculateAllStats(); // Deactivate original meshfilter meshfilter.transform.gameObject.SetActive(false); return(sloObj); }