Advertisement
tonynogo

Demo 88 - ADSE Blinn Phong per fragment

Jul 6th, 2017
3,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Lighting/PerFragment/Blinn-Phong"
  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, 100)) = 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.                 float3 worldPos : TEXCOORD1;
  45.                 float3 worldNormal : TEXCOORD2;
  46.             };
  47.  
  48.             v2f vert(appdata_base v)
  49.             {
  50.                 v2f o;
  51.                 // World position
  52.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  53.  
  54.                 // Clip position
  55.                 o.pos = mul(UNITY_MATRIX_VP, float4(o.worldPos, 1.));
  56.  
  57.                 // Normal in WorldSpace
  58.                 o.worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  59.  
  60.                 o.uv = v.texcoord;
  61.  
  62.                 return o;
  63.             }
  64.  
  65.             sampler2D _MainTex;
  66.  
  67.             fixed4 _LightColor0;
  68.            
  69.             // Diffuse
  70.             fixed _Diffuse;
  71.             fixed4 _DifColor;
  72.  
  73.             //Specular
  74.             fixed _Shininess;
  75.             fixed4 _SpecColor;
  76.            
  77.             //Ambient
  78.             fixed _Ambient;
  79.             fixed4 _AmbColor;
  80.  
  81.             // Emission
  82.             sampler2D _EmissionTex;
  83.             fixed4 _EmiColor;
  84.             fixed _EmiVal;
  85.  
  86.             fixed4 frag(v2f i) : SV_Target
  87.             {
  88.                 fixed4 c = tex2D(_MainTex, i.uv);
  89.  
  90.                 // Light direction
  91.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  92.  
  93.                 // Camera direction
  94.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
  95.  
  96.                 float3 worldNormal = normalize(i.worldNormal);
  97.  
  98.                 // Compute ambient lighting
  99.                 fixed4 amb = _Ambient * _AmbColor;
  100.  
  101.                 // Compute the diffuse lighting
  102.                 fixed4 NdotL = max(0., dot(worldNormal, lightDir) * _LightColor0);
  103.                 fixed4 dif = NdotL * _Diffuse * _LightColor0 * _DifColor;
  104.  
  105.                 fixed4 light = dif + amb;
  106.  
  107.                 // If enabled, compute the specular lighting (Blinn-phong)
  108.                 #if _SPEC_ON
  109.                 float3 HalfVector = normalize(lightDir + viewDir);
  110.                 float NdotH = max(0., dot(worldNormal, HalfVector));
  111.                 fixed4 spec = pow(NdotH, _Shininess) * _LightColor0 * _SpecColor;
  112.  
  113.                 light += spec;
  114.                 #endif
  115.  
  116.                 c.rgb *= light.rgb;
  117.  
  118.                 // Compute emission
  119.                 fixed4 emi = tex2D(_EmissionTex, i.uv).r * _EmiColor * _EmiVal;
  120.                 c.rgb += emi.rgb;
  121.                
  122.                 return c;
  123.             }
  124.  
  125.             ENDCG
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement