消融shader
记录平时遇到的shader
Shader "MyShader/Dissolve_Basic"
{
Properties
{
_MainColor("MainColor",Color) = (1,1,1,1)
_MainTex("MainTex",2D) = "white"{}
_NoiseTex("Noise",2D) = "white"{}
_Threshold("Threshoed",Range(0.0,1.0)) = 0 //镂空程度
_EdgeLength("Edge Length",Range(-0.2,0.2)) = 0.0 //边缘宽度
_RampTex("Ramp",2D) = "white"{} //边缘颜色需要采样的贴图
}
SubShader
{
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
pass
{
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uvMainTex : TEXCOORD0;
float2 uvNoiseTex : TEXCOORD1;
float2 uvRampTex : TEXCOORD2;
};
float4 _MainColor;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _NoiseTex;
float4 _NoiseTex_ST;
float _Threshold;
float _EdgeLength;
sampler2D _RampTex;
float4 _RampTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uvMainTex = TRANSFORM_TEX(v.uv,_MainTex);
o.uvNoiseTex = TRANSFORM_TEX(v.uv,_NoiseTex);
o.uvRampTex = TRANSFORM_TEX(v.uv,_RampTex);
return o;
}
fixed4 frag(v2f i) :SV_Target
{
fixed cutout = tex2D(_NoiseTex,i.uvNoiseTex).r;
clip(cutout - _Threshold); // 如果输入向量中的任何元素小于0,则丢弃当前像素
float degree = saturate( (cutout - _Threshold) / _EdgeLength); //确保0~1
fixed4 edgeColor = tex2D(_RampTex,float2(degree,degree));
fixed4 col = tex2D(_MainTex,i.uvMainTex);
col.rgb = _MainColor.rgb;
fixed4 final = lerp(edgeColor,col,degree);
return fixed4(final.rgb,1);
}
ENDCG
}
}
}
参考链接
https://www.jianshu.com/p/d8b535efa9db