Search Unity

[RELEASED] DirectX 11 Grass Shader

Discussion in 'Assets and Asset Store' started by Nonakesh, Aug 17, 2015.

  1. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Oh, very nice. I've actually been looking for that command, but didn't find anything that actually worked with 5.3. I will probably still try it on Linux sooner or later, just to be sure, but that is really helpful.

    The compiler is throwing quite a few errors, but at least I have something to work with.
     
  2. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    First the good news: The new patch was just accepted by Unity, so if you're updating to Unity 5.3, you should be able to use the shader as usual.

    Ok, now the bad new is, I've tried out the GLCore mode and tried to fix the compilation errors. The thing is, I don't think they are from the shader itself, but from the cross-compiler to GLSL. The only real error I'm getting is that OpenGL doesn't allow swizzling on scalar expressions, which is something I don't usually do. Just to be save I still went through all of the shader code and didn't find anything. I also looked at the compiled GLSL code and there seemed to be quite a few weird things going on, like "_WorldSpaceCameraPos.xyzx.xyz", which is completely fine computationally, but there might be other things that could actually cause errors.

    I'd still like to think that the error is on the shader side after all, which would give me a way of fixing it, but without proper errors I don't know where to start looking.

    Does anybody know a way of getting the GLCore compiler to be more verbose? It would be enough to have the real error line numbers in the compiled code, because looking through 95000 lines of generated code doesn't sound pleasant to me.
     
  3. Kabendji

    Kabendji

    Joined:
    Feb 9, 2013
    Posts:
    24
    Thank for your answer and still a really great asset ;)
     
  4. JonDadley

    JonDadley

    Joined:
    Sep 2, 2013
    Posts:
    139
    Has anyone been able to fix the issue with SSAO not working correctly with the grass as mentioned earlier in the thread? Unfortunately Seith's reply went over my head (I don't know how to make changes to shaders). I'd *really love* to get ambient occlusion working with this grass as currently the artifacts really ruin the look.

    Also, thanks Nonakesh for fixing the 5.3 shader issue so quickly! That was really helpful :)

     
  5. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'm quite busy right now, so I won't be able to do it this week, but after that I'll look into the problem!
     
  6. JonDadley

    JonDadley

    Joined:
    Sep 2, 2013
    Posts:
    139
    Thank you so much! I really appreciate that :)
     
  7. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    This asset really looks fantastic.

    My only question:
    Does it work together with RTP?
     
  8. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Thanks!

    I haven't tested it, but I don't see a reason why it shouldn't. It's a alternative terrain shader, right? So there really shouldn't be any problems.
     
  9. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    nice :) exactly what I need :)
     
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    @JonDadley I just fixed the SSAO problem. Turns out it really wasn't something easy to fix, instead I had to include parts of the shader in Unity's Camera-DepthNormalTexture shader. I will include the patch in the next update, but that might take a while, I still want to do some other work on the weekend and add the change in the documentation.

    If you want the fix right now, just send me a PM with your invoice number and I will send you the files. (That's not only directed at JonDadley, if you have the same problem, please go ahead!)
     
    JonDadley likes this.
  11. JonDadley

    JonDadley

    Joined:
    Sep 2, 2013
    Posts:
    139
    Thanks for the hard work, I really appreciate it! The level of support you've given has been excellent! Sending you a PM now.
     
  12. ninjaboyjohn

    ninjaboyjohn

    Joined:
    Mar 30, 2014
    Posts:
    23
    What an awesome-looking grass asset!

    For my game, I would need to have the grass up vector to change based on the curvature (using polygon or vertex normals?) of the mesh, like in that sphere picture. Has that support been added to the asset?

    Will buy immediately if so.

    Thanks!
     
    Last edited: Dec 11, 2015
  13. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That should definitely be possible. There might be some problems with the wind calculations though and the grass displacement (walking through the grass) will not work, except you do a lot of custom work on it. But having grass on a curvature using the vertex normals should work.
     
  14. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    I like what I see but after reading the first page it sounds like I can't use this with unity terrain? What does that mean? In you video I see tons of grass on the terrain? Please explain. Also, I cannot get you web player install in IE 10
     
  15. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    As I wrote in the FAQ:
    The web player only works on certain browsers now, there's nothing I can do about that. The shader doesn't work in WebGL yet (as with OpenGL in general). Just use Firefox if you want to see the example (Chrome has the same problem, not sure about other browsers except Firefox)!

    If you have any other questions, I'll be happy to answer them!
     
  16. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    In you video I see tons of grass on the terrain, so it appears your using it on a terrain, right?
     
  17. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes, I used it in the trailer. At least in the first (and last) scene and the underwater scene.
     
  18. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    so why are you saying you cant use this shader on terrains? When you are.
     
  19. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I never did say that. Only that there can be some minor problems with it.
     
  20. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new patch with the following changes:
    • Add support for the Curved World asset.
    • Add a mode where the grass follows the surface normal instead of the up direction. It looks a lot better than I expected:
    • Add some features to the manual that weren't documented yet.
    • Add additional (very short) tutorials to the manual.
    • Fix SSAO and probably other Screen Space shaders, when using the grass shader.
    As always, if you want to use the new version before it is published on the asset store, just send me a PM with your invoice number and I'll send you the package!
     
    Lex4art, Lasseastrup3 and elbows like this.
  21. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The new update was accepted today, you should be able to download it in the asset store by now!
     
    julianr likes this.
  22. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    Do you think this will still work with DirectX12 when 5.4 launches?
     
  23. GandiniS

    GandiniS

    Joined:
    Sep 9, 2015
    Posts:
    3
    I downloaded the latest version (1.2.2) but cannot make it work. And the material settings are differ from the manual.
    Did I miss something ? Thanks!
    Clip.jpg
     
  24. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I don't see a reason why it wouldn't, but I'll download the beta and test it.

    Edit: I just tried it and it seems to work fine in 5.4
    I'm not sure if DirectX 12 is actually enabled, but the Shader should work with it.

    When you select the Grass.shader file, is there a error message in the inspector?
     
    Last edited: Jan 14, 2016
  25. GandiniS

    GandiniS

    Joined:
    Sep 9, 2015
    Posts:
    3
    Thanks Nonakesh, problem SOLVED after I selected Grass.shader and then "re-import".
    The 3 error message is "fail to open source file: 'GrassConfig.cginc'" even when I re-import the whole package.
     
  26. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Great it worked out that easily. If you have any other problems, just ask them here!
     
  27. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    The grass is acting funny when used with multiple lights.

    I'm using deferred rendering.

    But it seems that the grass is listening to whether or not the lights shining on it are set to "Important" or "Not Important". Everything else doesn't care, and just does deferred per pixel lighting. It's only the grass.

    This would mean only the grass rendering in Forward. Which is weird?

    Vertex lighting is also quite dark, so that's not really an option.

    How do I set the grass to use Deferred rendering? Forward is quite a performance tank, and while during day with one static sun that doesn't matter, at night it's either performance tanking lights on the grass, pure black grass, or no grass.

    Thanks!
     
  28. Lightning-Zordon

    Lightning-Zordon

    Joined:
    May 13, 2014
    Posts:
    47
    I would also like the grass to work in deferred mode.
     
  29. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That's a bit strange, I'm sure I have answered that question previously, but it seems like the forum ignored my post...

    I've tried to implement the grass to work in real deferred mode, but that turned out to be impossible without overwriting Unity's deferred shaders. It might still be impossible with overwriting them, but I don't think doing that would be a good idea anyways. The reason why it's not possible is, because the lighting calculations of the grass need a different normal for every light source, or the grass would simply look bad (As you probably know, in deferred mode the normals of a object are calculated once and will then be used for all light, not like in forward, where everything is recalculated for each light). I've tried to get around this, but without luck.

    The thing is, the main performance cost of the shader is generating the blades of grass, not so much drawing them on the screen, so deferred rendering wouldn't be as useful, except you want to have a lot of small lights of course.
     
  30. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    Is it possible to make a pseudo vertex lit mode?

    Like the entire thing is unlit, basically, except when a blade of grass is near a light source, that blade's color is a lerp between a dark value and a light value, based on how far away it is from a light.

    The unlit aspect should keep it performance light, while the the colors still look relatively okay.

    I'm not sure if it's possible to give every blade of grass a different color, though?
     
  31. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'll have to look into it, but I'm quite sure that should be possible. I have quite a few tests next week and I'll probably be rather busy after that as well, but I'll look into it as soon as I can! Until then you could just use the unlit mode altogether. Not the most beautiful thing ever (except you are going for a comic look, of course), but you'll be able to see the performance you can expect, more or less at least.
     
  32. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    Sweet! Sounds great.

    I'm not in a hurry, you can take the time you need.
    In the meantime I'll work with unlit (which still looks pretty good by day), and I'll just disable the grass altogether at night.
     
  33. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Hi there,

    I've been following your asset for a time and skimmed through the forum posts, but just want to verify my assumptions before putting any money down.

    I'm looking to use either RTP or this to paint splat textures onto my world mesh (not a terrain, a mesh). In previous posts you mention that this may be possible, since the grass shader is tied to a texture. Am I correct in this understanding?

    The idea is I would paint my splat texture onto my mesh and this asset would add grass only to the areas that have a grass texture painted.
     
    Last edited: Feb 15, 2016
  34. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    To be honest, I've never tried that asset, but if you can output a texture and set it to the _Density shader variable, it's definitely possible.

    One Grass material can have up to 4 different grass types, so if you want more, you'll have to duplicate your world mesh and apply another material. (Of course that will increase performance cost)

    The 4 grass types (per material) are controlled by the splat map in the _Density variable (as you already thought), so the RGBA channels each represent one of the grass types.
     
  35. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Ok, so I tried implementing a pseudo vertex lit mode, but unfortunately it wasn't only a real pain to get it to work at all (if I had to finish it, I'd basically have to reprogram the whole lighting, with quite a few hacks to get to some internal variables), it's also doesn't really reduce the performance cost.

    I was afraid this would be the case, but I still wanted to try it first. The problem is, the main performance cost is generating the grass, not lighting it. If it was possible to do everything in the geometry shader that would probably result in a performance cost reduction of 10-20%, but that's just not technically possible. At least not the way I programmed the shader, probably not possible in general. (Unity uses lookup textures in the lighting calculations and as far as I've seen, texture lookup isn't possible in the geometry shader)

    Now I don't want to tell you to just give up on the shader, so here's a quick idea how you could fix your problem: If you have only static/unmoving lights, you could create a pseudo-lightmap for the grass (and put it in the "Color Texture (RGB), Height(A)" shader variable). In short, use a color texture that is dark where there is no light and colored where there are lights. You might even be able to generate that lightmap automatically. I'm not sure if Unity supports exporting lightmap textures, but I know for a fact that Blender can save lighting information in a texture. If you have a lot of moving lights, there's no solution I can think of.

    I hope that helps you, even if it's not the solution you wished for.
     
  36. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    need more information about the displacement map option, it appears to use R and G channel as noted in inspector, but if I just use a purely red texture for testing, the grass is stretched all over the place. Great asset nonetheless.
     
  37. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Is there a way to just displace density in world space? I have non uniform triangles and it is necessary.

    With density all the way up and falloff all the way down, the boarder area of mesh is full, but within that is about half as dense.
     
    Last edited: Feb 21, 2016
  38. Lightning-Zordon

    Lightning-Zordon

    Joined:
    May 13, 2014
    Posts:
    47
    Having a problem with grass and rendering culling, and maybe some kinda falloff problem.

    I have made a video to show you the problems.
     
  39. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I think the easiest way of doing things is using the RenderTexture displacement.
    But if you really want to use the displacement texture, they work like this (as far as I remember, I can't look it up right now): 0 on a channel displaces in one direction, 128 is neutral and 255 is displacement in the other direction.

    The displacement textures are in world space. But as I said, if you don't have a reason not to, you should just use the newer RenderTexture displacement, it's a lot more flexible and easier to use.

    I've never had that problem before. Can you post your shader settings?
    Do you have the "Improve viewing from the top" setting enabled? I know that this can cause some popping when the camera is close to the ground.
    Do you have Occlusion Culling enabled? I've never tried out how the shader reacts to that...
     
  40. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249

    I followed the tutorial using the camera prefab and the mesh I am using is just rendering blue. I must be missing something. The layer is set up, assigned in both culling mask and in the mesh layer, I am using camera prefab, and using dynamic mesh normal shader. Is there something else I need to add to the mesh?

    Also, can I just parent the camera to the player and take out the smooth follow script?Or would player rotations mess things up?
     
    Last edited: Feb 22, 2016
  41. Lightning-Zordon

    Lightning-Zordon

    Joined:
    May 13, 2014
    Posts:
    47
    I do use both improve viewing from top and have occlusion culling enabled. I'll do some more tests with those. Another thing to mention is when I had this problem earlier I modified the meshes bounding box to be larger and that fixed the problem however that has stopped working for me for some reason.

     
  42. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    anyone else get render texture option working?
     
  43. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Ok, it is at least giving me grass. Something is still definitely off. Using pure white density. The grass is only speckled around and warped. Tried different render texture sizes, all the same.

    Aside from this issue, it seems like we should be able to just use the mesh with grass to also get normal information from as a displacement renderer too. Because I have to use 3 of each terrain mesh now. 1 for terrain, 1 for grass, 1 for rendertexture displacement. Seems a bit excessive, will hurt memory. I'll be happy enough to get this just working with render textures, just thought I would point that out aswell.

    EDIT: I pressed play and more normal looking grass appeared, looks the same as simple no density. Still using the vertices I think. EDIT2: Ok I lied, the render texture size does make a difference on play. I upped it to 8000 and now it is displaying using that, but still same as simple no density shader, it is thicker where there are more verts with a pure white grass density text, and it doesnt appear to obey a different density texture with black and white patterns. Even tried all black, yet still renders the same. See latest pic for uneven density.

    0.JPG 1.JPG 2.JPG 3.JPG hgff.JPG
     
    Last edited: Feb 24, 2016
  44. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Is it possible for me to change a script or shader so that I can have even more density?
     
  45. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    You can change the density falloff to be even lower, but you can't increase the maximum density. For that you'll have to change the base geometry.

    That sounds very wrong to me. Have you looked at the example scene? You don't need the third terrain for displacement!
    The rendertexture displacement works like this: the prefab has a camera that renders a specific layer from the top. Only the actual displacement info should be on that layer, not the grass or anything else. The camera will have a default value of no displacement. You can add objects with the special displacement shaders to that layer, I've written in the manual how those work. The grass will be displaced accordingly to those objects. You don't need to add the rendertexture to the material, that's done automatically in the background. (The displacement texture variable should only be used if you want to use the old displacement system)

    Just look at the example scene, it has a simple setup of the RenderTexture displacement.
     
  46. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Thanks for the help and info,

    Could you make it so that I can mass place by a global/world variable for x and y that it can be any value, and still use the mesh for y value? Think it would look uniform and more full this way for lower poly terrain meshes. And using a displacement texture with that would be great. Would prefer this over render texture. Possible?
     
  47. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'm sorry, but I don't think I understand what you want to do with the global variable.

    What are your reasons against the render textures? If it's performance, they are most likely more efficient than displacement textures.
     
  48. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    So that I can choose an amount to fill density evenly in world space without regard to vertex of mesh but still using y coordinates to figure out a good height. Currently maxing out density on lower poly terrain isn't dense enough.
     
  49. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Unfortunately that's not possible. The grass is generated directly from the mesh, or more specific by tessellating the original mesh. What you want could be done by using compute shaders, or just generating the grass on the CPU, but both of these will cost most performance (especially doing things on the CPU, that's probably impossible as all, not with these densities).
     
  50. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    I see. So does the tessellation levels slider allow denser grass by adding even more to terrain mesh or is that something for just the grass blade geometry?