I'd like to add caves to my world. But how do i do that?
It's really easy to do.
But first, let's add blocks under our Grass Layer.
- Go to your "Chunk" script, where you created your Grass layer, in the "GenerateVirtualMap" method :
We used a line which checks if Y was the height value generated from the noise. Now we will check if Y is under this layer :
We asked if we were at the height value. Now this line is "If we are under this value". Everything under will be Dirt.
And everything else (above Grass) will be air.
- Test your game in Unity :
That's how our world looks like now. Filled with dirt.
But don't you want another layer made of stone, which generates differently than dirt and grass?
- Go to the "Noise" script. We will add a new generation method for the Stone layer :
It will calculate the height of the stone blocks. Feel free to play with it's value because for now, all it does is place stone at 5 blocks under the Grass layer.
- Now go back to the Chunk script and let's generate our stone :
Before adding the Dirt Layer, we made the Stone layer. There is a order to follow in the layers which is down to up. Otherwise one layer will replace the another one.
Before testing, i changed the Y offset to 2*World.chunkSize to see more stone.
- Now test your game in Unity :
We have our Stone layer. We can add caves to it now!
- Go back to the Noise script :
We need a new FBM method. It will be a 3D FBM to move in each axis and make air where it goes.
Here, we generate noise values for every direction. Then, to have the final value we add them together and divide by 6 to have it between 0 and 1.
We give positions, a smooth value and the number of octaves.
- Now let's add caves to the code :
After we generated our layers, we can dig caves. Here, we use the method we created and use our 3d perlin noise. It will replace blocks with air.
You can play with the smooth value and octaves.
"< 0.42" is the probability of caves we have in the world.
- Let's use it to generate ores :
We will put it with the stone, because we are placing ores inside the stone. If you want some ores to spawn out of the stone, you can add it after the cave digging line.
Same as the caves, we have smooth, octaves and probability.
After it added ores, it will place stone.
Also, after the probability, there is "&& worldY - offset < 10". This is the max height where the ore can spawn. It will move up or down with the offset.
- Now you can test your game to see how it looks like :
It looks really good! But there is something else we can add to it : A bedrock layer!
- After we created every layers, made our ores and caves, we can add it :
We put the layer to the bottom of the world but still follow the offset to move with it.
And it generates the bedrock layer :
Now another thing we can do is optimize the code.
- You now know how it works, we can try to make it smaller by using only one line :
- Finally, why don't' we add a random seed to the world?
Go to the Noise script :
In the variables, we will generate a random seed.
It gives an offset to the perlin noise. So each time we generate from a position in the noise, it will be added to the offset.
- Then add it to each of the height generation methods :
- Now if you test your game, every generation will be different :