Exemple #1
0
        private static bool CanGenerate()
        {
            if (metallicMap == null && aoMap == null && detailMap == null && smoothnessMap == null)
            {
                log.SetMessage("No textures set", Color.red);
                return(false);
            }

            if (width == 0 || height == 0)
            {
                log.SetMessage("Width or Height is 0", Color.red);
                return(false);
            }

            if (string.IsNullOrEmpty(fileName))
            {
                log.SetMessage("Filename can't be blank", Color.red);
                return(false);
            }

            return(true);
        }
        private static void GenerateMaskMap(string path)
        {
            bool metallicPendingForReadableEdit   = metallicMap != null && !metallicMap.isReadable;
            bool aoPendingForReadableEdit         = aoMap != null && !aoMap.isReadable;
            bool detailPendingForReadableEdit     = detailMap != null && !detailMap.isReadable;
            bool smoothnessPendingForReadableEdit = smoothnessMap != null && !smoothnessMap.isReadable;

            if (metallicPendingForReadableEdit || aoPendingForReadableEdit ||
                detailPendingForReadableEdit || smoothnessPendingForReadableEdit)
            {
                string metallicName   = metallicPendingForReadableEdit ? metallicMap.name + "\n" : "";
                string aoName         = aoPendingForReadableEdit ? aoMap.name + "\n" : "";
                string detailName     = detailPendingForReadableEdit ? detailMap.name + "\n" : "";
                string smoothnessName = smoothnessPendingForReadableEdit ? smoothnessMap.name : "";
                string dialogMessage  = $"These textures will be set to Read/Write Enabled\n" +
                                        $"{metallicName}{aoName}{detailName}{smoothnessName}";

                if (EditorUtility.DisplayDialog("Warning", dialogMessage, "Confirm", "Cancel"))
                {
                    Texture2D[] textures       = new Texture2D[] { metallicMap, aoMap, detailMap, smoothnessMap };
                    bool[]      pendingForEdit = new bool[]
                    {
                        metallicPendingForReadableEdit, aoPendingForReadableEdit,
                        detailPendingForReadableEdit, smoothnessPendingForReadableEdit
                    };

                    for (int i = 0; i < pendingForEdit.Length; i++)
                    {
                        if (pendingForEdit[i])
                        {
                            string          texturePath = AssetDatabase.GetAssetPath(textures[i]);
                            TextureImporter importer    = AssetImporter.GetAtPath(texturePath) as TextureImporter;

                            if (importer != null)
                            {
                                importer.textureType = TextureImporterType.Default;
                                importer.isReadable  = true;

                                AssetDatabase.ImportAsset(texturePath);
                                AssetDatabase.Refresh();
                            }
                        }
                    }
                }
            }

            maskMap = new Texture2D(width, height /*, DefaultFormat.LDR, TextureCreationFlags.None*/);

            Color colorCache = Color.black;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    colorCache.r = metallicMap != null?metallicMap.GetPixel(i, j).r : 0;

                    colorCache.g = aoMap != null?aoMap.GetPixel(i, j).g : 0;

                    colorCache.b = detailMap != null?detailMap.GetPixel(i, j).b : 0;

                    colorCache.a = smoothnessMap != null?smoothnessMap.GetPixel(i, j).a : 0;

                    maskMap.SetPixel(i, j, colorCache);
                }
            }

            byte[] bytes = maskMap.EncodeToPNG();

            using (Image image = Image.FromStream(new MemoryStream(bytes)))
            {
                image.Save(path + Path.DirectorySeparatorChar + fileName + ".png", ImageFormat.Png);
                log.SetMessage($"{fileName}.png successfully generated", Color.black);
            }

            AssetDatabase.Refresh();
        }