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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Toon Shader, Shadow with atten look broken, How to fix it ?

Discussion in 'Shaders' started by Han-Wen, Nov 13, 2022.

  1. Han-Wen

    Han-Wen

    Joined:
    Jul 11, 2019
    Posts:
    1
    Hi, everyone

    I am learning toon shader by myself, and get this king of effect.

    The shadow part is ugly and i have no i idea how to optimize it. It seems cause by shadow atten, but i also need atten to receive other's shadow. Please Help!!

    Here is my Code :

    Shader "Unlit/Chapter14_ToonShading"
    {
    Properties
    {
    _Color("Color Tint", Color) = (1, 1, 1, 1)
    _MainTex ("Texture", 2D) = "white" {}
    _Ramp("Ramp Texture", 2D) = "white" {}
    _Outline("Outline", Range(0, 1)) = 0.1
    _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
    _Specular("Specular", Color) = (1, 1, 1, 1)
    _SpecularBase("Specular Base", Range(0, 1)) = 0.5
    _SpecularScale("Specular Scale", Range(0, 0.1)) = 0.01
    }
    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 100


    Pass
    {
    NAME "OUTLINE"

    Cull Front
    ZWrite Off

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata
    {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    };

    struct v2f
    {
    float4 pos : SV_POSITION;
    };

    sampler2D _MainTex;
    float4 _MainTex_ST;
    fixed4 _Color, _OutlineColor, _Specular;
    sampler2D _Ramp;
    float _Outline, _SpecularScale;

    v2f vert(appdata v)
    {
    v2f o;

    /*float4 pos = mul(UNITY_MATRIX_MV, v.vertex);
    float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    normal.z = -0.5;
    pos = pos + float4(normalize(normal), 0) * _Outline;
    o.pos = mul(UNITY_MATRIX_P, pos);*/


    o.pos = v.vertex;
    o.pos.xyz += v.normal * _Outline;
    o.pos = UnityObjectToClipPos(o.pos);
    o.pos.z -= 0.0005 * o.pos.w;

    return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
    return fixed4(_OutlineColor.rgb, 1);
    }
    ENDCG
    }


    Pass
    {

    Tags {"LightMode" = "ForwardBase"}


    Cull Back


    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_fwdbase

    #include "UnityCG.cginc"
    #include "Lighting.cginc"
    #include "AutoLight.cginc"

    struct appdata
    {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float2 uv : TEXCOORD0;
    };

    struct v2f
    {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
    float3 worldNormal : TEXCOORD1;
    float3 worldPos : TEXCOORD2;
    SHADOW_COORDS(3)
    };

    sampler2D _MainTex, _Ramp;
    float4 _MainTex_ST;
    fixed4 _Color, _Specular;
    float _SpecularScale, _SpecularBase;

    v2f vert (appdata v)
    {
    v2f o;
    o.pos = UnityObjectToClipPos(v.vertex);
    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
    o.worldPos = mul(UNITY_MATRIX_M, v.vertex);

    TRANSFER_SHADOW(o);

    return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
    fixed3 worldNormal = normalize(i.worldNormal);
    fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
    fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
    fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);

    fixed4 c = tex2D(_MainTex, i.uv);
    fixed3 albedo = c.rgb * _Color.rgb;

    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

    UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

    fixed diff = dot(worldNormal, worldLightDir);
    diff = (diff * 0.5 + 0.5) * atten;

    fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, 0)).rgb;

    /*fixed spec = dot(worldNormal, worldHalfDir);
    fixed w = fwidth(spec) * 2;
    fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);*/

    fixed spec = dot(worldNormal, worldHalfDir);
    fixed3 specular = _Specular.rgb * smoothstep(_SpecularBase - _SpecularScale, _SpecularBase + _SpecularScale, spec) * atten;

    return fixed4(ambient + diffuse + specular, 1);
    //return atten;
    }
    ENDCG
    }
    }

    Fallback "Diffuse"
    }


    {9B037205-F218-4EAF-9A18-F8CA6B5C76AE}.png.jpg
    {38369099-6182-4B76-BB68-09E82DE9C44A}.png.jpg
    By Flamecky
     
    Last edited: Nov 13, 2022
  2. dancliffAuroch

    dancliffAuroch

    Joined:
    Oct 17, 2022
    Posts:
    4
    Did you ever find a solution to this? I'm struggling with the same issue currently too.