Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fix for shimmering trees in Deferred mode

Discussion in 'General Graphics' started by makeshiftwings, Mar 18, 2015.

  1. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    As anyone who's tried to use terrain trees in Deferred rendering mode knows, they are surrounded by ugly white pixels all over the leaves. This is because there's a bug in Unity's fragment shader processing where it's impossible to draw to the depth buffer without also erasing pixels in the color buffer. There's a discussion about it here: http://forum.unity3d.com/threads/terrain-trees-sparkle-in-deferred-rendering.278851/ where I gave a replacement shader that fixes it by increasing the amount of clip on the shadow casting pass. I decided that looked bad and figured out two other ways to fix the problem that look better.

    Option 1: (recommended) Remove the special leaf shadow pass entirely and instead just use the builtin shadow pass by adding "addshadow" to the surface shader. This is the shader I've attached below. Just drop it into your project, and delete any other "TreeCreatorLeavesOptimized.shader" if you have one.

    Shader 2: Keeps the special leaf shadow pass but move tree rendering into the "Transparent" queue, so that the white sparkles are replaced by transparent invisible ones. You can do that yourself by editing the attached shader to uncomment the whole shadow pass at the bottom and add a new line after line 24 ("RenderType"="TreeLeaf") of "Queue"="Transparent".

    I'm not sure if there is a performance hit using either of these methods but I didn't notice a big difference. Option 1 is recommended since Option 2 might give you issues if you have transparent objects like windows or water in front of trees.

    If anyone with more knowledge (especially someone from Unity) wants to chime in and let us know if there's a better way to fix this that would be great.
     

    Attached Files:

    Sycobob, Baldinoboy, Eric2241 and 4 others like this.
  2. jgilbert

    jgilbert

    Joined:
    Aug 20, 2014
    Posts:
    10
    If I had three thumbs to put up you would get all three.
     
  3. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,521
    Thank you for this. The funny thing is this saves me some frames. Having high res shadow textures to get rid of the artifacts caused a frame drop. This works great man. Thank you

    Now I am trying to figure out how to get rid of the super shiny specular of the leaves when there is low lighting. Will not be AFS but at least will not look horrible.
     
    Eric2241 and makeshiftwings like this.
  4. MarcusWatson

    MarcusWatson

    Joined:
    Dec 9, 2014
    Posts:
    53
    A preliminary check in 5.0.1p3 seems to indicate it was fixed (Fired up TerrainComposer, generate a sample plateau landscape with trees, no sparkles/shimmers). I can't test further at the moment, but does someone else want to confirm?
     
  5. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,521
    It looks like they did. I did not check the trees out just the shader and saw addshadow. Funny that they threw that in there all of the sudden. As a free user I was not using Deferred in U4. Was this issue happening then too or is it just U5 deferred?

    Edit - Never mind. Was looking at wrong shader. Still had to put in addshadow.
     
    Last edited: Apr 30, 2015
  6. RTSlang

    RTSlang

    Joined:
    May 3, 2013
    Posts:
    58
    The New Beta does not fix this for me.
     
  7. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    Still broken in 5.0.2f1. :(
     
  8. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,521
    I am guessing they will probably never fix or upgrade anything for tree creator trees.

    Fortunately we have Lars and makeshiftwings.
     
    Last edited: Jun 10, 2015
  9. amarchal

    amarchal

    Joined:
    Nov 19, 2014
    Posts:
    3
    I still see this issue in 5.1.0f3 with our trees authored in Tree creator. In my case It seems tied to the water materials in use, maybe the reflection cameras they create. No water and i can get the trees to play nice, turn the water back on and it goes all sparkly. I have ended up just yanking out the shadow part of the fast optimised leaf shader to get around this similar to what makeshiftwings did(thanks for the tip!), it kills the sparkle but now i have to fake all my tree shadows which is pretty gross.
     
  10. CandlewaxJames

    CandlewaxJames

    Joined:
    Dec 1, 2012
    Posts:
    4
    Thanks for taking the time to create a fix. I can't seem to get it to work though. Do I just drop the shader into the root folder of my project?

    I tried messing with the code of the shader after I imported it but it didn't have an effect on the trees in my scene so I get the feeling they are not updating to use this shader instead. Is there any reason or extra step I'd need to take in order to fix this? In my scene the trees are added as part of the terrain, if that helps.
     
  11. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,521
    All you need to do is move the shader to your project. For instance make the folders and put them in Shaders-TreeCreator. What I did is grabbed the builtin shaders from Unity and edited it my self. I do not like hidden shaders;).

    Grab the Built in Shaders in the Unity Download page




    Do not remember if it is a zip but once extracted open it then DefaultResourcesExtra-Nature-TreeCreator. There you will find all your tree creator shaders. All I did was change the name of the bark and the leaf shader to be unhidden. Then added the addshadow which @makeshiftwings made this whole thread for. Add right after noforwardadd.

     
    MarcusWatson likes this.
  12. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    As BaldinBoy said, you can just put it anywhere in your project files and it should work. Make sure you don't have any other "Tree Creator Optimized" shaders anywhere else in your project though. If you've got any asset store assets that add terrain or trees, some of them might have their own Tree Creator replacement shaders and you'll need to delete those.
     
  13. CandlewaxJames

    CandlewaxJames

    Joined:
    Dec 1, 2012
    Posts:
    4
    Thanks Baldinoboy and makeshiftwings for the help. I was using a scene I had bought from the asset store so it must have had it's own "Tree Creator Optimized" shader somewhere, although I couldn't find it for the life of me. In the end, I created a new material for all the foliage and replaced it with the old one in all of the prefabs. But even then it didn't update until I closed and re-opened Unity.

    It all seems to be working now except for one minor issue. When I enable depth of field on my camera I notice some black pixels around the tree leaves, is this to do with the changes you made to the shader, makeshiftwings? Or is this a different problem entirely?

    Either way, thanks again. This problem was really getting to me lol.
     
  14. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    91
    Baldinoboy likes this.