Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Shader only working with the Directional Light

Discussion in 'Shaders' started by MichaelKaimanaTaylor, Aug 13, 2019 at 7:11 AM.

  1. MichaelKaimanaTaylor

    MichaelKaimanaTaylor

    Joined:
    Feb 26, 2019
    Posts:
    17
    Forgive me if this's a stupid question (learning shaders at the moment).

    I've been trying to get a look similar to Killer7 or No More Heroes. I almost got it, but the problem I'm facing right not is that it only works with the Directional Light (Figure 1) and not any other light (Figure 2).

    I notices that the lights pull up a warning "Realtime indirect bounce shadowing is not supported for Spot and Point lights." Is there a way to fix this in the shader or else where. Below is the current shader code.

    Any help or advice is welcome.

    Code (CSharp):
    1. Shader "Custom/ToonTest"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.         _MainTex("Main Texture", 2D) = "white" {}
    7.  
    8.         [HDR]
    9.         _AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
    10.     }
    11.     SubShader
    12.     {
    13.         Pass
    14.         {
    15.             Tags
    16.             {
    17.                 "LightMode" = "ForwardBase"
    18.                 "PassFlags" = "OnlyDirectional"
    19.             }
    20.  
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #pragma multi_compile_fwdbase
    25.  
    26.             #include "UnityCG.cginc"
    27.             #include "Lighting.cginc"
    28.             #include "AutoLight.cginc"
    29.  
    30.             struct appdata
    31.             {
    32.                 float4 vertex : POSITION;
    33.                 float4 uv : TEXCOORD0;
    34.                 float3 normal : NORMAL;
    35.             };
    36.  
    37.             struct v2f
    38.             {
    39.                 float4 pos : SV_POSITION;
    40.                 float3 worldNormal : NORMAL;
    41.                 float2 uv : TEXCOORD0;
    42.                 float3 viewDir : TEXCOORD1;
    43.                 SHADOW_COORDS(2)
    44.             };
    45.  
    46.             sampler2D _MainTex;
    47.             float4 _AmbientColor;
    48.  
    49.             v2f vert(appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.pos = UnityObjectToClipPos(v.vertex);
    53.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    54.                 o.viewDir = WorldSpaceViewDir(v.vertex);
    55.                 return o;
    56.             }
    57.  
    58.             float4 _Color;
    59.  
    60.             float4 frag(v2f i) : SV_Target
    61.             {
    62.                 float3 normal = normalize(i.worldNormal);
    63.  
    64.                 float NdotL = dot(_WorldSpaceLightPos0, normal);
    65.  
    66.                 float2 uv = float2(1 - (NdotL * 0.5 + 0.5), 0.5);
    67.  
    68.                 float lightIntensity = NdotL > 0 ? 1 : 0;
    69.  
    70.                 float4 sample = tex2D(_MainTex, uv);
    71.  
    72.                 return _Color * sample * (_AmbientColor + lightIntensity);
    73.             }
    74.             ENDCG
    75.         }
    76.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    77.     }
    78. }
    79.  
     

    Attached Files:

  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    241