Blockchain Terrain Generation #7 - Trees

Without trees, the world seemed quite dead. I want to add trees to my world but they must meet these criteria:

  1. They can't spawn too close together

  2. They must be based on a seed so the same pattern can be reproduced

  3. The leaves must be semi-transparent

Poisson-disc Sampling Algorithm

I started researching ways of naturally separating the trees from one another to make sure they do not spawn too closely together, and so they look like trees you would find in nature.

I came across a forum post that referenced this video about an algorithm called "Poisson Disc Sampling". He thoroughly explains how this is done and it turns out to be a great solution to my problem.

I further researched this and found a video by Sebastian Lague where he does this in Unity.

Seed-based Noise Generation

The trees should not populate the entire map but should be random. Some "seeds" should have more trees in them, and others should be more sparse. To do this, I used my method of laying noise maps to create something that I think looks like the simple map of a real forest.

Adjusting my noise visualiser, I added a feature to visualise the layered treemap. The white areas represent where trees are able to spawn.

I use created 3 layers of this noise map:

  • A base layer, which had a very small scale, which makes a large variation, making some seeds have many trees, and others have very few.

  • The secondary layer is medium-scaled which makes holes in forests which makes it look more natural.

  • And the final layer is scaled much higher to make the pattern truly random and much more natural-looking and allows lone trees to spawn instead them always being grouped

Generating the Trees

The trees themselves have to look as natural as possible. After using the Poisson-disc algorithm, it finds where to create a tree, and starts building a random amount of logs based on a Perlin noise value adjusted by the seed. After the third log is generated, it starts generating leaves in a 4x4 grid around the log. Using 3D Perlin noise adjusted by the seed, it randomly dismisses leaves spawned on the corners which makes the tree look more natural.

I used photoshop to make the leaf texture and I made certain bits around each leaf transparent which made the trees look very nice in my project.

I adjusted the face creation code to count the leaf block as transparent so it would render the other side of it. Otherwise, you would not be able to see the wood in the middle and it did not look as nice without this change.

7 views0 comments