Example #1
0
        /// <summary>
        /// Creates terrain representing a LAND record.
        /// </summary>
        private IEnumerator InstantiateLANDCoroutine(LANDRecord LAND, GameObject parent)
        {
            Debug.Assert(LAND != null);

            // Don't create anything if the LAND doesn't have height data.
            if (LAND.VHGT == null)
            {
                yield break;
            }

            // Return before doing any work to provide an IEnumerator handle to the coroutine.
            yield return(null);

            const int LAND_SIDE_LENGTH_IN_SAMPLES = 65;
            var       heights = new float[LAND_SIDE_LENGTH_IN_SAMPLES, LAND_SIDE_LENGTH_IN_SAMPLES];

            // Read in the heights in Morrowind units.
            const int VHGTIncrementToMWUnits = 8;
            float     rowOffset = LAND.VHGT.referenceHeight;

            for (int y = 0; y < LAND_SIDE_LENGTH_IN_SAMPLES; y++)
            {
                rowOffset    += LAND.VHGT.heightOffsets[y * LAND_SIDE_LENGTH_IN_SAMPLES];
                heights[y, 0] = VHGTIncrementToMWUnits * rowOffset;

                float colOffset = rowOffset;

                for (int x = 1; x < LAND_SIDE_LENGTH_IN_SAMPLES; x++)
                {
                    colOffset    += LAND.VHGT.heightOffsets[(y * LAND_SIDE_LENGTH_IN_SAMPLES) + x];
                    heights[y, x] = VHGTIncrementToMWUnits * colOffset;
                }
            }

            // Change the heights to percentages.
            float minHeight, maxHeight;

            ArrayUtils.GetExtrema(heights, out minHeight, out maxHeight);

            for (int y = 0; y < LAND_SIDE_LENGTH_IN_SAMPLES; y++)
            {
                for (int x = 0; x < LAND_SIDE_LENGTH_IN_SAMPLES; x++)
                {
                    heights[y, x] = Utils.ChangeRange(heights[y, x], minHeight, maxHeight, 0, 1);
                }
            }

            // Texture the terrain.
            SplatPrototype[] splatPrototypes = null;
            float[,,] alphaMap = null;

            const int LAND_TEXTURE_INDICES_COUNT = 256;
            var       textureIndices             = (LAND.VTEX != null) ? LAND.VTEX.textureIndices : new ushort[LAND_TEXTURE_INDICES_COUNT];

            // Create splat prototypes.
            var splatPrototypeList = new List <SplatPrototype>();
            var texInd2splatInd    = new Dictionary <ushort, int>();

            for (int i = 0; i < textureIndices.Length; i++)
            {
                short textureIndex = (short)((short)textureIndices[i] - 1);

                if (!texInd2splatInd.ContainsKey((ushort)textureIndex))
                {
                    // Load terrain texture.
                    string textureFilePath;

                    if (textureIndex < 0)
                    {
                        textureFilePath = defaultLandTextureFilePath;
                    }
                    else
                    {
                        var LTEX = dataReader.FindLTEXRecord(textureIndex);
                        textureFilePath = LTEX.DATA.value;
                    }

                    var texture = textureManager.LoadTexture(textureFilePath);

                    // Yield after loading each texture to avoid doing too much work on one frame.
                    yield return(null);

                    // Create the splat prototype.
                    var splat = new SplatPrototype();
                    splat.texture    = texture;
                    splat.smoothness = 0;
                    splat.metallic   = 0;
                    splat.tileSize   = new Vector2(6, 6);

                    // Update collections.
                    var splatIndex = splatPrototypeList.Count;
                    splatPrototypeList.Add(splat);
                    texInd2splatInd.Add((ushort)textureIndex, splatIndex);
                }
            }

            splatPrototypes = splatPrototypeList.ToArray();

            // Create the alpha map.
            int VTEX_ROWS    = 16;
            int VTEX_COLUMNS = VTEX_ROWS;

            alphaMap = new float[VTEX_ROWS, VTEX_COLUMNS, splatPrototypes.Length];

            for (int y = 0; y < VTEX_ROWS; y++)
            {
                var yMajor = y / 4;
                var yMinor = y - (yMajor * 4);

                for (int x = 0; x < VTEX_COLUMNS; x++)
                {
                    var xMajor = x / 4;
                    var xMinor = x - (xMajor * 4);

                    var texIndex = (short)((short)textureIndices[(yMajor * 64) + (xMajor * 16) + (yMinor * 4) + xMinor] - 1);

                    if (texIndex >= 0)
                    {
                        var splatIndex = texInd2splatInd[(ushort)texIndex];

                        alphaMap[y, x, splatIndex] = 1;
                    }
                    else
                    {
                        alphaMap[y, x, 0] = 1;
                    }
                }
            }

            // Yield before creating the terrain GameObject because it takes a while.
            yield return(null);

            // Create the terrain.
            var heightRange     = maxHeight - minHeight;
            var terrainPosition = new Vector3(Convert.exteriorCellSideLengthInMeters * LAND.gridCoords.x, minHeight / Convert.meterInMWUnits, Convert.exteriorCellSideLengthInMeters * LAND.gridCoords.y);

            var heightSampleDistance = Convert.exteriorCellSideLengthInMeters / (LAND_SIDE_LENGTH_IN_SAMPLES - 1);

            var terrain = GameObjectUtils.CreateTerrain(heights, heightRange / Convert.meterInMWUnits, heightSampleDistance, splatPrototypes, alphaMap, terrainPosition);

            terrain.GetComponent <Terrain>().materialType = Terrain.MaterialType.BuiltInLegacyDiffuse;

            terrain.transform.parent = parent.transform;
        }
