Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Toon Shader Differences Between Unity 2018 and 2019

Discussion in 'General Graphics' started by Cherubim79, Feb 21, 2020.

  1. Cherubim79

    Cherubim79

    Joined:
    May 3, 2014
    Posts:
    55
    There are some weird outline things going on in Unity 2019 with a Toon Shader I downloaded way long ago for a game, I'm showing you here on the screenshots, and posting the shader code. Any ideas what could be the culprit? The 2018 version is more how it should look.

    Unity 2018:4.17f1:


    Unity 2019.3.0f6:


    Here's the shader code:


    Shader "Toon/Reflect Outline" {
    Properties {
    _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    _Outline ("Outline width", Range (.002, 0.03)) = .005
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    _BumpMap ("Normalmap", 2D) = "bump" {}

    }
    SubShader {
    Tags { "RenderType"="Opaque" }
    UsePass "Reflective/Bumped Specular/FORWARD"
    UsePass "Toon/Basic Outline/OUTLINE"
    }

    Fallback "Toon/Lighted"
    }



    Shader "Toon/Basic Outline" {
    Properties {
    _Color ("Main Color", Color) = (.5,.5,.5,1)
    _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    _Outline ("Outline width", Range (.002, 0.03)) = .005
    _MainTex ("Base (RGB)", 2D) = "white" { }
    _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { }
    }

    CGINCLUDE
    #include "UnityCG.cginc"

    struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    };
    struct v2f {
    float4 pos : SV_POSITION;
    UNITY_FOG_COORDS(0)
    fixed4 color : COLOR;
    };

    uniform float _Outline;
    uniform float4 _OutlineColor;

    v2f vert(appdata v) {
    v2f o;
    o.pos = UnityObjectToClipPos(v.vertex);
    float3 norm = normalize(mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal));
    float2 offset = TransformViewToProjection(norm.xy);
    #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
    o.pos.xy += offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(o.pos.z) * _Outline;
    #else
    o.pos.xy += offset * o.pos.z * _Outline;
    #endif
    o.color = _OutlineColor;
    UNITY_TRANSFER_FOG(o,o.pos);
    return o;
    }
    ENDCG
    SubShader {
    Tags { "RenderType"="Opaque" }
    UsePass "Toon/Basic/BASE"
    Pass {
    Name "OUTLINE"
    Tags { "LightMode" = "Always" }
    Cull Front
    ZWrite On
    ColorMask RGB
    Blend SrcAlpha OneMinusSrcAlpha
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_fog
    fixed4 frag(v2f i) : SV_Target
    {
    UNITY_APPLY_FOG(i.fogCoord, i.color);
    return i.color;
    }
    ENDCG
    }
    }

    Fallback "Toon/Basic"
    }


    Is the UNITY_APPLY_FOG(i.fogCoord, i.color); what I should be looking at? What would be different with it between 2018 and 2019?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    996
    try decreasing outline width
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    2,421
    I can't really tell, but I'm suspicious of the #ifdef in the outline shader. Rather than leaving it to the #ifdef to choose which branch to take, maybe try each of the two branches one-at-a-time and see if one works better than the other.
     
    Cherubim79 likes this.
  4. Cherubim79

    Cherubim79

    Joined:
    May 3, 2014
    Posts:
    55
    That did it. The correct line is:
    o.pos.xy += offset * o.pos.z * _Outline;

    I don't know what UNITY_Z_0_FAR_FROM_CLIPSPACE is, but, that seems to be the culprit to the not so nice outline in the second image.
     
unityunity