public override ActionResult DoAction()
        {
            if (MeshSelection.selectedObjectCount < 1)
            {
                return(ActionResult.NoSelection);
            }

            UndoUtility.RecordSelection("Delete Face");

            int count = 0;

            foreach (ProBuilderMesh pb in MeshSelection.topInternal)
            {
                if (pb.selectedFaceCount == pb.facesInternal.Length)
                {
                    Debug.LogWarning("Attempting to delete all faces on this mesh...  I'm afraid I can't let you do that.");
                    continue;
                }

                pb.DeleteFaces(pb.selectedFacesInternal);
                count += pb.selectedFaceCount;

                pb.ToMesh();
                pb.Refresh();
                pb.Optimize();
            }

            MeshSelection.ClearElementSelection();
            ProBuilderEditor.Refresh();

            if (count > 0)
            {
                return(new ActionResult(ActionResult.Status.Success, "Delete " + count + " Faces"));
            }

            return(new ActionResult(ActionResult.Status.Failure, "No Faces Selected"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return a pb_ActionResult indicating the success/failure of action.
        /// </summary>
        /// <returns></returns>
        protected override ActionResult PerformActionImplementation()
        {
            var selection = MeshSelection.top.ToArray();

            Undo.RecordObjects(selection, "Removing Edges");

            List <Face> edgeFaces = new List <Face>();

            Dictionary <Face, int> faceToMergeGroup = new Dictionary <Face, int>();
            HashSet <int>          mergeGroupIDs    = new HashSet <int>();
            List <List <Face> >    mergeGroups      = new List <List <Face> >();

            foreach (ProBuilderMesh pbMesh in selection)
            {
                if (pbMesh.selectedEdgeCount > 0)
                {
                    var selectedEdges = pbMesh.selectedEdges;
                    faceToMergeGroup.Clear();

                    foreach (var edge in selectedEdges)
                    {
                        edgeFaces.Clear();
                        mergeGroupIDs.Clear();

                        //Retrieving impacted faces from edge
                        ElementSelection.GetNeighborFaces(pbMesh, edge, edgeFaces);

                        //Chacking all edges status
                        foreach (var face in edgeFaces)
                        {
                            if (faceToMergeGroup.ContainsKey(face))
                            {
                                mergeGroupIDs.Add(faceToMergeGroup[face]);
                            }
                            else
                            {
                                faceToMergeGroup.Add(face, -1);
                            }
                        }

                        //These faces haven't been seen before
                        if (mergeGroupIDs.Count == 0)
                        {
                            foreach (var face in edgeFaces)
                            {
                                faceToMergeGroup[face] = mergeGroups.Count;
                            }
                            mergeGroups.Add(new List <Face>(edgeFaces));
                        }
                        //If only a face should already be merge, add other faces to the same merge group
                        else if (mergeGroupIDs.Count == 1)
                        {
                            foreach (var face in edgeFaces)
                            {
                                if (faceToMergeGroup[face] == -1)
                                {
                                    int index = mergeGroupIDs.First();
                                    faceToMergeGroup[face] = index;
                                    mergeGroups[index].Add(face);
                                }
                            }
                        }
                        //If more than a face already belongs to a merge group, merge these groups together
                        else
                        {
                            //Group the different mergeGroups together
                            List <Face> facesToMerge = new List <Face>();
                            foreach (var groupID in mergeGroupIDs)
                            {
                                facesToMerge.AddRange(mergeGroups[groupID]);
                                mergeGroups[groupID] = null;
                            }

                            foreach (var face in edgeFaces)
                            {
                                if (!facesToMerge.Contains(face))
                                {
                                    facesToMerge.Add(face);
                                }
                            }

                            //Remove unnecessary groups
                            mergeGroups.RemoveAll(group => group == null);
                            //Add newly created one
                            mergeGroups.Add(facesToMerge);

                            //Update groups references
                            for (int i = 0; i < mergeGroups.Count; i++)
                            {
                                foreach (var face in mergeGroups[i])
                                {
                                    faceToMergeGroup[face] = i;
                                }
                            }
                        }
                    }

                    foreach (var mergeGroup in mergeGroups)
                    {
                        MergeElements.Merge(pbMesh, mergeGroup);
                    }

                    pbMesh.ToMesh();
                    pbMesh.Refresh();
                    pbMesh.Optimize();
                }
            }

            MeshSelection.ClearElementSelection();

            // Rebuild the pb_Editor caches
            ProBuilderEditor.Refresh();

            return(new ActionResult(ActionResult.Status.Success, "Edges Removed"));
        }