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
    Sorry for the delay, I'll look at everything over the weekend!
     
  2. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    1. This would not increase performance significantly. I mean, instancing allows to draw the same mesh easily. Sure it would work better if you have thousands of rectangle meshes, but the whole grass generation already happens on the GPU. It would be a good addition, but I think it is not really the most important thing to work on. It would be a better idea to make this shader follow the spec in the first place before doing such things.
     
    Nonakesh likes this.
  3. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Some of us do have a lot of rectangle meshes with this grass on ;)
    It doesn't seem very difficult or time consuming to implement this either, but maybe I'm mistaken?
     
  4. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'll check out the beta. Unity probably changed something in their internal shader code again.

    The shader doesn't support instancing, but it's not really necessary anyways, all blades of grass are in one object / draw call anyways and I'd assume that it's rather rare to have a huge amount of individual grass objects.

    I'll definitely look into that, probably in the next few days.

    Oh, ok, so that makes more sense. How exactly are you using it that way?
    I'll look into instancing once I have the time!
     
  5. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    I have it as a material on a quad, then duplicating the quad to make blocks of grass around certain areas of the scene/terrain. (the terrain is flat)
    There might be a better way for me to do it, but duplicating the terrain isn't an option for me.
     
    Last edited: Nov 6, 2016
  6. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Can you post a screenshot of this? Also if your scene is flat, wouldn't it be more efficient to use a flat mesh instead?
     
  7. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Sure no problem

    It might be, but I'm using some of the other features of terrain like trees/detail mesh, texture blending etc. And if I decide I want to make it not flat, I can do that too.
     
  8. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Fair enough. The scene looks quite nice!
    As I said I'll look into instancing, but I don't know when I'll have the time to look into it yet. With a small amount of planes I wouldn't expect very large improvements anyways, but I might be wrong.
     
  9. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Thanks, it's just a prototype scene at the moment. Your grass always makes a scene look better too ;)

    Thanks for looking into it. You say I can draw all grass in 1 drawcall if I use a density (maybe texture density) on just one mesh? I'll have to look into that, with just these 2 in the screenshot it's ok but when I spawn in 60 of them the drawcalls double and it drops to 40fps on my GTX970 so I'll try some new methods as well.
     
    Nonakesh likes this.
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    For now you could try to reduce everything to one draw call, by combining all planes into one mesh. There are a few scripts around that do just that, or you could do it directly in a 3d modelling program!
     
  11. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just tried a few things and I'm quite sure this is a problem with Unity, not with the shader. I've created a bug report and will keep you updated when I get some more info!
     
  12. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    That seemed like a good idea so I tried it out. It lowered the drawcalls but doubled the milliseconds to render. I'm guessing it's causing some kind of stall on the GPU or something but I can't profile it because the latest nVidia drivers broke GPU profiling for me =/
     
  13. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    So combining the meshes made the overhead larger? That's quite unexpected to be honest. I would have thought it stays about the same!
     
  14. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Sorry if this got asked, but I have a hilly terrain. I duplicated it and added your grass material from the example. However, if I have both terrains active, the game lags immensely. The moment I disable the original terrain (without the grass shader), everything works flawlessly. So I can have the grass, but no terrain, haha.
    This is also in VR if that makes a difference.
    Any ideas?

    Edit: I'd also like to add that I have 4 terrains on the screen. The only time I lag is if the original terrain I duplicated for sake of the grass is enabled. The other 3 terrains make little to no difference in performance.
     
    Last edited: Nov 10, 2016
  15. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Oooh I think I figured it out. Apparently you aren't supposed to duplicate the gameObject in the scene. You go to the Terrain asset in the project view. Duplicate THAT. Then drag it into your scene. For some reason, if you just duplicate the gameObject, it freaks out.
     
  16. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I've heard about this problem a few times already, but I've never encountered it myself. Honestly I think this is a bug in Unity's terrain system, I don't see any reason why the duplicated terrain should use up that much power. You could submit a bug report, but I wouldn't have to much hope, I think they've kind of abandoned the current terrain system.

    Anyways thanks for telling me about the duplicated terrain object, I'll tell that to people with the same problem in the future!
     
  17. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new patch, I think quite a few of you will like this one! Here are the patch notes:

    1.5.0:
    • Adds a grass painting tool, for painting density textures directly in the editor. This is still a very early version, it will improve in the future.
    • Fixes a compilation error in Unity 5.5
    • Some slight progress in OpenGL Core rendering, it now works as long as grass smoothing is turned off.
    The new painter tool is still rather simple, but in my tests it seemed like it could be really useful!

    Here's the result of a really quick test, nothing fancy but it was extremely easy to use in comparison to using an external program.


    And here's a screenshot from the painter window, again nothing special yet, but quite useful in my opinion:


    The patch will probably be approved until next week, but if you want it right now, just send me your invoice number per PM or mail and I'll send the patch to you!
     
  18. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Hi I forgot about this question I had but the problem is still happening
     
  19. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    Thanks for the update! Now it compiles without errors on GLCore at least, though, no grass whatsoever. Even if I disable smoothing. I tried on Windows 7 GL 4.5, but not on linux because I run open source drivers and only windows VM has good graphics. Good to see you are progressing with it!
     
  20. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Can you post a short gif of the problem? I don't think I've seen this problem myself, so it would be very helpful for reproducing it!

    The update wasn't accepted by Unity yet, that will probably happen until next week. If you want the update right now you can send me your invoice number per PM and I'll send you the patch!
     
  21. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Yeah sure the link in the quote takes you to a gif of it but I'll paste it here again (https://embed.gyazo.com/1f20b982a15ad8789b2e7aef3c9d7ab1.gif
     
  22. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
  23. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The new patch has just been accepted by Unity!
     
  24. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    Thanks! Now I guess the smoothing is what is left to make the grass even more superior to what unity already provides, because currently it changes a lot when you get to the different level of detail. By the way, it is also now compatible with android which is really cool.
     
  25. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    How does it run on Android? Does it have the same problem with the smoothing?

    By the way, I wouldn't keep my hopes too high, I've been in contact with Unity's support and apparently they don't plan on fixing the problems right now. So unless the problems are actually in the shader itself, there won't be large improvements from what we have right now.

    Well, still a lot better than before, so I guess things will improve on the long term!
     
  26. liven999

    liven999

    Joined:
    Oct 7, 2015
    Posts:
    20
    Hello,

    first at all, thank you for your work, I have a lot of fun using your shader.


    A question : is it possible to modify the shader variables in script ? (to ajust some of them according to the quality level chosen by the player for exemple)


    Sorry if it is a noob question or if you already answered to it


    PS : I hope you anderstand me, english is not my natural language
     
  27. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes it's possible. You can change shader variables on the material object: https://docs.unity3d.com/ScriptReference/Material.html

    For example you could change the length of the grass like this: material.SetFloat("_MaxHeight00", 2);

    If you want to know the names of all variables, look at the top of the Grass.shader file.
     
  28. liven999

    liven999

    Joined:
    Oct 7, 2015
    Posts:
    20
    ok thank you !
     
  29. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    I haven't really tried running it on actual android device because my phone is broken now, but it works fine with OpenGL ES + Android Extension Pack. The same problems as on OpenGL, but it works.

    Also, a quick note: I couldn't really build a windows player, because grass painter needs UnityEditor. I think you should put the script into Editor folder.
     
  30. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yeah, sorry about that! I'll fix it in the next patch, for now just manually move the painter to a Editor folder!
     
  31. Gawayne-Nanaumi

    Gawayne-Nanaumi

    Joined:
    Oct 8, 2013
    Posts:
    3
    After update ver 1.5.0.... I can't make bundle(*.unity3d)
    I use legacy (Build StreamSceneAssetBundle) when I make a bundle.
    When I try make a bundle, unity output error messages.
    - Assets/StixGames - DirectX 11 Grass Shader/Scripts/GrassPainter.cs(5,7): error CS0246: The type or namespace name 'UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
    - Error building Player because scripts had compiler errors
     
  32. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Once again sorry about that, I forgot that editor scripts are supposed to be inside a editor folder. Just move the GrassPainter.cs to the Editor folder and it should work again!
     
  33. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new patch, here's the changelog:

    Version 1.5.1:
    • Adds support for painting on Unity terrain with the grass painter.
    • Moves the grass painter script to the Editor folder. Building projects should work again now.
    • Makes is slightly more obvious how the grass painter can be used.
    If you want to use the patch right now, just send me your invoice number per PM or mail!
     
  34. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    No problem, not sure if it's the displacement resolution or the swirl normal pattern but it's definitely strange behavior
     
  35. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The new patch should now be available from the asset store!
     
  36. Gawayne-Nanaumi

    Gawayne-Nanaumi

    Joined:
    Oct 8, 2013
    Posts:
    3
    Thanks your support.
    Um.... I had a new patch from asset store. How to use grass painter on Unity Terrain??
     
  37. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    First, open the grass painter window from the menu bar Window > Stix Games > Grass Painter.
    Then select the terrain with the grass material. You'll have to create a new density texture to paint the grass, a smaller texture will be faster to edit, but a larger one will have more detail. If you already have a density texture you want to edit, make sure it has Read/Write enabled in the importer.

    Now you should be able to paint on the terrain!
     
  38. Gawayne-Nanaumi

    Gawayne-Nanaumi

    Joined:
    Oct 8, 2013
    Posts:
    3
    Thanks... but I can't use grass painter with unity terrain.
    As you explained, I tried step by step, but I could not do it.
    Open the grass painter window from the menu bar.....Success.
    But I can't select the terrain in Hierachy. (The terrain's material's type is custom. That material is grass shader material. Grass type is 1~4 Texture, Density mode is texture density)
    And I don't know exactly what you mean that select the terrain with the grass material.

    If you possible.... Um..Can you make a tutorial videos (grass painter on unity terrain)??
     
  39. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Are you sure you have version 1.5.1? In Version 1.5.0 there was a grass painter but no support for Unity terrain yet. Other than that it should actually be really straight forward. Open the grass painter, click on the terrain with the grass material, create a density texture and paint.
     
  40. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
  41. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Sorry about the delay, but I've been really busy lately, so I wanted to concentrate on the grass painter instead, but I didn't forget about the issue! I think I'll be able to look into it during the holidays.
     
  42. theropodx

    theropodx

    Joined:
    May 1, 2011
    Posts:
    40
    Just got this package and can't get beyond pink textures (missing texture) with custom or "example" materials. Help!

    Edit: Ok, had some error log msgs about line endings, so recompiled those files for windows. Now I get this error: Shader warning in 'Stix Games/Grass': Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)

    Working on making sure my video drivers are updated. I'm wondering if this package will be tricky to roll out (like if end users will also have lots of issues?). Appreciate any insight.

    Edit 2: Ok finally found the D3D settings and of course it was set to 9 so I changed to 11. Should've figured this out sooner.
     
    Last edited: Dec 6, 2016
  43. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    17
    I've just purchased this asset and tried to add one of your example tiles to the scene, but the grass is just black. This looks amazing, and I would love to use it in my project, so any help would be appreciated.

    Edit:
    It's only doing it n one of my scenes, so I'm not really sure what's going on.
     
    Last edited: Dec 6, 2016
  44. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Do you have a directional light in the scene to light the grass? If not try using the unlit version of the shader. My grass went dark when I was using a day/night system and there was a part in the day/night cycle where no directional lights were enabled and it went black.
     
  45. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'm glad you were able to solve your problem by yourself!

    I haven't ever heard of that problem, are you using any special systems like @Liens mentioned? The grass doesn't work with lightmaps by default (it's constantly moving so it wouldn't really make sense anyways), but if your scene requires it, you could try some trickery, by using the unlit mode and put the lighting information in the color texture.

    How do those systems work? Are they using custom shaders for scene objects? What day/night system are you actually using, maybe I can contact the maker so we can work out support! (Of course I'll still work on your other problem first!)
     
  46. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    17
    Clearing the GI Cache for the project fixed it. I'm not entirely sure why, since the object I applied the shader to wasn't marked as a lightmap static, but it's fixed now. Thank you both for the input!

    I hope it doesn't re-darken when I redo the lightmaps for the scene.
     
    Nonakesh likes this.
  47. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Thanks for the offer but I already contacted the support for the asset but it wasn't very good (they said they got rid of the bug but I don't know for sure) so I've scrapped it. I'll probably go with Time of Day asset since it says it's VR compatible, which is what I need anyway.
    But yeah it was using custom shaders for the clouds and stuff, which were projected onto the insides of large spheres that surrounded the scene. There was a sun with light and a moon with light, during the day the sun was turned on, at night the moon was turned on as a separate light. There was an hour in between when neither was enabled and it caused the DX11 grass to be covered in a flickering shadow. Fixed as soon as I enabled one of the lights (sun or moon, they were both directional).
    It's not a problem anymore though, since I'm no longer using the asset, which was Unistorm.
     
  48. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Hi.
    Will this tool support Vulkan in Unity 5.6?
     
  49. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Well, I hope so. Same as with OpenGL this depends mostly on Unity, so if their shader compiler supports all the features necessary for my shader, it will work. I'll try it myself over the holidays and will post here if there are any good news.
     
  50. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    cool, we need performant foliage in our VR title (The standard one completely breaks performance in VR), but we are also looking at moving to Vulkan :D