//Texture2D testReference = new Texture2D(2048, 2048); // Use this for initialization void MakeChunk(int vertices, Vector3 position, int x, int y, float[,] major) { Debug.Log("MakeChunk"); float[,] perlinData = new float[vertices, vertices]; for (int i = 0; i < vertices; i++) { for (int j = 0; j < vertices; j++) { float mjd = requestMajorFeature(vertices, i, j, x, y); perlinData[i, j] = (float)perl.OctavePerlin(((i + ((vertices - 1) * x)) * 0.05d), (j + ((vertices - 1) * y)) * 0.05d, 0.1d, 5, 0.4d * mjd) + (5 * mjd); } } Mesh m = tp.Process(perlinData); GameObject newTile = new GameObject(); newTile.transform.position = position; MeshFilter mf = newTile.AddComponent <MeshFilter>(); mf.mesh = m; MeshRenderer mr = newTile.AddComponent <MeshRenderer>(); mr.material = terrainMaterial; Debug.Log("making water"); //Instantiate(water, new Vector3(position.x + 1000, 400, position.z + 1000), Quaternion.identity); }
public IntermissionState() { var _gen = new Perlin(); _perlinMap = new double[95, 67]; _perlinMap2 = new double[95, 67]; for (int x = 0; x < 95; x++) { for (int y = 0; y < 67; y++) { _perlinMap[x, y] = _gen.OctavePerlin(x * 0.2 + 5.3, y * 0.15 + 5.9, 14.44, 10, 0.5); _perlinMap2[x, y] = _gen.OctavePerlin(x * 0.4 + 45.3, y * 0.6 + 11.2, 18.7, 10, 0.5); } } }
void CalcNoise() { var perlin = new Perlin(Mathf.FloorToInt(pixSize * scale)); // For each pixel in the texture... float y = 0.0f; while (y < noiseTex.height) { float x = 0.0f; while (x < noiseTex.width) { float xCoord = xOrg + x * scale; float yCoord = yOrg + y * scale; float sample = (float)perlin.OctavePerlin(xCoord, yCoord, 0.0f, octaves, persistence); pix[(int)y * noiseTex.width + (int)x] = new Color(sample, sample, sample); x++; } y++; } // Copy the pixel data to the texture and load it into the GPU. noiseTex.SetPixels(pix); noiseTex.Apply(); }
public void GenerateHeights() { Perlin noise = new Perlin(); float[,] heights = source.terrainData.GetHeights(0, 0, source.terrainData.heightmapWidth, source.terrainData.heightmapHeight); for (int y = 0; y < heights.GetLength(0); ++y) { float ty = (float)y / (float)heights.GetLength(0); for (int x = 0; x < heights.GetLength(1); ++x) { float tx = (float)x / (float)heights.GetLength(1); //heights[y, x] = ((Mathf.Sin(Mathf.PI * (ty-0.125f) * 4f) * Mathf.Cos(Mathf.PI * (tx+0.25f) * 4f)) / 2.0f) + 0.5f; float tyo = ty - 0.5f; float txo = tx - 0.5f; // ripple heights[y, x] = (Mathf.Sin(Mathf.PI * Mathf.Sqrt(txo * txo + tyo * tyo) * 4f) / -2f) + 0.5f; // island heights[y, x] *= 1f - Mathf.Abs(txo * txo * 6f + tyo * tyo * 6f); heights[y, x] += Mathf.Abs(txo * txo * 9f + tyo * tyo * 9f) * 0.1f; // noise heights[y, x] += (float)noise.OctavePerlin(tx, ty, 64, 6, 32) * 0.1f; } } source.terrainData.SetHeights(0, 0, heights); }
public World(int seed, int size, double persistence, int octaves, double scaleX, double scaleY, double heightCoefficient, double[] biomesHeights, Color[] biomesColors) { Perlin noise = new Perlin(); Random rand = new Random(seed); bitmap = new Bitmap(size, size); double z = rand.NextDouble() * 10; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { double p = noise.OctavePerlin(i / (double)size * scaleX, j / (double)size * scaleY, z, octaves, persistence) * heightCoefficient - 0.1; Color col; if (p < biomesHeights[0]) { col = biomesColors[0]; } else if (p < biomesHeights[1]) { col = biomesColors[1]; } else if (p < biomesHeights[2]) { col = biomesColors[2]; } else if (p < biomesHeights[3]) { col = biomesColors[3]; } else if (p < biomesHeights[4]) { col = biomesColors[4]; } else if (p < biomesHeights[5]) { col = biomesColors[5]; } else if (p < biomesHeights[6]) { col = biomesColors[6]; } else { col = biomesColors[7]; } bitmap.SetPixel(i, j, col); } } }
public Tile[,] Generate(int size) { double[,] temp = new double[size, size]; Tile[,] tileTemp = new Tile[size, size]; Random random = new Random(); Double persistence = random.NextDouble() * 5; int octaves = random.Next(1, 4); for (double i = 0; i < size; i++) { for (double y = 0; y < size; y++) { temp[(int)i, (int)y] = (Perlin.OctavePerlin(i / size, y / size, 0, octaves, persistence)); } } temp = NormalizeArray(temp); for (int i = 0; i < size; i++) { for (int y = 0; y < size; y++) { string tileKey = null; if (temp[i, y] > 0.7) { tileKey = mountainString; } else { tileKey = grassString; } tileTemp[i, y] = new Tile(MapManager.Instance.GetTileType(tileKey), new Point(i, y)); } } List <int> availableValuesX = new List <int>(); List <int> availableValuesY = new List <int>(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { PrepareAvailableValues(size, size, availableValuesX, availableValuesY, i, j); AddNeighbours(availableValuesX, availableValuesY, i, j, tileTemp); availableValuesX.Clear(); availableValuesY.Clear(); } } return(tileTemp); }
private void initializeR16Texture(Texture2D tex) { if (tex == null) { Debug.Log("Weeeeee!!! NULLLLL"); return; } byte[] bits = new byte[tex.width * tex.height * 2]; for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { short nibble = (short)(Perlin.OctavePerlin(x / 0.001f, y / 0.001f, 0.5, 4, 0.5) * 65536.0); bits[(x * 2) + (y * 2 * tex.width)] = (byte)(nibble >> 8); bits[(x * 2) + (y * 2 * tex.width) + 1] = (byte)nibble;// this needs testing, really not sure if we are going big endian or little endian Debug.Log(nibble); } } }
public void Regenerate() { Perlin gen = new Perlin(); double seed = Random.Range(0,5000); int size = heightmapSize; float[,] noise = new float[size,size]; for(int x=0;x<size;x++) { for(int y=0;y<size;y++) { noise[x,y] = (float)gen.OctavePerlin(((float)x)/(1000f*spread),((float)y)/(1000f*spread),seed,octaves,persistence); } } Terrain terrain = GetComponent<Terrain>(); TerrainData data = terrain.terrainData; data.size = new Vector3(data.size.x,terrainHeight,data.size.z); data.heightmapResolution = size; data.SetHeights(0,0,noise); float[,,] splatmap = new float[splatmapSize,splatmapSize,splatmapSize]; }
//Generates Perlin noise and uses it to create a grid with double values, ranging from -1 to 1 private double[,] GeneratePerlinGrid(int width, int height, double intensity) { double positionX = MaxOfEmpires.Random.NextDouble() * 1000; double positionY = MaxOfEmpires.Random.NextDouble() * 1000; Perlin perlin = new Perlin(); double[,] returnlist = new double[width, height]; double highest = double.MinValue; double lowest = double.MaxValue; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { returnlist[x, y] = perlin.OctavePerlin((x * intensity) + positionX, (y * intensity) + positionY, 0, 1, 1); if (returnlist[x, y] > highest) { highest = returnlist[x, y]; } if (returnlist[x, y] < lowest) { lowest = returnlist[x, y]; } } } // Rescales range to -1 > 1 double middle = highest - (0.5 * (highest - lowest)); double range = highest - lowest; double newRange = 2 / range; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { returnlist[x, y] -= middle; returnlist[x, y] *= newRange; } } return(returnlist); }
void Start() { tex = new Texture2D(1024, 1024); perl = new Perlin(); //no repeat yet for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { double value = perl.OctavePerlin(x * 0.01d, y * 0.01d, 0.01d, 3, 0.5d); Color c = new Color((float)value, (float)value, (float)value); tex.SetPixel(x, y, c); } } tex.Apply(); img.texture = tex; Debug.Log(perl.perlin(0.1d, 0.1d, 0.1d)); Debug.Log(perl.perlin(0.2d, 0.2d, 0.2d)); Debug.Log(perl.perlin(1000.2d, 1500.12d, 0.1d)); }
public void Regenerate() { Perlin gen = new Perlin(); double seed = Random.Range(0, 5000); int size = heightmapSize; float[,] noise = new float[size, size]; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { noise[x, y] = (float)gen.OctavePerlin(((float)x) / (1000f * spread), ((float)y) / (1000f * spread), seed, octaves, persistence); } } Terrain terrain = GetComponent <Terrain>(); TerrainData data = terrain.terrainData; data.size = new Vector3(data.size.x, terrainHeight, data.size.z); data.heightmapResolution = size; data.SetHeights(0, 0, noise); float[,,] splatmap = new float[splatmapSize, splatmapSize, splatmapSize]; }
public void Awake() { Perlin p = new Perlin(256); Vector3 v1 = new Vector3(44,0,112); double d = p.perlin(v1,256); DebugOutput.Shout("Perlin " +d.ToString()); d = p.OctavePerlin(v1, 256,2,50.0); DebugOutput.Shout("OctavePerlin " + d.ToString()); Stopwatch timer = new Stopwatch(); timer.Start(); StartCoroutine(terrainTestNew()); //terrainTestNew(); //TerrainTest(); timer.Stop(); DebugOutput.Shout("timer says: " + timer.ElapsedMilliseconds.ToString()); }
private void initializeRGBA32HeightTexture(Texture2D tex) { if (tex == null) { Debug.Log("Weeeeee!!! NULLLLL"); return; } var data = tex.GetRawTextureData <Color32>(); float equatorMultiplier = 0; for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { equatorMultiplier = Mathf.Min(0 + y + generationSeed, tex.height - y + generationSeed) / ((float)(tex.height)); byte n = (byte)(Perlin.OctavePerlin(x / ((float)tex.width / 16.0f), y / ((float)tex.height / 16.0f), 0.5, 10, 0.5) * 255 * equatorMultiplier); Color32 nn = new Color32(n, n, n, 255); data[x + y * tex.width] = nn; } } tex.filterMode = FilterMode.Point; tex.wrapMode = TextureWrapMode.Repeat; tex.Apply(); }
void CreateShape() { vertices = new Vector3[(xsize + 1) * (zsize + 1)]; for (int i = 0, z = 0; z <= zsize; z++) { for (int x = 0; x <= xsize; x++) { //double y = p.OctavePerlin(xcor, zcor, 0, 6, 20.20) * 80f; float xcor = x * .34f / 250; float zcor = z * .34f / 250; double y = p.OctavePerlin(xcor, zcor, 0, 5, 20.20) * 90f; //height vertices[i] = new Vector3(x, (float)y, z); i++; //Tree if ((float)y > 69.507) { Vector3 Treepos = new Vector3(x, (float)y, z); Instantiate(tree, Treepos, Quaternion.identity); } //House if ((float)y > 40 && count < 1 && z == 183 && x == 183) { Vector3 Housepos = new Vector3(x, (float)y, z); Instantiate(House, Housepos, Quaternion.identity); count++; } //Tree2 if ((float)y > 40 && (float)y < 50 && countt < 1 && z > 30 && x > 30) { Vector3 Tree2pos = new Vector3(x, (float)y, z); Instantiate(grass, Tree2pos, Quaternion.identity); countt++; } Debug.Log("Perlin Noise Y is: " + (float)y); } } //Triangles triangles = new int[xsize * zsize * 6]; int vert = 0; int tris = 0; for (int z = 0; z < zsize; z++) { for (int x = 0; x < xsize; x++) { triangles[tris + 0] = vert + 0; triangles[tris + 1] = vert + xsize + 1; triangles[tris + 2] = vert + 1; triangles[tris + 3] = vert + 1; triangles[tris + 4] = vert + xsize + 1; triangles[tris + 5] = vert + xsize + 2; vert++; tris += 6; } vert++; } //UV Mapping uv = new Vector2[vertices.Length]; for (int i = 0, z = 0; z <= zsize; z++) { for (int x = 0; x <= xsize; x++) { uv[i] = new Vector2((float)x / xsize, (float)z / zsize); i++; } } }
public void GenerateWorld(int radius) { double statLowestPoint = double.MaxValue; double statHighestMountain = double.MinValue; int statSeaTiles = 0; Perlin perlin = new Perlin(5); float boundsX = TopographyHelper.HexToWorld(new Point(radius, 0)).X; float boundsY = TopographyHelper.HexToWorld(new Point(0, radius)).Y; var perlinZ = random.NextDouble(); scene.WorldMap = new Map(radius); for (int p = -radius; p <= radius; p++) { for (int q = -radius; q <= radius; q++) { var currentPoint = new Point(p, q); var cell = scene.WorldMap.GetCell(p, q); if (cell == null) { continue; } // TODO: Remove Magic Numbers, put them in WorldSettings // Set cell neighbors cell.Neighbours = scene.WorldMap.GetNeighbours(currentPoint); // Set cell height var worldPoint = TopographyHelper.HexToWorld(currentPoint); var perlinPoint = (worldPoint + new Vector2(boundsX, boundsY)) * 0.01f; var elevation = perlin.OctavePerlin(perlinPoint.X, perlinPoint.Y, perlinZ, 4, 0.7); // Flat valleys elevation = Math.Pow(elevation, scene.Settings.ElevationPower); // Actually multiply to max height elevation *= scene.Settings.ElevationMultiplier; // Give cells closer to the edge of the map a negative bias. This means the entire map will be surrounded by oceans. var distanceFromEdge = (radius - TopographyHelper.Distance(new Point(0, 0), currentPoint)) / radius; if (distanceFromEdge < 0 || distanceFromEdge > 1) { throw new Exception("Value out of range, check math"); } cell.Elevation = elevation * Math.Pow(distanceFromEdge, scene.Settings.EdgeDistancePower); // Update stats if (cell.Elevation > statHighestMountain) { statHighestMountain = cell.Elevation; } if (cell.Elevation < statLowestPoint) { // Now all data has been set, calculate the modifiers statLowestPoint = cell.Elevation; } // Set wetness by rain var rain = perlin.OctavePerlin(perlinPoint.X, perlinPoint.Y, perlinZ * 100, 2, 0.7) * scene.Settings.RainMultiplier; cell.Wetness = rain; } } // Creates oceans by flood filling from map edge FloodFillOceans(); // Do final touchups and modifier calculation // TODO: This can contain NULL cells, this is bad var cells = scene.WorldMap.Cells; foreach (var cell in cells) { if (cell == null) { continue; } // Remove single-cell 'seas' and islands if (cell.IsWater) { bool canBeWater = false; foreach (var n in cell.Neighbours) { if (n.IsWater) { canBeWater = true; break; } } cell.IsWater = canBeWater; } else { bool canBeLand = false; foreach (var n in cell.Neighbours) { if (!n.IsWater) { canBeLand = true; break; } } cell.IsWater = !canBeLand; } // Calculate temperature var distanceFromEdge = (radius - TopographyHelper.Distance(new Point(0, 0), cell.Coordinates)) / radius; var temperature = MathHelper.Lerp(scene.Settings.PoleTemperature, scene.Settings.EquatorTemperature, (float)Math.Pow(distanceFromEdge, scene.Settings.TemperatureCurvePower)); var relElevation = (float)cell.Elevation - scene.Settings.SeaLevel; if (!cell.IsWater) { // Ocean tiles should have temperature at sea level only, otherwise use altitude multiplier temperature += relElevation * scene.Settings.ElevationTemperatureMultiplier; } cell.Temperature = temperature; // Set values if (cell.IsWater == true) { statSeaTiles++; cell.FoodMod = random.NextDouble() + random.Next(1, 2); cell.ResourceMod = random.NextDouble() + random.Next(1, 2); cell.MaxHousing = 0; } else { // Now all data has been set, calculate the modifiers cell.FoodMod = random.NextDouble() + random.Next(1, 5); cell.ResourceMod = random.NextDouble() + random.Next(1, 5); cell.WealthMod = random.NextDouble() + random.Next(1, 5); cell.MaxHousing = random.Next(0, 1250) + random.Next(0, 1250) + random.Next(0, 1250) + random.Next(0, 1250); } // Set biome AssignBiome(cell); } //SimulateWaterflow(); // Log stats Debug.WriteLine("Stats for World Generation"); Debug.WriteLine("Highest Point: " + statHighestMountain); Debug.WriteLine("Lowest Point: " + statLowestPoint); Debug.WriteLine("Ocean Cells: " + statSeaTiles); Debug.WriteLine("Land Cells: " + (scene.WorldMap.CellCount - statSeaTiles)); Debug.WriteLine("Water coverage:" + ((float)statSeaTiles / scene.WorldMap.CellCount * 100).ToString("##.##") + "%"); }
public float GetHeight(float x, float y) { return((float)Perlin.OctavePerlin(OffsetX + x * XStretch, 0, OffsetY + y * YStretch, Octaves, Persitance) * 10f); }
public override double Generate(double x, double y) { return(Clamp(perlinMath.OctavePerlin(x * scale + offsetX, y * scale + offsetY, offsetZ, octaves, persistence))); }
public IEnumerator Generate() { var image_scale = 10; image_scale = heightmap.width / this.width; image_scale = Math.Max(image_scale, heightmap.height / this.height); /************************/ Stamp("Init and Preinit"); for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var t = grid[x, y]; //t.land = heightmap.GetPixel(x*image_scale, y*image_scale).grayscale < 0.5f; t.land = Perlin.OctavePerlin(x * scale + x_offset, y * scale + y_offset, 0, 7, 0.3) < land_threshold; t.x = x; t.y = y; grid[x, y] = t; } } /************************/ Stamp("Building Land"); yield return(new WaitForSeconds(0)); /************************/ Stamp("Finding Land"); var land = this.All().FindAll((t) => t.land); /************************/ Stamp("Shrinking Land"); land = MapGen.Shrink(land); land = MapGen.Shrink(land); land = MapGen.Shrink(land); /************************/ Stamp("Seperating into base regions"); var selection_list = SeperateContiguous(land); yield return(new WaitForSeconds(0)); /************************/ Stamp("Expanding into base regions"); selection_list = MapGen.ExpandRegions(selection_list); yield return(new WaitForSeconds(0)); /************************/ Stamp("Finding Islands"); var island_tiles = new TileSelection(); foreach (var t in this.All().tiles) { if (!t.land) { continue; } bool in_region = false; foreach (var region in selection_list) { if (region.Contains(t)) { in_region = true; } } if (!in_region) { island_tiles.Add(t); } } /************************/ Stamp("Seperating Islands"); var island_selections = SeperateContiguous(island_tiles); selection_list.AddRange(island_selections); Color[] colors = new[] { new Color(0.6f, 0.1f, 0.1f), new Color(0, 0.6f, 0), new Color(0, 0, 0.6f), new Color(0.6f, 0.5f, 0), new Color(1, 0, 1), new Color(0, 1, 1), new Color(0.6f, 0.5f, 1), new Color(1, 0.5f, 1), new Color(0, 0.3f, 0.7f), new Color(0.6f, 0.5f, 0.5f), new Color(0.5f, 0, 1), new Color(0, 0.6f, 0.4f) }; /************************/ Stamp("Founding Region Objects"); yield return(new WaitForSeconds(0)); var i = 0; foreach (var region_tiles in selection_list) { i++; i = i % colors.Length; var region = new Region(); this.regions.Add(region); region.color = colors[i]; foreach (var t in region_tiles.tiles) { region.Assign(t); } } for (var splitcount = 0; splitcount < 20; splitcount++) { foreach (var too_big_region in this.regions.FindAll((r) => r.tiles.Count() > max_size)) { /************************/ Stamp("Spliting Region"); yield return(new WaitForSeconds(2)); i++; i = i % colors.Length; var region = new Region(); region.color = colors[i]; this.regions.Add(region); too_big_region.Split(region); _gui_manager.selected_tiles = new TileSelection(); } } _gui_manager.selected_tiles = new TileSelection(); /************************/ Stamp("Merging in Islands"); foreach (var too_small_region in this.regions.FindAll((r) => r.tiles.Count() < min_size)) { yield return(new WaitForSeconds(0)); if (too_small_region.tiles.Count() != 0) { too_small_region.MergeInto(too_small_region.ClosestRegion()); _gui_manager.selected_tiles = too_small_region.tiles; } } _gui_manager.selected_tiles = new TileSelection(); /************************/ Stamp("Removing Edge Regions"); foreach (var region in this.regions) { if (region.tiles.Any((t) => t.adjacent.Count() != 4)) { foreach (var t in region.tiles.ToList()) { t.land = false; t.region = null; } } } /************************/ Stamp("Finished"); yield return(new WaitForSeconds(0)); }
void PerlinTest(int testSize,int stepSize,int octaves,double persistence) { if (stepSize < 1) { stepSize = 1; } Perlin p = new Perlin(0); Vector3 testVector = new Vector3(10, 10, 0); //string[] tests = new string[testSize * testSize * testSize]; testSize = testSize / 2; int successfullValues = 0; double moreThan = 0.44; double lessThan = 0.48; perlintest = "Test: octaves: "+octaves.ToString()+" persistence: "+persistence.ToString()+" moreThan: "+moreThan.ToString()+ " lessThan: "+lessThan.ToString(); DebugOutput.Shout(perlintest); //for (int i = testSize-1; i >= -testSize; i--) for (int i = -testSize; i < testSize; i+=stepSize) { for (int j = -testSize; j < testSize; j += stepSize) { for (int k = -30; k < 30; k++) { testVector.x = i; testVector.y = j; testVector.z = k; //p.perlin(testVector, 256); //double d = p.perlin(testVector, 256); // //double d = p.OctavePerlin(testVector, 16384, octaves, persistence); double d = p.OctavePerlin(testVector, 16384, octaves, persistence); Vector3 t2 = testVector; t2.x = testVector.z; t2.y = testVector.x; t2.z = testVector.y; double d2 = p.OctavePerlin(t2, 16384, octaves, persistence); if (d > moreThan && d < lessThan && d2 > moreThan && d2 < lessThan) { UnityEngine.Debug.DrawLine(testVector, testVector + Vector3.up, UtilityColor.hsv2rgb(1.0f - (float)d, 1, 1), 20.0f); UnityEngine.Debug.DrawLine(testVector + (Vector3.up * 0.5f) + (Vector3.left * 0.5f), testVector + (Vector3.up * 0.5f) + (Vector3.right * 0.5f), UtilityColor.hsv2rgb(1.0f - (float)d, 1, 1), 20.0f); } /* if (d>0.45 && d < 0.55) { UnityEngine.Debug.DrawLine(testVector, testVector + Vector3.up,Color.blue,23.0f); } else if (d > 0.45) { UnityEngine.Debug.DrawLine(testVector, testVector + Vector3.up,Color.red,23.0f); } else if (d > 0.40) { UnityEngine.Debug.DrawLine(testVector, testVector + Vector3.up, Color.green, 23.0f); }*/ } } } }