- Home /
Minecraft-like ore generation
The project is a simple voxel world made with perlin noise for the y making something similar to a minecraft chunk
//Function that creates the terrain
void CreateTerrain(int xRange, int yRange, int zRange)
{
//If random seed is checked, choose a random seed between negative max seed and positive max seed
if (randomSeed)
{
seed = Random.Range(-maxSeed, maxSeed);
}
//Nested for loops to create the terrain
for (int x = 0; x < xRange; x++)
{
for (int z = 0; z < zRange; z++)
{
//perlinY is used to make a wave-like terrain
float perlinY = (int)( Mathf.PerlinNoise(seed + x / detail, seed + z / detail) * scale );
//We add the ground level to the perlinY so it generates on top of all of the stone and dirt
if (groundLevel >= 0)
{
perlinY += groundLevel;
}
GameObject grass;
//Creates the grass cubes on the x, y, and z
grass = Instantiate(cubeTypes[0], new Vector3(x,perlinY,z), Quaternion.identity);
grass.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
//Creates the player at the middle of the terrain
if((x == xCubes/2 && z == zCubes / 2) && !created)
{
GameObject cam = Instantiate(player, new Vector3(x, perlinY + 1, z), Quaternion.identity);
created = true;
}
//Creates stone and dirt up from 0 to the y of each cube
if (groundLevel >= 0)
{
for (int y = 0; y < perlinY; y++)
{
//Used to randomize where dirt appears
int dirtLayers = Random.Range(1, 5);
//This is used to optimize the game, making it so it only draws the outside walls
if ((x == xRange - 1 || x == 0) || (z == zRange - 1 || z == 0))
{
//This is used to make dirt only appear past a certain y
if (y >= perlinY - dirtLayers)
{
//Creates the dirt cube
GameObject dirt = Instantiate(cubeTypes[1], new Vector3(x, y, z), Quaternion.identity);
//Parents the dirt cube to the terrain game object
dirt.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
}
else
{
//This is used to optimize the game, making it so it only draws the outside walls
if ((x == xRange - 1 || x == 0) || (z == zRange - 1 || z == 0))
{
//Creates the stone cube
GameObject stone = Instantiate(cubeTypes[2], new Vector3(x, y, z), Quaternion.identity);
//Parents the stone cube to the terrain game object
stone.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
}
}
}
}
}
}
}
}
What I want to add now is some ore generation, but instead of just doing it with random numbers (which works fine, but is too simple) I would like it to be based on the seed, so the same seed results in the same world. That's where I'm stuck, I've tried perlin noise but it just does some weird things. If anyone knows how to do it please help, I'm absolutely lost. Thanks.
Is it possible to make such a world generator and if so, can I get some pointers to how it can be done and what topics to research? If somebody wrote a tutorial about doing that it would be perfect UPSers website. I hope this isn't a duplicate question because I am specifically interested in generating each block completely independent from its surroundings.
There is a tutorial on this exact subject: https://youtu.be/chbIgz5H0lA
There are plenty of other tutorials and videos of you Google "voxel procedural world"
Answer by lgarczyn · Oct 04, 2019 at 09:12 PM
Since the other answer avoided the question, you can easily set the seed of the Random class. Random would have predictable results.
That taken care of, I wouldn't suggest using Mathf.PerlinNoise, as it doesn't allow real seeds, and only two dimensions.
The seed here mostly randomizes your starting location, not the actual world.
There are C# libraries for 3d noise generators, fractal noise, and well as the C# random class which allows you to have multiple generators and seeds.
Now as for a better answer, you could also do something like
float perlinOre = Mathf.PerlinNoise(seed + y / oreDetail, seed + oreFlatness * Mathf.PerlinNoise(seed + z / detail, seed + x / oreDetail) / oreDetail);
if (perlinOre > oreThreshold)
//blablabla
If possible, you should use a different seed for ores. Maybe use a string as your seed, and do this
int seed = inputString.GetHashCode();
int seedTerrain = seed & 0xFFFF;
int seedOre = seed >> 16;
Thanks a lot for the answer, but I'm really a noob, and can't quite get what this would do, forgive me, probably should have said it from the begging.
Can you be more precise about what you don't understand? The last part is simply a way of storing two 32bit ints into a 64bit long. It would allow you to have a single large number as a seed, and using it for multiple different seeds.
The first part I do understand, but the first line of the second code I don't. What I'm guessing it's doing is shifting it, as the second line is doing, but I'm not sure. Thanks.
Your answer
Follow this Question
Related Questions
Perlin noise for (sort of Minecraft) terrain generator 2 Answers
Anyone got a Voxel generator I can use? 0 Answers
Cube style terrain 1 Answer
How to load stacking chunks on the fly? 1 Answer
Add perlin noise to blocky terrain 0 Answers