Unity Shader 最简单的消融溶解效果
效果演示
需要的贴图资源
渐变纹理
噪点图
代码展示
主要是使用了 Shader 中的 Clip() 函数,使用 Surface 表面着色器。
Shader "Custom/dissolve"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {} //主纹理
_NoisePatternTex("NoisePatternTex",2D) = "white"{} //黑白噪声纹理 影响笑容的位置
_Threshold("Threshold",Range(0,1)) = 0 //消融阈值
_EdgeLength("EdgeLength", Range(0,0.2)) = 0.1 //边缘过渡部分的值
_GradientTex("GradientTex", 2D) = "white"{} //渐变图,过渡部分用
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _NoisePatternTex;
half _Threshold;
sampler2D _GradientTex;
half _EdgeLength;
struct Input
{
float2 uv_MainTex;
float2 uv_NoisePatternTex;
};
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
//纹理贴图采样
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) ;
o.Albedo = c.rgb;
//获得噪声图的颜色 r 值
float cutoutValue = tex2D(_NoisePatternTex, IN.uv_NoisePatternTex).r;
//clip函数会将参数小于0的像素点直接丢弃掉
clip(cutoutValue - _Threshold);
//saturate(x)的作用是如果x取值小于0,则返回值为0。如果x取值大于1,则返回值为1。若x在0到1之间,则直接返回x的值.
float temporary = saturate((cutoutValue - _Threshold) / _EdgeLength);
fixed4 edgeColor = tex2D(_GradientTex,float2(temporary,temporary));
fixed4 resultColor = lerp(edgeColor, fixed4(0,0,0,0), temporary);
o.Emission = resultColor.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
调整Threshold值,效果如图所示。
在自己的脚本中进行动态控制即可实现死亡溶解效果
void ChangeValue(float value)
{
Material m = this.transform.GetComponent<SkinnedMeshRenderer>().materials[0];
m.SetFloat("_Threshold", value);
}