- Home /
Two Textures Dissolve Shader (clip & lerp)
Hello! I've been trying to accomplish dissolving one texture to another just like in this Unity video (https://youtu.be/3penhrrKCYg?t=44m50s - watch at 44:50). My textures seem to blend and dissolve together at the same time. I'm new to shaders and I can't figure out how to do it, I would be really thankful if anyone could help. Here is my code :
Shader "Unlit/Dissolve2Tex"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_SecondTex ("SecondTexture", 2D) = "white" {}
_DisolveTexture("Disolve Texture", 2D) = "white" {}
_Blend("Blend Amount", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD1;
};
sampler2D _MainTex;
sampler2D _SecondTex;
float4 _MainTex_ST;
float4 _SecondTex_ST;
sampler2D _DisolveTexture;
float _Blend;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv = TRANSFORM_TEX(v.uv, _SecondTex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 texture1 = tex2D(_MainTex,i.uv);
float4 texture2 = tex2D(_SecondTex,i.uv);
float4 dissolveTex = tex2D(_DisolveTexture,i.uv);
clip(dissolveTex - _Blend);
fixed4 col =lerp(texture1,texture2,_Blend);
return col;
}
ENDCG
}
}
}
Comment
Best Answer
Answer by Bodhid · Aug 11, 2017 at 01:16 PM
Shader "Unlit/Dissolve2Tex"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_SecondTex ("SecondTexture", 2D) = "white" {}
_DisolveTexture("Disolve Texture", 2D) = "white" {}
_Blend("Blend Amount", Range(0,1)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _SecondTex;
float4 _MainTex_ST;
float4 _SecondTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv2=TRANSFORM_TEX(v.uv, _SecondTex);
return o;
}
half _Blend;
sampler2D _DisolveTexture;
fixed4 frag (v2f i) : SV_Target
{
fixed4 texture1 = tex2D(_MainTex,i.uv);
fixed4 texture2 = tex2D(_SecondTex,i.uv);
return lerp(texture1,texture2,saturate(sign(_Blend-tex2D(_DisolveTexture,i.uv).r)));
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
Mixing two textures using lerp function doesn't work as it should 2 Answers
how to solve shader/texture problem: putting white icons on colored planes 1 Answer
Assign Texture To Material Unity 4.6 1 Answer
Combine Shaders 0 Answers
Why is my UV'd texture displaying wrapped with the wrong scale? 0 Answers