Search Unity

Adding Shadow to my Shader Problem

Discussion in 'Shaders' started by RobertOne, Jan 14, 2022.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    Hey :)

    so ive got this shader;
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    4.  
    5. Shader "Unlit/CutoutShader"
    6. {
    7.     Properties
    8.     {
    9.         _MainTex ("Texture", 2D) = "white" {}
    10.         _Color ("Color", Color) = (1,1,1,1)
    11.         _vtx0("pnt0", Vector) = (0,0,0,0)
    12.         _vtx1("pnt1", Vector) = (0,0,0,0)
    13.         _vtx2("pnt2", Vector) = (0,0,0,0)
    14.         _vtx3("pnt3", Vector) = (0,0,0,0)
    15.         _dbg ("Debug Dots", Float) = 0
    16.     }
    17.     SubShader
    18.     {
    19.  
    20.     Tags {"Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout"}
    21.         LOD 100
    22.         Lighting On
    23.         Pass
    24.         {
    25.             CGPROGRAM
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.             // make fog work
    29.             #pragma multi_compile_fog
    30.  
    31.             #include "UnityCG.cginc"
    32.  
    33.             struct appdata
    34.             {
    35.                 float4 vertex : POSITION;
    36.                 float2 uv : TEXCOORD0;
    37.             };
    38.  
    39.             struct v2f
    40.             {
    41.                 float2 uv : TEXCOORD0;
    42.                 float4 vertex : SV_POSITION;
    43.                 float4 lsPos : TEXCOORD1;
    44.             };
    45.  
    46.             sampler2D _MainTex;
    47.             float4 _MainTex_ST;
    48.             fixed4 _Color;
    49.             float4 _vtx0;
    50.             float4 _vtx1;
    51.             float4 _vtx2;
    52.             float4 _vtx3;
    53.             float _dbg;
    54.  
    55.             v2f vert (appdata v)
    56.             {
    57.                 v2f o;
    58.                 o.vertex = UnityObjectToClipPos(v.vertex);//UnityObjectToClipPos(v.vertex);
    59.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    60.                 o.lsPos = v.vertex;
    61.                 return o;
    62.             }
    63.  
    64.             fixed4 frag (v2f i) : SV_Target
    65.             {
    66.                 // sample the texture
    67.                 float2 dst0 = i.lsPos - _vtx0;
    68.                 float2 dst1 = i.lsPos - _vtx1;
    69.                 float2 dst2 = i.lsPos - _vtx2;
    70.                 float2 dst3 = i.lsPos - _vtx3;
    71.                 float2 edg0 = _vtx1 - _vtx0;
    72.                 float2 edg1 = _vtx2 - _vtx1;
    73.                 float2 edg2 = _vtx3 - _vtx2;
    74.                 float2 edg3 = _vtx0 - _vtx3;
    75.                 float2 nrm0 = float2(edg0.y, -edg0.x);
    76.                 float2 nrm1 = float2(edg1.y, -edg1.x);
    77.                 float2 nrm2 = float2(edg2.y, -edg2.x);
    78.                 float2 nrm3 = float2(edg3.y, -edg3.x);
    79.  
    80.                 float alpha = max(
    81.                     max(dot(nrm0, dst0) *22, dot(nrm1, dst1) * 22),
    82.                     max(dot(nrm2, dst2) *22, dot(nrm3, dst3) * 22));
    83.  
    84.                 fixed4 col = tex2D(_MainTex, i.uv) * _Color;
    85.                 fixed4 indic = fixed4(0,0,0,1);
    86.                 indic.x = max(step(length(dst0), _dbg), step(length(dst1), _dbg));
    87.                 indic.y = max(step(length(dst2), _dbg), step(length(dst3), _dbg));
    88.                 indic.z = max(step(length(dst3), _dbg), step(length(dst1), _dbg));
    89.                
    90.                 clip(alpha);
    91.                 return lerp(col, indic,max(indic.x,indic.y));
    92.             }
    93.             ENDCG
    94.         }
    95.     }
    96. }
    97.  
    And i am currently stuggeling to add in shadows/make it not unlit. this is how it looks like:
    Capture3.PNG


    Can anyone tell me how can i change the shader to receive/drop shadows?
    thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352