Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[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
    For generating a terrain procedurally? I think it's fairly easy to do that without any asset at all. Just use the Mesh class, it's quite straight forward to use. Having LOD levels could be a bit tricky, but those aren't great in combination with the grass shader anyways.
     
  2. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I know how to generate terrain procedurally, but I have no idea how i'd generate terrain as rich and featured as, say, the output of World Machine. See the Map Magic asset I am currently using.

    I'm intending to use this grass shader with the help of Mesh Materializer processing terrain generated by Map Magic at runtime currently. I think I have the process figured out but I thought I might share the idea with anyone running into possible problems here
     
    Nonakesh likes this.
  3. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I suppose I should add, would it be possible to control grass density based on the surface normal? So grass wouldn't grow on downwards facing surfaces or sheer slopes. This would make it very useful for procedurally generated voxel terrain.
     
  4. Blank blank

    Blank blank

    Joined:
    Dec 1, 2014
    Posts:
    22
    Has anyone tried using the geometry blend feature of RTP? Just change the shader to the grass shader and paint the mesh onto your terrain.

    There's no problem with that I hope?
     
  5. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That's not possible, but I plan to add controlling the density via vertex color in the near future, so you could run a postprocessor that calculates density from vertex normal!

    I've never used that, so either you try it, or you could ask their support.
     
  6. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Haha um yes that's what i mean erm.. that would be great cheers!

    (Well after a look apparently you can get face normals with:

    Code (csharp):
    1.  normalize(cross(ddx(worldPos.xyz),ddy(worldPos.xyz)))
    but yes vertex normals would be a bunch faster)
     
    Last edited: Mar 29, 2016
  7. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new version to the asset store, it will probably take about a week until it is available from the asset store, but if you want the version right now, just send me a private message with your invoice number and I'll send it to you.

    Here are the changes for version 1.3.0:
    • Adds a new burn feature that can be used to remove grass and add burn effects.
    • Displacement of multiple displacers can now be blended, so you could have several trails behind characters cross each other, or add wind effects with the displacement system.
    • Add a way of setting the influence strength of displacers (Important with the new blending).
    • Add support for setting grass density with vertex normals. (@lazygunn, @Enoch and @GoGoGadget, I finally got around implementing it!)
    Here's a screenshot of the burn feature, it's a bit simple, but I'm sure with a bit of artistic work, it could lead to some great results. (Particle effects would be a nice start!):

     
    Last edited: Apr 5, 2016
  8. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The new patch is now on the asset store! If you have any questions about it, or found a error, just post here, or write me a private message!
     
  9. kzaurckichz

    kzaurckichz

    Joined:
    Nov 5, 2013
    Posts:
    52
    Updating to this version broke the whole thing...
    Looks like Object Space is not working anymore
    Also, for some reason it broke other unrelated materials in the scene, which are now pink.

    Edit: Here's the error I see in the console :

    Shader error in 'Stix Games/Grass': Vertex program 'vert': unknown input semantics TEXCOORD/4
    (on d3d11)

    Compiling Vertex program with SHADOWS_DEPTH GRASS_OBJECT_MODE GRASS_WIDTH_SMOOTHING SIMPLE_GRASS
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_TEXTURE_ALPHASPLIT_ALLOWED
     
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Sounds wonderful... can you post the error message that is shown when you're selecting the .shader file?
     
  11. kzaurckichz

    kzaurckichz

    Joined:
    Nov 5, 2013
    Posts:
    52
    I edited my post to add the error message.
     
  12. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'll look into the problem tomorrow. Just a warning: it could be that your GPU doesn't support the necessary features, so I'd have to make an option to disable vertex color based densities. It could also be another problem though.
     
  13. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    The forward slash in the TEXCOORD4 semantic looks odd to me. Not having tested myself
     
  14. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Is that actually in the code? I thought that was some bug in the error message... if it is in the shader, that's definitely wrong!
     
  15. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    Seems like in GrassDefinitionsAndFunctions.cginc, struct appdata is assigning objectSpacePos to TEXCOORD4, which doesn't exist according to this:http://docs.unity3d.com/Manual/SL-VertexProgramInputs.html

    I changed it to TANGENT (the only other space not used here...) and it seems to compile.
     
  16. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    It definitely worked when I tested it, but I guess there might be some hardware differences there. I'll upload a patch with exactly that change, so thanks for the help!
     
  17. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    Terrain.GenerateBasemap is generating absolutely enormous performance issues for me. I used Gaia to setup my terrain, but other than that there is absolutely nothing special about it.

    And we're talking like 500-600ms per-frame.
     
  18. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    As I've said to @KyleOlsen, I think that's a problem with Unity terrain and not with the shader. If possible you should try to convert the terrain into a mesh. @lazygunn mentioned using Mesh Materializer for that, but I haven't tried that myself.

    If you find out what's causing the problem, please tell me so I can update the FAQ and the manual!
     
  19. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    It kind of works around it, but I wouldn't really consider it a solution. I just uninstalled the grass shader for the time being.

    It's a shame, it looks super ace.
     
  20. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    We've been using the shader and it works well as long as there is no displacement. I have two questions..

    1) (With both "Follow surface normal" and "Object space mode" turned on) It seems like when you put on a displacement texture and rotate the plane that the grass is on, it doesn't follow the surface normal anymore -- the grass will move as you rotate the plane.

    2) There are some fixes I found (credits to the Unity folks) to get it running on PS4 -- would you like to know of them in general so you can patch your shader?

    Thanks a lot for your time! It's been a great shader so far -- we're just tweaking it to make it work on our meshes.
     
  21. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'll look into that, I've never tested those 3 combined, so I could've easily missed something.

    That sounds almost too good to be true! I'd really appreciate it!
     
  22. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    For compilation on PS4, there are only 2 things that needs to be fixed:

    1) domain shader cannot be named "domain", so changing it to something like "doma" or "domainShader" will work.
    2) inout somehow isn't working. This only affects GrassVertex.cginc -- just do "appdata vert(appdata v)", and create another appdata for returning in code should get this working.
     
  23. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    As for the displacement (in object space and follow surface normal mode) -- it seems like mostly because of object/world space mismatch. The calculations looks to be based on world coords (especially the direct manipulation of .xz and .y), and I am able to locally get it working if I get everything in object space (assuming texture input to be in local coords).

    Of course, then, the assumption that texture input is in local coords breaks down once you do dynamic displacement. I think perhaps the two displacement cases need to be dealt with separately.
     
  24. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Thanks for the info, I'll definitely make a patch for that!

    I thought it was something like that. The dynamic displacement really isn't made for object space displacement, but I'll fix the displacement on the texture based one.
     
  25. Dolzen

    Dolzen

    Joined:
    Mar 26, 2014
    Posts:
    90
    Hello!

    I have SSAO enabled in my main camera and it makes the grass shader to look like this:



    is there a way to disable SSAO in the grass shader?

    Thanks!
     
  26. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I don't think there's a way to deactivate it for the grass alone. That's up to the SSAO implementation. You should try to play around with its settings, maybe there's a way to improve the visuals.
     
  27. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    Hello!

    I've been tweaking the shader and it's been looking great, but now I stumbled onto some optimization problem. The mesh itself is processed at least 4-5 times in the render pipeline -- for shadows generation and actual rendering. It's turning out to take a big chunk of time because of that, and now I'm wondering if there's a way to cache the mesh per frame -- it's not really changing much apart from the influences from the wind and the displacement.

    Any ideas on that? Or perhaps there are other parameters I can tweak to reduce the number of passes?
     
  28. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That's kind of how shaders work. They process the whole thing every frame and multiple times at that. The only thing that is constant is the base mesh you put the shader on. That's actually why it's so fast! If you would be doing the wind processing on the CPU is would be extremely slow because of the insane amount of polygons.

    The easiest way to reduce passes, is deactivating shadow casting and reducing the lights that can affect the grass. And that's probably the best way of improving performance for this shader in general.
     
  29. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    I was mostly wondering if compute shaders would be a possibility -- perhaps the read/write would be slow but in general it could be a win if I need shadow casting and lighting.
     
  30. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Another guy had a whole system using compute shaders and it turns out that that was way slower than using tessellation alone. Also keeping all the vertices of the grass would use up a lot of memory.
     
  31. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    Thanks for the reply! I guess that's not unexpected (although it's so hard to say for compute shaders, you can spend forever tweaking them to get good performance!)

    I'm going down the path of doing in-depth optimization on the geom shader, as that seems to be the only way for us to use it now.

    Thanks again!
     
  32. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    If you get any good results, I'd really appreciate if you told me about them! I've tried optimizing the geometry shader, but the improvements were minimal, the real performance killer always seemed to be the fixed pipeline part of the geometry shaders (actually starting the geometry shader and generating the mesh), not the shaders themselves.

    The only thing I can think of, that noticeably improved performance was removing the wind calculations. You could do those outside of the shader, with the displacement system. Since the new patch, where I included weighting displacements, that should be entirely possible and I think that this way could be faster then doing it directly in the shader. And of course you'd get the advantage of having way more control over the wind.
     
  33. alice_funo

    alice_funo

    Joined:
    Mar 9, 2016
    Posts:
    14
    Yes! I actually have some successes (I'm optimizing for the PS4 as well), by using texture atlas, optimizing out branches, etc. We also decided the grass doesn't cast shadows to avoid the extra geom shader calculations (each geom shader invocation is pretty expensive).

    I'm going to PM you some of the optimizations and features we added.
     
  34. nubdev

    nubdev

    Joined:
    Mar 30, 2013
    Posts:
    87
    I am getting pink shaders with non working scenes when importing into a current project, using a fresh project has it working though... any help is appreciated!
     
  35. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    When you are selecting the Grass.shader file, is there a error message in the inspector?
     
  36. nubdev

    nubdev

    Joined:
    Mar 30, 2013
    Posts:
    87
    there was one about "compiled shader code uses too many instruction slots" but it was from an image effect file, deleted it and its gone.. though there are no other errors and everything is still pink
     
  37. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Have you tried reimporting the shader? Or if that doesn't work, deleting it and importing it again?
     
  38. nubdev

    nubdev

    Joined:
    Mar 30, 2013
    Posts:
    87
    numerous times now, yes. The only shader that seems to show any error is the Grass one, says it is not supported on this GPU... though it works in a fresh project

    The affected project is running PlayWay water, which is the only other thing I could think might be conflicting
     
  39. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The only thing I can think of, is that all shader keywords are used up. I have a own section about that in the manual, can you try to reduce them?

    If that's still not the cause, could you try adding elements from your main project to the empty one until it stops working? For example the water shader you were talking about. To find out what's actually causing the problem.
     
  40. nubdev

    nubdev

    Joined:
    Mar 30, 2013
    Posts:
    87
    Hey sorry for the late reply, I'll get to checking those shader keywords now, thanks. I ended up having to remove the package after getting some missing shader errors, which are gone now. Will let you know if I can get it working.
     
  41. ReignOfDave

    ReignOfDave

    Joined:
    Oct 22, 2015
    Posts:
    51
    Is it possible to assign grass to a certain Texture channels? eg. If it is a cliff texture then don't spawn the grass on the area!
     
  42. ReignOfDave

    ReignOfDave

    Joined:
    Oct 22, 2015
    Posts:
    51
    Ohh and the performance is pretty bad even with low density grass like 30cm between blades of grass, I am going from 200fps to fluctuating between 100fps and 60fps on a gtx 980ti and an i7 6700k with 32gb ram!

    Any tips on performance optimization.


    - Edit -
    Even high density unity grass performs better than the low density grass!

    - Edit 2 -
    Had to switch back to unity grass as the performance is much better. I was losing 100 fps on my rig and that is just not an option. Unity grass seems to just work better and looks better, also obviously unity grass would look worse than a full dense scene but the looks to performance ratio is not good enough.

    P.S i will remove this if you can help me get this to unity grass level and have better fps or even better quality and a slight drop in fps.
     
    Last edited: May 21, 2016
  43. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Will Direct X 12 be supported/working with this asset?
     
  44. JRRReynolds

    JRRReynolds

    Joined:
    Oct 29, 2014
    Posts:
    192
    Is this asset still not working with Unity terrain and requires conversion to mesh ...just curious.
     
  45. ReignOfDave

    ReignOfDave

    Joined:
    Oct 22, 2015
    Posts:
    51
    Okay i am going to try this out again with a new project to see that fps i get
    !
     
  46. ReignOfDave

    ReignOfDave

    Joined:
    Oct 22, 2015
    Posts:
    51
    Yeah boss there is something seriously wrong with this. Here are two examples with a plane and a terrain with the same material.

    This is terrain!


    And this is the mesh version!




    as you can see this performs really bad with terrain.

    This was in a new project!
     
  47. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Sorry for the late reply, I somehow didn't get any notifications from the unity forums.

    If you are talking about using the channel from Unity terrain, sorry but that's not possible. You could try to extract the splat map from the terrain and use it as a density texture.

    I don't think DirectX 12 would improve performance with this shader, all of the performance cost is on the GPU itself, so reducing the CPU cost wouldn't help. You should still be able to use it in DirectX 12 mode though.

    It always worked with Unity terrain, it just has some minor issues with it, like grass popping. Also you won't be able to use the terrain editor to directly place the grass, other than that the grass will work as expected.

    No reason to remove this, the forums are here to help with problems, no reason to try to hide that!

    Can you use the profiler to see where the performance cost is? Also which Unity version are you using? On your screenshot your mesh version looks denser than the terrain version, so there's definitely something wrong with the terrain! I hope that the problem is with the shader, if it's in the terrain I won't be able to do anything about it...
     
  48. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Hi, I tested by stripping out DX9 and 11 API's so only leaving DX12 but the grass does not show.. no worries.
     
  49. ReignOfDave

    ReignOfDave

    Joined:
    Oct 22, 2015
    Posts:
    51
    Okay so the fps is good when there are two terrain with normal mats but if one has your grass shader the fps goes really low

    and that means its an issue with your shader here are some images.





     
  50. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Really? I'll check is out, but I think that could be because the DX12 mode is still very early.

    Yeah, that's the same bug some other guys had before. It looks like the terrain is regenerating completely, every frame. Which is extremely stupid and I don't understand why.

    Can you try a empty scene, only with the shader? And if that doesn't work, can you try another Unity version?

    To be honest, I think you should try to extract your terrain information and generate the mesh yourself, at least for the grass.