Skip to content

Instantly share code, notes, and snippets.

@nukeop
Last active November 11, 2024 11:06
Show Gist options
  • Save nukeop/1d2e54bed93ca7e5a67794e45a402541 to your computer and use it in GitHub Desktop.
Save nukeop/1d2e54bed93ca7e5a67794e45a402541 to your computer and use it in GitHub Desktop.
Dissolve shader for Unity

dissolveeffect

Add this texture to the second slot. Set it to alpha from grayscale and alpha is transparency. Your texture goes in the first one.

Make a new material with this shader and play with the sliders and colours. You can create cool animated effects if you change dissolution level and/or edge width gradually over time.

Example of the shader in action:

anim

Shader "Unlit/DissolveEffectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_NoiseTex ("Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_EdgeColour1 ("Edge colour 1", Color) = (1.0, 1.0, 1.0, 1.0)
_EdgeColour2 ("Edge colour 2", Color) = (1.0, 1.0, 1.0, 1.0)
_Level ("Dissolution level", Range (0.0, 1.0)) = 0.1
_Edges ("Edge width", Range (0.0, 1.0)) = 0.1
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _NoiseTex;
float4 _EdgeColour1;
float4 _EdgeColour2;
float _Level;
float _Edges;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
#ifdef PIXELSNAP_ON
o.vertex = UnityPixelSnap (o.vertex);
#endif
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
float cutout = tex2D(_NoiseTex, i.uv).r;
fixed4 col = tex2D(_MainTex, i.uv);
if (cutout < _Level)
discard;
if(cutout < col.a && cutout < _Level + _Edges)
col =lerp(_EdgeColour1, _EdgeColour2, (cutout-_Level)/_Edges );
return col;
}
ENDCG
}
}
}
@nukeop
Copy link
Author

nukeop commented Dec 14, 2017

Free to use under the terms of the MIT License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment