Search Unity

Advanced foliage shader [released]

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

  1. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    I have a question regarding the vertex color painting for foliage bending (both using colliders and windzones).
    We've tried using a standard plane and coloring it RGB as shown in the attachment.

    messageImage_1471226892643.jpg

    However we only get bend reactions from the blue vertices as shown here:

    messageImage_1471226906891.jpg

    We've tried various shades and colors and it seems to only displace in the XZ plane with no rotation. Is there something we're missing or is there another way to control the bending to allow for pivot rotations? (ie. you have a flower rooted at the base and it bends along the stem)

    Thanks!
     
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    what you need is a gradient from blue = 0.0 at the root or pivot to blue = 1.0 at the top/outmost parts of your plants.
    green controls edge flutter, red the phase.
     
  3. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    is there a video or something to follow for vertex painting for objects from maya? I'm a little confused as to which shader in unity to use with which vertex paint
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i am a little bit confused by your question.
    however there is no video showing how to apply vertex colors in maya.
    and vertex painted meshes should be rendered using one of the foliage shaders.
     
  5. moure

    moure

    Joined:
    Aug 18, 2013
    Posts:
    184
    @larsbertram1 don't know if you have come across this, but there is a 3dsmax tool (made by unreal engine devs) that makes the process of foliage vertex painting a bit easier. I came across it a couple of days ago, when checking foliage shaders for custom meshes. You can check it out, and if the result is compatible with your shaders you could include a link in your documentation to help max users ;)
     
  6. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Might want to keep in mind UE and Unity using different axis setup
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    thanks for the link.
    unfortunately i do not have 3dmax. and i have my own tool which works as asset importer script in unity :)
     
  8. Trekopep

    Trekopep

    Joined:
    Dec 18, 2013
    Posts:
    15
    Hi there!
    I'm loving this, but I've got a couple questions for problems I'm having:
    • In the documentation it says to put materials on the base of the tree (the Mesh Renderer component, I'm assuming that means) that have the Afs Tree Creator Bark/Afs Tree Creator Bark Deferred and Afs Tree Creator Leaves shaders. However, I've found that if I do that, the trees disappear once they reach full billboard state. I applied the Optimized shaders instead and that seems to work, but I'm wondering if I'm missing something?
    • The lighting on the trees isn't quite the same when it switches from billboard to mesh. You can see the difference in the picture below with the trees on the left being mesh and on the right being billboard. I'm assuming I just haven't set something up right? (Other than that, the transition is smooth as butter.)
    Afs Tree Shader Light Issue.PNG
    • Due to some postprocessing effects requiring it, I'm using deferred rendering. As you know, this leads to some issues with rendering fog, as the billboards are not put into the depth buffer and thus take on the fog attribute of whatever is behind them. However, in this demo from your Advanced Terrain Shaders V2 Asset, you allow switching to deferred rendering, and your fog works perfectly with the billboarded trees. Any advice on how to achieve that?
    Afs Deferred Rendering Billboard Tree and Fog Issue.PNG

    Also, I'm curious what the difference is between the bark shader and the deferred bark shader. That might be a nice thing to put in the documentation?
     
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    hi there,
    i meant: put the "Afs Tree Creator Bark/Afs Tree Creator Bark Deferred and Afs Tree Creator Leaves shaders" on the "base materials" of your tree which are the materials that are assigned to the branch and leave nodes in the node editor or the tree creator.
    as soon as unity updates the tree it will create optimized materials using the "optimized" versions of the assigned shaders.
    the Mesh Renderer component of the tree always has to use the optimized materials/optimized shaders as only thes provide billboarding.
    i treally will never be. but you can get very close... it loos as if you were using gamma color space? please switch to linear as gamma is not really supported.
    that is unity – 3.x? and deferred here means legacy deferred or prepass deferred which had no problems with fog.
    but in order to solve your problem you would have to apply fog as image effect before rendering transparent geometry, see: https://docs.unity3d.com/ScriptReference/ImageEffectOpaque.html
    then billboards will be rendered on top using standard forward fog.

    i hope this helps,
    lars
     
  10. Trekopep

    Trekopep

    Joined:
    Dec 18, 2013
    Posts:
    15
    Thanks for the prompt response!

    Still a little lost here. First I tried having those shaders on my base materials and both materials in the Mesh Renderer set to "none", but the trees just didn't show up. I tried setting the Mesh Renderer materials to the optimized materials generated within the tree object, and that seemed much closer. It looks like translucency is working now where it wasn't before. However, as can be seen in the picture, the texture isn't as vibrant (on the right) as when I keep my base tree textures blank and put bark and leaf materials with the optimized shaders applied to them directly into the Mesh Renderer (on the left).
    Afs Tree Shader Light Texture.PNG
    Note that I have some purple ambient lighting, which accounts for the purplish hue on the trees. However, even with the ambient lighting turned off, the leaf textures just don't show up as visibly as I'd like.
    Afs Tree Shader Texture Not Showing Up.PNG

    Linear color space didn't seem to do anything to help that. Experimenting more, it appears the issue is that ambient lighting isn't taken into account for billboards, so that snaps on for the meshes.

    THANK YOU! I've been looking everywhere for this solution. Adding [ImageEffectOpaque] before OnRenderImage in the GlobalFog script made the fog ignore the trees. From there, I adjusted the regular fog in the lighting panel until it matched the GlobalFog (the regular fog also affected the GlobalFog, so it took a little tweaking of both), and now everything is beautiful in that respect.

    Thanks again for the amazing support,
    Trekopep
     
  11. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,526
    Hey @Trekopep ,

    For the materials you need to make two new materials using AFS Tree Creator ......... Optimized for the bark and leaf and then place these two new materials in the Mesh Renderer Materials slots like this:



    You will have to of course place the trees textures in the new AFS materials you create.
     
  12. Trekopep

    Trekopep

    Joined:
    Dec 18, 2013
    Posts:
    15
    Closer!

    @Baldinoboy I did as you suggested: I now have bark and Needles materials using the regular shaders, which I put into the tree base materials, and bark_optimized and Needles_optimized materials using the optimized shaders, which I put into the Mesh Renderer on the tree.
    Afs Tree Inspector Materials.PNG

    Now the only problem is that the textures have a jagged edge, as if they're being cut off somehow.
    Afs Tree Shader Texture Jagged Edge.PNG
     
  13. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,526
    Make sure to use the combined textures the treecreator made.
     
  14. Trekopep

    Trekopep

    Joined:
    Dec 18, 2013
    Posts:
    15
    Use the combined textures where? In the Needles_optimized material? Or the Needles material? Looking closer now, I can see that in the Tree 1_Textures folder, the diffuse texture (bottom left) is showing up as incredibly grey, which was my problem two posts ago.
    Afs Tree Textures Problem.PNG

    However, looking at how the textures are scaled here, I set my Needles_optimized material Base (RGB) Alpha (A) texture to the original needle texture, and set the tiling to 2:1 instead of 1:1. That appears to have worked (as shown below), but I can't help but think I'm doing something very wrong. I feel like I shouldn't need to make my own "optimized" materials, and that the default optimized materials created by the Tree Creator should be not coming out as grey like they are.
    Afs Tree Correct Texturing.PNG

    As a side note that may be related, I'm getting the following error in the console:
    [Edit] It looks like this error may have been because I moved the Afs folder into another folder somewhere. Nonetheless, when I took it back out to directly in the Assets folder, though the error hasn't appeared again, the problem persists.
     
    Last edited: Aug 26, 2016
  15. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,526
    Oh sorry, did not notice the comment about the grey textures. Think this is the same thing that drove me away from tree creator a while back. Took a few months to get a simple fix. Might not be it but it looks like this:

    http://answers.unity3d.com/questions/414291/unity-tree-creator-bug.html

    Fix for it was disabling fog when the tree creator renders the textures. So disable the fog and change something on the tree so it re-renders the materials.

    After that place the tree creators combined textures in the AFS optimized material you created for the finished tree and not the one you inputted into the tree creator script.
     
  16. Trekopep

    Trekopep

    Joined:
    Dec 18, 2013
    Posts:
    15
    That was absolutely the problem. Everything looks gorgeous now. There were a couple shader errors causing the bark to be pink, but I reimported the shaders and that fixed it. Thanks for all the help!
    Afs Working Trees with Fog.PNG
     
    Baldinoboy likes this.
  17. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,526
    Awesome! Glad it is working
     
  18. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    Hi guy's
    i'm really loving this pack however i'm having a few troubles with trying to setup the shaders on my models pacifically grass and custom trees Scene.PNG
    As you can see with this grass model i am using the AFS/Grass shader V4 and it looks pretty good in the scene view but when viewed in play mode it looks as though the back faces aren't getting the correct lighting and showing up darker than they should be as seen in this next image. PlayMode.PNG
    The second thing i'm having trouble with is applying the Afs Simple Tree V4 shader to a custom tree model such as this
    TreeNoShader.PNG
    The tree is manually modeled geometry from blender and i've been trying to set it up in a way thats similar to the pine trees in the demo scene, it all works fine except the bottom of the trunk is not stationary and the trunk verts tremble like leaves, splitting it apart.
    WithShaders.PNG
    TrunkTremble.PNG
    i've been trying to correctly set up the vertex color alphas but i just cant get my head around how it all works. any help will be greatly appreciated in letting me know how to set this tree up.
     
  19. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    it seems as if you haven't added the "SetupAdvancedFoliageShader" script to your scene.
    next to a lot of other important things this script will pass proper ambient lighting values to the tree rendertex shaders .
     
  20. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i recommend to just apply the regular (not optimized) "Nature/ Afs Tree Creator Bark" and "Nature/ Afs Tree Creator Leaves" shaders to the base materials of your tree. These base materials are assigned to e.g. the branch nodes of your tree like shown in the screen shoot attached – or to the mesh you use for your leaf planes.
    Unity will automatically combine the used textures, create the optimized materials using the "Afs optimized" shaders and assign to the tree's mesh renderer – so there is no need to do it manually.
    Please have a look at the "Conifer[TreeCreator]" tree that ships with the package to find out more.

    treematerial.png
     
  21. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    have you parented your grass meshes under a game object which hold the "combine children script"?
    grass needs to be combined. this will give you less draw calls and bake the underlying ground normals into the grass mesh to give you proper lighting on grass.
    in edit mode the grass shader simply takes a normal pointing upwards (in object space) to shade the grass while in play mode it expects proper normals to be baked into the mesh.
    as you do not seem to use the "combine children script" the grass shader in play mode picks up the original vertex normals of your grass model wich lead to the rather harsh lighting.
     
  22. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    I highly recommend to use the "foliage tool" to setup your custom model as the "Nature/AFS Simple Tree v4" shader expects bending information to be stored in vertex colors and UV4.
    you may simply assign some edge fluttering (vertex color green) and different phases (vertex color red) on the leaf planes in blender and than add the primary and secondary bending (both stored in UV4) using the "foliage tool".
     
  23. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    I'm getting this rather odd result
    BlackGrass.PNG
    this is the setup with the combined child script attached to the CombineChilds empty object
    Childs.PNG
    and i will be giving what you mentioned about the tree a shot also thanks
     
  24. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    Yes!! the bending works perfectly now thank you so much i really appreciate it!! only now figuring out how to use all the cool tools due to your help :)
     
  25. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    that looks as if the script can't find any underlying geometry to sample the normals from...
    have you assigned a terrian to the script?
     
  26. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    yeah I believe so, I am using gaia maybe that's got something to do with it?
    Terain.PNG
     
  27. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i do not think so.

    but what happens if you turn on "Debug sampled Ground Normal"?
    each grass object should show the sampled ground normal as red line pointing upwards. if a raycast misses the ground the line should turn to yellow (simply test it by moving a grass object up and down).

    and another test: simply hit play and look at the hierarchy: does grass really get combined?

    lars
     
  28. KaylxJang

    KaylxJang

    Joined:
    Aug 26, 2016
    Posts:
    2
    Hi, first time posting!
    I just bought the afs and it is fantastic so far, but I'm having trouble importing custom .fbx models for some custom grass. I've been at this for a while now and documentation, Googling, and thread hunting turned up nothing.

    I guess the real question is "How do I import custom grass models using Blender?"

    Here I have a .fbx model of some grass planes in blender
    Blender FBX Model.jpg


    When I throw the model into Unity with the standard unity shader, it's fine. But when I apply the (AFS/Foliage Shader Deferred SingleSided v4) shader to the model, it turns into this mess and wiggles all about in the editor when I drag it around.

    Grass Types.jpg
    [from left to right: default afs dry grass prefab, default afs dry grass prefab with custom texture, custom model with custom texture]

    I believe I even named the file correctly b/c I'm using Blender (which has trouble with vertex coloring). I already set up the scene with the Setup AFS Script and all the default afs prefabs are working wonderfully. But this had been really confusing me...

    Thanks for the help!

    (and sorry if i butted into the middle of a discussion here)
     
  29. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    well, these two things do not work together.
    naming your file: "xyz_AfsGM06.fbx" will make the GrassMeshPostprocessor script bake bending information to vertex color alpha – like it is needed by the grass shaders.
    the foliage shader need bending information to be stored in vertex color rgb or vertex color rg and uv4.

    so you can either:
    - change the shader to the afs grass shader.
    - use the foliage tool to bake proper bending information for the foliage shaders.

    lars
     
  30. KaylxJang

    KaylxJang

    Joined:
    Aug 26, 2016
    Posts:
    2
    Thanks lars! Got it working, and that cleared up a few things :D
     
  31. AntonBertelsen

    AntonBertelsen

    Joined:
    Oct 13, 2014
    Posts:
    37
    Hey. I know it's probably stupidly early to point this out, but your shaders seem to be broken in the new Unity 5.5 beta. :)
     
  32. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    unity 5.5 beta is out of scope right now :)
     
  33. miczec123

    miczec123

    Joined:
    Nov 11, 2014
    Posts:
    5
    Hey Lars,

    awesome product - i just got one probably really stupid beginner question for better understanding. I want my grass to have a constant, flat color. I do achieve this in the editor view - however, when i hit play, the color changes to a not so desired darker colored gradient. See the screenshot below:

    what am i doing wrong? do i somehow have to change the grass shader to a unlit one?
     
  34. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    may be. in editor the grass shader lights the grass by its up vector.
    in play mode the shader expects the ground normal to be baked into the mesh (which usually would be done by the combine children script).
    most simple way would be to edit the SetupAdvancedFoliageShader.cs script and comment those parts of the code which switche the shader keyword in:
    void afsSetupGrassShader()
    simply keep: Shader.EnableKeyword("IN_EDITMODE");
    you may of course also edit the shader to make it always use the "IN_EDITMODE" lighting.
     
  35. miczec123

    miczec123

    Joined:
    Nov 11, 2014
    Posts:
    5
    yep, that did the trick. thanks!
     
  36. miczec123

    miczec123

    Joined:
    Nov 11, 2014
    Posts:
    5
    Just one more question concerning the combine children script, then I'll shut up: Is it possible to disable receiving shadows while keeping casting shadows (on the ground) on? I don't wan't my grass to cast shadows on itself while bending.
     
  37. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    no, not in deferred unless you hack the lighting.
    in forward you might uncheck "receive" shadows in the mesh renderer component – but that is not supported by the script so you would have to add it manually to the script or statically combine the meshes and then disable it.
     
  38. architek1

    architek1

    Joined:
    Sep 12, 2015
    Posts:
    2
    Hello there. I'm currently having a problem with the combine children and manual placing of in this case rice. I have already manual placed the prefabs in the locations but it doesn't seem to work as the script that i'm running on the prefab is a grow script. Can someone explain to me how to get these to work with AFS and cut down my drawcalls?
     

    Attached Files:

  39. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i do not know how your grow script looks like... but i doubt that it plays nicely with the combine script :-(
    using the combine script you will get a single and huge mesh. so you won't be able to e.g. scale single instances – simple as they do not exist any more.
    you may however shift the growing into the vertex shader. depending on what shader you use on the rice you could simple scale v.vertex.y and mask it by v.color.a (grass) or v.texcoord3.y (foliage: vertex colors and uv) / v.color.b (foliage: vertex colors only).
     
  40. architek1

    architek1

    Joined:
    Sep 12, 2015
    Posts:
    2
    Thanks Lars! The grow script is in the Inspector on the picture I posted. I was looking for a way to be able to change, hence the combine script of yours, so I could bring down all those draw calls and change the grow time without having to change every single Prefab instance.
     
  41. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    well, once you have combined the instances you will have to find a way to make them grow as i mentioned before...
     
  42. EyeOnYou

    EyeOnYou

    Joined:
    Jun 11, 2015
    Posts:
    21
    Hi Lars,

    is the demo scene (
    ) included in the foliage pack v2?

    Thank you for a reply.
     
  43. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Hi Lars/everybody.

    I am looking into tree's/foliage assets for my project and just had a few questions.

    I use Terrain Composer, could I use AFS with this? I want to have the grass/foliage/tree's setup as objects rather than using built in terrain options so they can follow my procedural options for seasonal changes etc..

    I also want my tree's climbable so they cannot be part of the unity terrain.

    Thanks in advance,

    Andy
     
  44. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    nope...
     
  45. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    yes, you can.

    trees however are optimized to work with the terrain engine and offer improved billboarding.
    nevertheless, the tree shaders should also work nicely on manually placed trees: just make sure that the billboard distance is set rather high.

    all other shaders offers versions to work with the terrain engine and versions optimized for manually placed objects.

    lars
     
  46. EyeOnYou

    EyeOnYou

    Joined:
    Jun 11, 2015
    Posts:
    21
    Why not?
     
  47. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    because it is one a simple terrain and a model from the official bootcamp demo.
     
  48. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    Hey Lars any idea why my tree is see through when I have SSAO applied?
    Capture1.PNG Capture2.PNG
     
  49. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    it looks as if you were using a wrong "cameradepthnormal" shader:
    please make sure that
    • there is only one "cameradepthnormal" shader in your project
    • the one that ships with afs is assigned in the settings -> graphics window -> depth normals when using unity >= 5.4
    Bildschirmfoto 2016-10-24 um 12.34.34.png
     
  50. jamie-jenssen

    jamie-jenssen

    Joined:
    Apr 3, 2014
    Posts:
    11
    That worked! you're always helpful thank you :)