Ejemplo n.º 1
0
        /// <summary>
        /// Gets Unity Material from Daggerfall terrain using texture arrays.
        /// </summary>
        /// <param name="archive">Archive index.</param>
        /// <returns>Material or null.</returns>
        public Material GetTerrainTextureArrayMaterial(int archive)
        {
            // Ready check
            if (!IsReady)
            {
                return(null);
            }

            // Return from cache if present
            int key = MakeTextureKey((short)archive, (byte)0, (byte)0, TileMapKeyGroup);

            if (materialDict.ContainsKey(key))
            {
                CachedMaterial cm = GetMaterialFromCache(key);
                if (cm.filterMode == MainFilterMode)
                {
                    // Properties are the same
                    return(cm.material);
                }
                else
                {
                    // Properties don't match, remove material and reload
                    materialDict.Remove(key);
                }
            }

            // Generate texture array
            Texture2DArray textureArrayTerrainTiles              = textureReader.GetTerrainAlbedoTextureArray(archive);
            Texture2DArray textureArrayTerrainTilesNormalMap     = textureReader.GetTerrainNormalMapTextureArray(archive);
            Texture2DArray textureArrayTerrainTilesMetallicGloss = textureReader.GetTerrainMetallicGlossMapTextureArray(archive);

            textureArrayTerrainTiles.filterMode = MainFilterMode;

            Shader   shader   = Shader.Find(_DaggerfallTilemapTextureArrayShaderName);
            Material material = new Material(shader);

            material.name = string.Format("TEXTURE.{0:000} [TilemapTextureArray]", archive);

            material.SetTexture(TileTexArrUniforms.TileTexArr, textureArrayTerrainTiles);
            if (textureArrayTerrainTilesNormalMap != null)
            {
                // if normal map texture array was loaded successfully enable normalmap in shader and set texture
                material.SetTexture(TileTexArrUniforms.TileNormalMapTexArr, textureArrayTerrainTilesNormalMap);
                material.EnableKeyword(KeyWords.NormalMap);
            }
            if (textureArrayTerrainTilesMetallicGloss != null)
            {
                // if metallic gloss map texture array was loaded successfully set texture (should always contain a valid texture array - since it defaults to 1x1 textures)
                material.SetTexture(TileTexArrUniforms.TileMetallicGlossMapTexArr, textureArrayTerrainTilesMetallicGloss);
            }

            CachedMaterial newcm = new CachedMaterial()
            {
                key        = key,
                keyGroup   = TileMapKeyGroup,
                material   = material,
                filterMode = MainFilterMode,
            };

            materialDict.Add(key, newcm);

            return(material);
        }