Exemplo n.º 1
0
        /// <summary>
        /// This gets a texture position from a name passed as a parameter. Used as a quick and easy way to grab an appropriate texture position from a texture name
        /// </summary>
        /// <returns>The reference texture position.</returns>
        /// <param name="textureUVPositions">Texture UV positions.</param>
        /// <param name="textureName">Texture name.</param>
        static TexturePosition GetReferenceTexturePosition(IList <TexturePosition> textureUVPositions, string textureName)
        {
            TexturePosition texturePosition = textureUVPositions[0];

            for (int i = 0; i < textureUVPositions.Count; i++)
            {
                if (textureName.Length == textureUVPositions[i].textures[0].name.Length)
                {
                    if (textureName == textureUVPositions[i].textures[0].name)
                    {
                        texturePosition = textureUVPositions[i];
                        break;
                    }
                }
            }
            return(texturePosition);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds the combined meshes using data from the atlased dictionary
        /// </summary>
        void BuildAtlasedMeshes()
        {
            _numOfExportedMeshes = 0;
            foreach (KeyValuePair <string, Dictionary <Material, List <MeshInstance> > > firstPass in _dataTreeWithAtlasing)
            {
                List <Material> allMaterialTextures = new List <Material> (firstPass.Value.Keys);

                ReimportNonReadonlyTextures(allMaterialTextures);

                TextureCombineOutput textureCombineOutput = TextureCombineUtility.Combine(allMaterialTextures, _textureAtlasProperties, true);

                if (textureCombineOutput != null && textureCombineOutput.texturePositions != null)
                {
                    List <MeshInstance> meshIntermediates = new List <MeshInstance> ();
                    foreach (KeyValuePair <Material, List <MeshInstance> > kv in firstPass.Value)
                    {
                        TexturePosition refTexture = GetReferenceTexturePosition(textureCombineOutput.texturePositions, kv.Key.mainTexture.name);

                        for (int i = 0; i < kv.Value.Count; i++)
                        {
                            OffsetUvs(kv.Value [i].mesh, refTexture.position);
                            meshIntermediates.Add(kv.Value [i]);
                        }
                    }

                    IList <Mesh> combinedMeshes = MeshCombineUtility.Combine(meshIntermediates);
                    string       objectName     = firstPass.Key.Remove(firstPass.Key.LastIndexOf(' '));
                    GameObject   parent         = new GameObject("Combined " + objectName + " Mesh Parent");
                    parent.transform.parent   = _hiddenCombinedObject.transform;
                    parent.transform.position = _hiddenCombinedObject.transform.position;
                    parent.transform.rotation = _hiddenCombinedObject.transform.rotation;
                    for (int i = 0; i < combinedMeshes.Count; i++)
                    {
                        GameObject go = new GameObject("Combined " + objectName + " Mesh");
                        go.transform.parent        = parent.transform;
                        go.transform.localScale    = Vector3.one;
                        go.transform.localRotation = Quaternion.identity;
                        go.transform.localPosition = Vector3.zero;
                        MeshFilter filter = go.AddComponent <MeshFilter> ();
                        go.AddComponent <MeshRenderer> ().sharedMaterial = textureCombineOutput.combinedMaterial;
                        filter.mesh = combinedMeshes [i];
                        _numOfExportedMeshes++;
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The main combining method. This calls combining methods and organizes game objects in the scene
        /// </summary>
        /// <param name="allMeshesAndMaterials">The dictionary containing all of the data</param>
        void CreateBatchedAtlasedObjects(IDictionary <string, Dictionary <Material, List <MeshInstance> > > allMeshesAndMaterials)
        {
            foreach (KeyValuePair <string, Dictionary <Material, List <MeshInstance> > > firstPass in allMeshesAndMaterials)
            {
                List <Material> allMaterialTextures = new List <Material>(firstPass.Value.Keys);

                TextureCombineOutput textureCombineOutput = TextureCombineUtility.Combine(allMaterialTextures, _textureAtlasProperties);

                if (textureCombineOutput != null && textureCombineOutput.texturePositions != null)
                {
                    List <MeshInstance> meshIntermediates = new List <MeshInstance>();
                    foreach (KeyValuePair <Material, List <MeshInstance> > kv in firstPass.Value)
                    {
                        TexturePosition refTexture = GetReferenceTexturePosition(textureCombineOutput.texturePositions, kv.Key.mainTexture.name);

                        for (int i = 0; i < kv.Value.Count; i++)
                        {
                            OffsetUvs(kv.Value[i].mesh, refTexture.position);
                            meshIntermediates.Add(kv.Value[i]);
                        }
                    }

                    IList <Mesh> combinedMeshes = MeshCombineUtility.Combine(meshIntermediates);
                    GameObject   parent         = new GameObject("Combined " + gameObject.name + " " + firstPass.Key + " Mesh Parent");
                    parent.transform.position = transform.position;
                    parent.transform.rotation = transform.rotation;
                    for (int i = 0; i < combinedMeshes.Count; i++)
                    {
                        GameObject go = new GameObject("Combined " + gameObject.name + " Mesh");
                        go.transform.parent = parent.transform;
                        go.tag   = gameObject.tag;
                        go.layer = gameObject.layer;
                        go.transform.localScale    = Vector3.one;
                        go.transform.localRotation = Quaternion.identity;
                        go.transform.localPosition = Vector3.zero;
                        MeshFilter filter = go.AddComponent <MeshFilter>();
                        go.AddComponent <MeshRenderer>().sharedMaterial = textureCombineOutput.combinedMaterial;
                        filter.mesh = combinedMeshes[i];
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets the texture positions for each shader property etc
        /// </summary>
        /// <returns>The texture positions.</returns>
        /// <param name="combines">Combines.</param>
        /// <param name="properties">Properties.</param>
        static TexturePosition[] SetTexturePositions(IList <Material> combines, IList <ShaderProperties> properties)
        {
            TexturePosition[] texturePositions = new TexturePosition[combines.Count];
            for (int i = 0; i < combines.Count; i++)
            {
                TexturePosition tempTexturePosition = new TexturePosition();
                tempTexturePosition.textures = new List <Texture2D>(properties.Count);
                for (int j = 0; j < properties.Count; j++)
                {
                    if (combines [i].GetTexture(properties [j].propertyName) == null)
                    {
                        Debug.LogError("Cannot combine textures when using Unity's default material texture");

                        return(null);
                    }
                    tempTexturePosition.textures.Add(combines [i].GetTexture(properties [j].propertyName) as Texture2D);
                }
                texturePositions [i] = tempTexturePosition;
            }

            return(texturePositions);
        }
        /// <summary>
        /// Sets the texture positions for each shader property etc
        /// </summary>
        /// <returns>The texture positions.</returns>
        /// <param name="combines">Combines.</param>
        /// <param name="properties">Properties.</param>
        static TexturePosition[] SetTexturePositions(IList<Material> combines, IList<ShaderProperties> properties)
        {
            TexturePosition[] texturePositions = new TexturePosition[combines.Count];
            for (int i = 0; i < combines.Count; i++) {
                TexturePosition tempTexturePosition = new TexturePosition();
                tempTexturePosition.textures = new List<Texture2D>(properties.Count);
                for (int j = 0; j < properties.Count; j++) {
                    if (combines [i].GetTexture(properties [j].propertyName) == null) {
                        Debug.LogError("Cannot combine textures when using Unity's default material texture");

                        return null;
                    }
                    tempTexturePosition.textures.Add(combines [i].GetTexture(properties [j].propertyName) as Texture2D);
                }
                texturePositions [i] = tempTexturePosition;
            }

            return texturePositions;
        }
        /// <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;
        }