Exemplo n.º 1
0
        public PBAutoUnwrapSettings GetSettings(MeshSelection selection)
        {
            if (selection == null)
            {
                return(PBAutoUnwrapSettings.defaultAutoUnwrapSettings);
            }

            selection = selection.ToFaces(false, false);

            PBAutoUnwrapSettings unwrapSettings = PBAutoUnwrapSettings.defaultAutoUnwrapSettings;
            IList <Face>         faces          = new List <Face>();

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();
                faces.Clear();
                mesh.GetFaces(kvp.Value, faces);
                for (int i = 0; i < faces.Count;)
                {
                    Face face = faces[i];
                    unwrapSettings = face.uv;
                    break;
                }
            }

            return(unwrapSettings);
        }
Exemplo n.º 2
0
        private void SetTextureGroup(MeshSelection selection, int textureGroup = 0)
        {
            selection = selection.ToFaces(false, false);

            List <Face> faces = new List <Face>();
            Dictionary <GameObject, IList <int> > selectedFaces = selection.SelectedFaces;

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selectedFaces)
            {
                faces.Clear();

                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();
                mesh.GetFaces(kvp.Value, faces);

                int texGroup = textureGroup >= 0 ? GetUnusedTextureGroup(mesh) : -1;
                for (int i = 0; i < faces.Count; ++i)
                {
                    Face face = faces[i];
                    face.textureGroup = texGroup;
                }

                mesh.RefreshUV(faces);

                mesh.ToMesh();
                mesh.Refresh();
            }
        }
        public override void SetSelection(MeshSelection selection)
        {
            if (m_faceSelection != null)
            {
                m_faceSelection.Clear();
            }

            if (selection != null)
            {
                selection = selection.ToFaces(false);

                m_faceSelection.BeginChange();

                foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
                {
                    ProBuilderMesh mesh   = kvp.Key.GetComponent <ProBuilderMesh>();
                    PBMesh         pbMesh = mesh.GetComponent <PBMesh>();
                    if (pbMesh.IsMarkedAsDestroyed)
                    {
                        continue;
                    }
                    foreach (int face in kvp.Value)
                    {
                        m_faceSelection.Add(mesh, face);
                    }
                }

                m_faceSelection.EndChange();
            }
        }
Exemplo n.º 4
0
        public void ApplySettings(PBAutoUnwrapSettings settings, MeshSelection selection)
        {
            if (selection == null)
            {
                return;
            }

            selection = selection.ToFaces(false, false);

            AutoUnwrapSettings unwrapSettings = settings;
            IList <Face>       faces          = new List <Face>();

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();

                faces.Clear();
                mesh.GetFaces(kvp.Value, faces);
                for (int i = 0; i < faces.Count; ++i)
                {
                    Face face = faces[i];
                    face.uv = unwrapSettings;
                }

                mesh.RefreshUV(faces);


                mesh.ToMesh();
                mesh.Refresh();
            }
        }
Exemplo n.º 5
0
        public static void ApplySmoothingGroup(PBMesh pbMesh, MeshSelection selection, float angleThreshold)
        {
            selection = selection.ToFaces(false);

            ProBuilderMesh mesh  = pbMesh.ProBuilderMesh;
            IList <Face>   faces = new List <Face>();

            mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), faces);
            Smoothing.ApplySmoothingGroups(mesh, faces, angleThreshold);
        }
Exemplo n.º 6
0
        public override void Delete()
        {
            MeshSelection selection = GetSelection();

            selection = selection.ToFaces(false, true);

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();
                mesh.DeleteFaces(kvp.Value);
                mesh.ToMesh();
                mesh.Refresh();
            }
        }
