예제 #1
0
파일: Map.cs 프로젝트: Genae/VoxelEngine
        // ReSharper disable once UnusedMember.Local
        public IEnumerator CreateMap(BiomeConfiguration biomeConfig, GameLoader loader)
        {
            if (GenerateMap)
            {
                loader.SetStatus("Calculating Heightmap", 0.03f);
                var hmg = new HeightmapGenerator();
                yield return(hmg.CreateHeightMap(129, 129, 42));

                loader.SetStatus("Building Map", 0.1f);
                MapData      = new MapData(hmg.Values.GetLength(0) / Chunk.ChunkSize, 100 / Chunk.ChunkSize, 2f);
                AStarNetwork = new VoxelGraph();
                yield return(MapData.LoadHeightmap(hmg.Values, hmg.BottomValues, hmg.CutPattern, 100));

                yield return(null);
                //RemoveTerrainNotOfType(new[] { MaterialRegistry.Instance.GetMaterialFromName("Iron"), MaterialRegistry.Instance.GetMaterialFromName("Gold"), MaterialRegistry.Instance.GetMaterialFromName("Copper"), MaterialRegistry.Instance.GetMaterialFromName("Coal") });
                //TestAStar();
            }
            else
            {
                MapData      = new MapData(129 / Chunk.ChunkSize, 100 / Chunk.ChunkSize, 2f);
                AStarNetwork = new VoxelGraph();
            }
            IsDoneGenerating          = true;
            VoxelContainer.EnableDraw = true;
        }
예제 #2
0
    private void GenerateHeightmap()
    {
        float[,] heightmap;

        /* Generate Perlin Map and Normalize to 0.2 to 1.0 Range */
        heightmap = HeightmapGenerator.GeneratePerlinNoise(worldWidth, level1PerlinScale, true);
        heightmap = HeightmapGenerator.ScaleHeightmapRange(heightmap, 0, 1, level1PerlinMin, 1);

        /* Add Island Mask To Perlin Map */
        float[,] islandMaskHeightmap = HeightmapGenerator.GenerateIslandMask(worldWidth, islandMaskStrength);

        for (int x = 0; x < worldWidth; x++)
        {
            for (int y = 0; y < worldWidth; y++)
            {
                heightmap[x, y] -= islandMaskHeightmap[x, y];

                /* For Debug Purposes, Apply To a Texture */
                texture2d.SetPixel(x, y, new Color(heightmap[x, y], heightmap[x, y], heightmap[x, y]));
            }
        }

        /* Set World Data to Heightmaps */
        RaiseTerrain(heightmap, level1Threshold, 1);

        heightmap = HeightmapGenerator.GeneratePerlinNoise(worldWidth, level2PerlinScale, true);
        RaiseTerrain(heightmap, level2Threshold, 2);

        heightmap = HeightmapGenerator.GeneratePerlinNoise(worldWidth, level3PerlinScale, true);
        RaiseTerrain(heightmap, level3Threshold, 3);

        /* Replace Bottom Level Floor with Water */
        ReplaceLayer(0, World.Layer.Floor, World.Tile.Pfb_Floor_Small, World.Tile.Pfb_Water);
    }
    public override void RunModule(HeightmapGenerator generator)
    {
        float width  = generator.LandscapeRoot.Width;
        float height = generator.LandscapeRoot.Height;

        Color[,] heightmap = generator.CurrentHeightmap;

        for (int i = 0; i < Iterations; ++i)
        {
            float x      = Random.value;
            float y      = Random.value;
            float radius = Random.Range(MinSize, MaxSize);

            int startX = (int)(x * heightmap.GetLength(0));
            int startY = (int)(y * heightmap.GetLength(1));

            int vertexRadius = (int)(radius * heightmap.GetLength(0));

            for (int offsetX = -vertexRadius; offsetX < vertexRadius; ++offsetX)
            {
                for (int offsetY = -vertexRadius; offsetY < vertexRadius; ++offsetY)
                {
                    float magnitude = (new Vector3((float)offsetX, (float)(offsetY), 0.0f)).magnitude / (float)heightmap.GetLength(0);
                    float heightVal = Mathf.Max(radius - magnitude, 0.0f);

                    ChangeTerrainHeight(startX + offsetX, startY + offsetY, heightVal, ref heightmap);
                }
            }
        }

        // Re-normalize the terrain
        float minHeight = float.MaxValue;
        float maxHeight = -float.MaxValue;

        for (int x = 0; x < heightmap.GetLength(0); ++x)
        {
            for (int y = 0; y < heightmap.GetLength(1); ++y)
            {
                if (heightmap[x, y].r < minHeight)
                {
                    minHeight = heightmap[x, y].r;
                }
                if (heightmap[x, y].r > maxHeight)
                {
                    maxHeight = heightmap[x, y].r;
                }
            }
        }

        for (int x = 0; x < heightmap.GetLength(0); ++x)
        {
            for (int y = 0; y < heightmap.GetLength(1); ++y)
            {
                heightmap[x, y].r  = (heightmap[x, y].r - minHeight) / (maxHeight - minHeight);
                heightmap[x, y].r  = Mathf.Pow(heightmap[x, y].r, ScalePower);
                heightmap[x, y].r *= ScaleFactor;
            }
        }
    }
    void GenerateFreshMap()
    {
        vertexHeightmap = HeightmapGenerator.GenerateHeightmap(mapSize);
        GenerateTerrain();

        // saving the map
        MapEncoder.instance.SetHeightMap(vertexHeightmap);
        MapEncoder.instance.SaveMapToFile();
    }
