Hardware instance on-demand entities?

I attempted Hardware Instancing by implementing grass foliage for the map.

How can I ensure the entire terrain plane is covered in grass foliage? I’m using the combination of a vertex shader / hardware instancing. The shader is applied to each plane spawned via hardware instancing to create the moving grass effect.

Also, what is the best way to go about placing on-demand entries spawned by players? The context of the game is a sandbox, there shouldn’t be a performance hit considering the amount of entities placed.

Hi @khaelou,

Usually you need to implement a point sampler, a way to get valid points, on the terrain surface. How are you placing your grass points in the hardware instancing buffer right now?

Similarly to place on demand instances you can grow your instancing buffer, and add additional points on runtime.


I managed to get a performant result that covers the entire plane after making changes to existing code.
Yet I’m still having trouble on how to calculate positions for non-flat terrain surfaces.

The problem now is that I don’t know if I should be doing this through the shader or mesh instance itself, what’s the best way to go about this?

Both ways are viable:

  • You could render a top down depth map of your terrain surface, pass it to the grass material shader and calculate the corresponding Y elevation for each position in the vertex shader.

  • You could use Ammo.js raycasts to sample positions on the terrain collider and get the required Y elevation. In my experience I found out that Ammo.js raycasts are very fast and you can sample thousand of positions without long wait times. But if that becomes a problem, another solution is to load Ammo.js in a web worker and do the sampling there.