Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This shader will blend two textures using the vertex color AND the alpha of the main texture as a guide
- // This lets us have a more realistic blend distribution, such as water filtering in between the gaps in brickwork
- Shader "FX/Diffuse Alpha Blend"
- {
- // 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
- // 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
- // Using the standard Unity property names also lets Unity apply a fallback Shader if the user's hardware can't handle this one
- // The second part in brackets is the name of the property in the Inspector, eg. "Main Texture" - This can be anything you like.
- // The third part in brackets is the variable type, eg. "2D" declares it to be a texture file -
- // The fourth part is the property's default value, eg. "white {}" - Textures are generally given default values of white, gray or black
- Properties
- {
- _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
- _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
- _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
- }
- // The SubShader is where the Shader calculations actually happen
- SubShader
- {
- // This allows Unity to intelligently substitute this Shader when needed
- Tags { "RenderType"="Opaque" }
- Blend Off
- LOD 250
- // The syntax that was use after "pragma" tells Unity what kind of shader this is and how it should be applied, eg.
- // 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
- CGPROGRAM
- #pragma surface surf Lambert exclude_path:prepass
- //These match the shader properties
- uniform sampler2D _MainTex;
- uniform sampler2D _BlendTex;
- uniform fixed4 _ColorTint;
- // This contains the inputs to the surface function
- // Valid fields are listed at: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html
- // 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
- struct Input
- {
- float2 uv_MainTex;
- float2 uv_BlendTex;
- float4 color : COLOR;
- };
- // 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
- // In the first two lines, we're simply unwrapping both textures
- // Blend0 is the value we'll use to interpolate between the two :
- // 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
- // "Lerp" is a word programmers use when they're blending between two sets of numbers, this is what we're doing with our textures
- // 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
- void surf (Input IN, inout SurfaceOutput o)
- {
- fixed4 Tex2D0 = tex2D(_MainTex, IN.uv_MainTex);
- fixed4 Tex2D1 = tex2D(_BlendTex, IN.uv_BlendTex);
- 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
- fixed4 Lerp0 = lerp(Tex2D0, Tex2D1, Blend0);
- 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
- o.Alpha = 1.0; // No alpha in this shader
- }
- ENDCG
- }
- FallBack "Mobile/Diffuse" // Shader to use if the user's hardware cannot incorporate this one
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement