最近《元神》上线了,里面有个拾取宝箱后,宝箱溶解消失效果,试着做一下,整体还是很简单的。
直接上效果
达到这个效果需要一个噪声图用来做伪随机,然后通过一个阈值和噪声图任意一通道值比较进行clip,产从而实现控制现实与隐藏。图中蓝色部分是在将阈值进行偏移,偏移的部分显示一个新的颜色实现。
下面是代码:
Shader "box_disapper"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {
}
_NoiseTex ("Texture", 2d) = "white" {
}
_Strength ("Strength", range(0, 1)) = 0
_Edge ("Edge", range(0, 1)) = 0
_EdgeColor ("EdgeColor", color) = (1, 1, 1, 1)
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _NoiseTex;
float4 _NoiseTex_ST;
float _Strength;
float _Edge;
fixed4 _EdgeColor;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 noise_col = tex2D(_NoiseTex, i.uv);
clip(noise_col.r - _Strength);
col = noise_col.r - _Strength > _Edge ? col : _EdgeColor;
return col;
}
ENDCG
}
}
}
用到的噪声图: