static void InterpolateBiomes(int worldBaseX, int worldBaseZ, ref Biome[,] biomes, ref ushort[,] heights, ref ushort[,,] voxels) { byte interpolationLength = 6; byte interpolationLengthHalfed = (byte)(interpolationLength / 2); for (ushort x = 0; x < CommonConstants.World.ChunkSize.X; x++) { for (ushort z = 0; z < CommonConstants.World.ChunkSize.Z; z++) { if (x - interpolationLengthHalfed > 0 && x + interpolationLengthHalfed < CommonConstants.World.ChunkSize.X && z - interpolationLengthHalfed > 0 && z + interpolationLengthHalfed < CommonConstants.World.ChunkSize.Z && (biomes[x, z] != biomes[x - 1, z] || biomes[x, z] != biomes[x, z - 1])) { ushort heightBL = heights[x - interpolationLengthHalfed, z - interpolationLengthHalfed]; ushort heightBR = heights[x + interpolationLengthHalfed, z - interpolationLengthHalfed]; ushort heightTL = heights[x - interpolationLengthHalfed, z + interpolationLengthHalfed]; ushort heightTR = heights[x + interpolationLengthHalfed, z + interpolationLengthHalfed]; for (int ix = -interpolationLengthHalfed; ix < interpolationLengthHalfed; ix++) { ushort heightTop = (ushort)MathHelper.InterpolateLinear(heightTL, heightTR, (ix / (float)interpolationLength) + 0.5f); ushort heightBottom = (ushort)MathHelper.InterpolateLinear(heightBL, heightBR, (ix / (float)interpolationLength) + 0.5f); for (int iz = -interpolationLengthHalfed; iz < interpolationLengthHalfed; iz++) { ushort heightCenter = (ushort)MathHelper.InterpolateLinear(heightTop, heightBottom, (iz / (float)interpolationLength) + 0.5f); heights[x + ix, z + iz] = heightCenter; } } } } } }
void DrawBiomeDoodads(float[,] heightMap, Biome[,] biomeMap) { int width = heightMap.GetLength(0); int height = heightMap.GetLength(1); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Biome biome = biomeMap[x, y]; BiomeTheme.Doodad doodad = biomeColorTheme.GetDoodad(biome); if (doodad != null) { GameObject obj = new GameObject("Biome Doodad"); obj.AddComponent <MeshRenderer> (); obj.AddComponent <MeshFilter> (); obj.GetComponent <MeshFilter> ().sharedMesh = doodad.mesh; obj.GetComponent <Renderer>().material = doodad.material; obj.GetComponent <Renderer>().material.color = doodad.color; obj.transform.localPosition = new Vector3(x - width / 2 + .5f, heightMap[x, y] + doodad.height / 2, y - width / 2 + .5f); obj.transform.parent = transform; doodads.Add(obj); } } } }
public override Task OnActivateAsync() { _densityMap = new float[5, 33, 5]; _depthMap = new float[5, 1, 5]; _mainNoiseMap = new float[5, 33, 5]; _minLimitMap = new float[5, 33, 5]; _maxLimitMap = new float[5, 33, 5]; _surfaceMap = new float[16, 1, 16]; _seed = (int)this.GetPrimaryKeyLong(); _random = new Random(_seed); _depthNoise = new OctavedNoise <PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F); _mainNoise = new OctavedNoise <PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F); _maxNoise = new OctavedNoise <PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F); _minNoise = new OctavedNoise <PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F); _surfaceNoise = new OctavedNoise <PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F); _biomeWeights = new float[5, 5]; for (int i = -2; i <= 2; ++i) { for (int j = -2; j <= 2; ++j) { float f = 10.0F / (float)Math.Sqrt((i * i + j * j) + 0.2D); _biomeWeights[i + 2, j + 2] = f; } } _biomesForGeneration = new Biome[16, 16]; _genlayer = GenLayer.InitAllLayer(_seed); return(Task.CompletedTask); }
static void GenerateFinishers(int worldBaseX, int worldBaseZ, ref Biome[,] biomes, ref ushort[,] heights, ref ushort[,,] voxels) { for (ushort x = 0; x < CommonConstants.World.ChunkSize.X; x++) { for (ushort z = 0; z < CommonConstants.World.ChunkSize.Z; z++) { biomes[x, z].GetFinishers(worldBaseX + x, worldBaseZ + z, x, z, heights[x, z] + 1, ref voxels); } } }
static void GetHeights(int worldBaseX, int worldBaseZ, ref Biome[,] biomes, ref ushort[,] heights, ref ushort[,,] voxels) { for (ushort x = 0; x < CommonConstants.World.ChunkSize.X; x++) { for (ushort z = 0; z < CommonConstants.World.ChunkSize.Z; z++) { heights[x, z] = biomes[x, z].GetHeight(worldBaseX + x, worldBaseZ + z); } } }
public World2dRegion(World world, Vector2i position) { _world = world; _position = position; _biomes = new Biome[RegionLength, RegionLength]; _heights = new int[RegionLength, RegionLength]; _interpolatedHeights = new int[RegionLength, RegionLength]; _interpolatedGrassColors = new Rgba32I[RegionLength, RegionLength]; _sunlightData = new SunlightData[RegionLength, RegionLength]; }
static void GetBiomes(int worldBaseX, int worldBaseZ, ref Biome[,] biomes, ref ushort[,] heights, ref ushort[,,] voxels) { for (ushort x = 0; x < CommonConstants.World.ChunkSize.X; x++) { for (ushort z = 0; z < CommonConstants.World.ChunkSize.Z; z++) { biomes[x, z] = RegionManager.GetRegion(worldBaseX + x, worldBaseZ + z).GetBiome(worldBaseX + x, worldBaseZ + z); } } }
static void GenerateTerrainComposition(int worldBaseX, int worldBaseZ, ref Biome[,] biomes, ref ushort[,] heights, ref ushort[,,] voxels) { for (ushort x = 0; x < CommonConstants.World.ChunkSize.X; x++) { for (ushort y = 0; y < CommonConstants.World.ChunkSize.Y; y++) { for (ushort z = 0; z < CommonConstants.World.ChunkSize.Z; z++) { voxels[x, y, z] = biomes[x, z].GetVoxel(worldBaseX + x, y, worldBaseZ + z, heights[x, z]); } } } }
public TileData(float[,] heightMap, float[,] heatMap, float[,] moistureMap, TerrainType[,] chosenHeightTerrainTypes, TerrainType[,] chosenHeatTerrainTypes, TerrainType[,] chosenMoistureTerrainTypes, Biome[,] chosenBiomes, Mesh mesh) { this.heightMap = heightMap; this.heatMap = heatMap; this.moistureMap = moistureMap; this.chosenHeightTerrainTypes = chosenHeightTerrainTypes; this.chosenHeatTerrainTypes = chosenHeatTerrainTypes; this.chosenMoistureTerrainTypes = chosenMoistureTerrainTypes; this.chosenBiomes = chosenBiomes; this.mesh = mesh; }
public void DrawBoard(BoardNode[,] board, bool forceRedraw = false) { int width = board.GetLength(0); int height = board.GetLength(1); bool redrawMesh = false; bool redrawTexture = false; bool redrawDoodads = false; bool redrawWater = false; if (lastBoardCopy == null || forceRedraw || !drawPartialUpdates) { redrawMesh = true; redrawTexture = true; redrawDoodads = true; redrawWater = true; } else if (lastBoard != null) { BoardDiffState diff = BoardDiffState.Compare(board, lastBoardCopy); if (!diff.hasNodeChange) { return; } redrawDoodads = true; redrawWater = true; if (diff.hasHeightChange) { redrawMesh = true; } if (diff.hasBiomeChange) { redrawTexture = true; } } lastBoard = board; if (lastBoardCopy == null) { lastBoardCopy = new BoardNode[width, height]; } if (drawPartialUpdates) { Array.Copy(board, lastBoardCopy, width * height); } float[,] heightMap = GenerateHeightMap(board); Biome[,] biomeMap = BiomeGenerator.GenerateBiomeData(board); Color[] colorMap = GenerateBiomeColorMap(board, heightMap, biomeMap); if (redrawMesh) { // The cubes rendered in DrawBoardCubes are added to the doodads list, so that call // needs to happen after that list has been cleared. if (!drawTerrainAsCubes) { DrawBoardMesh(heightMap, colorMap); } } if (redrawTexture) { DrawBoardTexture(board, colorMap); } if (redrawDoodads) { foreach (GameObject obj in doodads) { Destroy(obj); } doodads.Clear(); if (drawTerrainAsCubes) { meshFilter.mesh = null; bottomMeshFilter.mesh = null; DrawBoardCubes(heightMap, colorMap); } else if (drawBiomeDoodads) { DrawBiomeDoodads(heightMap, biomeMap); } if (drawBiomeClouds) { DrawBiomeClouds(board, heightMap); } } if (redrawWater) { if (waterCube != null) { float waterLevel = GetWaterLevel(board); waterCube.transform.localScale = new Vector3(width - 1 + 2 * borderSize, waterLevel, height - 1 + 2 * borderSize); waterCube.transform.localPosition = new Vector3(0, waterLevel / 2 - waterLevelOffset, 0); } } }
public void GenerateBiomes() { random = new Random(seed); // start the RNG over biomeGrid = new Biome[size, size]; int midPoint = size / 2; //int count = 0; foreach (int row in Enumerable.Range(0, size).OrderBy(x => random.Next())) { // random iteration prevents right-to-left striation foreach (int col in Enumerable.Range(0, size).OrderBy(x => random.Next())) { // find neighbors of current biome Biome[] neighbors = new Biome[4]; // find north neighbor coords if (row == 0) { // it's a globe, find the north neighbor of the northmost int ncol; if (col >= midPoint) { ncol = col - midPoint; } else { ncol = col + midPoint; } neighbors[0] = biomeGrid[0, ncol]; } else { neighbors[0] = biomeGrid[row - 1, col]; } // find east neighbor coords if (col == size - 1) { neighbors[1] = biomeGrid[row, 0]; // wrap around the world } else { neighbors[1] = biomeGrid[row, col + 1]; } // find south neighbor coords if (row == size - 1) { int scol; if (col >= midPoint) { scol = col - midPoint; } else { scol = col + midPoint; } neighbors[2] = biomeGrid[size - 1, scol]; } else { neighbors[2] = biomeGrid[row + 1, col]; } // find west neighbor coords if (col == 0) { neighbors[3] = biomeGrid[row, size - 1]; } else { neighbors[3] = biomeGrid[row, col - 1]; } //find zone of current biome int zone; int distanceFromEquator = row - midPoint; if (distanceFromEquator < 0) { distanceFromEquator *= -1; } zone = distanceFromEquator / (midPoint / 3); if (zone == 3) { zone = 2; } biomeGrid[row, col] = DetermineBiome((TemperatureZone)zone, neighbors); } // count++; // if (count % 10 == 0) { // Console.WriteLine(BiomeGridToString()); // } } }
private Texture2D BuildBiomeTextures(TerrainType[,] heightTerrainTypes, TerrainType[,] heatTerrainTypes, TerrainType[,] moistureTerrainTypes, Biome[,] chosenBiomes) { int tileDepth = heatTerrainTypes.GetLength(0); int tileWidth = heatTerrainTypes.GetLength(1); Color[] colorMap = new Color[tileDepth * tileWidth]; for (int zIndex = 0; zIndex < tileDepth; zIndex++) { for (int xIndex = 0; xIndex < tileWidth; xIndex++) { int colorIndex = zIndex * tileWidth + xIndex; TerrainType heightTerrainType = heightTerrainTypes[zIndex, xIndex]; if (heightTerrainType.name != "water".ToLower()) { TerrainType heatTerrainType = heatTerrainTypes[zIndex, xIndex]; TerrainType moistureTerrainType = moistureTerrainTypes[zIndex, xIndex]; Biome biome = this.biomes[moistureTerrainType.index].biomes [heatTerrainType.index]; colorMap[colorIndex] = biome.color; } else { colorMap[colorIndex] = this.waterColor; } } } Texture2D tileTexture = new Texture2D(tileWidth, tileDepth); tileTexture.filterMode = FilterMode.Point; tileTexture.wrapMode = TextureWrapMode.Clamp; tileTexture.SetPixels(colorMap); tileTexture.Apply(); return(tileTexture); }
Color[] GenerateBiomeColorMap(BoardNode[,] board, float[,] heightMap, Biome[,] biomeMap) { int width = board.GetLength(0); int height = board.GetLength(1); int colorMapLength = (drawHDTexture) ? width * height * 4 : width * height; Color[] colorMap = new Color[colorMapLength]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { BoardNode node = board[x, y]; Biome biome = biomeMap[x, y]; Color baseColor = ColorFromBiome(biome); Color altitudeColor = Color.Lerp(Color.black, Color.white, node.altitude / MAX_ALTITUDE); // Original thought was to have this be on a biome-basis, e.g. to only apply it to "fluid" biomes. // Each tile has a 2x2 pixel texture. If any quadrant of that texture has a neighboring tile with a // greater altitude, that tile's texture is blended into that pixel. if (drawHDTexture) { Color colorA = baseColor; Color colorB = baseColor; Color colorC = baseColor; Color colorD = baseColor; bool nIsHigher = y > 0 && heightMap[x, y - 1] > heightMap[x, y]; bool neIsHigher = y > 0 && x <width - 1 && heightMap[x + 1, y - 1]> heightMap[x, y]; bool eIsHigher = x <width - 1 && heightMap[x + 1, y]> heightMap[x, y]; bool seIsHigher = x < width - 1 && y <height - 1 && heightMap[x + 1, y + 1]> heightMap[x, y]; bool sIsHigher = y <height - 1 && heightMap[x, y + 1]> heightMap[x, y]; bool swIsHigher = y < height - 1 && x > 0 && heightMap[x - 1, y + 1] > heightMap[x, y]; bool wIsHigher = x > 0 && heightMap[x - 1, y] > heightMap[x, y]; bool nwIsHigher = x > 0 && y > 0 && heightMap[x - 1, y - 1] > heightMap[x, y]; if (nwIsHigher) { colorA = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x - 1, y - 1]), .5f); } else if (nIsHigher) { colorA = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x, y - 1]), .5f); } else if (wIsHigher) { colorA = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x - 1, y]), .5f); } if (neIsHigher) { colorB = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x + 1, y - 1]), .5f); } else if (nIsHigher) { colorB = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x, y - 1]), .5f); } else if (eIsHigher) { colorB = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x + 1, y]), .5f); } if (swIsHigher) { colorC = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x - 1, y + 1]), .5f); } else if (sIsHigher) { colorC = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x, y + 1]), .5f); } else if (wIsHigher) { colorC = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x - 1, y]), .5f); } if (seIsHigher) { colorD = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x + 1, y + 1]), .5f); } else if (sIsHigher) { colorD = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x, y + 1]), .5f); } else if (eIsHigher) { colorD = Color.Lerp(baseColor, ColorFromBiome(biomeMap[x + 1, y]), .5f); } colorMap[x * 2 + width * y * 4] = Color.Lerp(colorA, altitudeColor, .33f); colorMap[x * 2 + width * y * 4 + 1] = Color.Lerp(colorB, altitudeColor, .33f); colorMap[x * 2 + width * y * 4 + 2 * width] = Color.Lerp(colorC, altitudeColor, .33f); colorMap[x * 2 + width * y * 4 + 2 * width + 1] = Color.Lerp(colorD, altitudeColor, .33f); } else { colorMap[x + width * y] = Color.Lerp(baseColor, altitudeColor, .33f); } } } return(colorMap); }
private void ReplaceBiomeBlocks(GeneratorSettings settings, int x, int z, ChunkColumnStorage chunk, Biome[,] biomesIn) { _surfaceNoise.Noise( _surfaceMap, new Vector3(x * 16 + 0.1F, 0, z * 16 + 0.1F), new Vector3(0.0625F, 1.0F, 0.0625F)); for (int x1 = 0; x1 < 16; ++x1) { for (int z1 = 0; z1 < 16; ++z1) { Biome biome = biomesIn[z1, x1]; biome.GenerateBiomeTerrain(settings.SeaLevel, _random, chunk, x, z, x1, z1, (_surfaceMap[x1, 0, z1] - 0.5) * 2); } } }
public void Init() { //populating array of biomes to pull from biomeArray = new Biome[4, 3]; biomeArray[0, 0] = new Biome(); biomeArray[0, 0].name = "Ice"; biomeArray[0, 0].Init(); biomeArray[0, 1] = new Biome(); biomeArray[0, 1].name = "Desert"; biomeArray[0, 1].Init(); biomeArray[0, 2] = new Biome(); biomeArray[0, 2].name = "Gas"; biomeArray[0, 2].Init(); biomeArray[1, 0] = new Biome(); biomeArray[1, 0].name = "Ocean"; biomeArray[1, 0].Init(); biomeArray[1, 1] = new Biome(); biomeArray[1, 1].name = "Volcano"; biomeArray[1, 1].Init(); biomeArray[2, 0] = new Biome(); biomeArray[2, 0].name = "Forest"; biomeArray[2, 0].Init(); biomeArray[2, 1] = new Biome(); biomeArray[2, 1].name = "Gem"; biomeArray[2, 1].Init(); biomeArray[3, 0] = new Biome(); biomeArray[3, 0].name = "Inhabited"; biomeArray[3, 0].Init(); biomeArray[3, 1] = new Biome(); biomeArray[3, 1].name = "Star"; biomeArray[3, 1].Init(); /* biomeArray[0, 0]=ice; * biomeArray[0, 1]=desert; * biomeArray[0, 2]=gas; * biomeArray[1, 0]=ocean; * biomeArray[1, 1]=volcano; * biomeArray[2, 0]=forest; * biomeArray[2, 1]=gem; * biomeArray[3, 0]=inhabited; * biomeArray[3, 1]=star;*/ //randomizing the rarity_val int rand = Random.Range(0, 100); if (rand <= 40) { rarity_val = 0; } else if (rand <= 70) { rarity_val = 1; } else if (rand <= 90) { rarity_val = 2; } else { rarity_val = 3; } }
private Texture2D BuildBiomeTexture(TerrainType[,] heightTerrainTypes, TerrainType[,] heatTerrainTypes, TerrainType[,] moistureTerrainTypes, Biome[,] chosenBiomes) { int tileDepth = heatTerrainTypes.GetLength(0); int tileWidth = heatTerrainTypes.GetLength(1); Color[] colorMap = new Color[tileDepth * tileWidth]; for (int zIndex = 0; zIndex < tileDepth; zIndex++) { for (int xIndex = 0; xIndex < tileWidth; xIndex++) { int colorIndex = zIndex * tileWidth + xIndex; TerrainType heightTerrainType = heightTerrainTypes [zIndex, xIndex]; // check if the current coordinate is a water region if (heightTerrainType.name != "water") { // if a coordinate is not water, its biome will be defined by the heat and moisture values TerrainType heatTerrainType = heatTerrainTypes [zIndex, xIndex]; TerrainType moistureTerrainType = moistureTerrainTypes [zIndex, xIndex]; // terrain type index is used to access the biomes table Biome biome = this.biomes [moistureTerrainType.index].biomes [heatTerrainType.index]; // assign the color according to the selected biome colorMap [colorIndex] = biome.color; // save biome in chosenBiomes matrix only when it is not water chosenBiomes [zIndex, xIndex] = biome; } else { // water regions don't have biomes, they always have the same color colorMap [colorIndex] = this.waterColor; } } } // create a new texture and set its pixel colors Texture2D tileTexture = new Texture2D(tileWidth, tileDepth); tileTexture.wrapMode = TextureWrapMode.Clamp; tileTexture.SetPixels(colorMap); tileTexture.Apply(); return(tileTexture); }
private Texture BuildGameTexture(TerrainType[,] heightTerrainTypes, TerrainType[,] heatTerrainTypes, TerrainType[,] moistureTerrainTypes, Biome[,] chosenBiomes) { int tileDepth = heatTerrainTypes.GetLength(0); int tileWidth = heatTerrainTypes.GetLength(1); Color[] colorMap = new Color[tileDepth * tileWidth]; //for (int zIndex = 0; zIndex < tileDepth; zIndex++) //{ // for (int xIndex = 0; xIndex < tileWidth; xIndex++) // { var heightTerrainType = heightTerrainTypes[0, 0]; var tileTexture = new Texture2D(tileWidth, tileDepth); if (heightTerrainType.name != "water") { var heatTerrainType = heatTerrainTypes[0, 0]; var moistureTerrainType = moistureTerrainTypes[0, 0]; Biome biome = this.biomes[moistureTerrainType.index].biomes[heatTerrainType.index]; chosenBiomes[0, 0] = biome; return(biome.texture); } else { colorMap[0] = Color.blue; } // } //} //var tileTexture = new Texture2D(tileWidth, tileDepth); tileTexture.filterMode = FilterMode.Point; tileTexture.wrapMode = TextureWrapMode.Clamp; tileTexture.SetPixels(colorMap); tileTexture.Apply(); return(tileTexture); }