예제 #5
0
    public void Generate()
    {
        SetupMeshComponents();

        SetupBiomes();

        SetupMaterial();

        TerrainData.Setup(width, length);

        TerrainData.heightMap = HeightmapGenerator.Generate(noiseSettings, width, length, true);

        for (int y = 0; y <= length - 1; y++)
        {
            for (int x = 0; x <= width - 1; x++)
            {
                TerrainData.AddTile(x, y);

                //Top
                Vector3[] topVerts = AddTop(x, y);

                // Sides

                bool isWaterTile = TerrainData.IsWaterTile(x, y);

                if (x == 0 || (TerrainData.IsWaterTile(x - 1, y) && !isWaterTile))
                {
                    AddSide(Sides.Left, topVerts, x, y);
                }

                if (x == width - 1 || (TerrainData.IsWaterTile(x + 1, y) && !isWaterTile))
                {
                    AddSide(Sides.Right, topVerts, x, y);
                }

                if (y == 0 || (TerrainData.IsWaterTile(x, y - 1) && !isWaterTile))
                {
                    AddSide(Sides.Down, topVerts, x, y);
                }

                if (y == length - 1 || (TerrainData.IsWaterTile(x, y + 1) && !isWaterTile))
                {
                    AddSide(Sides.Up, topVerts, x, y);
                }
            }
        }

        landData.attach();
        waterData.attach();

        SpawnFolliage(treeData);
        SpawnFolliage(bushData);

        landData.navMeshSurface.BuildNavMesh();
    }
