Ejemplo n.º 1
0
        private Material CreateAtlasMaterialAndTexture(Atlasser generatedAtlas, string shaderToAtlas, TextureReuseManager textureReuseManager)
        {
            string fileName           = ((ObjectsGUI.CustomAtlasName == "") ? "Atlas " : (ObjectsGUI.CustomAtlasName + " ")) + shaderToAtlas.Replace('/', '_');
            string folderToSaveAssets = EditorApplication.currentScene;

            if (folderToSaveAssets == "") //scene is not saved yet.
            {
                folderToSaveAssets = Constants.NonSavedSceneFolderName + ".unity";
                Debug.LogWarning("WARNING: Scene has not been saved, saving baked objects to: " + Constants.NonSavedSceneFolderName + " folder");
            }

            folderToSaveAssets = folderToSaveAssets.Substring(0, folderToSaveAssets.Length - 6) + "-Atlas";//remove the ".unity" and add "-Atlas"
            if (!Directory.Exists(folderToSaveAssets))
            {
                Directory.CreateDirectory(folderToSaveAssets);
                AssetDatabase.ImportAsset(folderToSaveAssets);
            }

            string atlasTexturePath = folderToSaveAssets + Path.DirectorySeparatorChar + fileName;
            //create the material in the project and set the shader material to shaderToAtlas
            Material atlasMaterial = new Material(Shader.Find(shaderToAtlas));

            //save the material to the project view
            AssetDatabase.CreateAsset(atlasMaterial, atlasTexturePath + "Mat.mat");
            AssetDatabase.ImportAsset(atlasTexturePath + "Mat.mat");
            //load a reference from the project view to the material (this is done to be able to set the texture to the material in the project view)
            atlasMaterial = (Material)AssetDatabase.LoadAssetAtPath(atlasTexturePath + "Mat.mat", typeof(Material));

            List <string> shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas);

            for (int k = 0; k < shaderDefines.Count; k++)                                                //go trough each property of the shader.
            {
                List <Texture2D> texturesOfShader = GetTexturesToAtlasForShaderDefine(shaderDefines[k]); //Get thtextures for the property shderDefines[k] to atlas them
                List <Vector2>   scales           = GetScalesToAtlasForShaderDefine(shaderDefines[k]);
                List <Vector2>   offsets          = GetOffsetsToAtlasForShaderDefine(shaderDefines[k]);
                if (AdvancedMenuGUI.Instance.ReuseTextures)
                {
                    texturesOfShader = Utils.FilterTexsByIndex(texturesOfShader, textureReuseManager.GetTextureIndexes());
                    scales           = Utils.FilterVec2ByIndex(scales, textureReuseManager.GetTextureIndexes());
                    offsets          = Utils.FilterVec2ByIndex(offsets, textureReuseManager.GetTextureIndexes());
                }
                generatedAtlas.SaveAtlasToFile(atlasTexturePath + k.ToString() + ".png", texturesOfShader, scales, offsets);//save the atlas with the retrieved textures
                AssetDatabase.ImportAsset(atlasTexturePath + k.ToString() + ".png");
                Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasTexturePath + k.ToString() + ".png", typeof(Texture2D));

                atlasMaterial.SetTexture(shaderDefines[k], //set property shderDefines[k] for shader shaderToAtlas
                                         tex);
            }
            return(atlasMaterial);
        }
Ejemplo n.º 2
0
        private Material CreateAtlasMaterialAndTexture(Atlasser generatedAtlas, string shaderToAtlas, TextureReuseManager textureReuseManager, int atlasNumber /*in case we have several atlases*/, int start, int end /*start & end used for multiple atlases*/)
        {
            string fileName           = ((ObjectsGUI.CustomAtlasName == "") ? "Atlas " : (ObjectsGUI.CustomAtlasName + " ")) + atlasNumber + shaderToAtlas.Replace('/', '_');
            string folderToSaveAssets = (PersistenceHandler.Instance.PathToSaveOptimizedObjs != "") ?
                                        PersistenceHandler.Instance.PathToSaveOptimizedObjs + Path.DirectorySeparatorChar + Utils.GetCurrentSceneName() :
                                        EditorSceneManager.GetActiveScene().path; //<- 5.4+ -- 5.3 -> EditorApplication.currentScene;

            if (EditorSceneManager.GetActiveScene().path == "")                   //scene is not saved yet.
            {
                folderToSaveAssets = Constants.NonSavedSceneFolderName + ".unity";
                Debug.LogWarning("WARNING: Scene has not been saved, saving baked objects to: " + Constants.NonSavedSceneFolderName + " folder");
            }

            folderToSaveAssets = folderToSaveAssets.Substring(0, folderToSaveAssets.Length - 6) + "-Atlas";//remove the ".unity" and add "-Atlas"
            if (!Directory.Exists(folderToSaveAssets))
            {
                Directory.CreateDirectory(folderToSaveAssets);
                AssetDatabase.ImportAsset(folderToSaveAssets);
            }
            string atlasTexturePath = folderToSaveAssets + Path.DirectorySeparatorChar + fileName;
            //create the material in the project and set the shader material to shaderToAtlas
            Material atlasMaterial = new Material(Shader.Find(standardShader ? Utils.ExtractStandardShaderOriginalName(shaderToAtlas) : shaderToAtlas));

            //save the material to the project view
            AssetDatabase.CreateAsset(atlasMaterial, atlasTexturePath + "Mat.mat");
            AssetDatabase.ImportAsset(atlasTexturePath + "Mat.mat");
            //load a reference from the project view to the material (this is done to be able to set the texture to the material in the project view)
            atlasMaterial = (Material)AssetDatabase.LoadAssetAtPath(atlasTexturePath + "Mat.mat", typeof(Material));

            List <string> shaderDefines;

            if (standardShader)
            {
                shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas, false, objectsToOptimize[0].ObjectMaterial);//we need the 1rst object in the list to know what textures are used.
            }
            else
            {
                shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas);
            }

            for (int k = 0; k < shaderDefines.Count; k++)   //go trough each property of the shader.
            {
                if (SettingsMenuGUI.Instance.ReuseTextures) //if we are reusing textures, get all the textures and then filter them by the TextureReuseManager
                {
                    start = 0;
                    end   = objectsToOptimize.Count;
                }
                List <Texture2D> texturesOfShader = GetTexturesToAtlasForShaderDefine(shaderDefines[k], start, end);//Get the textures for the property shaderDefines[k] to atlas them
                List <Vector2>   scales           = GetScalesToAtlasForShaderDefine(shaderDefines[k], start, end);
                List <Vector2>   offsets          = GetOffsetsToAtlasForShaderDefine(shaderDefines[k], start, end);
                if (SettingsMenuGUI.Instance.ReuseTextures)
                {
                    texturesOfShader = Utils.FilterTexsByIndex(texturesOfShader, textureReuseManager.GetTextureIndexes());
                    scales           = Utils.FilterVec2ByIndex(scales, textureReuseManager.GetTextureIndexes());
                    offsets          = Utils.FilterVec2ByIndex(offsets, textureReuseManager.GetTextureIndexes());
                }
                generatedAtlas.SaveAtlasToFile(atlasTexturePath + k.ToString() + ".png", texturesOfShader, scales, offsets);//save the atlas with the retrieved textures
                AssetDatabase.ImportAsset(atlasTexturePath + k.ToString() + ".png");
                Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasTexturePath + k.ToString() + ".png", typeof(Texture2D));

                atlasMaterial.SetTexture(shaderDefines[k], //set property shaderDefines[k] for shader shaderToAtlas
                                         tex);
            }
            return(atlasMaterial);
        }
Ejemplo n.º 3
0
        private Material CreateAtlasMaterialAndTexture(Atlasser generatedAtlas, string shaderToAtlas, TextureReuseManager textureReuseManager, int atlasNumber /*in case we have several atlases*/, int start, int end /*start & end used for multiple atlases*/)
        {
            string fileName           = ((ObjectsGUI.CustomAtlasName == "") ? "Atlas " : (ObjectsGUI.CustomAtlasName + " ")) + atlasNumber + shaderToAtlas.Replace('/', '_');
            string folderToSaveAssets = (PersistenceHandler.Instance.PathToSaveOptimizedObjs != "") ?
                                        PersistenceHandler.Instance.PathToSaveOptimizedObjs + Path.DirectorySeparatorChar + Utils.GetCurrentSceneName() :
                                        #if UNITY_5_4_OR_NEWER
                                        UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;
                                        #else
                                        EditorApplication.currentScene;
                                        #endif
        #if UNITY_5_4_OR_NEWER
            if (UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path == "")
            {
        #else
            if (EditorApplication.currentScene == "") //scene is not saved yet.
            {
        #endif
                folderToSaveAssets = Constants.NonSavedSceneFolderName + ".unity";
                Debug.LogWarning("WARNING: Scene has not been saved, saving baked objects to: " + Constants.NonSavedSceneFolderName + " folder");
            }

            folderToSaveAssets = folderToSaveAssets.Substring(0, folderToSaveAssets.Length - 6) + "-Atlas";//remove the ".unity" and add "-Atlas"
            if (!Directory.Exists(folderToSaveAssets))
            {
                Directory.CreateDirectory(folderToSaveAssets);
                AssetDatabase.ImportAsset(folderToSaveAssets);
            }
            string atlasTexturePath = folderToSaveAssets + Path.DirectorySeparatorChar + fileName;
            //create the material in the project and set the shader material to shaderToAtlas
            Material atlasMaterial = new Material(Shader.Find(standardShader ? Utils.ExtractStandardShaderOriginalName(shaderToAtlas) : shaderToAtlas));

            /*
             * if(standardShader) {
             *  atlasMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
             *  atlasMaterial.SetFloat("_Mode", 2);
             *  atlasMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
             *  atlasMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
             *  atlasMaterial.SetInt("_ZWrite", 0);
             *  atlasMaterial.DisableKeyword("_ALPHATEST_ON");
             *  atlasMaterial.EnableKeyword("_ALPHABLEND_ON");
             *  atlasMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
             *  atlasMaterial.renderQueue = 3000;
             *  //atlasMaterial.SetFloat("_Mode", 3f);d
             * }
             * //ACA CAMBIAR EL RENDERING MODE AL SHADER!!!
             *///SOLO FUCNIONA PARA RENDER MODE TRANSPARENTE, NO FUNCIONA PARA CUTOUT

            //save the material to the project view
            AssetDatabase.CreateAsset(atlasMaterial, atlasTexturePath + "Mat.mat");
            AssetDatabase.ImportAsset(atlasTexturePath + "Mat.mat");
            //load a reference from the project view to the material (this is done to be able to set the texture to the material in the project view)
            atlasMaterial = (Material)AssetDatabase.LoadAssetAtPath(atlasTexturePath + "Mat.mat", typeof(Material));

            List <string> shaderDefines;
            if (standardShader)
            {
                shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas, false, objectsToOptimize[0].ObjectMaterial);//we need the 1rst object in the list to know what textures are used.
            }
            else
            {
                shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas);
            }

            for (int k = 0; k < shaderDefines.Count; k++)   //go trough each property of the shader.
            {
                if (SettingsMenuGUI.Instance.ReuseTextures) //if we are reusing textures, get all the textures and then filter them by the TextureReuseManager
                {
                    start = 0;
                    end   = objectsToOptimize.Count;
                }
                List <Texture2D> texturesOfShader = GetTexturesToAtlasForShaderDefine(shaderDefines[k], start, end);//Get the textures for the property shaderDefines[k] to atlas them
                List <Vector2>   scales           = GetScalesToAtlasForShaderDefine(shaderDefines[k], start, end);
                List <Vector2>   offsets          = GetOffsetsToAtlasForShaderDefine(shaderDefines[k], start, end);
                if (SettingsMenuGUI.Instance.ReuseTextures)
                {
                    texturesOfShader = Utils.FilterTexsByIndex(texturesOfShader, textureReuseManager.GetTextureIndexes());
                    scales           = Utils.FilterVec2ByIndex(scales, textureReuseManager.GetTextureIndexes());
                    offsets          = Utils.FilterVec2ByIndex(offsets, textureReuseManager.GetTextureIndexes());
                }
                generatedAtlas.SaveAtlasToFile(atlasTexturePath + k.ToString() + ".png", texturesOfShader, scales, offsets);//save the atlas with the retrieved textures
                AssetDatabase.ImportAsset(atlasTexturePath + k.ToString() + ".png");
                Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasTexturePath + k.ToString() + ".png", typeof(Texture2D));

                atlasMaterial.SetTexture(shaderDefines[k], //set property shaderDefines[k] for shader shaderToAtlas
                                         tex);
            }
            return(atlasMaterial);
        }