Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Adding shadows to an HLSL shader

Discussion in 'Universal Render Pipeline' started by Sneekjs, Jan 16, 2024.

  1. Sneekjs

    Sneekjs

    Joined:
    May 25, 2017
    Posts:
    1
    I followed this tutorial to create a toon shader: https://roystan.net/articles/toon-shader/
    However, I am working in a URP project and this shader does not work for URP. I made some adjustements to make it functional, however, it is unable to render any shadows.

    I am a complete beginner when it comes to shaders and HLSL code. Could anyone explain how I need to adjust my current code to make it receive shadows in a URP project?


    Code (HLSL):
    1. Shader "Roystan/Toon"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (0.5, 0.65, 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.         [HDR]
    12.         _SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)
    13.         _Glossiness("Glossiness", Float) = 32
    14.  
    15.         [HDR]
    16.         _RimColor("Rim Color", Color) = (1,1,1,1)
    17.         _RimAmount("Rim Amount", Range(0, 1)) = 0.716
    18.  
    19.         _RimThreshold("Rim Threshold", Range(0, 1)) = 0.1
    20.     }
    21.     SubShader
    22.     {
    23.         Pass
    24.         {
    25.             CGPROGRAM
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.            
    29.             #include "UnityCG.cginc"
    30.             #include "Lighting.cginc"
    31.  
    32.             struct appdata
    33.             {
    34.                 float4 vertex : POSITION;              
    35.                 float4 uv : TEXCOORD0;
    36.                 float3 normal : NORMAL;
    37.             };
    38.  
    39.             struct v2f
    40.             {
    41.                 float4 pos : SV_POSITION;
    42.                 float3 worldNormal : NORMAL;
    43.                 float3 viewDir : TEXCOORD1;
    44.                 float2 uv : TEXCOORD0;
    45.             };
    46.  
    47.             sampler2D _MainTex;
    48.             float4 _MainTex_ST;
    49.            
    50.             v2f vert (appdata v)
    51.             {
    52.                 v2f o;
    53.                 o.pos = UnityObjectToClipPos(v.vertex);
    54.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    55.                 o.viewDir = WorldSpaceViewDir(v.vertex);
    56.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    57.                 return o;
    58.             }
    59.            
    60.             float4 _Color;
    61.  
    62.             float4 _AmbientColor;
    63.  
    64.             float _Glossiness;
    65.             float4 _SpecularColor;
    66.  
    67.             float4 _RimColor;
    68.             float _RimAmount;
    69.  
    70.             float _RimThreshold;
    71.  
    72.             float4 frag (v2f i) : SV_Target
    73.             {
    74.                 float3 normal = normalize(i.worldNormal);
    75.                
    76.                 float NdotL = dot(_WorldSpaceLightPos0, normal);
    77.                
    78.                 float lightIntensity = smoothstep(0, 0.01, NdotL);
    79.  
    80.                 float4 light = lightIntensity * _LightColor0;
    81.  
    82.                 float3 viewDir = normalize(i.viewDir);
    83.  
    84.                 float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
    85.                 float NdotH = dot(normal, halfVector);
    86.  
    87.                 float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
    88.  
    89.                 float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
    90.                 float4 specular = specularIntensitySmooth * _SpecularColor;
    91.  
    92.                 float4 rimDot = 1 - dot(viewDir, normal);
    93.  
    94.                 float rimIntensity = rimDot * pow(NdotL, _RimThreshold);
    95.                 rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);              
    96.                
    97.                 float4 rim = rimIntensity * _RimColor;
    98.  
    99.                 float4 sample = tex2D(_MainTex, i.uv);
    100.                
    101.                 return _Color * sample * (_AmbientColor + light + specular + rim);
    102.             }
    103.             ENDCG
    104.         }
    105.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    106.     }
    107. }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,202