Simple Vertx-colour Blend Shader

Coguelin Oct 21st, 2013 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This shader will blend two textures using the vertex color as a guide.
  3. Shader "FX/Diffuse Blend"
  4. {
  5.         // These are the fields that will appear for this Material in the Inspector - see Unity doc at
  6.         // 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
  7.         // Using the standard Unity property names also lets Unity apply a fallback Shader if the user's hardware can't handle this one
  8.         // The second part in brackets is the name of the property in the Inspector, eg. "Main Texture" - This can be anything you like.
  9.         // The third part in brackets is the variable type, eg. "2D" declares it to be a texture file -
  10.         // The fourth part is the property's default value, eg. "white {}" - Textures are generally given default values of white, gray or black
  11.         Properties
  12.         {
  13.                 _MainTex ("Main Texture (vertex A = black)", 2D) = "gray" {} // This is the material's Texture property and we want it to be included in Lightmap calculations (especially LightProbes)
  14.             _BlendTex ("Blend Texture (vertex A = white)", 2D) = "gray" {} // This shader is a little strange since we actually want to blend two textures, this is the texture we will blend in
  15.             _ColorTint ("Main Color", Color) =  (0.7, 0.7, 0.7, 0.1) // This is a color value we will use to tint the texture after it is blended, but before the light is applied
  16.         }
  18.         // The SubShader is where the Shader calculations actually happen
  19.         SubShader
  20.         {
  21.                 // This allows Unity to intelligently substitute this Shader when needed
  22.             Tags { "RenderType"="Opaque" }
  23.             Blend Off
  24.             LOD 250
  26.                 // The syntax that was use after "pragma" tells Unity what kind of shader this is and how it should be applied, eg.
  27.                 // This shader is a surface Shader of type Lambert, and don't want Unity to bother with a prepass because we're not doing anything with light
  28.                 CGPROGRAM
  29.                 #pragma surface surf Lambert exclude_path:prepass
  31.                 //These match the shader properties    
  32.                 uniform sampler2D _MainTex;    
  33.                 uniform sampler2D _BlendTex;
  34.                 uniform fixed4 _ColorTint;
  36.                 // This contains the inputs to the surface function
  37.                 // Valid fields are listed at:
  38.                 // The color syntax is actually accessing the vertex color that we put in our object to guide the blend. You normally don't need this
  39.                 struct Input
  40.                 {
  41.                     float2 uv_MainTex;
  42.                     float2 uv_BlendTex;
  43.                     float4 color : COLOR;
  44.                 };
  46.                 // This is where we prepare the surface for lighting by propagating a SurfaceOutput structure, I'm going to breaqk it down so you know what's happening
  47.                 // In the first two lines, we're unwrapping both textures
  48.                 // "Lerp" is a word programmers use when they're blending between two sets of numbers, this is what we're doing with our textures
  49.                 // So we're Lerping between our main texture and our blend texture, and we're using the values of the vertex color to guide us... in theory this could be any value set
  50.                 void surf (Input IN, inout SurfaceOutput o)
  51.                 {
  52.                     fixed4 Tex2D0 = tex2D(_MainTex, IN.uv_MainTex);
  53.                     fixed4 Tex2D1 = tex2D(_BlendTex, IN.uv_BlendTex);
  54.                     fixed4 Lerp0 = lerp(Tex2D0, Tex2D1, IN.color);     
  56.                     o.Albedo = Lerp0.rgb * _ColorTint; // We modulate our coloors by our main colour, BUT we also multiply it by our tint... this works a little like a color filtre in Photoshop
  57.                     o.Alpha = 1.0; // No alpha in this shader
  58.                 }
  59.                 ENDCG
  61.         }
  62.         FallBack "Mobile/Diffuse" // Shader to use if the user's hardware cannot incorporate this one
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand