Advertisement
tonynogo

Demo 79 - Diffuse and Emission

Jul 6th, 2017
8,744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Lighting/Emission"
  2. {
  3.     Properties
  4.     {
  5.         [Header(Diffuse)]
  6.         _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
  7.         _Diffuse ("Diffuse value", Range(0, 1)) = 1.0
  8.         [Header(Emission)]
  9.         _MainTex ("Emissive Map", 2D) = "white" {}
  10.         [HDR] _EmissionColor ("Emission Color", Color) = (0,0,0)
  11.         _Threshold ("Threshold", Range(0., 1.)) = 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 v2f {
  25.                 float4 pos : SV_POSITION;
  26.                 fixed4 col : COLOR0;
  27.                 float2 uv : TEXCOORD0;
  28.             };
  29.  
  30.             fixed4 _Color;
  31.             fixed4 _LightColor0;
  32.             float _Diffuse;
  33.            
  34.             sampler2D _MainTex;
  35.             float4 _MainTex_ST;
  36.  
  37.             v2f vert(appdata_base v) {
  38.                 v2f o;
  39.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  40.                 float3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  41.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  42.                 float NdotL = max(0.0, dot(worldNormal, lightDir));
  43.                 fixed4 diff = _Color * NdotL * _LightColor0 * _Diffuse;
  44.                 o.col = diff;
  45.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  46.                 return o;
  47.             }
  48.  
  49.             float4 _EmissionColor;
  50.             float _Threshold;
  51.  
  52.             fixed4 frag(v2f i) : SV_Target {
  53.                 fixed3 emi = tex2D(_MainTex, i.uv).r * _EmissionColor.rgb * _Threshold;
  54.                 i.col.rgb += emi;
  55.                 return i.col;
  56.             }
  57.  
  58.             ENDCG
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement