Foliage Shader: Need Advice

I’m working on a project which involves writing custom foliage shaders. Currently, I’m working on just grass as everything I want to implement would be present there and other shaders can be more simplified versions of this one, but I’m hitting a road block that seems to have hit everyone else, except for this guy:

but he doesn’t explain how its done.

So right now, I’m trying to do an alpha cutout surface shader that takes shadows from the world (realtime would be wonderful but I’d settle for baked), and uses the standard shader model. I’m also doing animation in the vertex shader to simulate wind. This is just the way I’ve figured out to do it, so if there is an easier way to get the same effect I would LOVE to know. (I do want there to be a lot of grass in my world so I figured doing as much on the gpu as possible would be a good idea)

Anyways short of having an alternative way, or
TLDR; I’d like to find a way of doing an alpha masked shader that has shadows on it. Everything else (vertex animation, the actual alpha masking, standard shader etc.) all works. Its literally just the shadows now.

Have any of you implemented custom grass before? How did you do it? (I know there are things on the asset store, but part of this project is to learn how to do this stuff by hand.)

Thanks!
Peter

Ok, I don’t want to edit/delete this as I’m still interested in other solutions other people have used for grass, BUT, basically as soon as I wrote this post, I found another that has exactly what I was looking for. Here’s the link: Unity Grass Shader with Wind Sway

For two sided grass, you can just add this line at the end of the vertex shader:
v.normal = dot(WorldSpaceViewDir(v.vertex), mul(_Object2World, float4(v.normal, 0.0)).xyz) > 0 ? v.normal : -v.normal;

So this solution works for me and I’m good if nobody responds but if someone has other solutions, I’d love to hear!

I believe you could also use the VFACE semantic here (floating-point scalar that indicates a back-facing primitive), which might run a little faster.

See here for an example, search for VFACE:
http://docs.unity3d.com/Manual/SL-ShaderSemantics.html

1 Like