daily pastebin goal
28%
SHARE
TWEET

Simple Vertx-colour + Alpha channel Blend Shader

Coguelin Oct 21st, 2013 36 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 AND the alpha of the main texture as a guide
  2. // This lets us have a more realistic blend distribution, such as water filtering in between the gaps in brickwork
  3.  
  4. Shader "FX/Diffuse Alpha Blend"
  5. {
  6.         // 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
  7.         // 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
  8.         // Using the standard Unity property names also lets Unity apply a fallback Shader if the user's hardware can't handle this one
  9.         // The second part in brackets is the name of the property in the Inspector, eg. "Main Texture" - This can be anything you like.
  10.         // The third part in brackets is the variable type, eg. "2D" declares it to be a texture file -
  11.         // The fourth part is the property's default value, eg. "white {}" - Textures are generally given default values of white, gray or black
  12.         Properties
  13.         {
  14.                 _MainTex ("Main Texture (vertex A = white)", 2D) = "gray" {} // This is the material's Texture property, we'll also be using it's alpha to guide our blend texture
  15.             _BlendTex ("Blend Texture (vertex A = black)", 2D) = "gray" {} // This shader is a little strange since we actually want to blend two textures, this is the texture we will blend in
  16.             _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
  17.         }
  18.        
  19.         // The SubShader is where the Shader calculations actually happen
  20.         SubShader
  21.         {
  22.                 // This allows Unity to intelligently substitute this Shader when needed
  23.             Tags { "RenderType"="Opaque" }
  24.             Blend Off
  25.             LOD 250
  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 don't want Unity to bother with a prepass because we're not doing anything with light
  29.                 CGPROGRAM
  30.                 #pragma surface surf Lambert exclude_path:prepass
  31.                
  32.                 //These match the shader properties    
  33.                 uniform sampler2D _MainTex;    
  34.                 uniform sampler2D _BlendTex;
  35.                 uniform fixed4 _ColorTint;
  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.                 // 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
  40.                 struct Input
  41.                 {
  42.                     float2 uv_MainTex;
  43.                     float2 uv_BlendTex;
  44.                     float4 color : COLOR;
  45.                 };
  46.                
  47.                 // 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
  48.                 // In the first two lines, we're simply unwrapping both textures
  49.                 // Blend0 is the value we'll use to interpolate between the two :
  50.                 // We're taking the biggest of the-alpha-plus-the-vertex-color and the-vector-color, multiplying it by the vertex color, and clamping it between 1 and 0
  51.                 // "Lerp" is a word programmers use when they're blending between two sets of numbers, this is what we're doing with our textures
  52.                 // 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
  53.                 void surf (Input IN, inout SurfaceOutput o)
  54.                 {
  55.                     fixed4 Tex2D0 = tex2D(_MainTex, IN.uv_MainTex);
  56.                     fixed4 Tex2D1 = tex2D(_BlendTex, IN.uv_BlendTex);
  57.                     fixed4 Blend0 = saturate(max((Tex2D0.a + IN.color), IN.color) * IN.color); // "saturate" is a syntax that actually just clamps a value between 0 and 1, "max" takes the biggest of two values
  58.                     fixed4 Lerp0 = lerp(Tex2D0, Tex2D1, Blend0);       
  59.                  
  60.                     o.Albedo = Lerp0.rgb * _ColorTint; // We modulate our colors by our main color, BUT we also multiply it by our tint... this works a little like a color filtre in Photoshop
  61.                     o.Alpha = 1.0; // No alpha in this shader
  62.                 }
  63.                 ENDCG
  64.                
  65.         }
  66.         FallBack "Mobile/Diffuse" // Shader to use if the user's hardware cannot incorporate this one
  67. }
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
 
Top