Search Unity

Advanced foliage shader [released]

Discussion in 'Assets and Asset Store' started by larsbertram1, Apr 24, 2012.

  1. bac9-flcl

    bac9-flcl

    Joined:
    Dec 5, 2012
    Posts:
    829
    Thanks, will do. Best approach would probably be to weight it by some UV or vertex color channel, so that I can simply mark problematic Y-centers with a value that kills the effect while leaving edges fluttering.

    Gotcha, just implementing simple height based bending for stuff like grass and wondered if I need to feed different values into those two channels.

    Makes sense. Can you elaborate a bit on the normals there? Do they look like the rightmost example (not necessarily with the "base" object still present)?



    It's a popular trick that actually improves the look by completely killing hard shading edges between differently oriented but closely situated faces. It's not at odds with PBS. :)

     
    Last edited: Jun 12, 2017
    Lex4art likes this.
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    yes, they do.
    and normals like this do not play well with translucent lighting nor with specular highlights.
    translucent light has to calculate the flipped normal in case we lit a backface (single sided geometry).
    and specualr lighting needs a proper normal as well.
    both functions will create rather strange results when fed which such smoothed normals.
    not to mention that speed tree leaves bend a lot – without doing anything to the normals. so the normal will always be the same whereas the view and light directions change.
    so it is at odds with pbs... the reason why untiy made speetrees use real lambert lighting.
     
  3. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    I am having an odd issue with dropping detail meshes on terrains set as vertex lit. I have my own multi-threaded foliage and details renderer that simply uses the lossy positioning in the detail map as meta data (and then clears it out). In my test project everything worked fine. I have brought it over into my game now that it is complete, and for some reason, on some terrains adding a vertex lit prefab as a detail mesh simply doesn't display anything when dropped (in grass render mode it display as red). This seems to happen on some terrains and not others.

    I have checked the terrain settings and I just cannot seem to figure out why this is occurring. I'm not using a custom material for the terrain, using deferred + linear, have AFS setup in the scene configured for the terrain, etc etc. I searched all my shaders to make sure no one else was overwriting the hidden terrain vegatation shaders (none were). I can get by with setting them as grass since I just discard them anyway for now at run-time but it is certainly ugly at design time. As a note, the AFS grass prefabs dropped as grass detail meshes work fine.

    Unity 5.5.4. Any ideas what is going on?
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    that sounds very interesting and i would love to hear more about it.
    although i have no idea how you create meshes multithreaded. doesn't it always have to come back to main thread when you finally combine vertex, index, normal, tangent and uv arrays to an actual mesh?

    no, sorry.
    as i do not know how your custom solution works.
    i doubt its a problem caused by the shaders as it works on some terrains.
    you may however check this by wiping out the afs shaders.
    if it works on all terrains when using the built shaders then we could have another look into afs.

    lars
     
  5. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    Thanks! To be clear, my renderer doesn't do anything at design time. This question about design time scene editing.

    This is really just a basic AFS question. I drop a foliage prefab as a vertex lit, detail mesh and it doesn't display on some terrains (but I can set it to grass and it displays as red). This didn't happen in my test project and also only happens on some terrains in my game.

    My custom stuff doesn't happen until run-time and wouldn't affect any of this.
     
  6. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i see. so it is all about design time?
    and by "I drop a foliage prefab as a vertex lit" you mean you paint it to the terrain?.
    do the terrains, where the vertex lit prefabs do not show up, do have negative coordinates?
     
  7. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    You rock! Moving the terrain to full positive fixed it. The terrains I was playing with that weren't working were gaia terrains, which offset negative in order to center at 0,0,0, but standard new terrains don't do this.

    Sorry, yes :). I add a detail mesh, vertex lit prototype and then paint it on the terrain (and it is invisible unless I pick the rendering mode as grass)
     
  8. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    that should not be necessary though.
    try to fix it by editing the "AfsFoliageShader Terrain" and find:
    v.color.b =
    this should be changed to:
    v.color.b = saturate( abs(2 * (_AfsWaveAndDistance.w - dot (offset, offset)) * ( 0.85 /* was 1.0 */ / _AfsWaveAndDistance.w) ) );

    i added an "abs" so the shader should now handle negative values properly.
     
    hopeful likes this.
  9. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    Thanks! I can confirm this resolved the issue.
     
  10. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    fine.
     
  11. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    1,151
    Hello! This asset looking really good, and im really considering buying it, but I wonder a bit what plants and such it works with? Does it work with turbosculpurs HQ Photographic Textures Grass pack? Or what do you recommend?

    Thanks! =)
     
  12. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    the tree shaders work on any tree creator created tree.
    the grass shader works with grass textures (not recommended) and/or simple "detail meshes" (recommended).
    in order to use the foliage shader you will have to setup your plant meshes properly by adding vertex colors and(or uv4. you can do this manually in your 3d app or use the included "foliage tool" to do so.
    please have a look at the docs:
    https://docs.google.com/document/d/...Y5H46FtpSDM1UQdRk/edit#heading=h.3zmd2wgozbsj

    i think so. but in order to get the most out of it (translucent lighting) you may have to add your own translucency texture.
     
  13. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    1,151
    [QUOTE="
    the grass shader works with grass textures (not recommended) and/or simple "detail meshes" (recommended).[/QUOTE]

    Could you refer to any grass on the asset store that have been used with this asset, that are recommended and have and/or simple "detail meshes"?
     
  14. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    unfortunately no.
    but you can use a simple quad for example and add some vertex color alpha to it (or use the grass mesh importer script to do so) in case you just want to make it look like a simple texture added to the terrain as grass.
    unity would do the same (add the texture as quad), but in case you use your own quad you can manipulate the uvs und use a manually controlled texture atlas on the grass which is mandatory in case you want to add a 2nd texture for normals and/or translucency.
     
  15. bac9-flcl

    bac9-flcl

    Joined:
    Dec 5, 2012
    Posts:
    829
    @larsbertram1

    Figured you'd like to see the how we use your shader! :)

    • http://i.imgur.com/XDsDYxZ.gifv - Height based wind seen here (every object has a root height set through material property block on an instancing-friendly property, allowing it to bend proportionally to distance off the ground without any vertex color setup)
    • http://i.imgur.com/1WGkYKB.gifv - Wind scales up a bit with velocity and falls off once branches fall closer to the ground
    • http://i.imgur.com/6s9Xvf1.gifv - Ground avoidance (vertex positions within certain range off the ground are remapped in world space to stop penetration of the ground; similar approach is also used to apply world-space vertical scaling to downed trees to make them look more natural).
    I quite liked extending your code, it's very clean and fun to work with! :)
     
    mattis89 and Lex4art like this.
  16. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    1,151
    That looked really damn good!! holy S*** this is a good shader... Im just yet too noob to use it tough :/.. think I have to let it be for now...
     
  17. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    thanks for sharing! your modifications look pretty awesome.
    are you sure you have edited my shaders? :)
     
  18. Dwight_Everhart

    Dwight_Everhart

    Joined:
    May 11, 2014
    Posts:
    123
    I just discovered AFS on the Asset Store. It looks great! But I'm wondering if it will work with MapMagic. Does it need a special integration, or will it work automatically with the grass generated by MapMagic? MapMagic generates standard Unity terrains with standard Unity grass.

    Also, are there more videos of AFS? The one on the Asset Store page looks amazing but is very short. I'd like to see more videos, with demonstrations of all the features, particularly grass bending.

    Finally, does AFS offer any performance improvements over Unity grass? Does it eliminate the patch rebuilding performance hit that Unity's grass creates? Will I be able to place more grass than I can with Unity's standard grass?
     
  19. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    the afs grass shader replaces the built in one. so it should work fine.
    please make sure you add grass as "detail mesh" and not as a simple texture – even if you use just a simple quad.
    sorry, not at the moment.
    no, as it is based on the built in grass.
     
    Dwight_Everhart likes this.
  20. Dwight_Everhart

    Dwight_Everhart

    Joined:
    May 11, 2014
    Posts:
    123
    Thanks for the information. I may give it a try.
     
  21. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    So when building a player I get a shader error:

    Shader error in 'Hidden/AfsCamera-DepthNormalTexture': semantic 'VFACE' unsupported on ps_4_0_level_9_3 at line 302 (on d3d11_9x)

    First I'm not sure why this is being compiled since I do not have a depth normal shader assigned (which in Unity 5.5.4 I have to see by changing the inspector to debug mode - where it is set to the unity default shader). In any event, this error occurs if my platforms are dx9/dx11 or even just dx11. The build completes and will run in a test app when running through a simple build in a test application. However, BuildPipeline will return this as an error message, and that means my custom build processes I use to build my game panic and fail out before they complete when this occurs because it is reported as an error. Now, I can hack it to treat this as a non-error for now, but I would prefer a better solution. I'm not a shader guy, and I'm sure this is an eye-rolling moment for those that are - but any help would be appreciated.
     
  22. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you have to edit the "Afs_Camera-DepthNormalTexture" shader. so open it and find:
    Code (csharp):
    1. SubShader {
    2.     Tags { "RenderType"="AFSFoliageBendingInstanced" }
    3.     Pass {
    4.     Cull Off
    5. CGPROGRAM
    and append:
    Code (csharp):
    1. // target 3.0 needed because of vface
    2. #pragma target 3.0
    find:
    Code (csharp):
    1. SubShader {
    2.     Tags { "RenderType"="AFSFoliageBendingVertexLit" }
    3.     Pass {
    4. CGPROGRAM
    and append:
    Code (csharp):
    1. // target 3.0 needed because of vface
    2. #pragma target 3.0
    that should fix the problem.
     
  23. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    The manual says we should enable `Bypass sRGB Sampling`. However the editor in 5.6 has changed, i cant find this option for normal maps. Since we are having issues with normals i fear this is the cause.
     
  24. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    in 5.6. it is the other way around:
    - set "Texture Type" = "Default"
    - uncheck "sRGB (Color Texture)"
     
  25. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
  26. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    the textures should be "combined normals" – so you have to set "Texture Type" = "Default".
    the texture in your gif however looks like a regular normal – which will not work.
     
  27. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    In the Foliage Shader Overview Demo rainy scene I get the following error:

    Shader error in 'Lux/Standard Lighting/Nature/Terrain/Standard': unrecognized identifier 'UNITY_SHADOW_COORDS' at line 139 (on d3d11)
    Compiling Vertex program with POINT _TERRAIN_NORMAL_MAP
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP

    I'm on Unity 5.6.2f1. Forward/Linear rendering. It looks like all the Lux shaders have compile errors. And the terrain in the demo scene is all pink.

    [EDIT]
    This is for AFS 5
     
  28. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    lux has been updated to version 2.02 which works fine in unity 5.6.
    you can grab it from the asset store or download it from github:
    https://github.com/larsbertram69/Lux-2.02-Personal
     
  29. jjobby

    jjobby

    Joined:
    Nov 28, 2009
    Posts:
    161
    Hi, I'm using Unity 5.6.2f1. I've imported latest version and the foliage movement is strange. This is demo scene from the package.



    What's the problem? Is it not compatible with Unity 5.6?
     
  30. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    looks as if the wrong bending mode is assigned.
    i will have a look into tat later.
     
  31. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I'm still missing something. Lux compiler errors have gone away, but the terrain is pink.

    upload_2017-6-29_6-30-53.png
     
  32. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    if the errors are gone you may have to reassign material/shader to the terrain.
     
  33. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Also, nothing seems to be rendering correctly. And it doesn't look anything like the video example. In some demos, for example, I see the same tree with 3 different colorings depending on distance and as I approach them then the colors change. This is from the CTI tree demo.

    upload_2017-6-29_6-48-46.png

    In the wind demo, the plants warp and morph until they are very distorted.

    upload_2017-6-29_6-50-35.png

    In the Trees demo, The tree also morphs and distorts in the wind the same way.
     
  34. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    The terrain material has InternalErrorShader set. What shader is it supposed to use?
     
  35. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you have to assign the proper deferred shaders: lighting and reflection. please follow the documentation.
    that has been reported above: i will look into that tonight. maybe tomorrow.
     
  36. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    lux terrain.
     
  37. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    OK. I had assumed that the demo scenes would have already had the correct setup. My mistake.
     
  38. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    they have - in unity 5.4. and 5.5 though.
     
  39. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    OK, I'll try to find it in the documentation, but I have to take off until this evening. I tried to find it in there a little this morning, but couldn't find the info. If you could point me to the correct section that would be helpful.
     
  40. Migueljb

    Migueljb

    Joined:
    Feb 27, 2008
    Posts:
    562
    Is there a tree tutorial showing how to take a unity tree and use the vertex colors script. Showing the proper way to bake in the vertex colors for good wind movement. I can never get it to work right always feels to up and down never a direct way to make it work properly. I either get to much wind or not enough no matter how I tweak the options.
     
  41. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i had to open the demo in 5.6. myself to remember this issue, sorry.

    all you have to do to fix this is to check "Enable instancing" in the material inspector of the given plant.
    Unity 5.6. disables instancing by default, but the shaders, plants and the setup up script expect instancing to be enabled. So the shaders fall back in the "dynamic batching" mode – which does not play well with the given "wave size" as defined in the Setup script.
     
  42. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i am not sure what you mean with that.

    do you want to convert a tree creator tree?
    then there are not many options apart from the texture settings. the script will simply copy the baked bending information to the proper channels used by afs.

    do you want to convert just any kind of tree geometry?
    in this case you will most likely have to spend some time in your 3d app adding proper vertex colors. the foliage tool can not handle complex geometry properly.
     
  43. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Thanks. I'll give that a ty. Also, why do the rocks look all green in the Tree demo?
     
  44. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    there are no rocks in the tree demo.
     
  45. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    uploading version 5.02 right now which brings some minor bug fixes and LOD cross fading to the foliage shader.
     
  46. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I may have the wrong demo then, but I'm almost certain it was that demo. Whatever the case, one of those demos has big green rocks in it.
     
  47. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    OK, so it was the Foliage Shader Overview Demo rainy. here is a pic.

    upload_2017-6-29_18-13-50.png
     
  48. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    that is caused by the fact, that you did not assign the proper deferred lighting and reflection shaders.
    you have to do this manually. there is nothing i can do about it as far as the package is concerned.
     
  49. jjobby

    jjobby

    Joined:
    Nov 28, 2009
    Posts:
    161
    That fixes the problem. Thanks!
     
  50. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Where in the document can I find that information? It's not exactly obvious.