Advertisement
Coguelin

Simple Diffuse Rimlight Shader

Oct 21st, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. Shader "FX/DiffuseRimlight"
  2. {
  3.     // These are the fields that will appear for this Material in the Inspector - see Unity doc at http://docs.unity3d.com/Documentation/Components/SL-Properties.html
  4.     // The first part is the property's variable, eg. "_MainTex" - Naming your main texture "_MainTex" signals to Unity that this Shader should be included in Lightmap calculations
  5.     // Using the standard Unity property names also lets Unity apply a fallback Shader if the user's hardware can't handle this one
  6.     // The second part in brackets is the name of the property in the Inspector, eg. "Main Texture" - This can be anything you like.
  7.     // The third part in brackets is the variable type, eg. "2D" declares it to be a texture file -
  8.     // The fourth part is the property's default value, eg. "white {}" - Textures are generally given default values of white, gray or black
  9.     Properties
  10.     {
  11.         _MainTex ("Main Texture", 2D) = "white" {} // This is the material's Texture property and we want it to be included in Lightmap calculations (especially LightProbes)
  12.         _RimColor ("Rim Color", Color) = (0.26, 0.19, 0.16, 0.0) // This is the material's RimLight Color
  13.         _RimPower ("Rim Power", float) = 3.0 // This is the material's RimLight strength.  0 will be maximum rimlighting... Rim power is generally exposed as a Range property type
  14.     }
  15.    
  16.     // The SubShader is where the Shader calculations actually happen.
  17.     SubShader
  18.     {
  19.    
  20.         // This allows Unity to intelligently substitute this Shader when needed
  21.         Tags { "RenderType"="Opaque" }
  22.         LOD 200
  23.        
  24.        
  25.         Cull Back
  26.        
  27.         // The syntax that was use after "pragma" tells Unity what kind of shader this is and how it should be applied, eg.
  28.         // This shader is a surface Shader of type Lambert, and we want Unity to approximate view direction to speed up calculations
  29.         CGPROGRAM
  30.         #pragma surface surf Lambert approxview
  31.  
  32.         //These match the shader properties
  33.         uniform sampler2D _MainTex;
  34.         uniform fixed4 _Color, _RimColor;
  35.         uniform fixed _RimPower;
  36.  
  37.         // This contains the inputs to the surface function
  38.         // Valid fields are listed at: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html
  39.         struct Input
  40.         {
  41.             float2 uv_MainTex;
  42.             float3 viewDir; // Since this is a RimLight Shader, we need to account for the Camera View Direction.  This value accesses Unity's internal calculations
  43.             float3 worldNormal; // Since this is a Mobile Shader, we want to try to avoid complex calculations.  This value uses the Unity's world normals instead of the object normals
  44.         };
  45.  
  46.         // This is where we prepare the surface for lighting by propagating a SurfaceOutput structure
  47.         void surf (Input IN, inout SurfaceOutput o)
  48.         {
  49.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex); // Samples the texture
  50.             o.Albedo = c.rgb; // Modulate by main colour
  51.             o.Alpha = 1.0; // No alpha in this shader
  52.            
  53.             fixed3 VdotN = dot(normalize(IN.viewDir), IN.worldNormal); // Here, we calculate what is called the "dot product", basically the effect that a light has based on the angle of a vertex
  54.            
  55.             fixed rim = 1.0 - saturate(VdotN); // Here, we calculate the effect of the rim light based on the dot product
  56.            
  57.             o.Emission = _RimColor.rgb * pow (rim, _RimPower); // We apply our rim light and place it in the Material's emmission value
  58.         }
  59.         ENDCG
  60.     }
  61.     FallBack "Mobile/Diffuse" // Shader to use if the user's hardware cannot incorporate this one
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement