void OnEnable()
        {
            drawCallTarget            = target as DrawCallMinimizer;
            destroyOriginalGameObject = serializedObject.FindProperty("destroyOriginalGameObject");
            textureAtlasProperties    = serializedObject.FindProperty("_textureAtlasProperties");
            skipAtlasingTextures      = serializedObject.FindProperty("skipTextureAtlasing");
            propertiesToLookFor       = drawCallTarget.textureAtlasProperties;

            if (propertiesToLookFor == null)
            {
                propertiesToLookFor = new DrawCallMinimizerInfo();
            }
            IEnumerable <string> propertiesList = ShaderPropertyUtility.GetUniqueShaderPropertyNames(drawCallTarget.GetComponentsInChildren <Renderer>().Select(X => X.sharedMaterial).Distinct());

            //here we clear out properties that are no longer referenced
            IList <ShaderProperties> activeProperties = propertiesToLookFor.shaderPropertiesToLookFor.Where(x => propertiesList.Contains(x.propertyName)).ToList();

            //then we figure out which properties still need to be added to the list
            propertiesList = propertiesList.Where(x => !activeProperties.Select(property => property.propertyName).Contains(x));

            foreach (string s in propertiesList)
            {
                activeProperties.Add(new ShaderProperties(false, s));
            }

            propertiesToLookFor.shaderPropertiesToLookFor = activeProperties;
            serializedObject.ApplyModifiedProperties();
            ReimportNonReadonly();
            //Reimport anything that is needed
        }
