예제 #1
0
        public ApplyMaterialResult ApplyMaterial(Material material, GameObject gameObject)
        {
            MeshMaterialsState oldState = new MeshMaterialsState();
            MeshMaterialsState newState = new MeshMaterialsState();

            ProBuilderMesh[] meshes = gameObject.GetComponentsInChildren <ProBuilderMesh>(true);
            for (int i = 0; i < meshes.Length; ++i)
            {
                ProBuilderMesh mesh = meshes[i];
                AddAllFacesToState(oldState, mesh);
                AddMaterialsToState(oldState, mesh);

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

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

                AddAllFacesToState(newState, mesh);
                AddMaterialsToState(newState, mesh);
            }

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

            return(new ApplyMaterialResult(oldState, newState));
        }
예제 #2
0
        private static void AddAllFacesToState(MeshMaterialsState state, ProBuilderMesh mesh)
        {
            IList <Face> faces = mesh.faces;

            for (int j = 0; j < faces.Count; ++j)
            {
                state.FaceToSubmeshIndex.Add(new FaceToSubmeshIndex(mesh, j, faces[j].submeshIndex));
            }
        }
        public ApplyMaterialResult ApplyMaterial(Material material, MeshSelection selection)
        {
            if (selection == null)
            {
                return(null);
            }

            if (selection.HasVertices)
            {
                selection.VerticesToFaces(false, false);
            }
            else if (selection.HasEdges)
            {
                selection.EdgesToFaces(false, false);
            }

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

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

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

                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 (oldState.FaceToSubmeshIndex.Count == 0 && newState.FaceToSubmeshIndex.Count == 0)
            {
                return(null);
            }

            return(new ApplyMaterialResult(oldState, newState));
        }
예제 #4
0
        private static void AddMaterialsToState(MeshMaterialsState state, ProBuilderMesh mesh)
        {
            Renderer renderer = mesh.GetComponent <Renderer>();

            Material[] materials;
            if (renderer != null)
            {
                materials = renderer.sharedMaterials.ToArray();
            }
            else
            {
                materials = new Material[0];
            }
            state.Materials.Add(mesh, materials);
        }
예제 #5
0
        public ApplyMaterialResult ApplyMaterial(Material material, MeshSelection selection, Camera camera, Vector3 mousePosition)
        {
            MeshAndFace meshAndFace = PBUtility.PickFace(camera, mousePosition);

            if (meshAndFace.mesh != null && meshAndFace.face != null)
            {
                MeshMaterialsState oldState = new MeshMaterialsState();
                MeshMaterialsState newState = new MeshMaterialsState();

                int faceIndex = meshAndFace.mesh.faces.IndexOf(meshAndFace.face);

                IList <int> faceIndexes;
                if (selection != null && selection.SelectedFaces.TryGetValue(meshAndFace.mesh.gameObject, out faceIndexes))
                {
                    if (faceIndexes.Contains(faceIndex))
                    {
                        return(ApplyMaterial(material, selection));
                    }
                }

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

                meshAndFace.mesh.SetMaterial(new[] { meshAndFace.face }, material);
                meshAndFace.mesh.Refresh();
                meshAndFace.mesh.ToMesh();

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

                FaceToSubmeshIndex newFaceToIndex = new FaceToSubmeshIndex(meshAndFace.mesh, meshAndFace.mesh.faces.IndexOf(meshAndFace.face), meshAndFace.face.submeshIndex);
                newState.FaceToSubmeshIndex.Add(newFaceToIndex);
                AddMaterialsToState(newState, meshAndFace.mesh);

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

                return(new ApplyMaterialResult(oldState, newState));
            }
            return(null);
        }
예제 #6
0
 public ApplyMaterialResult()
 {
     OldState = new MeshMaterialsState();
     NewState = new MeshMaterialsState();
 }
예제 #7
0
 public ApplyMaterialResult(MeshMaterialsState oldState, MeshMaterialsState newState)
 {
     OldState = oldState;
     NewState = newState;
 }
예제 #8
0
        public void ApplyMaterials(MeshMaterialsState state)
        {
            foreach (KeyValuePair <ProBuilderMesh, object[]> kvp in state.Materials)
            {
                ProBuilderMesh mesh     = kvp.Key;
                Renderer       renderer = mesh.GetComponent <Renderer>();
                if (renderer != null)
                {
                    Material[] materials = new Material[kvp.Value.Length];
                    for (int i = 0; i < materials.Length; ++i)
                    {
                        if (kvp.Value[i] is Material)
                        {
                            materials[i] = (Material)kvp.Value[i];
                        }
                    }

                    renderer.sharedMaterials = materials;
                }
            }

            IList <FaceToSubmeshIndex>            faceToSubmeshIndexes = state.FaceToSubmeshIndex;
            Dictionary <ProBuilderMesh, Renderer> meshToRenderer       = new Dictionary <ProBuilderMesh, Renderer>();
            Dictionary <ProBuilderMesh, Dictionary <int, List <Face> > > meshToFaces = new Dictionary <ProBuilderMesh, Dictionary <int, List <Face> > >();

            for (int i = 0; i < faceToSubmeshIndexes.Count; ++i)
            {
                FaceToSubmeshIndex faceToSubmeshIndex = faceToSubmeshIndexes[i];
                ProBuilderMesh     mesh = faceToSubmeshIndex.Mesh;

                if (!meshToRenderer.ContainsKey(mesh))
                {
                    Renderer renderer = mesh.GetComponent <Renderer>();
                    meshToRenderer.Add(mesh, renderer);
                }

                Dictionary <int, List <Face> > indexToFaces;
                if (!meshToFaces.TryGetValue(mesh, out indexToFaces))
                {
                    indexToFaces = new Dictionary <int, List <Face> >();
                    meshToFaces.Add(mesh, indexToFaces);
                }

                List <Face> faceList;
                if (!indexToFaces.TryGetValue(faceToSubmeshIndex.SubmeshIndex, out faceList))
                {
                    faceList = new List <Face>();
                    indexToFaces.Add(faceToSubmeshIndex.SubmeshIndex, faceList);
                }

                Face face = mesh.faces[faceToSubmeshIndex.FaceIndex];
                faceList.Add(face);
            }

            foreach (KeyValuePair <ProBuilderMesh, Dictionary <int, List <Face> > > meshToFace in meshToFaces)
            {
                ProBuilderMesh mesh = meshToFace.Key;
                Dictionary <int, List <Face> > indexToFaces = meshToFace.Value;
                Renderer renderer = meshToRenderer[mesh];

                foreach (KeyValuePair <int, List <Face> > kvp in indexToFaces)
                {
                    int         submeshIndex = kvp.Key;
                    List <Face> faceList     = kvp.Value;

                    Material material = renderer.sharedMaterials[submeshIndex];
                    mesh.SetMaterial(faceList, material);
                }

                mesh.Refresh();
                mesh.ToMesh();
            }
        }