public HeightMap(float[,] values, float minValue, float maxValue, HeightMapSettings settings) { this.Values = values; this.MinValue = minValue; this.MaxValue = maxValue; this.Settings = settings; }
public static HeightMap GenerateHeightMap(int size, HeightMapSettings settings, Vector2 sampleCenter) { float[,] values = Noise.GenerateNoiseMap(size, settings.NoiseSettings, sampleCenter); AnimationCurve heightCurve_threadSafe = new AnimationCurve(settings.HeightCurve.keys); float minValue = float.MaxValue; float maxValue = float.MinValue; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { values[x, y] *= heightCurve_threadSafe.Evaluate(values[x, y]) * settings.HeightMultiplier; if (values[x, y] > maxValue) { maxValue = values[x, y]; } if (values[x, y] < minValue) { minValue = values[x, y]; } } } return(new HeightMap(values, minValue, maxValue, settings)); }
/// <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)); }
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; }