void UpdateHeights(GridDungeonModel model)
        {
            if (terrain == null || terrain.terrainData == null)
            {
                return;
            }
            var rasterizer = new LandscapeDataRasterizer(terrain, groundLevelHeight);

            rasterizer.LoadData();
            var gridSize = model.Config.GridCellSize;

            // Raise the terrain
            foreach (var cell in model.Cells)
            {
                var locationGrid = cell.Bounds.Location;
                var location     = locationGrid * gridSize;
                var size         = cell.Bounds.Size * gridSize;
                var cellY        = location.y + layoutLevelOffset;
                rasterizer.DrawCell(location.x, location.z, size.x, size.z, cellY);
            }

            // Smooth the terrain
            foreach (var cell in model.Cells)
            {
                var locationGrid = cell.Bounds.Location;
                var location     = locationGrid * gridSize;
                var size         = cell.Bounds.Size * gridSize;
                var cellY        = location.y + layoutLevelOffset;
                var curve        = (cell.CellType == CellType.Room) ? roomElevationCurve : corridorElevationCurve;
                rasterizer.SmoothCell(location.x, location.z, size.x, size.z, cellY, smoothingDistance, curve);
            }

            rasterizer.SaveData();
        }
Beispiel #2
0
        /// <summary>
        /// Gets the height of the terrain at the specified world space
        /// </summary>
        /// <param name="terrain">The terrain object</param>
        /// <param name="worldX">X cooridnate in world space</param>
        /// <param name="worldZ">Z cooridnate in world space</param>
        /// <returns>The Y height of the terrain at the specified location</returns>
        public static float GetHeight(Terrain terrain, float worldX, float worldZ)
        {
            int gx, gy;

            LandscapeDataRasterizer.WorldToTerrainCoord(terrain, worldX, worldZ, out gx, out gy);
            var height = terrain.terrainData.GetHeight(gx, gy);

            return(height + terrain.transform.position.y);
        }
        void UpdateBaseTexture(GridDungeonModel model, float[,,] map)
        {
            if (terrain == null)
            {
                return;
            }
            int fillIndex = GetTextureIndex(LandscapeTextureType.Fill);

            if (fillIndex < 0)
            {
                return;
            }

            var data = terrain.terrainData;

            // Fill up the entire space with the fill texture
            for (var y = 0; y < data.alphamapHeight; y++)
            {
                for (var x = 0; x < data.alphamapWidth; x++)
                {
                    for (int t = 0; t < textures.Length; t++)
                    {
                        var ratio = (t == fillIndex) ? 1 : 0;
                        map[y, x, t] = ratio;
                    }
                }
            }

            int roadIndex = GetTextureIndex(LandscapeTextureType.Corridor);

            if (roadIndex >= 0)
            {
                var gridSize  = model.Config.GridCellSize;
                var layoutMap = new float[map.GetLength(0), map.GetLength(1)];
                foreach (var cell in model.Cells)
                {
                    var bounds = cell.Bounds;
                    var locationGrid = bounds.Location;
                    var location = locationGrid * gridSize;
                    var size = bounds.Size * gridSize;
                    int gx1, gy1, gx2, gy2;
                    LandscapeDataRasterizer.WorldToTerrainTextureCoord(terrain, location.x, location.z, out gx1, out gy1);
                    LandscapeDataRasterizer.WorldToTerrainTextureCoord(terrain, location.x + size.x, location.z + size.z, out gx2, out gy2);
                    for (var gx = gx1; gx <= gx2; gx++)
                    {
                        for (var gy = gy1; gy <= gy2; gy++)
                        {
                            layoutMap[gy, gx] = 1;
                        }
                    }
                }

                // Blur the layout data
                var filter = new BlurFilter(roadBlurDistance);
                layoutMap = filter.ApplyFilter(layoutMap);

                // Fill up the inner region with corridor index
                for (var y = 0; y < data.alphamapHeight; y++)
                {
                    for (var x = 0; x < data.alphamapWidth; x++)
                    {
                        bool corridor = (layoutMap[y, x] > corridorBlurThreshold);
                        if (corridor)
                        {
                            map[y, x, roadIndex] = 1;
                            map[y, x, fillIndex] = 0;
                        }
                    }
                }
            }
        }