Пример #1
0
        public DataMap(int uniformSize, HeightMap heightMap, BiomeMap biomeMap, Dictionary <Vector2, TerrainChunkPart> chunkParts)
        {
            this.UniformSize = uniformSize;

            this.HeightMap  = heightMap;
            this.BiomeMap   = biomeMap;
            this.ChunkParts = chunkParts;
        }
Пример #2
0
        /// <summary>
        /// Generates a <see cref="DataMap"/> that consists of a heightmap, biomeMap and resourceMap based on a single size. Divides the resourceMap in <see cref="DataMap.ChunkParts"/> based on <see cref="MeshSettings.ChunkPartSizeRoot"/>
        /// </summary>
        /// <param name="terrainChunk">The terrain chunk that requested the DataMap.</param>
        /// <returns></returns>
        public static DataMap GenerateDataMap(MeshSettings meshSettings, HeightMapSettings heightMapSettings, BiomeMapSettings biomeMapSettings, ResourceMapSettings resourceMapSettings, TerrainChunk terrainChunk)
        {
            int size        = meshSettings.NumVertsPerLine;
            int uniformSize = size - 2;

            // Generate data maps
            HeightMap heightMap = HeightMapGenerator.GenerateHeightMap(size, heightMapSettings, terrainChunk.SampleCenter);
            BiomeMap  biomeMap  = BiomeMapGenerator.GenerateBiomeMap(uniformSize, biomeMapSettings, terrainChunk.SampleCenter);

            // Create chunk parts
            Dictionary <Vector2, TerrainChunkPart> chunkParts = CreateChunkParts(uniformSize, meshSettings, terrainChunk);

            FillChunkParts(uniformSize, ref chunkParts, meshSettings, resourceMapSettings, biomeMap, heightMap, terrainChunk);

            return(new DataMap(uniformSize, heightMap, biomeMap, chunkParts));
        }
Пример #3
0
        public static Texture2D TextureFromBiomeMap(BiomeMap biomeMap)
        {
            int width  = biomeMap.Values.GetLength(0);
            int height = biomeMap.Values.GetLength(1);

            Color[] colorMap = new Color[width * height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    colorMap[y * width + x] = biomeMap.GetBiome(x, y).Color;
                }
            }

            return(TextureFromColorMap(colorMap, width, height));
        }
        public static ResourceMap GenerateResourceMap(int size, ResourceMapSettings settings, Vector2 sampleCenter, BiomeMap biomeMap, HeightMap heightMap)
        {
            size -= 1; // To avoid overlap with other chunks on edges

            float[,] values = new float[size, size];

            // Create a unique seed for this chunk
            System.Random randomNum = new System.Random((int)(0.5 * (sampleCenter.x + sampleCenter.y) * (sampleCenter.x + sampleCenter.y + 1) + sampleCenter.y + settings.NoiseSettings.seed));

            List <ResourcePoint> resourcePoints = new List <ResourcePoint>();

            for (int x = 0; x < size; x++)
            {
                for (int z = 0; z < size; z++)
                {
                    float random = randomNum.Next(0, 100) / 100f;

                    Biome biome = biomeMap.GetBiome(x, z);

                    if (random > biome.ResourceDensity)
                    {
                        int biomeId = (int)biome.BiomeType;

                        if (TerrainGenerator.BiomeResources.ContainsKey(biomeId) && TerrainGenerator.BiomeResources[biomeId].worldResourceEntries.Count > 0)
                        {
                            int resourceIndex = randomNum.Next(0, TerrainGenerator.BiomeResources[biomeId].worldResourceEntries.Count);

                            resourcePoints.Add(new ResourcePoint(x, z, biomeId, resourceIndex));
                        }
                    }
                }
            }

            return(new ResourceMap(values, resourcePoints.ToArray(), settings));
        }
Пример #5
0
        public static ObjectPoint[] CreateObjectPoints(int uniformSize, MeshSettings meshSettings, ResourceMapSettings resourceMapSettings, BiomeMap biomeMap, HeightMap heightMap, TerrainChunk terrainChunk)
        {
            string fileName = $"chunkInfo{terrainChunk.Coord.x}{terrainChunk.Coord.y}.dat";

            // Try to load a save file
            if (File.Exists(SaveDataManager.WorldDataPath + fileName))
            {
                return(ObjectMapLoader.LoadObjectMap(terrainChunk, SaveDataManager.WorldDataPath).ObjectPoints);
            }
            else
            {
                //ResourceMap resourceMap = ResourceMapGenerator.GenerateResourceMap(uniformSize, resourceMapSettings, terrainChunk.SampleCenter);

                ResourceMap resourceMap = ResourceMapGenerator.GenerateResourceMap(uniformSize, resourceMapSettings, terrainChunk.SampleCenter, biomeMap, heightMap);

                List <ObjectPoint> tempObjectPoints = new List <ObjectPoint>();

                // Resource points to object points
                foreach (var resourcePoint in resourceMap.resourcePoints)
                {
                    int x = resourcePoint.x;
                    int z = resourcePoint.z;

                    HeightMapLayer layer = heightMap.GetLayer(x + 1, z + 1);

                    // Don't spawn objects on water.
                    if (layer.IsWater)
                    {
                        continue;
                    }

                    float   height   = heightMap.Values[x + 1, z + 1];
                    Vector3 position = new Vector3(terrainChunk.Bounds.center.x, 0f, terrainChunk.Bounds.center.y) + new Vector3((x - (uniformSize - 1) / 2f) * meshSettings.MeshScale, height, (z - (uniformSize - 1) / 2f) * -meshSettings.MeshScale);

                    // Create a seeded System.Random for the rotation based on the position
                    float         a    = position.x + position.y;
                    float         b    = position.z + position.y;
                    System.Random rand = new System.Random((int)(0.5 * (a + b) * (a + b + 1) + b));

                    Quaternion rotation = Quaternion.Euler(new Vector3(0f, rand.Next(0, 360), 0f));

                    int chunkPartSize = uniformSize / meshSettings.ChunkPartSizeRoot + 1;
                    int coordX        = Mathf.FloorToInt(x / chunkPartSize) - 1;
                    int coordZ        = Mathf.FloorToInt(z / chunkPartSize) - 1;

                    Vector2 chunkPartCoords = terrainChunk.Coord * meshSettings.ChunkPartSizeRoot + new Vector2(coordX, -coordZ);

                    tempObjectPoints.Add(new ObjectPoint(position, rotation, resourcePoint.biomeId, resourcePoint.worldResourcePrefabId, chunkPartCoords.x, chunkPartCoords.y));
                }

                return(tempObjectPoints.ToArray());
            }
        }
Пример #6
0
        private static void FillChunkParts(int uniformSize, ref Dictionary <Vector2, TerrainChunkPart> chunkParts, MeshSettings meshSettings, ResourceMapSettings resourceMapSettings, BiomeMap biomeMap, HeightMap heightMap, TerrainChunk terrainChunk)
        {
            ObjectPoint[] objectPoints = CreateObjectPoints(uniformSize, meshSettings, resourceMapSettings, biomeMap, heightMap, terrainChunk);

            // Fill chunk parts
            for (int i = 0; i < objectPoints.Length; i++)
            {
                Vector2          chunkPartCoords  = new Vector2(objectPoints[i].chunkPartCoordX, objectPoints[i].chunkPartCoordZ);
                TerrainChunkPart terrainChunkPart = chunkParts[chunkPartCoords];

                terrainChunkPart.AddObjectPoint(objectPoints[i]);
            }
        }