예제 #6
0
    void Start()
    {
        // Here we can get away with a lower resolution for the render texture as it doesn't need the full float
        flowMap  = RTUtility.CreateRenderTextures(TEX_SIZE, RenderTextureFormat.ARGBHalf);
        waterMap = RTUtility.CreateRenderTextures(TEX_SIZE, RenderTextureFormat.RFloat);

        terrainMap = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat)
        {
            wrapMode   = TextureWrapMode.Clamp,
            filterMode = FilterMode.Point
        };

        GenerateWorld();

        // Clear all the render textures and generate the heightmap
        RTUtility.ClearColor(terrainMap);
        RTUtility.ClearColor(flowMap);
        RTUtility.ClearColor(waterMap);

        terrainMap = HeightmapGenerator.GenerateHeightMap(terrainMap, TERRAIN_HEIGHT, 8, 0.3f, 3f);
    }
        public TerrainData Generate()
        {
            CreateMeshComponents();

            var numTilesPerLine = Mathf.CeilToInt(worldSize);
            var min             = centralize ? -numTilesPerLine / 2f : 0;
            var map             = HeightmapGenerator.GenerateHeightmap(terrainNoise, numTilesPerLine);

            var vertices  = biomes.Select(_ => new List <Vector3>()).ToArray();
            var triangles = biomes.Select(_ => new List <int>()).ToArray();

            var terrainData = new TerrainData(numTilesPerLine);
            var colors      = biomes.Select(_ => new List <Color>()).ToArray();

            for (var y = 0; y < numTilesPerLine; y++)
            {
                for (var x = 0; x < numTilesPerLine; x++)
                {
                    var biomeAndStep = GetBiomeInfo(map[x, y]);
                    var biomeIndex   = (int)biomeAndStep.x;
                    var biome        = biomes[biomeIndex];
                    terrainData.BiomeIndices[x, y] = biomeIndex;
                    terrainData.BiomesStep[x, y]   = biomeAndStep.y;
                    terrainData.Depths[x, y]       = biomeIndex > 0 ? Mathf.Lerp(biomes[biomeIndex - 1].maxTerrainHeight, biome.maxTerrainHeight, biomeAndStep.y) : 0f;
                }
            }

            // Bridge gaps between water and land tiles, and also fill in sides of map
            for (var y = 0; y < numTilesPerLine; y++)
            {
                for (var x = 0; x < numTilesPerLine; x++)
                {
                    var biomeIndex = terrainData.BiomeIndices[x, y];
                    var biome      = biomes[biomeIndex];
                    var step       = terrainData.BiomesStep[x, y];
                    var color      = Color.Lerp(biome.startColor, biome.endColor, step);
                    var height     = terrainData.Depths[x, y];

                    var vertexCount = vertices[biomeIndex].Count;

                    var topRight    = new Vector3(min + x - 0.5f, GetCornerHeight(terrainData, x, y, -1, -1), min + y - 0.5f);
                    var topLeft     = new Vector3(min + x + 0.5f, GetCornerHeight(terrainData, x, y, 1, -1), min + y - 0.5f);
                    var bottomRight = new Vector3(min + x - 0.5f, GetCornerHeight(terrainData, x, y, -1, 1), min + y + 0.5f);
                    var bottomLeft  = new Vector3(min + x + 0.5f, GetCornerHeight(terrainData, x, y, 1, 1), min + y + 0.5f);

                    vertices[biomeIndex].AddRange(new[]
                    {
                        topLeft,
                        topRight,
                        bottomRight,
                        bottomLeft
                    });
                    colors[biomeIndex].AddRange(new[]
                    {
                        color,
                        color,
                        color,
                        color
                    });
                    triangles[biomeIndex].AddRange(new[]
                    {
                        vertexCount + 0, vertexCount + 1, vertexCount + 2,
                        vertexCount + 0, vertexCount + 2, vertexCount + 3,
                    });
                }
            }


            for (var biomeIndex = 0; biomeIndex < biomes.Length; biomeIndex++)
            {
                var mesh = _meshes[biomeIndex];
                mesh.SetVertices(vertices[biomeIndex]);
                mesh.SetTriangles(triangles[biomeIndex], 0, true);
                mesh.SetColors(colors[biomeIndex]);
                mesh.Optimize();
                mesh.RecalculateNormals();
                mesh.RecalculateTangents();
                mesh.RecalculateBounds();

                _meshRenderers[biomeIndex].sharedMaterial = biomes[biomeIndex].material;
            }

            return(terrainData);
        }
예제 #8
0
 public abstract void RunModule(HeightmapGenerator generator);