Exemple #2
0
        /// <summary>
        /// Combines the material's textures together using the atlas options
        /// </summary>
        /// <param name="combines">The materials that are being combined.</param>
        /// <param name="atlasInfo">Atlas options information.</param>
        public static TextureCombineOutput Combine(IList <Material> combines, DrawCallMinimizerInfo atlasInfo)
        {
            if (atlasInfo == null || atlasInfo.shaderPropertiesToLookFor == null || atlasInfo.shaderPropertiesToLookFor.Count <= 0)
            {
                Debug.LogError("You need to enter some shader properties to look for into Atlas Info. Cannot combine with 0 properties");
                return(null);
            }
            TextureCombineOutput output = new TextureCombineOutput();

            IList <ShaderProperties> properties = GetShaderProperties(combines, atlasInfo);

            for (int i = 0; i < combines.Count; i++)
            {
                FillInNullMainTexture(combines [i]);
                for (int j = 0; j < properties.Count; j++)
                {
                    FillInNulls(combines [i], properties [j]);
                }
            }

            output.texturePositions = SetTexturePositions(combines, properties);
            Texture2D textureAtlas = new Texture2D(atlasInfo.maxTextureSize, atlasInfo.maxTextureSize, (atlasInfo.ignoreTransparency && !properties [0].markAsNormal) ? TextureFormat.RGB24 : TextureFormat.ARGB32, true);

            Rect[] UVs = PackOriginalTexture(output.texturePositions, textureAtlas, atlasInfo);

            if (UVs != null)
            {
                Material newMaterial = new Material(combines [0]);
                newMaterial.SetTexture(properties [0].propertyName, textureAtlas);
                for (int i = 1; i < properties.Count; i++)
                {
                    Texture2D additionalAtlas = PackAdditionalTexture(GetTexturesAsArray(output.texturePositions, i), textureAtlas.width, textureAtlas.height, atlasInfo, UVs, properties [i].markAsNormal);

                    newMaterial.SetTexture(properties [i].propertyName, additionalAtlas);
                }
                output.combinedMaterial = newMaterial;
                return(output);
            }

            Debug.LogError("There was some sort of issue while trying to pack the textures...");
            return(null);
        }
        /// <summary>
        /// Combines the material's textures together using the atlas options
        /// </summary>
        /// <param name="combines">The materials that are being combined.</param>
        /// <param name="atlasInfo">Atlas options information.</param>
        public static TextureCombineOutput Combine(IList<Material>combines, DrawCallMinimizerInfo atlasInfo)
        {
            if (atlasInfo == null || atlasInfo.shaderPropertiesToLookFor == null || atlasInfo.shaderPropertiesToLookFor.Count <= 0) {
                Debug.LogError("You need to enter some shader properties to look for into Atlas Info. Cannot combine with 0 properties");
                return null;
            }
            TextureCombineOutput output = new TextureCombineOutput();

            IList<ShaderProperties> properties = GetShaderProperties(combines, atlasInfo);

            for (int i = 0; i < combines.Count; i++) {
                FillInNullMainTexture(combines [i]);
                for (int j = 0; j < properties.Count; j++) {
                    FillInNulls(combines [i], properties [j]);
                }
            }

            output.texturePositions = SetTexturePositions(combines, properties);
            Texture2D textureAtlas = new Texture2D(atlasInfo.maxTextureSize, atlasInfo.maxTextureSize, (atlasInfo.ignoreTransparency && !properties [0].markAsNormal) ? TextureFormat.RGB24 : TextureFormat.ARGB32, true);
            Rect[] UVs = PackOriginalTexture(output.texturePositions, textureAtlas, atlasInfo);

            if (UVs != null) {
                Material newMaterial = new Material(combines [0]);
                newMaterial.SetTexture(properties [0].propertyName, textureAtlas);
                for (int i = 1; i < properties.Count; i++) {
                    Texture2D additionalAtlas = PackAdditionalTexture(GetTexturesAsArray(output.texturePositions, i), textureAtlas.width, textureAtlas.height, atlasInfo, UVs, properties [i].markAsNormal);

                    newMaterial.SetTexture(properties [i].propertyName, additionalAtlas);
                }
                output.combinedMaterial = newMaterial;
                return output;
            }

            Debug.LogError("There was some sort of issue while trying to pack the textures...");
            return null;
        }
 /// <summary>
 /// Combine the specified materials, but this overrides the readable option so that textures wil remain readable/non-readable for certain situations within the editor combining
 /// </summary>
 /// <param name="combines">Combines.</param>
 /// <param name="atlasInfo">Atlas info.</param>
 /// <param name="readableOveride">If set to <c>true</c> readable overide.</param>
 public static TextureCombineOutput Combine(IList<Material>combines, DrawCallMinimizerInfo atlasInfo, bool readableOveride)
 {
     atlasInfo.readableTexture = readableOveride;
     return Combine(combines, atlasInfo);
 }
        /// <summary>
        /// Packs the original texture using Unity's texture packing method
        /// </summary>
        /// <returns>The original texture.</returns>
        /// <param name="texturePositions">Texture positions.</param>
        /// <param name="textureAtlas">Texture atlas.</param>
        /// <param name="atlasInfo">Atlas info.</param>
        static Rect[] PackOriginalTexture(TexturePosition[] texturePositions, Texture2D textureAtlas, DrawCallMinimizerInfo atlasInfo)
        {
            textureAtlas.anisoLevel = atlasInfo.anisoLevel;
            textureAtlas.filterMode = atlasInfo.filterMode;
            textureAtlas.wrapMode = atlasInfo.wrapMode;
            Rect[] UVpositions = textureAtlas.PackTextures(GetTexturesAsArray(texturePositions, 0), atlasInfo.padding, atlasInfo.maxTextureSize, !atlasInfo.readableTexture);

            for (int i = 0; i < texturePositions.Length; i++) {
                texturePositions [i].position = UVpositions [i];
            }

            return UVpositions;
        }
        /// <summary>
        /// Packs the additional textures using the Rects recieved from the original packing method
        /// </summary>
        /// <returns>The additional texture.</returns>
        /// <param name="textures">Textures.</param>
        /// <param name="textureWidth">Texture width.</param>
        /// <param name="textureHeight">Texture height.</param>
        /// <param name="atlasInfo">Atlas info.</param>
        /// <param name="UVs">U vs.</param>
        /// <param name="markAsNormal">If set to <c>true</c> mark as normal.</param>
        static Texture2D PackAdditionalTexture(IList<Texture2D> textures, int textureWidth, int textureHeight, DrawCallMinimizerInfo atlasInfo, IList<Rect> UVs, bool markAsNormal)
        {
            Texture2D textureAtlas = new Texture2D(textureWidth, textureHeight, (atlasInfo.ignoreTransparency && !markAsNormal) ? TextureFormat.RGB24 : TextureFormat.ARGB32, true);
            textureAtlas.anisoLevel = atlasInfo.anisoLevel;
            textureAtlas.filterMode = atlasInfo.filterMode;
            textureAtlas.wrapMode = atlasInfo.wrapMode;

            for (int i=0; i<textures.Count; i++) {
                if (!Mathf.Approximately(textures [i].width, textureWidth * UVs [i].width) || !Mathf.Approximately(textures [i].height, textureHeight * UVs [i].height)) {
                    textures [i] = ScaleTexture(textures [i], (int)(UVs [i].width * textureWidth), (int)(UVs [i].height * textureHeight));
                }

                try {
                    textureAtlas.SetPixels((int)(UVs [i].x * textureWidth), (int)(textureHeight * UVs [i].y), (int)(UVs [i].width * textureWidth), (int)(textureHeight * UVs [i].height), textures [i].GetPixels());
                } catch (System.Exception exception) {
                    Debug.LogException(exception);
                    return null;
                }
            }

            textureAtlas.Apply(true, !atlasInfo.readableTexture);

            return textureAtlas;
        }
 /// <summary>
 /// Gets all of the shader properties for a given material and returns a list of them
 /// </summary>
 /// <returns>The shader properties.</returns>
 /// <param name="combines">The materials being combined.</param>
 /// <param name="atlasInfo">Atlas information containing the properties to look for</param>
 static IList<ShaderProperties> GetShaderProperties(IList<Material> combines, DrawCallMinimizerInfo atlasInfo)
 {
     return GetShaderProperties(combines [0], atlasInfo);
 }
        /// <summary>
        /// Gets all of the shader properties for a given material and returns a list of them
        /// </summary>
        /// <returns>The shader properties.</returns>
        /// <param name="combines">The materials being combined.</param>
        /// <param name="atlasInfo">Atlas information containing the properties to look for</param>
        public static IList<ShaderProperties> GetShaderProperties(Material material, DrawCallMinimizerInfo atlasInfo)
        {
            List<ShaderProperties> properties = new List<ShaderProperties>();

            for (int i = 0; i < atlasInfo.shaderPropertiesToLookFor.Count; i++) {
                if (material.HasProperty(atlasInfo.shaderPropertiesToLookFor [i].propertyName)) {
                    properties.Add(atlasInfo.shaderPropertiesToLookFor [i]);
                }
            }
            return properties.AsReadOnly();
        }
