• Create Account

## Grass Rendering Questions

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

406
Like
0Likes
Like

Posted 07 July 2014 - 12:54 PM

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).

### #2Krzysztof Narkowicz  Members

2156
Like
11Likes
Like

Posted 07 July 2014 - 02:41 PM

POPULAR

You can use pixel shader input VFACE / SV_IsFrontFace / gl_FrontFacing to check if current pixel belongs to a front facing or to a back facing triangle. It it's back facing then just flip normal and shade as usually.

blog | twitter | "Don't have any friends? Still a virgin? Programming is for you!"

406
Like
0Likes
Like

Posted 07 July 2014 - 03:03 PM

Exactly what I needed! Thanks Krzysztof!

### #4imoogiBG  Members

2910
Like
3Likes
Like

Posted 08 July 2014 - 01:07 AM

Please post WIP images. I love grass fields!

406
Like
5Likes
Like

Posted 14 July 2014 - 08:45 PM

POPULAR

Please post WIP images. I love grass fields!

Ha. Sure. Here's the latest.

You can see the lighting is still busted(haven't had time to really dig into it yet), but there are many more blades of grass. I had based most my work so far on the white paper that Lee did, but I've slowly started to drift away from some of the elements he uses. For example he populates his world with a vertex per grass blade (which means he needs N verts for N blades of grass). Right now I have 1 vert per heightmap cell and I use geometry shader instancing to populate that cell with 1 - 32 blades of grass. The attached picture is a 3x3 grid of grass blades, so 9 GS instances, per cell (with some small position offset randomization within the cell).

My hope is that massive parallel pipelines on today's cards can happily chew through the added work in the GS instance, so far it seems to be working. The framerate in the image is running in debug build, with no LOD on the grass (I have it setup, just not turned on yet), no visibility culling (beyond what the HW provides), and using a completely unoptimized shader. Turning the image to max instances(i.e. 32 blades of grass per cell) drops the FPS to 30 - 40s (which isn't bad and I suspect is more a fill rate issue than the actual number of verts but that's just a wild guess).

I'm sure I'll come across some massive flaw in my current approach, but for now it's working pretty well.

#### Attached Thumbnails

406
Like
1Likes
Like

Posted 10 August 2014 - 09:18 PM

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):

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):

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:

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.

### #7Pottuvoi  Members

268
Like
3Likes
Like

Posted 11 August 2014 - 05:07 AM

There was couple of nice blog posts about blue noise that might interest you.
http://mollyrocket.com/casey/stream_0014.html
http://mollyrocket.com/casey/stream_0015.html

### #8unbird  Members

8298
Like
2Likes
Like

Posted 11 August 2014 - 09:56 AM

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.

406
Like
0Likes
Like

Posted 11 August 2014 - 10:39 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.