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)); }
public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, BiomeMapSettings biomeMapSettings, ResourceMapSettings resourceMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, TerrainViewer viewer, Material terrainMeshMaterial) { this.Coord = coord; this.HeightMapSettings = heightMapSettings; this.BiomeMapSettings = biomeMapSettings; this.ResourceMapSettings = resourceMapSettings; this.MeshSettings = meshSettings; this.detailLevels = detailLevels; this.colliderLODIndex = colliderLODIndex; this.Viewer = viewer; SampleCenter = coord * meshSettings.MeshWorldSize / meshSettings.MeshScale; Vector2 position = coord * meshSettings.MeshWorldSize; Bounds = new Bounds(position, Vector2.one * meshSettings.MeshWorldSize); MeshObject = new GameObject("Terrain Chunk"); meshRenderer = MeshObject.AddComponent <MeshRenderer>(); meshFilter = MeshObject.AddComponent <MeshFilter>(); meshCollider = MeshObject.AddComponent <MeshCollider>(); meshRenderer.material = terrainMeshMaterial; MeshObject.AddComponent <TerrainChunkInteraction>().TerrainChunk = this; MeshObject.transform.position = new Vector3(position.x, 0, position.y); MeshObject.transform.parent = parent; MeshObject.layer = parent.gameObject.layer; SetVisible(false); lodMeshes = new LODMesh[detailLevels.Length]; for (int i = 0; i < detailLevels.Length; i++) { lodMeshes[i] = new LODMesh(detailLevels[i].Lod); lodMeshes[i].UpdateCallback += UpdateTerrainChunk; if (i == colliderLODIndex) { lodMeshes[i].UpdateCallback += UpdateCollisionMesh; } } MaxViewDistance = detailLevels[detailLevels.Length - 1].VisibleDistanceThreshold; }
public ResourceMap(float[,] values, ResourcePoint[] resourcePoints, ResourceMapSettings settings) { this.values = values; this.resourcePoints = resourcePoints; this.settings = settings; }
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()); } }
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]); } }
/// <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)); }