Exemple #9
0
        /// <summary>
        /// Packs the additional textures using the Rects recieved from the original packing method
        /// </summary>
        /// <returns>The additional texture.</returns>
        /// <param name="textures">Textures.</param>
        /// <param name="textureWidth">Texture width.</param>
        /// <param name="textureHeight">Texture height.</param>
        /// <param name="atlasInfo">Atlas info.</param>
        /// <param name="UVs">U vs.</param>
        /// <param name="markAsNormal">If set to <c>true</c> mark as normal.</param>
        static Texture2D PackAdditionalTexture(IList <Texture2D> textures, int textureWidth, int textureHeight, DrawCallMinimizerInfo atlasInfo, IList <Rect> UVs, bool markAsNormal)
        {
            Texture2D textureAtlas = new Texture2D(textureWidth, textureHeight, (atlasInfo.ignoreTransparency && !markAsNormal) ? TextureFormat.RGB24 : TextureFormat.ARGB32, true);

            textureAtlas.anisoLevel = atlasInfo.anisoLevel;
            textureAtlas.filterMode = atlasInfo.filterMode;
            textureAtlas.wrapMode   = atlasInfo.wrapMode;

            for (int i = 0; i < textures.Count; i++)
            {
                if (!Mathf.Approximately(textures [i].width, textureWidth * UVs [i].width) || !Mathf.Approximately(textures [i].height, textureHeight * UVs [i].height))
                {
                    textures [i] = ScaleTexture(textures [i], (int)(UVs [i].width * textureWidth), (int)(UVs [i].height * textureHeight));
                }

                try {
                    textureAtlas.SetPixels((int)(UVs [i].x * textureWidth), (int)(textureHeight * UVs [i].y), (int)(UVs [i].width * textureWidth), (int)(textureHeight * UVs [i].height), textures [i].GetPixels());
                } catch (System.Exception exception) {
                    Debug.LogException(exception);
                    return(null);
                }
            }

            textureAtlas.Apply(true, !atlasInfo.readableTexture);

            return(textureAtlas);
        }
