Texture Blending

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.

Isn’t it frustrating when you don’t get an answer? I’m wondering the same thing.

How do you want to blend between the 2 images? If you want to use a slider then try this:

Shader "2 Texture Blend" {
	Properties {
		_Blend ("Blend", Range (0, 1)) = 0.5
		_MainTex ("Base (RGB)", 2D) = "white"
		_BlendTex ("Blend (RGB)", 2D) = "black"
	}
	SubShader {
		Pass {
			SetTexture [_MainTex]
			
			SetTexture [_BlendTex] {
				constantColor (0, 0, 0, [_Blend])
				combine texture lerp (constant) previous
			}
		}
	}
}

This is great! Thanks for replying so quickly. :slight_smile:

@maethor: yup. I posted this a while back and almost forgot about it. thanks for the bump. :smile:

@daniel: thanks for the reply. I will try it out :smile:

Thank you so much! This is exactly what i’ve been looking for.

is there a way to do it using vertex alpha, (instead of a slider or from a texture) ?

Works wonderfully! What if we can get it do be dissolve-looking thing? It would be great for animating rust on metal or something…

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.

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

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.

Alrighty then, I guess I’ll be learning a bit of SL.

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 !

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.

How to modify the shader from wiki to make the _Blend can pingPang between 0-1 by Time?

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
}
}
}

}