Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shadows in Vertex Shader

Discussion in 'Shaders' started by Frostbite23, Nov 29, 2014.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    How can I add shadows in my vertex shader, do I have to add a vertex shadow pass? Im not sure, I've looked everywhere and I can't find a solution. Is there a way i can add shadows without using "LightingCoords(5,5) Transfer to fragments, etc"
    Code (JavaScript):
    1. Shader "NewShader" {
    2.     Properties {
    3.         _Color ("Diffuse Color", Color) = (1.0, 1.0, 1.0, 1.0)
    4.         _SpecularColor ("Specular Color", Color) = (1.0, 1.0, 1.0, 1.0)
    5.         _SpecPow ("Specular Power", Float) = 1
    6.     }
    7.     SubShader {
    8.         Pass {
    9.             Tags { "LightMode" = "ForwardBase" "RenderType" = "Opaque" }
    10.             Cull Back
    11.             CGPROGRAM
    12.             #pragma vertex vert
    13.             #pragma fragment frag
    14.             #pragma multi_compile_fwdbase_fullshadows
    15.             #include "UnityCG.cginc"
    16.             #include "AutoLight.cginc"
    17.             #pragma target 3.0
    18.            
    19.             //user defined
    20.             fixed4 _Color;
    21.             fixed4 _SpecularColor;
    22.            
    23.             fixed _SpecPow;
    24.            
    25.             //unity defined
    26.             fixed4 _LightColor0;
    27.            
    28.             struct vertexInput {
    29.                 fixed4 vertex : POSITION;
    30.                 fixed4 texcoord : TEXCOORD0;
    31.                 fixed4 tangent : TANGENT;
    32.                 fixed3 normal : NORMAL;
    33.             };
    34.            
    35.             struct vertexOutput {
    36.                 fixed4 pos : SV_POSITION;
    37.                 fixed4 tex : TEXCOORD0;
    38.                 fixed4 posWorld : TEXCOORD1;
    39.                 fixed3 normalWorld : TEXCOORD2;
    40.                 fixed3 tangentWorld : TEXCOORD3;
    41.                 fixed3 binormalWorld : TEXCOORD4;
    42.             };
    43.            
    44.             vertexOutput vert(vertexInput v){
    45.                 vertexOutput o;
    46.                
    47.                 o.normalWorld = normalize( mul( fixed4( v.normal, 0.0), _World2Object ).xyz );
    48.                 o.tangentWorld = normalize( mul( _Object2World, v.tangent ).xyz );
    49.                 o.binormalWorld = normalize( cross(o.normalWorld, o.tangentWorld) * v.tangent.w );
    50.                 o.posWorld = mul(_Object2World, v.vertex);
    51.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    52.                 o.tex = v.texcoord;
    53.                 return o;
    54.             }
    55.            
    56.             fixed4 frag(vertexOutput i) : COLOR
    57.             {
    58.                 fixed3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz );
    59.                 fixed3 lightDirection = normalize( _WorldSpaceLightPos0.xyz );
    60.                 fixed3 normalDirection = normalize( i.normalWorld.xyz );
    61.                 fixed3 halfDir = normalize(viewDirection + lightDirection);
    62.                
    63.                 //Lighting
    64.                 fixed3 diffuseReflection = _LightColor0.xyz * saturate( dot( normalDirection, lightDirection ) );
    65.                 fixed3 specularReflection = _SpecularColor.xyz * pow( saturate( dot( reflect(-lightDirection, normalDirection), viewDirection) ), _SpecPow) * _SpecPow;
    66.                
    67.                 fixed3 lightFinal = diffuseReflection + specularReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
    68.                
    69.                 return fixed4(lightFinal * _Color.xyz, 1.0);
    70.             }
    71.            
    72.             ENDCG
    73.         }
    74.     }
    75.     Fallback "Diffuse"
    76. }
    You must give me a full answer, don't refer me to any thread just give me an answer because I looked everywhere and I can't find another solution, I tried implementing it to my shader and it didn't work.