Example #2
0
        /// <summary>
        /// Creates terrain representing a LAND record.
        /// </summary>
        IEnumerator InstantiateLANDCoroutine(LANDRecord land, GameObject parent)
        {
            Debug.Assert(land != null);
            // Don't create anything if the LAND doesn't have height data.
            if (land.VHGT.HeightData == null)
            {
                yield break;
            }
            // Return before doing any work to provide an IEnumerator handle to the coroutine.
            yield return(null);

            const int LAND_SIDELENGTH_IN_SAMPLES = 65;
            var       heights = new float[LAND_SIDELENGTH_IN_SAMPLES, LAND_SIDELENGTH_IN_SAMPLES];
            // Read in the heights in Morrowind units.
            const int VHGTIncrementToUnits = 8;
            var       rowOffset            = land.VHGT.ReferenceHeight;

            for (var y = 0; y < LAND_SIDELENGTH_IN_SAMPLES; y++)
            {
                rowOffset    += land.VHGT.HeightData[y * LAND_SIDELENGTH_IN_SAMPLES];
                heights[y, 0] = rowOffset * VHGTIncrementToUnits;
                var colOffset = rowOffset;
                for (var x = 1; x < LAND_SIDELENGTH_IN_SAMPLES; x++)
                {
                    colOffset    += land.VHGT.HeightData[(y * LAND_SIDELENGTH_IN_SAMPLES) + x];
                    heights[y, x] = colOffset * VHGTIncrementToUnits;
                }
            }
            // Change the heights to percentages.
            heights.GetExtrema(out var minHeight, out var maxHeight);
            for (var y = 0; y < LAND_SIDELENGTH_IN_SAMPLES; y++)
            {
                for (var x = 0; x < LAND_SIDELENGTH_IN_SAMPLES; x++)
                {
                    heights[y, x] = Utils.ChangeRange(heights[y, x], minHeight, maxHeight, 0, 1);
                }
            }

            // Texture the terrain.
            SplatPrototype[] splatPrototypes     = null;
            const int        LAND_TEXTUREINDICES = 256;
            var textureIndices = land.VTEX != null ? land.VTEX.Value.TextureIndicesT3 : new ushort[LAND_TEXTUREINDICES];
            // Create splat prototypes.
            var splatPrototypeList = new List <SplatPrototype>();
            var texInd2SplatInd    = new Dictionary <ushort, int>();

            for (var i = 0; i < textureIndices.Length; i++)
            {
                var textureIndex = (int)(textureIndices[i] - 1);
                if (!texInd2SplatInd.ContainsKey((ushort)textureIndex))
                {
                    // Load terrain texture.
                    string textureFilePath;
                    if (textureIndex < 0)
                    {
                        textureFilePath = _defaultLandTextureFilePath;
                    }
                    else
                    {
                        var LTEX = _data.FindLTEXRecord(textureIndex);
                        textureFilePath = LTEX.ICON.Value;
                    }
                    var texture = _asset.LoadTexture(textureFilePath);
                    // Yield after loading each texture to avoid doing too much work on one frame.
                    yield return(null);

                    // Create the splat prototype.
                    var splat = new SplatPrototype
                    {
                        texture    = texture,
                        smoothness = 0,
                        metallic   = 0,
                        tileSize   = new Vector2(6, 6)
                    };
                    // Update collections.
                    var splatIndex = splatPrototypeList.Count;
                    splatPrototypeList.Add(splat);
                    texInd2SplatInd.Add((ushort)textureIndex, splatIndex);
                }
            }
            splatPrototypes = splatPrototypeList.ToArray();

            // Create the alpha map.
            var VTEX_ROWS    = 16;
            var VTEX_COLUMNS = VTEX_ROWS;

            float[,,] alphaMap = null;
            alphaMap           = new float[VTEX_ROWS, VTEX_COLUMNS, splatPrototypes.Length];
            for (var y = 0; y < VTEX_ROWS; y++)
            {
                var yMajor = y / 4;
                var yMinor = y - (yMajor * 4);
                for (var x = 0; x < VTEX_COLUMNS; x++)
                {
                    var xMajor   = x / 4;
                    var xMinor   = x - (xMajor * 4);
                    var texIndex = ((short)textureIndices[(yMajor * 64) + (xMajor * 16) + (yMinor * 4) + xMinor] - 1);
                    if (texIndex >= 0)
                    {
                        var splatIndex = texInd2SplatInd[(ushort)texIndex]; alphaMap[y, x, splatIndex] = 1;
                    }
                    else
                    {
                        alphaMap[y, x, 0] = 1;
                    }
                }
            }

            // Yield before creating the terrain GameObject because it takes a while.
            yield return(null);

            // Create the terrain.
            var heightRange          = maxHeight - minHeight;
            var terrainPosition      = new Vector3(ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.x, minHeight / ConvertUtils.MeterInUnits, ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.y);
            var heightSampleDistance = ConvertUtils.ExteriorCellSideLengthInMeters / (LAND_SIDELENGTH_IN_SAMPLES - 1);
            var terrain = GameObjectUtils.CreateTerrain(-1, heights, heightRange / ConvertUtils.MeterInUnits, heightSampleDistance, splatPrototypes, alphaMap, terrainPosition);

            terrain.GetComponent <Terrain>().materialType = Terrain.MaterialType.BuiltInLegacyDiffuse;
            terrain.transform.parent = parent.transform;
            terrain.isStatic         = true;
        }
Example #3
0
        /// <summary>
        /// Creates terrain representing a LAND record.
        /// </summary>
        IEnumerator InstantiateLANDCoroutine(LANDRecord land, GameObject parent)
        {
            Debug.Assert(land != null);
            // Don't create anything if the LAND doesn't have data.
            if (land.Heights == null || land.Tiles == null)
            {
                yield break;
            }
            // Return before doing any work to provide an IEnumerator handle to the coroutine.
            yield return(null);

            const int LAND_SIDELENGTH = 8 * DataFile.CELL_PACK;
            var       heights         = new float[LAND_SIDELENGTH, LAND_SIDELENGTH];
            // Read in the heights in units.
            const int VHGTIncrementToUnits = 1;

            for (var y = 0; y < LAND_SIDELENGTH; y++)
            {
                for (var x = 0; x < LAND_SIDELENGTH; x++)
                {
                    var height = land.Heights[(y * LAND_SIDELENGTH) + x];
                    heights[y, x] = height * VHGTIncrementToUnits;
                }
            }
            // Change the heights to percentages.
            heights.GetExtrema(out float minHeight, out float maxHeight);
            for (var y = 0; y < LAND_SIDELENGTH; y++)
            {
                for (var x = 0; x < LAND_SIDELENGTH; x++)
                {
                    heights[y, x] = Utils.ChangeRange(heights[y, x], minHeight, maxHeight, 0, 1);
                }
            }
            // Texture the terrain.
            SplatPrototype[] splatPrototypes = null;
            float[,,] alphaMap = null;
            var textureIndices = land.Tiles.Select(x => x.TextureId).ToArray();
            // Create splat prototypes.
            var splatPrototypeList = new List <SplatPrototype>();
            var texInd2SplatInd    = new Dictionary <short, int>();

            for (var i = 0; i < textureIndices.Length; i++)
            {
                var textureIndex = textureIndices[i];
                if (!texInd2SplatInd.ContainsKey(textureIndex))
                {
                    // Load terrain texture.
                    var textureFilePath = $"tex{textureIndex}";
                    var texture         = _asset.LoadTexture(textureFilePath);
                    // Yield after loading each texture to avoid doing too much work on one frame.
                    yield return(null);

                    // Create the splat prototype.
                    var splat = new SplatPrototype
                    {
                        texture    = texture,
                        smoothness = 0,
                        metallic   = 0,
                        tileSize   = new Vector2(6, 6)
                    };
                    // Update collections.
                    var splatIndex = splatPrototypeList.Count;
                    splatPrototypeList.Add(splat);
                    texInd2SplatInd.Add(textureIndex, splatIndex);
                }
            }
            splatPrototypes = splatPrototypeList.ToArray();
            // Create the alpha map.
            var VTEX_ROWS    = 16;
            var VTEX_COLUMNS = VTEX_ROWS;

            alphaMap = new float[VTEX_ROWS, VTEX_COLUMNS, splatPrototypes.Length];
            for (var y = 0; y < VTEX_ROWS; y++)
            {
                var yMajor = y / 4;
                var yMinor = y - (yMajor * 4);
                for (var x = 0; x < VTEX_COLUMNS; x++)
                {
                    var xMajor   = x / 4;
                    var xMinor   = x - (xMajor * 4);
                    var texIndex = textureIndices[(yMajor * 64) + (xMajor * 16) + (yMinor * 4) + xMinor];
                    if (texIndex >= 0)
                    {
                        var splatIndex = texInd2SplatInd[texIndex]; alphaMap[y, x, splatIndex] = 1;
                    }
                    else
                    {
                        alphaMap[y, x, 0] = 1;
                    }
                }
            }
            // Yield before creating the terrain GameObject because it takes a while.
            yield return(null);

            // Create the terrain.
            var heightRange          = maxHeight - minHeight;
            var terrainPosition      = new Vector3(ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.x, minHeight / ConvertUtils.MeterInUnits, ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.y);
            var heightSampleDistance = ConvertUtils.ExteriorCellSideLengthInMeters / LAND_SIDELENGTH;
            var terrain = GameObjectUtils.CreateTerrain(0, heights, heightRange / ConvertUtils.MeterInUnits, heightSampleDistance, splatPrototypes, alphaMap, terrainPosition);

            terrain.GetComponent <Terrain>().materialType = Terrain.MaterialType.BuiltInLegacyDiffuse;
            terrain.transform.parent = parent.transform;
            terrain.isStatic         = true;
        }
        /// <summary>
        /// Creates terrain representing a LAND record.
        /// </summary>
        IEnumerator InstantiateLANDCoroutine(LANDRecord land, GameObject parent)
        {
            Assert(land != null);
            // Don't create anything if the LAND doesn't have height data.
            if (land.VHGT == null)
            {
                yield break;
            }
            // Return before doing any work to provide an IEnumerator handle to the coroutine.
            yield return(null);

            const int LAND_STRIDE = 64;
            var       heights     = new float[LAND_STRIDE, LAND_STRIDE];
            // Read in the heights in Unity units.
            const int VHGTIncrementToUnits = 8;

            for (var y = 0; y < LAND_STRIDE; y++)
            {
                for (var x = 0; x < LAND_STRIDE; x++)
                {
                    var vhgt = land.VHGT[(y * LAND_STRIDE) + x];
                    heights[y, x] = vhgt * VHGTIncrementToUnits;
                }
            }
            // Change the heights to percentages.
            heights.GetExtrema(out var minHeight, out var maxHeight);
            for (var y = 0; y < LAND_STRIDE; y++)
            {
                for (var x = 0; x < LAND_STRIDE; x++)
                {
                    heights[y, x] = Utils.ChangeRange(heights[y, x], minHeight, maxHeight, 0, 1);
                }
            }

            // Texture the terrain.
            TerrainLayer[] terrainLayers  = null;
            var            textureIndices = land.VTEX ?? new ushort[LAND_STRIDE * LAND_STRIDE];
            // Create splat prototypes.
            var terrainLayerList = new List <TerrainLayer>();
            var texInd2SplatInd  = new Dictionary <ushort, int>();

            for (var i = 0; i < textureIndices.Length; i++)
            {
                var textureIndex = textureIndices[i];
                if (!texInd2SplatInd.ContainsKey(textureIndex))
                {
                    // Load terrain texture.
                    var texture = _assetPack.LoadTexture($"bitmap/{textureIndex}");
                    // Yield after loading each texture to avoid doing too much work on one frame.
                    yield return(null);

                    // Create the splat prototype.
                    var layer = new TerrainLayer
                    {
                        diffuseTexture = texture,
                        smoothness     = 0,
                        metallic       = 0,
                        tileSize       = new Vector2(16, 16)
                    };
                    // Update collections.
                    var splatIndex = terrainLayerList.Count;
                    terrainLayerList.Add(layer);
                    texInd2SplatInd.Add(textureIndex, splatIndex);
                }
            }
            terrainLayers = terrainLayerList.ToArray();

            // Create the alpha map.
            var alphaMap = new float[LAND_STRIDE, LAND_STRIDE, terrainLayers.Length];

            for (var y = 0; y < LAND_STRIDE; y++)
            {
                for (var x = 0; x < LAND_STRIDE; x++)
                {
                    var texIndex = textureIndices[x + y * LAND_STRIDE];
                    if (texIndex >= 0)
                    {
                        alphaMap[y, x, texInd2SplatInd[texIndex]] = 1;
                    }
                    else
                    {
                        alphaMap[y, x, 0] = 1;
                    }
                }
            }

            // Yield before creating the terrain GameObject because it takes a while.
            yield return(null);

            // Create the terrain.
            var heightRange          = maxHeight - minHeight;
            var terrainPosition      = new Vector3(ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.x, minHeight / ConvertUtils.MeterInUnits, ConvertUtils.ExteriorCellSideLengthInMeters * land.GridId.y);
            var heightSampleDistance = ConvertUtils.ExteriorCellSideLengthInMeters / (LAND_STRIDE - 1);
            var terrain = GameObjectUtils.CreateTerrain(-1, heights, heightRange / ConvertUtils.MeterInUnits, heightSampleDistance, terrainLayers, alphaMap, terrainPosition, null);

            terrain.GetComponent <Terrain>().materialType = Terrain.MaterialType.BuiltInLegacyDiffuse;
            terrain.transform.parent = parent.transform;
            terrain.isStatic         = true;
        }