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

How to correctly use unity_LODFade.x in cross fade mode?

Discussion in 'Shaders' started by Project-Mysh, Apr 14, 2015.

  1. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    Hi there community!

    I'm trying to figure out how to correctly use unity_LODFade.x in cross fade mode with my own shader to get a disolve effect in LOD transitions.

    The problem is, as it says in the manual (http://docs.unity3d.com/Manual/class-LODGroup.html), that unity_LODFade.x goes from 1 to 0 in current LOD and from 0 to 1 for the next LOD.
    I can't figure out how to use this float to lerp any effect because as you can imagine, there is a point where the two floats get to equal 0,5. This means, for example, that if you use alpha fade to do a transition between the 2 LODs, there is a point where you can see through the object.

    Anyone have a solution for this? Any ideas? (all the LODs have to share the same shader and materials).
     
    Last edited: Apr 15, 2015
  2. teknic

    teknic

    Joined:
    Oct 18, 2012
    Posts:
    32
    Not sure if you're still looking into this, but if you simply add unity_LODFade.x to itself and then clamp01, the crossfade more aggressively transitions between LODs - which solves the problem.

    add something like
    Code (csharp):
    1.  
    2. float alpha = clamp(unity_LODFade.x + unity_LODFade.x, 0, 1);
    3.  
    or if you're working in shaderforge add a code node which contains this:

    Code (csharp):
    1.  
    2. #pragma multi_compile _ LOD_FADE_CROSSFADE
    3.  
    4. float f = max(step(unity_LODFade.x, 0), unity_LODFade.x);
    5. return clamp(f + f, 0, 1);
    6.  
    and connect it to either opacity or opacity clip.
     
    Last edited: Mar 28, 2017
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Unity's LODFade is intended to be used with their dither function.
    See UNITY_APPLY_DITHER_CROSSFADE in UnityCG.cginc