Example #1
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);
        }
Example #2
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();
            }
        }