🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Spawning from density maps

Started by
2 comments, last by emanuel8 1 year, 3 months ago

My Situation

I'm currently building a library for grass rendering and want to build a small tool for creating the grass.

Instead of saving each grass blades positions I plan on abstracting each chunk as an aabb box with a density map and height map.

With the density map I want to save some density parameter (how dense the grass in a dense area should be).

The Problem

Now I'm trying to generate positions of the grass blades from the density map, with a density factor on the aabb plane and I'm hopelessly lost on how to do so.

Do you even think my system is feasible?

Does anyone have resources for comparable system or ideas on how to implement this?

Advertisement

I use a similar system for spawning the positions of boulders in my erosion simulator. I calculate a boulder density map (which depends on slope, etc.), and then use this density to spawn boulders when creating a terrain tile at a particular level of detail. Bigger boulders get spawned at less detailed LOD, and smaller boulders at more detailed LOD. As you approach a tile, the LOD gets increased, which causes more smaller boulders to be spawned (in addition to bigger boulders spawned at lower LOD). A boulder of a given size is spawned only when it is close to the size of one heightmap pixel (and that depends on the LOD subdivision).

The actual spawning works by generating a second image with a deterministic random number for every heightmap pixel, and then checking to see if that pixel value is less than the density. The density needs to be scaled to approximately [0,1] range so that it can be interpreted as a probability. Then, you spawn an object if the random number in range [0,1] is less than density. The random numbers need to be generated deterministically, so that for a given terrain tile and LOD you always get the same spawning. I use a hash of the terrain tile integer (x,y) position and the subdivision depth to initialize the random seed.

To determine additional random attributes of each boulder (e.g. scale, sub-pixel position offset, height relative to surface, rotation), you can either just generate more random numbers in a similar deterministic way (random values are made different by XOR-ing seed with a different constant), or hash the original random number that was used to spawn the boulder at the pixel where it is located. In my case, the second option is more efficient because it doesn't require generating a whole tile of random values for each additional object attribute (this is not a win because the spawning rate is low).

If the number of objects to spawn is very low, it might be more efficient to generate a random (x,y) position in the terrain tile, then generate a random value in [0,1] to check against the spawning density value at that pixel. This can be repeated until you have the desired number of objects per tile. This doesn't work for me because of needing overlap between terrain tiles to be consistent, so I have to RNG in a different way so that adjacent tiles match up.

@Aressera This seems like a great approach, thanks for the write up! ?

Since my density map and height map are very tiny compared to the area they should cover I'll try to adapt some heavy linear interpolation but shouldn't be much of a problem.

This topic is closed to new replies.

Advertisement