Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Texture Blending

Discussion in 'Shaders' started by purplelilgirl, Feb 25, 2009.

  1. purplelilgirl

    purplelilgirl

    Joined:
    Feb 24, 2009
    Posts:
    102
    Is there a shader in Unity that allows blending of two textures? I found a shader in the Wiki, which is the Alpha Blend, which sort of achieves the effect that I want, however, it requires the use of a separate map to define which part uses what textures.

    Thanks in advance.
     
  2. Maethor

    Maethor

    Joined:
    Apr 2, 2009
    Posts:
    27
    Isn't it frustrating when you don't get an answer? I'm wondering the same thing.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    How do you want to blend between the 2 images? If you want to use a slider then try this:

    Code (csharp):
    1. Shader "2 Texture Blend" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1)) = 0.5
    4.         _MainTex ("Base (RGB)", 2D) = "white"
    5.         _BlendTex ("Blend (RGB)", 2D) = "black"
    6.     }
    7.     SubShader {
    8.         Pass {
    9.             SetTexture [_MainTex]
    10.            
    11.             SetTexture [_BlendTex] {
    12.                 constantColor (0, 0, 0, [_Blend])
    13.                 combine texture lerp (constant) previous
    14.             }
    15.         }
    16.     }
    17. }
     
  4. Maethor

    Maethor

    Joined:
    Apr 2, 2009
    Posts:
    27
    This is great! Thanks for replying so quickly. :)
     
  5. purplelilgirl

    purplelilgirl

    Joined:
    Feb 24, 2009
    Posts:
    102
    @maethor: yup. I posted this a while back and almost forgot about it. thanks for the bump. :D

    @daniel: thanks for the reply. I will try it out :D
     
  6. HMachuca

    HMachuca

    Joined:
    May 4, 2009
    Posts:
    36
    Thank you so much! This is exactly what i've been looking for.
     
  7. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    is there a way to do it using vertex alpha, (instead of a slider or from a texture) ?
     
  8. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Works wonderfully! What if we can get it do be dissolve-looking thing? It would be great for animating rust on metal or something...
     
  9. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    This thread is very old. Last year, Unity 3 was released with surface shaders. I recommend that anyone looking to write simple shaders that work properly with Unity 3's lighting read the documentation and look at the examples.
     
  10. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Very useful resource. This is helpful.

    Nonetheless, it will take time to learn all of this. I've been using Strumpy for some time now, but I've only managed to create a single shader
     
  11. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    All the visual shader editors are just front-ends to surface shaders. Unless you want really complex, efficient effects, there's nothing wrong with using Strumpy instead of writing surface shaders manually.
     
  12. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Alrighty then, I guess I'll be learning a bit of SL.
     
  13. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    As I'm not yet used to Shaders, I've personnaly used a struct/class with Texture2D and blend factor, and during OnGUI(), foreach object, I switch GUI.color with a Color(1,1,1, blendfactor) and then draw the texture. Works well !
     
  14. jonpeter001

    jonpeter001

    Joined:
    Jun 23, 2011
    Posts:
    5
    The tutorial below covers the principles used when texture blending in Doom 3 powered games (currently Doom 3 itself and Quake 4), how models need to be set-up, how blending works and a rundown of the special material (shader) file the Doom 3 engine needs so it can work everything out.

    The tutorial won't however, go into the specific details on how you work with a 3D app to produce the model, or indeed D3Edit, so it's assumed you know at least the basics of modeling objects in 3D, how to prep them for Doom 3, as well as understanding how to get them into the game itself.
     
  15. lzt120

    lzt120

    Joined:
    Apr 13, 2010
    Posts:
    93
    How to modify the shader from wiki to make the _Blend can pingPang between 0-1 by Time?
     
  16. lzt120

    lzt120

    Joined:
    Apr 13, 2010
    Posts:
    93
    Finally Make it out:This shader can switch between two textures.

    Shader "Custom/Transparent/Cutout/TimeBasedSoftEdgeBlendedUnlit" {
    Properties {
    _Color ("Main Color", Color) = (1, 1, 1, 1)
    _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
    _DelayFactor ("Blend Factor", Range (1,10)) = 5
    _Texture1 ("Texture 1", 2D) = ""
    _Texture2 ("Texture 2", 2D) = ""
    }

    SubShader {
    Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    Lighting off

    // Render both front and back facing polygons.
    Cull Off

    // first pass:
    // render any pixels that are more than [_Cutoff] opaque
    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    };

    struct v2f {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    };

    sampler2D _Texture1;
    sampler2D _Texture2;
    float4 _Texture1_ST;
    float4 _Texture2_ST;
    float _Cutoff;
    float _Blend;
    float _DelayFactor;

    v2f vert (appdata_t v)
    {
    v2f o;
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    o.color = v.color;
    o.texcoord = TRANSFORM_TEX(v.texcoord, _Texture1);
    return o;
    }

    float4 _Color;
    half4 frag (v2f i) : COLOR
    {

    half4 col;

    _Blend = sin(_Time.yyyy) * _DelayFactor;

    if(_Blend > 1)
    {
    _Blend = 1;
    }

    if(_Blend < 0)
    {
    _Blend = 0;
    }

    col = lerp(tex2D(_Texture1, i.texcoord),tex2D(_Texture2, i.texcoord), _Blend);
    clip(col.a - _Cutoff);

    return col;
    }
    ENDCG
    }

    // Second pass:
    // render the semitransparent details.
    Pass {
    Tags { "RequireOption" = "SoftVegetation" }

    // Dont write to the depth buffer
    ZWrite off

    // Set up alpha blending
    Blend SrcAlpha OneMinusSrcAlpha

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    };

    struct v2f {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    };

    sampler2D _Texture1;
    sampler2D _Texture2;
    float4 _Texture1_ST;
    float4 _Texture2_ST;
    float _Cutoff;
    float _Blend;
    float _DelayFactor;

    v2f vert (appdata_t v)
    {
    v2f o;
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    o.color = v.color;
    o.texcoord = TRANSFORM_TEX(v.texcoord, _Texture1);
    _Blend = _Time;
    return o;
    }

    float4 _Color;
    half4 frag (v2f i) : COLOR
    {
    half4 col;

    _Blend = sin(_Time.yyyy) * _DelayFactor;

    if(_Blend > 1)
    {
    _Blend = 1;
    }

    if(_Blend < 0)
    {
    _Blend = 0;
    }

    col = lerp(tex2D(_Texture1, i.texcoord),tex2D(_Texture2, i.texcoord), _Blend);
    clip(col.a - _Cutoff);

    return col;
    }
    ENDCG
    }
    }

    SubShader {
    Tags { "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    Lighting off

    // Render both front and back facing polygons.
    Cull Off

    // first pass:
    // render any pixels that are more than [_Cutoff] opaque
    Pass {
    AlphaTest Greater [_Cutoff]
    SetTexture [_Texture1] {
    constantColor [_Color]
    combine texture * constant, texture * constant
    }
    }

    // Second pass:
    // render the semitransparent details.
    Pass {
    Tags { "RequireOption" = "SoftVegetation" }

    // Dont write to the depth buffer
    ZWrite off

    // Only render pixels less or equal to the value
    AlphaTest LEqual [_Cutoff]

    // Set up alpha blending
    Blend SrcAlpha OneMinusSrcAlpha

    SetTexture [_Texture1] {
    constantColor [_Color]
    Combine texture * constant, texture * constant
    }
    }
    }

    }