Advertisement
tonynogo

Demo 59 - Diffuse and specular per vertex

Jul 6th, 2017
2,799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Lighting/VertexSpecular"
  2. {
  3.     Properties
  4.     {
  5.     [Header(Diffuse)]
  6.         _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
  7.         _Diffuse ("Value", Range(0, 1)) = 1.0
  8.        
  9.     [Header(Specular)]
  10.         _SpecColor ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
  11.         _Shininess ("Shininess", Range(0.1, 10)) = 1
  12.     }
  13.     SubShader
  14.     {
  15.         Tags { "LightMode"="ForwardBase" }
  16.  
  17.         Pass
  18.         {
  19.             CGPROGRAM
  20.             #pragma vertex vert
  21.             #pragma fragment frag
  22.             #include "UnityCG.cginc"
  23.  
  24.             struct appdata {
  25.                 float4 vertex : POSITION;
  26.                 float3 normal : NORMAL;
  27.             };
  28.  
  29.             struct v2f {
  30.                 float4 pos : SV_POSITION;
  31.                 fixed4 col : COLOR0;
  32.             };
  33.  
  34.             fixed4 _LightColor0;
  35.  
  36.             fixed4 _Color;
  37.             fixed4 _SpecColor;
  38.            
  39.             float _Diffuse;
  40.             float _Shininess;
  41.            
  42.             v2f vert(appdata v) {
  43.                 v2f o;
  44.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  45.  
  46.                 float3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  47.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
  48.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  49.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos.xyz);
  50.                 float3 refl = reflect(-lightDir, worldNormal);
  51.  
  52.                 float NdotL = max(0.0, dot(worldNormal, lightDir));
  53.                 float RdotV = max(0.0, dot(refl, viewDir));
  54.                
  55.                 fixed4 diff = _Color * NdotL * _LightColor0 * _Diffuse;
  56.                 fixed4 spec = ceil(NdotL) * _LightColor0 * _SpecColor * pow(RdotV, _Shininess);
  57.  
  58.                 o.col = diff + spec;
  59.                 return o;
  60.             }
  61.  
  62.             float4 frag(v2f i) : SV_Target {
  63.                 return i.col;
  64.             }
  65.  
  66.             ENDCG
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement