Esempio n. 1
0
        private void CheckTexturesConformity()
        {
            // Calculate the maximum texture count found on a _material
            int maxTextureCount = 0;

            foreach (KeyValuePair <string, List <Texture2D> > keyValue in _texturesForAtlas)
            {
                if (keyValue.Value.Count > 0)
                {
                    maxTextureCount = Mathf.Max(maxTextureCount, keyValue.Value.Count);
                }
            }

            if (maxTextureCount > 0)
            {
                foreach (KeyValuePair <string, List <Texture2D> > keyValue in _texturesForAtlas)
                {
                    if (keyValue.Value.Count > 0 && keyValue.Value.Count < maxTextureCount)
                    {
                        // Add the missing textures with colored one
                        int numberTexturesMissing = (maxTextureCount - keyValue.Value.Count);
                        for (int i = 0; i < numberTexturesMissing; i++)
                        {
                            int width  = _texturesForAtlas ["_MainTex"] [i].width;
                            int height = _texturesForAtlas ["_MainTex"] [i].height;
                            keyValue.Value.Insert(0, CreateColoredTexture2D(width, height, DefaultColoredTexture.GetDefaultTextureColor(keyValue.Key)));
                            Logger.Instance.AddLog("SuperCombiner", "Creating a colored texture " + DefaultColoredTexture.GetDefaultTextureColor(keyValue.Key) + " of size " + GetStringTextureSize(width, height) + " for " + TexturePropertyNames[keyValue.Key] + " because texture is missing.");
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Process a new _material, adding all textures found based on the _material property list in the lists for the atlas
        /// </summary>
        /// <param name="mat">Mat.</param>
        /// <param name="combineMaterials">If set to <c>true</c> combine materials.</param>
        /// <param name="materialUVBounds">Material UV bounds.</param>
        /// <param name="meshUVBounds">Mesh UV bounds.</param>
        /// <param name="tilingFactor">Tilling factor.</param>
        public void SetTextures(Material mat, bool combineMaterials, Rect materialUVBounds, Rect meshUVBounds, float tilingFactor)
        {
            // Here we have to check if all textures have the same size, otherwise, rescale the texture with the biggest one
            Vector2 maxTextureSize = checkTexturesSize(mat, false);

            // Manage tiling factor
            if (tilingFactor > 1)
            {
                combinedResult._combinedMaterials[_combinedIndex].scaleFactors.Add(tilingFactor);
                materialUVBounds.size  = Vector2.Scale(materialUVBounds.size, Vector2.one * tilingFactor);
                meshUVBounds.position -= new Vector2(meshUVBounds.width * (tilingFactor - 1) / 2f, meshUVBounds.height * (tilingFactor - 1) / 2f);
            }
            else
            {
                combinedResult._combinedMaterials[_combinedIndex].scaleFactors.Add(1);
            }

            combinedResult._combinedMaterials[_combinedIndex].meshUVBounds.Add(meshUVBounds);

            // Calculate the tiled texture size
            Vector3 textureInAtlasSize = GetTextureSizeInAtlas(maxTextureSize, Mathf.Abs(materialUVBounds.width), Mathf.Abs(materialUVBounds.height), mat.name);

            foreach (KeyValuePair <string, List <Texture2D> > keyValue in _texturesForAtlas)
            {
                if (mat.HasProperty(keyValue.Key))
                {
                    Texture texture = mat.GetTexture(keyValue.Key);
                    if (texture != null)
                    {
                        Texture2D textureInAtlas = CopyTexture((Texture2D)texture, materialUVBounds, meshUVBounds, mat, textureInAtlasSize, maxTextureSize, keyValue.Key.Equals("_MainTex"));
                        textureInAtlasSize = new Vector2(textureInAtlas.width, textureInAtlas.height);
                        keyValue.Value.Add(textureInAtlas);

                        // If texture is normal, revert modification in import settings of original texture
                        if (importedTextures.ContainsKey(texture.GetInstanceID()))
                        {
                            if (importedTextures[texture.GetInstanceID()].isNormal)
                            {
#if UNITY_EDITOR
                                string          path            = AssetDatabase.GetAssetPath(texture);
                                TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
#if UNITY_2017_1_OR_NEWER
                                textureImporter.textureType = TextureImporterType.NormalMap;
#else
                                textureImporter.textureType = TextureImporterType.Bump;
#endif
                                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
#endif
                            }
                        }
                    }
                    else
                    {
                        if (keyValue.Key.Equals("_MainTex"))
                        {
                            keyValue.Value.Add(CreateColoredTexture2D((int)textureInAtlasSize.x, (int)textureInAtlasSize.y, mat.HasProperty("_Color") ? mat.color : Color.white));
                            Logger.Instance.AddLog("SuperCombiner", "Creating a colored texture " + (mat.HasProperty("_Color") ? mat.color : Color.white) + " of size " + GetStringTextureSize(textureInAtlasSize.x, textureInAtlasSize.y) + " for " + TexturePropertyNames[keyValue.Key] + " in _material '" + mat.name + "' because texture is missing.");
                        }
                        else if (keyValue.Key.Equals("_EmissionMap") && mat.IsKeywordEnabled("_EMISSION") && mat.GetColor("_EmissionColor") != Color.black)
                        {
                            keyValue.Value.Add(CreateColoredTexture2D((int)textureInAtlasSize.x, (int)textureInAtlasSize.y, Color.white));

                            hasEmission   = true;
                            emissionColor = mat.GetColor("_EmissionColor");
                            Logger.Instance.AddLog("SuperCombiner", "Creating a white texture of size " + GetStringTextureSize(textureInAtlasSize.x, textureInAtlasSize.y) + " for " + TexturePropertyNames[keyValue.Key] + " in material '" + mat.name + "' because texture is missing.");
                        }
                        else if (keyValue.Value.Count > 0)
                        {
                            // Add color texture because this texture is missing in _material
                            keyValue.Value.Add(CreateColoredTexture2D((int)textureInAtlasSize.x, (int)textureInAtlasSize.y, DefaultColoredTexture.GetDefaultTextureColor(keyValue.Key)));
                            Logger.Instance.AddLog("SuperCombiner", "Creating a colored texture " + DefaultColoredTexture.GetDefaultTextureColor(keyValue.Key) + " of size " + GetStringTextureSize(textureInAtlasSize.x, textureInAtlasSize.y) + " for " + TexturePropertyNames[keyValue.Key] + " in _material '" + mat.name + "' because texture is missing.");
                        }
                    }
                }
                else
                {
                    // The _material doesn't have this texture property
                    if (keyValue.Key.Equals("_MainTex"))
                    {
                        keyValue.Value.Add(CreateColoredTexture2D((int)maxTextureSize.x, (int)maxTextureSize.y, mat.HasProperty("_Color") ? mat.color : Color.white));
                        Logger.Instance.AddLog("SuperCombiner", "Creating a colored texture " + DefaultColoredTexture.GetDefaultTextureColor(keyValue.Key) + " of size " + GetStringTextureSize(textureInAtlasSize.x, textureInAtlasSize.y) + " for " + TexturePropertyNames[keyValue.Key] + " in _material '" + mat.name + "' because texture is missing.");
                    }
                    else if (keyValue.Value.Count > 0)
                    {
                        // Error here because we found materials with textures properties that don't match!
                        Logger.Instance.AddLog("SuperCombiner", "Found materials with properties that don't match. Maybe you are trying to combine different shaders that don't share the same properties.", Logger.LogLevel.LOG_WARNING);
                    }
                }
            }
        }