Exemplo n.º 7
0
        public ApplyMaterialResult ApplyMaterial(Material material, MeshSelection selection)
        {
            if (selection == null)
            {
                return(null);
            }

            selection = selection.ToFaces(false, false);

            MeshMaterialsState oldState = new MeshMaterialsState();
            MeshMaterialsState newState = new MeshMaterialsState();

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

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();

                AddAllFacesToState(oldState, mesh);
                AddMaterialsToState(oldState, mesh);

                faces.Clear();
                mesh.GetFaces(kvp.Value, faces);

                mesh.SetMaterial(faces, material);
                mesh.Refresh();
                mesh.ToMesh();

                RemoveUnusedMaterials(mesh);
                mesh.Refresh();
                mesh.ToMesh();

                GetFaceToSubmeshIndexes(newState.FaceToSubmeshIndex, kvp, mesh);
                AddMaterialsToState(newState, mesh);
            }

            if (MaterialsApplied != null)
            {
                MaterialsApplied();
            }

            if (oldState.FaceToSubmeshIndex.Count == 0 && newState.FaceToSubmeshIndex.Count == 0)
            {
                return(null);
            }

            return(new ApplyMaterialResult(oldState, newState));
        }
Exemplo n.º 8
0
        public MeshSelection SelectFaceGroup(MeshSelection currentSelection)
        {
            if (currentSelection == null)
            {
                return(null);
            }

            currentSelection = currentSelection.ToFaces(false);
            if (!currentSelection.HasFaces)
            {
                return(null);
            }


            ProBuilderMesh mesh = currentSelection.SelectedFaces.Last().Key.GetComponent <ProBuilderMesh>();
            IList <int>    currentlySelectedFaces = currentSelection.SelectedFaces.Last().Value;

            if (currentlySelectedFaces.Count == 0)
            {
                return(currentSelection);
            }
            //HashSet<int> facesHs = new HashSet<int>(currentlySelectedFaces);
            int faceIndex    = currentlySelectedFaces.Last();
            int textureGroup = mesh.faces[faceIndex].textureGroup;

            if (textureGroup == -1)
            {
                return(currentSelection);
            }

            MeshSelection selection     = new MeshSelection();
            IList <Face>  faces         = mesh.faces;
            List <int>    selectedFaces = new List <int>();

            for (int i = 0; i < faces.Count; ++i)
            {
                Face face = faces[i];
                if (face.textureGroup == textureGroup /*&& !facesHs.Contains(i)*/)
                {
                    selectedFaces.Add(i);
                }
            }

            selection.SelectedFaces.Add(mesh.gameObject, selectedFaces);
            return(selection);
        }
Exemplo n.º 9
0
        public static void SetGroup(PBMesh pbMesh, MeshSelection selection, int index)
        {
            selection = selection.ToFaces(false);

            ProBuilderMesh mesh  = pbMesh.ProBuilderMesh;
            IList <Face>   faces = new List <Face>();

            mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), faces);

            foreach (Face face in faces)
            {
                face.smoothingGroup = index;
            }

            mesh.ToMesh();
            mesh.Refresh();
        }