예제 #9
0
    public override void RunModule(HeightmapGenerator generator)
    {
        float width  = generator.LandscapeRoot.Width;
        float height = generator.LandscapeRoot.Height;

        Color[,] heightmap = generator.CurrentHeightmap;

        int xSize = heightmap.GetLength(0);
        int ySize = heightmap.GetLength(1);

        int maxSize   = Mathf.Max(xSize, ySize);
        int nextPower = (int)Mathf.Pow(2, Mathf.Ceil(Mathf.Log(maxSize - 1) / Mathf.Log(2)));

        Debug.Log("Next power: " + nextPower);

        Color[,] target = new Color[nextPower + 1, nextPower + 1];

        const float minHeight = 30.0f;
        const float maxHeight = 30.0f;

        target[0, 0].r                 = Random.Range(minHeight, maxHeight);
        target[nextPower, 0].r         = Random.Range(minHeight, maxHeight);
        target[0, nextPower].r         = Random.Range(minHeight, maxHeight);
        target[nextPower, nextPower].r = Random.Range(minHeight, maxHeight);

        target[0, 0].a                 = 1.0f;
        target[nextPower, 0].a         = 1.0f;
        target[0, nextPower].a         = 1.0f;
        target[nextPower, nextPower].a = 1.0f;

        int halfOffset = nextPower / 2;
        int iteration  = 0;

        int texSize = nextPower + 1;

        /*
         * while(halfOffset >= 1 && iteration < 10000000)
         * {
         *  // Diamond
         *  for (int y = halfOffset; y + halfOffset < texSize; y += halfOffset * 2)
         *  {
         *      for (int x = halfOffset; x + halfOffset < texSize; x += halfOffset * 2)
         *      {
         *
         *          if (target[x, y].a == 1.0f) continue;
         *
         *
         *
         *          float v0 = target[x - halfOffset, y - halfOffset].r;
         *          float v1 = target[x + halfOffset, y - halfOffset].r;
         *          float v2 = target[x - halfOffset, y + halfOffset].r;
         *          float v3 = target[x + halfOffset, y + halfOffset].r;
         *
         *          target[x, y].r = (v0 + v1 + v2 + v3) / 4.0f;
         *          target[x, y].a = 1.0f;
         *          Debug.Log("Diamond (" + x + ", " + y + ") : " + target[x, y].r.ToString());
         *      }
         *  }
         *
         *  // Square
         *  for (int y = 0; y < texSize; y += halfOffset)
         *  {
         *      for (int x = 0; x < texSize; x += halfOffset)
         *      {
         *
         *          if (target[x, y].a == 1.0f) continue;
         *
         *          int index0X = ((x - halfOffset) + texSize) % texSize;
         *          int index1X = (x + halfOffset) % texSize;
         *          int index2X = x;
         *          int index3X = x;
         *
         *          int index0Y = y;
         *          int index1Y = y;
         *          int index2Y = ((y - halfOffset) + texSize) % texSize;
         *          int index3Y = (y + halfOffset) % texSize;
         *
         *          float v0 = target[index0X, index0Y].r;
         *          float v1 = target[index1X, index1Y].r;
         *          float v2 = target[index2X, index2Y].r;
         *          float v3 = target[index3X, index3Y].r;
         *
         *          target[x, y].r = (v0 + v1 + v2 + v3) / 4.0f;
         *          target[x, y].a = 1.0f;
         *          Debug.Log("Square (" + x + ", " + y + ") : " + target[x, y].r.ToString());
         *      }
         *  }
         *
         *  halfOffset = halfOffset / 2;
         *  iteration++;
         * }
         *
         * for(int x = 0; x < heightmap.GetLength(0); ++x)
         * {
         *  for (int y = 0; y < heightmap.GetLength(1); ++y)
         *  {
         *      float normalisedX = (float)x / (float)heightmap.GetLength(0);
         *      float normalisedY = (float)y / (float)heightmap.GetLength(1);
         *
         *
         *      heightmap[x, y] = target[(int)(normalisedX * (float)target.GetLength(0)), (int)(normalisedY * (float)target.GetLength(1))];
         *  }
         * }*/
        generator.CurrentHeightmap = target;
    }