Exemple #10
0
        /// <summary>
        /// Packs the original texture using Unity's texture packing method
        /// </summary>
        /// <returns>The original texture.</returns>
        /// <param name="texturePositions">Texture positions.</param>
        /// <param name="textureAtlas">Texture atlas.</param>
        /// <param name="atlasInfo">Atlas info.</param>
        static Rect[] PackOriginalTexture(TexturePosition[] texturePositions, Texture2D textureAtlas, DrawCallMinimizerInfo atlasInfo)
        {
            textureAtlas.anisoLevel = atlasInfo.anisoLevel;
            textureAtlas.filterMode = atlasInfo.filterMode;
            textureAtlas.wrapMode   = atlasInfo.wrapMode;
            Rect[] UVpositions = textureAtlas.PackTextures(GetTexturesAsArray(texturePositions, 0), atlasInfo.padding, atlasInfo.maxTextureSize, !atlasInfo.readableTexture);

            for (int i = 0; i < texturePositions.Length; i++)
            {
                texturePositions [i].position = UVpositions [i];
            }

            return(UVpositions);
        }
Exemple #11
0
        /// <summary>
        /// Gets all of the shader properties for a given material and returns a list of them
        /// </summary>
        /// <returns>The shader properties.</returns>
        /// <param name="combines">The materials being combined.</param>
        /// <param name="atlasInfo">Atlas information containing the properties to look for</param>
        public static IList <ShaderProperties> GetShaderProperties(Material material, DrawCallMinimizerInfo atlasInfo)
        {
            List <ShaderProperties> properties = new List <ShaderProperties>();

            for (int i = 0; i < atlasInfo.shaderPropertiesToLookFor.Count; i++)
            {
                if (material.HasProperty(atlasInfo.shaderPropertiesToLookFor [i].propertyName))
                {
                    properties.Add(atlasInfo.shaderPropertiesToLookFor [i]);
                }
            }
            return(properties.AsReadOnly());
        }
Exemple #12
0
 /// <summary>
 /// Gets all of the shader properties for a given material and returns a list of them
 /// </summary>
 /// <returns>The shader properties.</returns>
 /// <param name="combines">The materials being combined.</param>
 /// <param name="atlasInfo">Atlas information containing the properties to look for</param>
 static IList <ShaderProperties> GetShaderProperties(IList <Material> combines, DrawCallMinimizerInfo atlasInfo)
 {
     return(GetShaderProperties(combines [0], atlasInfo));
 }
Exemple #13
0
 /// <summary>
 /// Combine the specified materials, but this overrides the readable option so that textures wil remain readable/non-readable for certain situations within the editor combining
 /// </summary>
 /// <param name="combines">Combines.</param>
 /// <param name="atlasInfo">Atlas info.</param>
 /// <param name="readableOveride">If set to <c>true</c> readable overide.</param>
 public static TextureCombineOutput Combine(IList <Material> combines, DrawCallMinimizerInfo atlasInfo, bool readableOveride)
 {
     atlasInfo.readableTexture = readableOveride;
     return(Combine(combines, atlasInfo));
 }
        void OnEnable()
        {
            drawCallTarget = target as DrawCallMinimizer;
            destroyOriginalGameObject = serializedObject.FindProperty("destroyOriginalGameObject");
            textureAtlasProperties = serializedObject.FindProperty("_textureAtlasProperties");
            skipAtlasingTextures = serializedObject.FindProperty("skipTextureAtlasing");
            propertiesToLookFor = drawCallTarget.textureAtlasProperties;

            if (propertiesToLookFor == null) {
                propertiesToLookFor = new DrawCallMinimizerInfo();

            }
            IEnumerable<string> propertiesList = ShaderPropertyUtility.GetUniqueShaderPropertyNames(drawCallTarget.GetComponentsInChildren<Renderer>().Select(X => X.sharedMaterial).Distinct());

            //here we clear out properties that are no longer referenced
            IList<ShaderProperties> activeProperties = propertiesToLookFor.shaderPropertiesToLookFor.Where(x => propertiesList.Contains(x.propertyName)).ToList();

            //then we figure out which properties still need to be added to the list
            propertiesList = propertiesList.Where(x => !activeProperties.Select(property => property.propertyName).Contains(x));

            foreach (string s in propertiesList) {
                activeProperties.Add(new ShaderProperties(false, s));
            }

            propertiesToLookFor.shaderPropertiesToLookFor = activeProperties;
            serializedObject.ApplyModifiedProperties();
            ReimportNonReadonly();
            //Reimport anything that is needed
        }