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
    By the way, I asked this question in the new forums, but as that was kinda scratched, here it is again:

    Can anybody confirm that the shader works on PS4? Since the changes in the last patch it should work now, but I don't have one myself, so I can't test it.
     
  2. Lightning-Zordon

    Lightning-Zordon

    Joined:
    May 13, 2014
    Posts:
    47
    It works great on PS4 now.
     
    Arkade likes this.
  3. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Thank you for testing it! I'll update the asset store description to include that with the next patch!
     
    Arkade likes this.
  4. TimeSniper

    TimeSniper

    Joined:
    Nov 26, 2012
    Posts:
    18
    I am testing grass shader in my baseball project. The grass shader is applied to the meshes as you see in this picture.

    https://drive.google.com/file/d/0B2R3Nh1kjfOJanNjMF9RejNwcUU/view?usp=sharing

    Here, the base ball ground is separated with several meshes as you see. One thing you should notice is that they are not in the square form. And when it is not the square form, the grass isn't equally placed. In other words, in some area, the grass is densely planted but in some other places it is not. Please see this image for your reference.

    https://drive.google.com/file/d/0B2R3Nh1kjfOJSFlIZTV5ODlQZnc/view?usp=sharing

    As you guess, we need equally planted grasses in baseball ground. Please advise me how I can get the proper result.

    Edit: the images inserted don't work correctly. I choose links instead
     
  5. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The problem here, is that the grass density is dependent on the polygon density. In your case you have polygons of different sizes, higher in the middle, less in the outer areas.
    You could easily solve that by using a grid mesh over the whole area and using a density texture to set where the grass should be.
    In this case, the density texture can just be white where you want grass to be and black where you don't want it.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Why not calculate the triangle area in the geometry shader and adjust the density? It's fairly trivial to do using Heron's Formula.
     
  7. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I didn't think of it at the time, honestly. In most cases the terrain is either made out of a grid anyways, or it's easy to create a grid like in this case.
    I'll look into it though. As long as it doesn't interfere with backwards compatibility I might patch it, but I can imagine that it will still break some peoples scenes. I'd probably have to include a legacy version for those cases.
     
  8. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Can you post a gif showing the problem? Also what unity version are you using? Are you using the latest version of the shader?
     
  9. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Hey @Nonakesh
    Any results re. the above? (No vertices for distant geometry)
    Thx!
     
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Actually, yes. I just finished it! Send me your invoice number per private message and I'll send you the fixed file!
     
    Arkade likes this.
  11. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Awesome! Sent, thanks!
     
  12. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    turboscalpeur and Arkade like this.
  13. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Wow! Like-tastic! Especially love the way the colours blend, the hints of dryness of the browns. Gorgeous! Any tips on how to achieve this? (Beyond ”work it out for your own lazy self :p;) )
     
    turboscalpeur likes this.
  14. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I don't know the specifics how he made it, but I think instead of using a single large mesh for the grass, he used individual smaller islands, to get the bushy kind of look. Best you ask @turboscalpeur himself!
    Oh and of course he did a lot of great texturing work.

    I don't know about the dryness though.. I think it's just a combination of the floor texture, the grass color (and texture obviously) and the lighting.
    Looking at it, it is probably mostly the texture, being slightly green at the bottom and starting to look dryer at the ends, that way it looks convincing and natural.
     
    turboscalpeur and Arkade like this.
  15. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654
    Just skimming his thread -- ah, he's the grass texture guy (sorry if miss-gendering!) That might affect things. Though I haven't seen mention of your asset on front page?
    Not sure whether can afford (performance) textures for my grass yet so had wondered whether achievable with solely colours, etc.

    Yeah that lovely bushy effect is likely separate meshes to do the normals *but* I could imagine doing some sort of normal tweak either in shader or on terrain. Perhaps shader could have option to either use a redundant channel or infer from existing one? Dunno. Just some thoughts.
     
  16. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I think working with my shader is a new thing, he's written to me directly with it. I think you might be able to get the effect with displacement objects, or with the displacement texture, if you don't like the new system. I don't really want to add a shader feature for specific things like this, it seems like it's more suited for being creative with the current systems.

    But yeah, using normals, putting them on a plane and then using them with the render-texture displacement would probably work.
     
  17. turboscalpeur

    turboscalpeur

    Joined:
    Nov 29, 2015
    Posts:
    302
    Hello !

    I combine my Grass Textures Assets and the DX11 Grass Shader in a very simple way !! :

    I use some Custom Quads Meshes with my Grass Textures in 1024p, then I settling each of them in the StixGames/Grass;
    When done, I create a Prefab for each Grass and then using a Beta Asset for scattering Grass Prefabs.

    When I will have more time, I will give more detailed Tips on how to get Great results :)

    Some others Screens:




    Cheers,
    turboscalpeur
     
    StormchaserZA and Arkade like this.
  18. Lasse-Loepfe

    Lasse-Loepfe

    Joined:
    Mar 28, 2014
    Posts:
    29
    Hi,

    this is looking great. Just a pre-purchase question.
    I assume that the problem of grass turning with camera rotation (because of billboards no really facing camera) would not be an issue here. Is that right?
    Best
    Lasse
     
  19. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The individual blades of grass are still billboards (well, a bit more than that... it has some more geometry so it can bend), but because there's only one blade per billboard it looks a lot more natural than with classic billboard grass. I'm also thinking about adding a mode where the grass doesn't rotate with the camera at all, but I haven't experimented with it yet, so I can't make any promises.
    But to be honest, if you use the shader for single blades of grass, it doesn't look like billboards at all, at least in my opinion.
     
  20. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new version of the shader. Here's the changelog:

    1.4.0:
    • Adds support for texture atlases. Each grass type can now have a texture atlas to add variety.
    • Fixes NormalDepth rendering.
    • Cleans up the UI
    • Makes parts of the code more readable.
    As always, if you don't want to wait until Unity approves it, just send me your invoice number via PM or mail and I'll send you the package right away.
     
    turboscalpeur likes this.
  21. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello Nonakesh,

    I am working on Xbox One, does this shader works on that platform?

    Regards,
    Carlos
     
  22. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Currently the shader only works on Windows with DirectX 11 and on PS4. But to be honest I have to way of testing it on Xbox One, so it might be like on PS4, where only a few small changes were necessary to make it work. OpenGL on the other hand seemed to have some serious problems with Unity's cross compiler.
     
  23. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello Nonakesh,

    Would you like me to test it for you on the Xbox One? I could do that and if it works you would know and I would buy it. It is a win win solution. Let me know.

    Regards,
    Carlos
     
  24. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    I have just got a bug report on OpenGL issues reproduced and it is in the issue tracker, maybe voting will get things working faster.
     
  25. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Thanks, I voted on the issue. I should have probably done that myself. If they send you a message you can always forward them to my support email, I'd be happy to change things in the shader to solve the issue.

    To be honest I'm not really a fan of giving away the shader for free, even for testing, but in this case I guess it makes sense. I've sent you an PM and I'll just trust you with buying the shader if it works.
     
  26. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello Nonakesh,

    In this attachements I show the grass in the editor in play mode, and in the Xbox One which is the pink one. I created a mesh and added the material with the shader and this is what I get in the Xbox One. Any ideas on why?

    I get not error in the debugger. In another test I added the shader to the "Edit/Graphics/Always use shaders" list and while publishing the game to create the .exe file for Xbox One, the editor never finished the process and I had to close it through the Task manager.

    I will keep testing meanwhile.

    Please let me know.
    Regards,
    Carlos
     

    Attached Files:

  27. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Well that's pretty much what I expected. The material being pink just means that there's an error in the shader. The question is, does it write any error messages in the inspector when you select the Grass.shader file?
     
  28. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The new version of the shader was just accepted by Unity!

    Let's hope I didn't miss any bugs, but if I did, post here and I'll try to make a hotfix as soon as possible. (It's sometimes a bit hard to test these things, especially when I'm changing data types in the shader, those can be extremely hardware dependent)
     
    turboscalpeur likes this.
  29. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello Nonakesh,

    This "Compiled shader" file is what I get after compiling the shader. I am using the variant "simple with density" since I made a density map for grass location. Also this screen grab shows some red error messages after compiling the shader. I notice that just for the Xbox One, the shader is keeping around 240 some variants and I know Unity's limit for shader variants is 128, may be this has something to do with the problem? I know nothing about shaders.

    Let me know what you think.
     

    Attached Files:

  30. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Just so you know, I have had similar problems with other DirectX11 shaders in Xbox One, and they have been fixed easily. Usually they have been because of some files in the wrong place and that sort of things. You know Xbox One is from the company that came up with DX11 after all.
     
  31. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    If we can come up with a solution, I can advertise you grass shader to a lot of Xbox One developers, because as far as the performance goes it seems that it would be very fast and beautiful compare to any other.
     
  32. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yeah, I was thinking the same thing, there shouldn't be any reason for it to work on PS4 and not on Microsoft's Xbox One.

    I almost wanted to tell you to delete the compiled shader file, but after downloading I guess that won't be a problem, it's basically empty except for meta data.

    I'll look into the error messages in the inspector tomorrow, maybe I can do something about them.

    Edit: by the way, the limit isn't 128 shader variants, it's 128 shader variant keywords. That's a global limit though, so if you are using multiple complex shaders this might be a problem. I've included a section in the documentation about reducing shader keywords for this case, but I'm pretty sure Unity would have a specific error message for that.
     
  33. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Yeah you are right, I would assume that Unity3d would throw an error if that limit would be violated.

    Ok great, I will look forward to your feedback tomorrow.

    Regards,
    Carlos
     
  34. hmroz

    hmroz

    Joined:
    Jan 28, 2014
    Posts:
    5
    Hi,

    It's a really great asset but I have a strange problem with performance.

    When I play my scene in the editor everything works great with 80-100 FPS but after building the game it drops down to 5-15 FPS. I removed everything from the scene to check that it's a problem with grass. It's placed on quite big mesh that was created from terrain using Terrain Composer. I use Unity 5.3.5, Deferred, DX11.

    EDIT
    I made some more tests and find out that the reason is not the building process but game window resolution. Increasing the resolution has huge impact on performance i.e in my scene 800x600 - 56FPS and 1024x768 - 39 FPS. Could it be connected with LOD/Density system? Is it calculated based on object size on screen or distance from camera?
     
    Last edited: Sep 21, 2016
  35. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hi,

    Great shader! Got it to work beautifully yesterday. There was just one problem, I could sometimes see a blue/pink-ish trail even tough I set the culling mask of the displacement camera and the layer of the displacement renderer to a new layer called displacement.

    Today I have another problem, after rebooting I get pink everywhere.
    For some reason all the settings are set to 0, wasn't like that when I saved. I will redo the setup, it should fix things.

    Any ideas about the blue trail?
     
    Last edited: Sep 25, 2016
  36. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Ok so I deleted and reimported all the assets and same pink problem... turns out a few of your files had mixed return chars (mac os and windows) fixed it in mono and my pink is gone now. Its weird that it was working yesterday.

    I also fixed my blue trail problem, I tought I RTFM but apparently I didn't. Removing the displacement layer from the selected culling masks of my main camera did the trick.

    Thank you for the addon!
    Let me know when you do clouds and or water.
     
    Last edited: Sep 25, 2016
  37. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Sorry I didn't answer until now, I didn't get an email notification for some reason. (That's happend a few times already unfortunately... Just send a mail to my support email when I'm unresponsive here, I try to be as responsive as possible)

    It looks like you've got a performance problem with the fill rate here. Normally the main performance cost with the shader should be generating the geometry, but if your grass is very wide or your resolution is very large you can still have problems with the fill rate. The easiest ways of fixing this are decreasing the grass density, using a simpler grass type (less textures or just simple mode) and using a different lighting mode (that might be quite significant in your case).

    Sorry about the different line endings, Visual Studio always F***s those up on my PC, I have no idea why. I've never heard about that causing actual problems though. Could it be that you just randomly fixed a import bug by changing the file?

    Also great you could fix the problem!
     
  38. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a new patch, 1.4.1:
    • Fixes a compiler error that could occur when activating texture atlases in simple grass mode.
    Because the editor doesn't work any more when there's a error, the material would get stuck in that state.

    If you want the fixed file immediately, just send me a PM with your invoice and I'll send you the fix!
     
  39. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Here is the attachment with the warnings.
     

    Attached Files:

  40. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Those warnings shouldn't cause any problems (I should probably get rid of them though).
    Does the grass work in the editor? If not, can you select the Grass.shader file and look for errors in the inspector?
    If it doesn't work and there are no error messages I'm afraid I won't be able to do anything here, maybe you should create a bug report for Unity. (I don't have a XBox One otherwise I'd do it myself)
     
  41. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    282
    i must say I'm pretty disappointed(though still hope it's a resolvable issue).
    On a laptop with i7-4702MQ, 8Gb of Ram and GTX 760M I get 10-20 fps (Unity 5.4.1) in a demo scene in editor (fullscreen - 1600x900) and it's written there "This scene was created to have a small performance impact." And even with such low fps it looks nothing like the screenshots and youtube video with low density grass and jerky animation(it's even jerky (grass blades and flower buds jump from one position to another) when it's rendered on low resolution with 60fps).

    Edit.
    It wasn't a shader issue after all. Shader works perfectly on this setup and looks brilliant!
     
    Last edited: Sep 27, 2016
  42. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'm sorry you are having trouble with my shader, here are a few tips for improving performance:
    Can you try deactivating all grass areas except for one? Also try to decrease the max density of the grass material, the density falloff should already be fairly low.

    You should also check the profiler, where does the main performance cost come from? Maybe the main issue isn't with the shader itself.
    Also try following the documentation, there's a section about reducing performance cost there.

    Are you sure that Unity is using the Nvidia GPU and not the internal Intel GPU? (You have an i7, so I'm assuming it's a double GPU solution that can switch between performance and power mode)

    Lastly, if Unity is using the right GPU and you can't get good results, I'm afraid it might be the performance difference. Your GPU is 3 years old and wasn't high end tier back then, even more as it's a mobile GPU. My shader is first of all a high end effect, so while it is extremely performant considering the sheer amount of polygons it creates, it is still a costly effect. I personally believe that the result is worth the performance cost though, of course I'll try to optimize it further in the future. If you could try to find out which settings affect performance the most on your PC, it could help me with optimizations.
     
  43. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    282
    Hm. That's really weird.
    I know this is not a super powerfull setup but it handles fullscreen ray marching volumetric clouds for unity with 60fps, so I don't think that it's the main issue.
    Unity is set to always use nvidia gpu in gf control panel. But it appears that from time to time it uses an intel one for some reason.
    After zero changes and restarting the laptop everything works at 60fps.
     
  44. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Unity tends to have some performance quirks at times.. and those dual GPU setups aren't the most stable either (I've had some extreme problems with them myself).
    I'm glad it's working now though. If you have any other problems, just write here again or send me another mail!
     
  45. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hey guys, regarding preformance, I kinda found out something yesterday. I was testing the grass on a very simple 800x500 resolution terrain and the performance was really taking a major hit. Around 16 fps. Then I switched the base terrain's material from "built in standard" to "built int legacy Diffuse" and my fps jumped straight to 85! I couldnt even see a difference in the way it looked.

    Now If only I could figure out how to do this in my other scene were the grass is not setup as a terrain object that would be a welcome performance gain!

    *DISCLAMER* In case were this was allready mentioned in the manual (god knows I hate to read those) please disregard this post hahaha.

    *EDIT*
    To clarify, I had a terrain object which had the grass shader running and a copy of the same object under the grass with a green material so we wouldnt see through the ground. Its the shader of the object with the green material that I changed.
     
    Last edited: Sep 30, 2016
  46. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I was having that error during cloud build, updated your package and now I get a different eror :confused:

    [Unity] Player export failed. Reason: Shader error in 'Stix Games/Grass': redefinition of 'width' at Assets/Resources/StixGames - DirectX 11 Grass Shader/GrassSurface.cginc(44) (on d3d11)
     
  47. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Well, to be honest that doesn't have anything to do with the shader. Except you are talking about changing the lighting mode of the grass itself (you can do that at the top of the material, it can definitely improve your performance).
    But if you just want to know how to use a simpler shader for the base object, just create a new material with the legacy diffuse shader and put it on the floor object.

    I didn't even change anything in that file... Is there any more info on the error, like shader keywords that are active while compiling?
     
  48. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Never mind that, it was a bug from patch 1.4.0

    I just uploaded a patch to fix it, if you don't want to wait, just send me a private message with your invoice number and I'll send you the package directly. Here's the changelog for 1.4.2:
    • Fixes a compiler error that occurred when multible grass types were selected and texture atlases were enabled.
     
  49. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Yes that is quite possible, im quite new at this and end up fiddling with a lot of things :p
     
    Nonakesh likes this.
  50. tsukiyomi

    tsukiyomi

    Joined:
    Apr 1, 2015
    Posts:
    9
    Hello, i tried your shader and it was really the solution i needed for my project.
    I'm working on a windows project, in directx11, but when i build (Windows, x86_64) the shader just doesn't work (pink).
    It doesn't show any message or error, it just doesn't work.

    I know i'm certainly just missing a parameter but it seems i can't find it x)