Exemplo n.º 10
0
        public bool HasAutoUV(MeshSelection selection, bool auto)
        {
            if (selection == null)
            {
                return(false);
            }

            selection = selection.ToFaces(false, false);

            IList <Face> faces = new List <Face>();

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();

                faces.Clear();
                mesh.GetFaces(kvp.Value, faces);
                for (int i = 0; i < faces.Count; ++i)
                {
                    Face face = faces[i];
                    if (auto)
                    {
                        if (!face.manualUV)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        if (face.manualUV)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 11
0
        public void SetAutoUV(MeshSelection selection, bool auto)
        {
            if (selection == null)
            {
                return;
            }

            selection = selection.ToFaces(false, false);

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

            foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();

                faces.Clear();
                mesh.GetFaces(kvp.Value, faces);

                PBAutoUVConversion.SetAutoUV(mesh, faces.ToArray(), auto);
                mesh.ToMesh();
                mesh.Refresh();
            }
        }
Exemplo n.º 12
0
        public static MeshSelection ExpandSelection(MeshSelection selection)
        {
            selection = selection.ToFaces(false);

            foreach (PBMesh pbMesh in selection.GetSelectedMeshes().ToArray())
            {
                ProBuilderMesh mesh          = pbMesh.ProBuilderMesh;
                IList <Face>   selectedFaces = new List <Face>();
                mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), selectedFaces);

                HashSet <int> groupsHs = new HashSet <int>();
                for (int i = 0; i < selectedFaces.Count; ++i)
                {
                    Face face = selectedFaces[i];
                    if (!groupsHs.Contains(face.smoothingGroup))
                    {
                        groupsHs.Add(face.smoothingGroup);
                    }
                }


                IList <int>  selectedIndices = new List <int>();
                IList <Face> faces           = mesh.faces;
                for (int i = 0; i < faces.Count; ++i)
                {
                    Face face = faces[i];
                    if (groupsHs.Contains(face.smoothingGroup))
                    {
                        selectedIndices.Add(i);
                    }
                }

                selection.SelectedFaces[pbMesh.gameObject] = selectedIndices;
            }

            return(selection);
        }
        public virtual void BeginDrag(MeshSelection selection, Vector3 initialPosition, Quaternion initialRotation)
        {
            Selection = selection.ToFaces(false, false);

            InitialPosition = initialPosition;
            InitialRotation = initialRotation;
            Matrix          = Matrix4x4.TRS(InitialPosition, InitialRotation, Vector3.one);
            MatrixInv       = Matrix.inverse;

            List <ProBuilderMesh> allMeshes      = new List <ProBuilderMesh>();
            List <Vector2[]>      allOrigins     = new List <Vector2[]>();
            List <Vector2[]>      allFaceCenters = new List <Vector2[]>();
            List <PBAutoUVConversion.UVTransform[]> allUVTransforms = new List <PBAutoUVConversion.UVTransform[]>();
            List <bool[]> allIsManualUv = new List <bool[]>();
            List <int[]>  allIndexes    = new List <int[]>();

            HashSet <int>  indexes     = new HashSet <int>();
            List <Vector2> origins     = new List <Vector2>();
            List <Vector2> faceCenters = new List <Vector2>();
            List <PBAutoUVConversion.UVTransform> uvTransforms = new List <PBAutoUVConversion.UVTransform>();
            List <bool> isManualUv = new List <bool>();
            List <Face> faces      = new List <Face>();

            foreach (KeyValuePair <GameObject, IList <int> > kvp in Selection.SelectedFaces)
            {
                ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>();
                mesh.GetFaces(kvp.Value, faces);

                IList <Vector2> textures = mesh.textures;
                // IList<Vector4> tangents = mesh.tangents;

                for (int f = 0; f < faces.Count; ++f)
                {
                    Face        face        = faces[f];
                    IList <int> faceIndexes = face.indexes;
                    for (int i = 0; i < faceIndexes.Count; ++i)
                    {
                        int faceIndex = faceIndexes[i];
                        if (!indexes.Contains(faceIndex))
                        {
                            indexes.Add(faceIndex);
                            origins.Add(textures[faceIndex]);
                            faceCenters.Add(GetCenterOfMass(textures, face));

                            PBAutoUVConversion.UVTransform transform = PBAutoUVConversion.GetUVTransform(mesh, face);
                            if (!face.manualUV)
                            {
                                Vector2 scale = transform.scale;
                                scale.x         = -scale.x;
                                transform.scale = scale;
                            }

                            uvTransforms.Add(transform);
                        }
                    }

                    isManualUv.Add(face.manualUV);
                    face.manualUV = true;
                }

                allIndexes.Add(indexes.ToArray());
                allOrigins.Add(origins.ToArray());
                allFaceCenters.Add(faceCenters.ToArray());
                allUVTransforms.Add(uvTransforms.ToArray());
                allIsManualUv.Add(isManualUv.ToArray());
                allMeshes.Add(mesh);

                indexes.Clear();
                origins.Clear();
                faceCenters.Clear();
                uvTransforms.Clear();
                isManualUv.Clear();
                faces.Clear();
            }

            Indexes      = allIndexes.ToArray();
            Origins      = allOrigins.ToArray();
            FaceCenters  = allFaceCenters.ToArray();
            UVTransforms = allUVTransforms.ToArray();
            IsManualUv   = allIsManualUv.ToArray();
            Meshes       = allMeshes.ToArray();
        }