Esempio n. 1
0
        public void DestroyInvalidObjects()
        {
            Manifold manifold  = Extrudable._manifold;
            var      toDeleteV = new List <int>(_vertexHandles.Keys).FindAll(key => !manifold.IsVertexInUse(key));

            foreach (var v in toDeleteV)
            {
                var obj = _vertexHandles[v];
                _vertexHandles.Remove(v);
                Destroy(obj.gameObject);
            }
            var toDeleteF = new List <int>(_faceHandles.Keys).FindAll(key => !manifold.IsFaceInUse(key));

            foreach (var f in toDeleteF)
            {
                var obj = _faceHandles[f];
                _faceHandles.Remove(f);
                Destroy(obj.gameObject);
            }
            var toDeleteE = new List <int>(_edgeHandles.Keys).FindAll(key => !manifold.IsHalfedgeInUse(key) || key < manifold.GetOppHalfEdge(key));

            foreach (var e in toDeleteE)
            {
                var obj   = _edgeHandles[e];
                var faces = new[] { obj.FirstFace, obj.SecondFace };
                foreach (var face in faces)
                {
                    FaceHandleController fhc;
                    if (_faceHandles.TryGetValue(face, out fhc))
                    {
                        fhc.ConnectedLatches.Remove(obj);
                    }
                }
                _edgeHandles.Remove(e);
                Destroy(obj.gameObject);
            }

            // Brute force delete all edge controllers every cleanup, might not be effecient but circumvents bug that doesn't remove all invalid edgecontrollers.
            int[] ekeys = _edgeHandles.Keys.ToArray();
            foreach (int key in ekeys)
            {
                var obj = _edgeHandles[key];
                if (!obj.Locked)
                {
                }
                _edgeHandles.Remove(key);
                Destroy(obj.gameObject);
            }

            int[] fkeys = _faceHandles.Keys.ToArray();
            foreach (int key in fkeys)
            {
                var obj = _faceHandles[key];
                _faceHandles.Remove(key);
                Destroy(obj.gameObject);
            }

            int[] vkeys = _vertexHandles.Keys.ToArray();
            foreach (int key in vkeys)
            {
                var obj = _vertexHandles[key];
                _vertexHandles.Remove(key);
                Destroy(obj.gameObject);
            }
        }
Esempio n. 2
0
        public void UpdateControls()
        {
            Manifold manifold    = Extrudable._manifold;
            var      faceIds     = new int[manifold.NumberOfFaces()];
            var      vertexIds   = new int[manifold.NumberOfVertices()];
            var      halfedgeIds = new int[manifold.NumberOfHalfEdges()];

            manifold.GetHMeshIds(vertexIds, halfedgeIds, faceIds);

            // update face handles
            foreach (var faceHandleController in _faceHandles)
            {
                var handleFace = faceHandleController.Value.AssociatedFaceID;

                if (!faceHandleController.Value.IsDragged)
                {
                    var center = manifold.GetCenterTriangulated(handleFace);

                    //Use average of edgecenters if we have more than 4 edges.
                    var temp = manifold.GetAdjacentFaceIdsAndEdgeCenters(handleFace);
                    if (temp.edgeCenter.Length > 4)
                    {
                        Vector3 cent = new Vector3();
                        foreach (Vector3 v in temp.edgeCenter)
                        {
                            cent[0] += v[0];
                            cent[1] += v[1];
                            cent[2] += v[2];
                        }
                        cent[0] /= temp.edgeCenter.Length;
                        cent[1] /= temp.edgeCenter.Length;
                        cent[2] /= temp.edgeCenter.Length;

                        center = cent;
                    }

                    // Rotate the handle to look in the opposite direction of face's normal
                    var normal     = manifold.GetFaceNormal(handleFace);
                    var edgeNormal = manifold.GetFirstEdgeDirection(handleFace);
                    faceHandleController.Value.UpdatePositionAndRotation(center, normal, edgeNormal);
                    faceHandleController.Value.gameObject.SetActive(true);
                }

                var adjacentFaces = manifold.GetAdjacentFaceIdsAndEdgeCenters(handleFace);

                for (int j = 0; j < adjacentFaces.edgeId.Length; j++)
                {
                    EdgeHandleController latch;
                    if (_edgeHandles.TryGetValue(adjacentFaces.edgeId[j], out latch))
                    {
                        var otherFace = latch.GetOtherFace(handleFace);

                        var edgeCenter         = adjacentFaces.edgeCenter[j];
                        var normal             = manifold.GetFaceNormal(handleFace);
                        var adjacentFaceNormal = manifold.GetFaceNormal(otherFace);

                        latch.UpdatePositionAndRotation(edgeCenter, adjacentFaces.edgeNormals[j],
                                                        adjacentFaceNormal + normal);
                        latch.gameObject.SetActive(true);
                    }
                }
            }

            for (int i = 0; i < faceIds.Length; i++)
            {
                int id = faceIds[i];
                FaceHandleController visitedHandleController = null;
                _faceHandles.TryGetValue(id, out visitedHandleController);

                var newHandle = visitedHandleController != null
                    ? visitedHandleController
                    : InstantiateFaceHandle(manifold, id);
                InstantiateLatches(manifold, newHandle);
            }

            foreach (var vertexHandleController in _vertexHandles)
            {
                if (!vertexHandleController.Value.IsDragged)
                {
                    Vector3 vertexPosition = manifold.VertexPosition(vertexHandleController.Key);
                    vertexHandleController.Value.transform.localPosition = vertexPosition;

                    Vector3 vertexNormal = manifold.GetVertexNormal(vertexHandleController.Key);
                    //Debug.Log("VertexHandleNormal: " + vertexNormal);
                    Quaternion handleRotation = Quaternion.LookRotation(-vertexNormal);
                    vertexHandleController.Value.transform.localRotation = handleRotation;
                }
                vertexHandleController.Value.gameObject.SetActive(true);
            }

            for (int i = 0; i < vertexIds.Length; i++)
            {
                int id = vertexIds[i];
                VertexHandleController vertexHandle = null;
                _vertexHandles.TryGetValue(id, out vertexHandle);
                if (!manifold.IsVertexInUse(id) || vertexHandle != null)
                {
                    continue;
                }

                InstantiateVertexHandle(manifold, id);
            }

            //update sizes
            foreach (var faceHandleController in _faceHandles.Values)
            {
                faceHandleController.UpdateHandleSize();
            }
            foreach (var edgeHandleController in _edgeHandles.Values)
            {
                edgeHandleController.UpdateHandleSize();
            }
            foreach (var vertexHandleController in _vertexHandles.Values)
            {
                vertexHandleController.UpdateHandleSize();
            }
        }