- Home /
Generating Chunks of Ore Using Perlin Noise
Hi all,
I have a script which generates ore around a cave system based on Perlin noise values:
// Spawn tiles for (float x = 0; x < width; x++) { for (float y = 0; y < height; y++) { int xx = Mathf.RoundToInt(x); //Int required for map[] check, float required for noise int yy = Mathf.RoundToInt(y);
if (map[xx, yy] == 1) // If map index is a filled tile (not empty);
{
GameObject selectedTile = null;
int seed = UnityEngine.Random.Range(0, 9999);
float oreNoise = Mathf.PerlinNoise((x / frequency) + seed, (y / frequency) + seed);
if (oreNoise > 0.95)
{
selectedTile = diamondTile;
}
else if (oreNoise > 0.90)
{
selectedTile = goldTile;
}
else if (oreNoise > 0.87)
{
selectedTile = ironTile;
}
else if (oreNoise > 0.80)
{
selectedTile = coalTile;
}
else
{
selectedTile = stoneTile;
}
GameObject newTile;
newTile = Instantiate(selectedTile, new Vector2(x, y), Quaternion.identity);
newTile.transform.SetParent(gameObject.transform, false);
}
}
}
With the focus being on:
float oreNoise = Mathf.PerlinNoise((x / frequency) + seed, (y / frequency) + seed);
Currently this script will generate single-ore veins around the map:
My question is: how do I adjust this script so the ore spawns in chunks (as seen in Terraria for example)? I understand this would mean I would need bigger patches of higher value perlin noise, however after researching further into Perlin noise, I am still unsure how to generate noise which appears more so in 'patches'.
I have messed around with different frequencies, multiplied by different scales, added different octaves, of which none seem to add the 'chunkier' effect I am looking for.
Any suggestions/explanations would be greatly appreciated.
Answer by MixGrey · Jan 01, 2019 at 10:57 PM
You're choosing a new seed for the Perlin noise inside the loop, every time you get a value from it. What this means is that you're basically creating an entirely new Perlin noise "image" for every tile, instead of creating one Perlin noise image for the whole map and then having each tile sample from that one noise image.
You need to choose a seed outside of the loop, then use the same seed each time you call the Perlin noise function. Once you do that, adjusting the frequency should do what you expect it to do.
Once you fix that, you will encounter a second issue: the conditional branches you've set up for selecting the type of ore will tend to cause diamond to be surrounded by gold, and gold will tend to be surrounded by iron. This is because the Perlin noise function generates something like a heightmap, and you're using peaks for diamond and areas slightly below peaks for gold, etc.
The way most games do this is they use a different Perlin noise seed for each type of ore. The peaks of the diamond Perlin noise can spawn diamonds. The peaks of the gold Perlin noise can spawn gold, etc.
Hope this helps!
Also just a little tip when working with noise, maybe consider checking out fastnoise (you can find it on github) its alot faster than the default PerlinNoise method.
Answer by DownER149 · Jan 02, 2019 at 12:18 AM
newTile = Instantiate(selectedTile, new Vector2(x, y), Quaternion.identity); put a component on the script with something like on start raycast rect( or use the the collider already on the tile) for each object in the raycast array (assumeing there is already dirt nearby with colliders) give it say a 50% chance to turn the tile next to it into the same type of ore so the first tile will hit 8 so four of thoes will turn into ore, then maybe decrease the chance each time the tile branchs out so maybe the next tiles wich will be less than 4 have a 25% then again 5% this will cause each ore to kind of bloom from its center spawn point
Answer by AliAnkara · Feb 23, 2019 at 01:28 PM
see you guys are WebGL experts. I couldn't run the Cookies. I don't know how it works :(
I need an example on this. Is it possible to send me sample code?
Unity 3D 2018.2.5f1.... WebGL
Thank you from now... Ali