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. tsukiyomi

    tsukiyomi

    Joined:
    Apr 1, 2015
    Posts:
    9
    correction it show me this error

    Shader error in 'Stix Games/Grass': undeclared identifier 'textureAtlasRandVal' at Assets/StixGames - DirectX 11 Grass Shader/GrassGeom.cginc(128) (on d3d11)

    Compiling Vertex program with DIRECTIONAL SHADOWS_OFF LIGHTMAP_OFF DIRLIGHTMAP_OFF DYNAMICLIGHTMAP_OFF GRASS_OBJECT_MODE SIMPLE_GRASS_DENSITY VERTEX_DENSITY GRASS_USE_TEXTURE_ATLAS
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING
     
  2. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    Unity have just released public build of unity with vulkan. I wanted to try if your shader works with it, unfortunately, it does not (just like with dx12 as far as I know). It throws out bunch of errors like "'out; must be an array: undefined 146" or "'assign' : |-value required (can't modify a const) 166". There are like 5 more errors in 166. But most likely it is just a cross compilation bug, I will report it in the forums. Just letting you know.
     
    Nonakesh likes this.
  3. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    282
    I noticed that there is a big difference between light source with 0.01 intensity and 0 intensity. Looks like ambient light stops affecting grass after light intensity becomes zero.
    Is it supposed to work like this?
     
  4. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I found a weird behavior today. I created a terrain in unity and used a script to generate a mesh from it. The converter tool had diferent quality setting (triangle count). When I chose the lowest one and add grass at the maximum density it is the equivalent of the lowest density setting on a regular terrain. If I conver to a higher quality setting it doesnt do this either.

    Is it normal for the shader to react this way with low poly meshes?

    *EDIT*
    Just tested things again, and even with the low poly mesh converted back to terrain I experience a lower grass density, but its much less pronounced than when applying the grass to the mesh.

    I have screenshots but dropbox has broken the public link system so itll have to wait.
     
    Last edited: Sep 30, 2016
  5. tsukiyomi

    tsukiyomi

    Joined:
    Apr 1, 2015
    Posts:
    9
    Hello, i'm working on a project where i would have wanted to use your shader, but when i build (standalone for windows in _x86_64) the shader doesn't seems to work (render in pink)I link my project settings and my settings of the shader below.

    https://gyazo.com/e9bffc698011662d368dd17ffe09629e
    https://gyazo.com/891561057676c34f210746563d9ad9c4
    https://gyazo.com/bfd1112fa94d3cdf188e160aff7412e6

    And it show me this error :
    https://gyazo.com/b68a3809efd03d91b20a907e31518470
    ----------------------------------------------------------------------------------------------
    Shader error in 'Stix Games/Grass': undeclared identifier 'textureAtlasRandVal' at Assets/StixGames - DirectX 11 Grass Shader/GrassGeom.cginc(128) (on d3d11)

    Compiling Vertex program with DIRECTIONAL SHADOWS_OFF LIGHTMAP_OFF DIRLIGHTMAP_OFF DYNAMICLIGHTMAP_OFF GRASS_USE_TEXTURE_ATLAS SIMPLE_GRASS UNIFORM_DENSITY UNLIT_GRASS_LIGHTING
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING
    ----------------------------------------------------------------------------------------------

    Thk you for your help, even if i already know that you have many things to do, it's just that i don't seems to understand where is the problem
    I tried to build your showcase scene example but it did the same :/
     
    Last edited: Sep 30, 2016
  6. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just tested building on 64 bit and it seems to work. Are you sure there aren't any error messages? There were a few compiler errors in version 1.4.0, one of them will be fixed in 1.4.1 and another one will be fixed in the upcoming 1.4.2 (if you send me your invoice number per PM or mail, I can send the patch to you right now)

    Edit: I didn't see your correction before, yes I think that error was one of the two that were fixed.

    By the way, sorry for the late answer, I was sick this weekend, so I couldn't work on the shader!
     
  7. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That doesn't sound right, I'll look into it!
     
  8. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    The grass density is dependent on the polygon density, so if your conversion tool changes the size of the triangles, it will also change the density of the grass. If even the maximum grass density setting isn't enough with the converted mesh, I'd look for a setting in the tool to get a higher polygon count in the converted mesh.

    I've never used this conversion tool, so I can't really help you with it. If it doesn't have any settings to create a higher polygon mesh, you could try exporting the mesh to Blender and subdividing the whole area. It's not a beautiful solution but it should work.
     
  9. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    17
    Does this shader work with shorter grass? I'm doing an architectural visualization, and we'd like to show grass in a few areas, but we want it to be fairly short and well-kept.
     
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I've had OK looking results for short grass (10cm height) on my GTX 1080, but you'll actually need power like that for it. The problem is, that smaller grass means that a higher density and further view distance is necessary for the grass to look convincing, which of course needs more performance in turn.

    If you don't have the performance to waste I'd either go with a plain grass texture (kind of ugly) or a raytracing based grass shader (I remember there were some of those on the asset store). Those still have a lot of disadvantages (for example they are unusable for long grass), but I've seen them be used with great success, like in Rocket League.
     
  11. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    17
    Thank you for the honest feedback! I know a lot of devs that would just push their product without pointing out any of the potential follies. I'll probably need to go with a different shader in this instance, but if I ever have need of long grass, yours will be the first in my list.
     
    Nonakesh likes this.
  12. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
  13. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That sounds almost too good to be true, thanks for posting!
     
  14. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    EDIT
    ---------------------

    Before you read all this noob nonsense (been using unity for a month), I found a much better way to deal with this. I will finish my tests so I can get everything like I want it and share back the results. But just so you know, IT LOOKS AWESOME! ;)
    ---------------------

    This sounds like what I need. I am trying to get the grass to play ball with Gaia (amazing terrain generator) but its a real PITA.

    I know the Gaia dev worked with other devs to integrate their asset. Maybe he could give you a hand.

    Here is the process I am using right now:

    1-Export the terrain to a mesh (Built into gaia. Can also be found here http://wiki.unity3d.com/index.php?title=TerrainObjExporter)

    2-Load the mesh in Unity and line it up with the terrain

    3-Export a splat map of all your terrain's textures you want covered in grass (tool is built into gaia, there are other ways but i dont know them)

    4-If you have multiple splat maps you need to combine them otherwise go to step 5, I used GIMP. Load both splat maps in the same image as separate layers and set the mode of the top layer to Lighten only, it will combine them. It doesn't matter which layer is on top. (if you want to use multiple textures in the grass shader, you need to put the images in different RGBA channels instead of combining them, read the grass manual for more info)

    5-In Gimp (or whatever you use) rotate the image 90 clockwise and filp it verticaly, job done, save it.

    6-Set the mesh to use the grass shader. Select the grass type simple with density and load the image we created before as the density map (aka Grass Density)

    Tip: You can adjust the brightness of your density map texture to tweak the global density.

    Congratulation you now have grass... kind of.

    You can only get this to work right if you export the mesh in high res. Lower than half and it the mesh doesnt follow the terrain very well and you end up with grass at the wrong places (floating in the air or under the terrain).

    The problem is that when exporting at higher resolutions, the export tool splits the mesh in chunks. Its actually a good thing for Unity but I dont have a clue how to get my texture onto that. I have the feeling that I will have to slice the density map texture into peices that match the chunks of the mesh and then set individual shaders for each chunks. WHAT A PAIN!

    Using a texture channel/layers would be much better. Ideally it needs to support enabling it over several layers so it covers all our grass texture types. It would also be very cool if the grass could use the colors of the texture layer (but we should still be able to override them).

    Cheers!
     
    Last edited: Oct 6, 2016
  15. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I got my Density and RGBH maps working with the terrain. It think it looks great! Its even better when moving. Maybe I'll make a video. I am attaching a few screenshots. Look at the density, color and height transitions.









    I'm also having a weird problems with the grass. When walking uphill, looking up at the top of the hill makes the grass dissapear, like its clipping. Not sure what is going on. Any idea how to fix this? Is it me or the shader?
     
    Last edited: Oct 6, 2016
    AdamGoodrich likes this.
  16. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hello again,

    Here are some screenshots of the problem I described (gras disapearing when viewed at an angle from bellow)

    The grass is close to us and should be rendered, you can see all the shadows


    Same scene viewed from above, on top of the hill. Looks great!


    Maybe its just some setting, I dont know what is causing it, does anyone have a solution?
     
    Last edited: Oct 6, 2016
  17. camposanto

    camposanto

    Joined:
    Oct 2, 2015
    Posts:
    4
    Oh as a small PS4 note, we were getting some compile errors in Grass.shader when building but that's because "row" is a PSSL keyword. Renaming uint row, column to uint atlasrow, atlascolumn fixed it right up for us.
     
  18. LostPanda

    LostPanda

    Joined:
    Apr 5, 2013
    Posts:
    173
    hello,how to define my custom area,mesh or terrain? thanks!
     
  19. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    If you mean define were the grass grows, you need to create a density texture. Imagine a top down, grayscale view/image of your terrain where white= full density and black= no grass. You can define were grass grows and how dense. You can paint this texture manually but the best way is to export splat maps of your terrain's textures and combine them in Photoshop or gimp to generate whatever density pattern that suits your need.
     
    Nonakesh likes this.
  20. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Have you tried using the "ReadjustBoundingBox" script? It fixes issues like this in some cases. Just put it onto your grass object and set maxGrassHeight. I should have put that further up in the manual, I'll definitely do that.


    Thanks for the heads up, I'll do exactly the same!

    Exactly! Just don't forget to activate densities in the shader. (Just don't use simple grass mode, there's simple with density instead)
     
  21. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I've not been able to reproduce this, can you send me an example scene where you are having this problem?
     
  22. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I've just submitted a new patch/hotfix 1.4.3:
    • Fixes a compiler error on PS4
    • Adds a new (very short) tutorial for fixing a problem where certain viewing angles can cause the grass to disappear
    The script for the latter problem was already included, but it was only mentioned in the back of the Documentation, now it's mentioned right in the tutorial section, I hope that helps to clear up some confusion.
     
  23. LostPanda

    LostPanda

    Joined:
    Apr 5, 2013
    Posts:
    173
    thanks!
     
  24. cubedparadox

    cubedparadox

    Joined:
    Sep 16, 2014
    Posts:
    16
    I'm using your grass in a vr application, and I noticed some sever stereo disparity when looking straight down.
    After some testing, I think the problem is that your grass planes are billboarding around one edge, instead of the center of the plane.


    https://i.gyazo.com/7ef6d15396d10f44722bab61c135a3d3.gif

    Is there a simple fix for this? Thanks!
     
  25. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Well, first of all you're slightly misusing my shader... it's not really made for billboard grass, more for single blades of grass. But of course if it works for you, that's fine.
    There's not really a simple fix for it, but I understand that this is a problem in VR, I'll fix is as soon as I get the time!
     
  26. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I just uploaded a patch that fixes this!
     
  27. Abnormalia_

    Abnormalia_

    Joined:
    Jul 23, 2013
    Posts:
    128
    This asset is really cool! Top work! saved me a lot of time will never look back to unity terrain grass!

    This is WIP gameplay of our VR game:



    Performance and visuals are super ! DX11 was last piece to puzzle, thanks !

    1 more question. So to use more than 4 types of grass I need to divide terrain mesh to smaller area or there is another workaround to that ?
     
    turboscalpeur likes this.
  28. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That looks very nice, thanks for sharing!

    You basically have 3 ways to do this:
    • You can split your terrain into smaller areas, but limit your grass types for each tile to 4.
    • You duplicate the terrain once more and use 2 different materials. (That might use up a lot of performance though)
    • Or you use the new texture atlas feature, where you can have unlimited textures per grass type with virtually no overhead.
     
  29. Abnormalia_

    Abnormalia_

    Joined:
    Jul 23, 2013
    Posts:
    128
    Seems third is WTG. So I can specify atlas where I have for example 10 flowers for R channel. How this will work ? I'm still limited by 4 texture channels right ? Maybe you can provide little more details how it works or link whatever, thanks !
     
  30. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes, you still only have 4 channels, but once you activate texture atlases you can split each texture into multiple sub-textures. They will share the same density and grass settings (min/max-height, width and so on), but for example for flowers it should still be possible to add a lot of variety this way.

    To use them, first activate texture atlases at the top of the shader settings, then add a texture atlas where you would normally put a single flower/grass texture. Then you can set how the texture is split up for each grass type. In other words, you can still have 3 grass types with only 1 texture and a 4th that has 1000 flower textures. (Although 1000 would probably have relatively low resolution per sub-texture)
     
  31. cubedparadox

    cubedparadox

    Joined:
    Sep 16, 2014
    Posts:
    16
    Thanks for the quick fix, any idea how long it'll take for that patch to appear on the store?
     
  32. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I guess it will be sometime in the next few days. If you want it now, just send me your invoice number per PM and I can send you the patch tomorrow!
     
  33. cubedparadox

    cubedparadox

    Joined:
    Sep 16, 2014
    Posts:
    16
    Just tested out the patch, convergence is much better in vr now, still not perfect when looking straight down due to the per-eye billboarding, but neither is unity grass, so this is certainly good enough. Thanks for the help!
     
  34. cubedparadox

    cubedparadox

    Joined:
    Sep 16, 2014
    Posts:
    16
    I just had a colleague test the new version, and apparently convergence is actually worse than unity grass. Upon further investigation, I figured out why.
    On the left in this gif is how your grass works, the billboard points towards each eye separately, which looks great in 2d, but causes deconvergence in vr.
    On the right is unity grass, which just copies the eye rotation, which will always be the same for both eyes. This means you have perfect stereo convergence, but the grass doesn't look as nice when you turn your head.
    Ideally the grass would point directly between both eyes, but I don't know if that attribute is exposed in unity. For now, it'd be great to have an option to billboard like unity grass does just for vr comfort's sake.
    Thanks for all the support!
     
  35. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I'm afraid that won't be something easy to change. Because of the billboarding for the grass I had to change quite a few things in the lighting calculations.

    I've actually been thinking about implementing a mode where the grass isn't rotating with the camera, so it's more like natural grass. That would be ideal for VR anyways. The only problem is that I have extremely little time right now, so I won't be able to do anything but small-ish patches in the near future. So please bear with the deconvergence for now, I will definitely look into this problem. I'm a VR fan myself, so I obviously want the shader to run there too!

    By the way, the new patch was just accepted in the asset store!
     
  36. AurimasBlazulionis

    AurimasBlazulionis

    Joined:
    Aug 13, 2013
    Posts:
    209
    Some info for the OpenGL bug:

     
  37. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Nice! That's something I can actually work with! Thanks for passing on the message!
     
  38. Valthaer

    Valthaer

    Joined:
    Mar 21, 2015
    Posts:
    6
    Hi!

    I want to ask if the latest version works with HeightMaps and unity terrain. Thanks!
     
  39. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    If you mean changing the grass height with a texture, yes. And yeah, it supports Unity terrain!
     
  40. AdamGoodrich

    AdamGoodrich

    Joined:
    Feb 12, 2013
    Posts:
    3,780
    @Nonakesh - I would be happy to work together and add support for this to Gaia. Am also about to release a new product Gena, which is a sort of super spawner, and adding support to that as well would be cool.

    Reach out via PM if you are interested :)
     
  41. DrewMedina

    DrewMedina

    Joined:
    Apr 1, 2013
    Posts:
    418
    hello,
    Im having an issue where I can see the displacement normal trail rendering over objects, behind my rolling object. Its displacing very well otherwise... You cannot see it in draw over the grass, just over world objects. I cans see it distinctly through my gun the normal map texture and all.
    thanks!
     
  42. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    All displacement objects have to be on their own layer, that layer should not be rendered by the main camera and be the only layer rendered by the displacement camera.
    In other words, look at which layer is rendered by the displacement camera and put the trail renderer in that layer! If there is no layer selected, you have to create your own displacement layer (the layer settings are at the top right of the editor).

    By the way, if you have anything nice to show off, I'd love to see your results of using my shader! :)
     
  43. DrewMedina

    DrewMedina

    Joined:
    Apr 1, 2013
    Posts:
    418
    perfect! Working great now, thank you for the explanation, I had tagged the wrong stuff.
    I'll post screens here someday when I have something cool to share, amazing tech...thank you!!
     
    Nonakesh likes this.
  44. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Hi, I can't seem to get the Circular Displacement script working. I've followed the PDF's instructions and looked through this forum thread for what might be wrong. Do you have an example working with Circle Displacement I can take a look at? Thanks.
     
  45. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Do you really need the Circular displacement? I implemented a better displacement, the rendertexture displacement. It's shown in the example scene and there are tutorial section about it in the manual too.
    I personally think that the new system is easier to deal with and more flexible, although I've realized that it's still not exactly intuitive. I'm planning to create a more in-depth tutorial for it (maybe video), once I have the time.

    Anyways, just tell me what your exact problem is and I will try to help you!
     
  46. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Thanks, yeah I just have a few animals in tall grass and I want to be able to notice them from the disruptions in the grass they're making. Kind of like what happens on a cartoon, not really a trail just a rustling that moves with them as they move.
    This seemed like the quickest path to achieve that, oh and I also need it to be low cost on performance (so it works with many at once), having a camera for each animal seemed a bit overkill haha. I'm also running this in VR.
     
    Last edited: Oct 30, 2016
  47. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    If you look at the example scene that is included with the shader, you can see exactly that effect in use! There are actually two effects on the moveable sphere. One is the rustling you described, the second is a trail. You can disable the trail and just use the rustling!

    And you only need one camera, the displacement camera automatically follows the player camera, so the displacement will work wherever the player moves. From my experience that is WAY more efficient than the old displacement.
     
  48. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Awesome that works perfectly. Thanks!
     
    Nonakesh likes this.
  49. Abnormalia_

    Abnormalia_

    Joined:
    Jul 23, 2013
    Posts:
    128
    All grass vanished in 5.5 B10. No shader compile error as well. I know developers usually do not support betas but but I needed to test as they added Oculus 1.9 support there. Can you take quick look what's going there ?

    Update: It seems all grass is rendered at same spot at 0,0,0.
     

    Attached Files:

    Last edited: Nov 2, 2016
  50. Liens

    Liens

    Joined:
    Feb 2, 2012
    Posts:
    49
    Hi again, just have a few things to discuss here.

    1. Does this shader support GPU instancing (https://docs.unity3d.com/Manual/GPUInstancing.html) ? I think it would be a great fit since the requirements for it are also DX11 and it seems like a good performance enhancer.

    2. On a side note, I'm having a problem with the shader where the displacement mesh is causing the grass around the edges to displace, then snap back a little before fully displacing. If you keep your eyes on a single blade of grass at the front of the displacement you'll see what I mean:
    https://i.gyazo.com/1f20b982a15ad8789b2e7aef3c9d7ab1.gif

    3. It also has some initial snapping from normal grass to displaced grass and from displaced back to normal (you can see that in the gif as well), is there any way to solve both of these two issues?