# Grass Rendering Questions

This topic is 1254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all,

I've been working on grass rendering trying to achieve an effect much like Flower achieved.

I've got a basic setup working using points in the cells of a heightfield which are then extruded to grass blades in a geometry shader (I have the distribution set to 1 blade per cell while debugging). I'm running into a nasty hang up and wanted to ping the community for some ideas:

Right now I generate a one sided (although culling is disabled) triangle strip, this means I only have normals for 1 side of the strip. Ideally I'd like both sides to have normals so the blade is properly lit on both sides, but I see no way of doing that without either generating double the geometry or billboarding the grass blade (which I really don't want to do). Any thoughts?

Screenshot attached (note the blades are unlit currently and they need a random rotation applied since they currently look pretty nasty whenever you are perpendicular to them).

##### Share on other sites

Exactly what I needed! Thanks Krzysztof!

##### Share on other sites

Please post WIP images. I love grass fields!

##### Share on other sites

Alright, Unbird is trying to keep me honest and requested an update so here we go.

I've mainly been working on partitioning up the grass into various cells so I can enable a greater density without the framerate hit. Even in this very early stage it seems to be working (the red lines are the cell bounds):

[attachment=23108:moarGrass.png]

That's at 5 x 5 blades per 1 cell of the heightmap (although I have some doubts about what is actually being rendered which I'll get to in a moment). Previously that would slam my system down to 20 - 30 FPS but now it sits at 60FPS without any trouble. Success! Right now I have a 512M x 512M divided up in 8 x 8 chunks, obviously there is a balance there between doing visibility tests against each cell's bounds (as well as the overhead of more draw calls) but right now my system does that in another thread happily so I may play with how finely I divide up my grass cells.

I also turned on some very basic LOD to help smooth things out, Ideally I'd like to just toss the vertex away if the vertex is far away enough (and thus avoid the GS work), but I'm not sure how I could do that in the vertex shader. If anyone has any suggestions I'd appreciate it.

Here you can see the LOD zones (these are just really simply first passes, I probably need to get more aggressive):

[attachment=23109:lodDebug.png]

Red > Orange > Blue > Green

Finally I noticed an annoying issue where my HLSL random function doesn't seem to actually be generating random values. Hence the large amount of uniformity in all these screenshots. You can see here where there's some small random being introduced, but then directly next to those blades sit a bunch of uniform blades:

[attachment=23110:grassRandom.png]

I'm currently using the very basic GLSL random (in HLSL):

float GetRandomValue(float2 uv)
{
// Internet magic numbers follow...
float2 noise = (frac(sin(dot(uv, float2(12.9898, 78.233)*2.0)) * 43758.5453));
return abs(noise.x + noise.y) * 0.5;
}


In my shader I have a table of prime numbers which I just multiple the root vertex's UV coords by, using the GS Instance as the key into the prime numbers table:

// IN = Root position vertex passed from the Vertex Shader
// primeTable = Array of prime numbers.
// instanceId = SV_GSInstanceID

float random = GetRandomValue(IN.Uv * (float)(primeTable[instanceId]);


My thoughts are:

• Not sure how well this works if you have UV (0.0, 0.0).
• It probably just makes more sense to populate a texture coordinate with some random numbers rather than key off the UVs of the root vertex.
• I'm curious if this is causing some blades to stack on top of each other so I'm not getting the full effect currently.

Anyway, that's the latest. Comments / Thoughts / Suggestions always welcomed.

##### Share on other sites
Thanks for the update

Procedural randomness (or rather randomness on the fly) sounds attractive, but I found it tricky get right. Since you can't use a PRNG with state you end up using a hash of some sort. This looks like one of those typical float based hashes (some transcendental functions and magic numbers). They're cheap but I found that they can expose a pattern. Integer based ones look better but aren't that cheap. I also wonder if you can get e.g. blue noise or something this way (cheaply, that is). So yeah, a lookup (vertex stream or texture) sounds reasonable. One can of course combine the two approaches.

Not sure how well this works if you have UV (0.0, 0.0).

Currently, you just multiply, so all instances get the same random feed. You have to experiment here, e.g. use a offset:

float random = GetRandomValue(IN.Uv + primeTable[instanceId].xx);
And I wonder why primes. Also check the dimensions. You could provide a random (float2) offset table.

I'm curious if this is causing some blades to stack on top of each other so I'm not getting the full effect currently.

Depends on how you apply the randomness. If you don't offset the position more than half a blade's cell size, they should never intersect.

##### Share on other sites

Thanks for the update

Being on the hook helped me knock out quite a bit yesterday, so thanks for proding me.

Procedural randomness (or rather randomness on the fly) sounds attractive, but I found it tricky get right. Since you can't use a PRNG with state you end up using a hash of some sort. This looks like one of those typical float based hashes (some transcendental functions and magic numbers). They're cheap but I found that they can expose a pattern. Integer based ones look better but aren't that cheap. I also wonder if you can get e.g. blue noise or something this way (cheaply, that is). So yeah, a lookup (vertex stream or texture) sounds reasonable. One can of course combine the two approaches.

Yea, I'm finding this to be very true. My plan is to overhaul how I'm currently doing random with the shader. Doing something like white/blue noise during mesh construction wouldn't be the end of the world. The problem being is that each blade generated needs its own "Randomness", this means up to 32 random values need to be generated per root vertex (one for each possible blade). A better solution would might be to use some noise texture, maybe randomly rotate it based on some initial seed value, and then just have each blade sample that. But I'm just thinking out loud here and will need to experiment.

And I wonder why primes. Also check the dimensions. You could provide a random (float2) offset table.

Using primes was just an attempt to alter the hash enough per blade to get unique values - obviously didn't work too well. :)

Depends on how you apply the randomness. If you don't offset the position more than half a blade's cell size, they should never intersect.

Correct, I believe I'm currently doing that but I saw some blades intersected but that could be just bad math in how I apply the randomness. I'll need to verify.

Thanks for all the feedback / suggestions.

##### Share on other sites

This topic is 1254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.