orels1 / orels-Unity-Shaders

A collection of practical Unity shaders for your next project
https://shaders.orels.sh
Other
169 stars 14 forks source link

Add LOD Cross Fade support to the Cutout shader #2

Open An00nymushun opened 2 years ago

An00nymushun commented 2 years ago

Unity enables shaders to smoothen the transition between LOD meshes (and during culling) and a good balanced approach is to do it with a cutout shader and dithering.

You just have to make the CutoutFragment() look something like this

sampler2D unity_DitherMask;

void CutoutFragment() {

    float2 vpos = d.screenPos.xy / d.screenPos.w * _ScreenParams.xy;

    vpos /= 4; // the dither mask texture is 4x4
    float mask = tex2D(unity_DitherMask, vpos).a;
    float sgn = unity_LODFade.x > 0 ? 1.0f : -1.0f;
    clip(unity_LODFade.x - mask * sgn);

    #if !defined(_NATIVE_A2C)
    if (o.Alpha < _Cutoff)  {
        clip(-1);
    }
    #endif
}

Code is taken from https://github.com/keijiro/CrossFadingLod and UnityCG.cginc

orels1 commented 2 years ago

Yeah, I have LOD fade planned, as I need it for my own WIP world too. Hopefully will be able to push it out together with the current in-progress rework of the shaders