Exemplo n.º 1
0
        private static Material CreateMaterialFromMaps(BakerProfile profile, string name)
        {
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Assert.IsNotNull(profile.shader);
            ProceduralMaterial pm = bakingMaterials[name];

            bakingMaterials.Remove(name);
            Material mat;

            if (profile.shader != pm.shader)
            {
                mat = new Material(profile.shader);
                foreach (var field in profile.AdditionFields)
                {
                    try
                    {
                        switch (field.FieldType)
                        {
                        case (MappableFields.Color):
                        {
                            mat.SetColor(field.To, pm.GetColor(field.From));
                            break;
                        }

                        case (MappableFields.Float):
                        {
                            mat.SetFloat(field.To, pm.GetFloat(field.From));
                            break;
                        }

                        case (MappableFields.Texture):
                        {
                            mat.SetTexture(field.To, pm.GetTexture(field.From));
                            break;
                        }

                        case (MappableFields.Int):
                        {
                            mat.SetInt(field.To, pm.GetInt(field.From));
                            break;
                        }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("Additional fields " + field.From + " fialed to set, Skipping field.");
                        Debug.LogException(e);
                        continue;
                    }
                }
            }
            else
            {
                mat = new Material(pm);
            }
            var path = UnityPath(Path.Combine(profile.materialFolder, name));
            var mats = Baker.LoadAllAssetsAtPath <Texture>(path);

            foreach (Texture2D tex in mats)
            {
                if (tex.name.ToLower().Contains("ambient_occlusion"))
                {
                    mat.SetTexture(profile.AOName, tex);
                }
                else if (tex.name.ToLower().Contains("basecolor") || tex.name.ToLower().Contains("diffuse"))
                {
                    mat.SetTexture(profile.albedoName, tex);
                }
                else if (tex.name.ToLower().Contains("roughness"))
                {
                    mat.SetTexture(profile.roughnessName, tex);
                }
                else if (tex.name.ToLower().Contains("height"))
                {
                    mat.SetTexture(profile.heightName, tex);
                }
                else if (tex.name.ToLower().Contains("normal"))
                {
                    mat.SetTexture(profile.normalName, tex);
                }
                else if (tex.name.ToLower().Contains("specular"))
                {
                    mat.SetTexture(profile.specularName, tex);
                }
                else if (tex.name.ToLower().Contains("glossiness"))
                {
                    mat.SetTexture(profile.glossinessName, tex);
                }
            }
            if (profile.removeSubstance)
            {
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(pm));
            }
            AssetDatabase.CreateAsset(mat, path + "/" + name + ".mat");
            Material mat2 = new Material(pm);

            AssetDatabase.CreateAsset(mat2, path + "/" + name + "2.mat");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Resources.UnloadUnusedAssets();
            return(mat);
        }