Advertisement
tonynogo

Demo 85 - ADSE Phong per vertex

Jul 6th, 2017
4,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Lighting/BasicLightingPerVertex"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Main Texture", 2D) = "white" {}
  6.  
  7.         [Header(Ambient)]
  8.         _Ambient ("Intensity", Range(0., 1.)) = 0.1
  9.         _AmbColor ("Color", color) = (1., 1., 1., 1.)
  10.  
  11.         [Header(Diffuse)]
  12.         _Diffuse ("Val", Range(0., 1.)) = 1.
  13.         _DifColor ("Color", color) = (1., 1., 1., 1.)
  14.  
  15.         [Header(Specular)]
  16.         [Toggle] _Spec("Enabled?", Float) = 0.
  17.         _Shininess ("Shininess", Range(0.1, 10)) = 1.
  18.         _SpecColor ("Specular color", color) = (1., 1., 1., 1.)
  19.  
  20.         [Header(Emission)]
  21.         _EmissionTex ("Emission texture", 2D) = "gray" {}
  22.         _EmiVal ("Intensity", float) = 0.
  23.         [HDR]_EmiColor ("Color", color) = (1., 1., 1., 1.)
  24.     }
  25.  
  26.     SubShader
  27.     {
  28.         Pass
  29.         {
  30.             Tags { "RenderType"="Opaque" "Queue"="Geometry" "LightMode"="ForwardBase" }
  31.  
  32.             CGPROGRAM
  33.             #pragma vertex vert
  34.             #pragma fragment frag
  35.  
  36.             // Change "shader_feature" with "pragma_compile" if you want set this keyword from c# code
  37.             #pragma shader_feature __ _SPEC_ON
  38.  
  39.             #include "UnityCG.cginc"
  40.  
  41.             struct v2f {
  42.                 float4 pos : SV_POSITION;
  43.                 float2 uv : TEXCOORD0;
  44.                 fixed4 light : COLOR0;
  45.             };
  46.  
  47.             fixed4 _LightColor0;
  48.            
  49.             // Diffuse
  50.             fixed _Diffuse;
  51.             fixed4 _DifColor;
  52.  
  53.             //Specular
  54.             fixed _Shininess;
  55.             fixed4 _SpecColor;
  56.            
  57.             //Ambient
  58.             fixed _Ambient;
  59.             fixed4 _AmbColor;
  60.  
  61.             v2f vert(appdata_base v)
  62.             {
  63.                 v2f o;
  64.                 // World position
  65.                 float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
  66.  
  67.                 // Clip position
  68.                 o.pos = mul(UNITY_MATRIX_VP, worldPos);
  69.  
  70.                 // Light direction
  71.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  72.  
  73.                 // Normal in WorldSpace
  74.                 float3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  75.  
  76.                 // Camera direction
  77.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos.xyz);
  78.  
  79.                 // Compute ambient lighting
  80.                 fixed4 amb = _Ambient * _AmbColor;
  81.  
  82.                 // Compute the diffuse lighting
  83.                 fixed4 NdotL = max(0., dot(worldNormal, lightDir) * _LightColor0);
  84.                 fixed4 dif = NdotL * _Diffuse * _LightColor0 * _DifColor;
  85.  
  86.                 o.light = dif + amb;
  87.  
  88.                 // Compute the specular lighting
  89.                 #if _SPEC_ON
  90.                 float3 refl = reflect(-lightDir, worldNormal);
  91.                 float RdotV = max(0., dot(refl, viewDir));
  92.                 fixed4 spec = pow(RdotV, _Shininess) * _LightColor0 * ceil(NdotL) * _SpecColor;
  93.  
  94.                 o.light += spec;
  95.                 #endif
  96.                
  97.                 o.uv = v.texcoord;
  98.  
  99.                 return o;
  100.             }
  101.  
  102.             sampler2D _MainTex;
  103.  
  104.             // Emission
  105.             sampler2D _EmissionTex;
  106.             fixed4 _EmiColor;
  107.             fixed _EmiVal;
  108.  
  109.             fixed4 frag(v2f i) : SV_Target
  110.             {
  111.                 fixed4 c = tex2D(_MainTex, i.uv);
  112.                 c.rgb *= i.light;
  113.  
  114.                 // Compute emission
  115.                 fixed4 emi = tex2D(_EmissionTex, i.uv).r * _EmiColor * _EmiVal;
  116.                 c.rgb += emi.rgb;
  117.  
  118.                 return c;
  119.             }
  120.  
  121.             ENDCG
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement