Shader "Unlit/Dissolve2"
{
Properties
{
_ResAmount("溶解",Range(0.0,1.0)) = 0.0
_LineWidth ("LineWidth", Range(0.0,0.2)) = 0.1
_MainTex ("Texture", 2D) = "white" {}
_Gloss("Gliss",Range(1,100)) = 1
_NormalST("NormalST", Float) = 1
_NormalMap("NormalMap",2D) = "while"{}
_ResMap("ResMap",2D) = "while"{}
_EDGColor ("EDGColor",Color) = (1,1,1,1)
}
SubShader
{
Pass
{
//Tags{"LightMode" = "ForwardBase"}
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
float _ResAmount;
float _LineWidth;
sampler2D _MainTex;
float4 _MainTex_ST;
float _Gloss;
float _NormalST;
sampler2D _NormalMap;
float4 _NormalMap_ST;
sampler2D _ResMap;
float4 _ResMap_ST;
float4 _EDGColor;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent:TANGENT;
};
struct v2f
{
float2 uvMaintex : TEXCOORD0;
float2 uvNormal : TEXCOORD1;
float3 lightDir : TEXCOORD2;
float3 worldvertex : TEXCOORD3;
float3 viewDir : TEXCOORD4;
float2 uvResMap:TEXCOORD5;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uvMaintex = TRANSFORM_TEX(v.uv, _MainTex);
o.uvNormal = TRANSFORM_TEX(v.uv,_NormalMap);
o.uvResMap = TRANSFORM_TEX(v.uv,_ResMap);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
o.worldvertex = mul(unity_ObjectToWorld,v.vertex).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 res = tex2D(_ResMap,i.uvResMap).rgb;
clip(res.r-_ResAmount);//小于0时剔除像素
//计算漫反射
float3 tangenLightDir = normalize(i.lightDir);
fixed3 tangentNormalDir = UnpackNormal(tex2D(_NormalMap,i.uvNormal));
tangentNormalDir.xy = tangentNormalDir.xy*_NormalST;
float3 diff = tex2D(_MainTex,i.uvMaintex);
fixed3 diffuse = _LightColor0.rgb*diff*max(dot(tangenLightDir,tangentNormalDir)*0.5+0.5,0);
//计算高光
fixed3 viewDir = normalize(i.viewDir);//视线方向
fixed3 harfDir = normalize(viewDir + tangenLightDir);//视角与平行光的平分方向
fixed3 specular = _LightColor0.rgb * diff * pow(max(dot(tangentNormalDir, harfDir),0), _Gloss);
fixed3 col = diffuse + specular+UNITY_LIGHTMODEL_AMBIENT.rgb*diff;
//添加边线颜色
fixed3 eColor = _EDGColor;//边线颜色
eColor = pow(eColor,5);//加强
fixed t = 1-smoothstep(0.0,_LineWidth,res.r-_ResAmount);//像素未被剔除时差值大于线宽输出1,相减t=0:未剔除的显示原色。
//差值小于边线,接近0时输出接近0,t增大:将被剔除的像素显色边界色
fixed3 finalColor = lerp(col,eColor,t*step(0.0001,_ResAmount));//_ResAmount大于0时t即生效t=0原色,=1边界色
return fixed4( finalColor,1);
}
ENDCG
}
//消融部分阴影处理
Pass {
Tags{"LightMode" = "ShadowCaster"}
CGPROGRAM
float _ResAmount;
sampler2D _ResMap;
float4 _ResMap_ST;
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
struct v2f{
V2F_SHADOW_CASTER;
float2 uvBurnMap:TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
o.uvBurnMap = TRANSFORM_TEX(v.texcoord,_ResMap);
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 burn = tex2D(_ResMap,i.uvBurnMap).rgb;
clip(burn.r-_ResAmount);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
}
//调用脚本(Update中)
r = Mathf.Lerp(0, 1, r+=Time.deltaTime/rtime);//rtime时间内r变为1
mt.SetFloat("_ResAmount", r);
//仅溶解部分代码
Shader "Unlit/Dissolve"
{
Properties
{
_BurnAmount ("Burn Amount", Range(0.0,1.0)) = 0.0
_LineWidth ("LineWidth", Range(0.0,0.2)) = 0.1
_EdgLightST("边缘光强度",Range(1,5)) = 3
_BaseColor("BaseColor",Color) = (1,1,1,1)
_BurnMap("BurnMap",2D) = "while"{}
//_Color1 ("Color1",Color) = (1,1,1,1)
[HDR]_Color2 ("Color2",Color) = (1,1,1,1)
}
SubShader
{
Pass{
//Tags{"LightMode" = "ForwardBase"}
Cull Off//双面可见
CGPROGRAM
float _BurnAmount;
float _LineWidth;
int _EdgLightST;
float4 _BaseColor;
sampler2D _BurnMap;
float4 _BurnMap_ST;
//float4 _Color1;
float4 _Color2;
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//#pragma multi_compile_fwdbase
struct a2v
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uvBurnMap: TEXCOORD2;
};
v2f vert(a2v v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uvBurnMap = TRANSFORM_TEX(v.uv,_BurnMap);
return o;
}
fixed4 frag(v2f i):SV_Target{
fixed3 burn = tex2D(_BurnMap,i.uvBurnMap).rgb;
clip(burn.r-_BurnAmount);
fixed t = 1-smoothstep(0.0,_LineWidth,burn.r-_BurnAmount);
fixed3 burnColor = _Color2;//lerp(_Color1,_Color2,t);
burnColor = pow(burnColor,_EdgLightST);
fixed3 finalColor = lerp(_BaseColor,burnColor,t*step(0.0001,_BurnAmount));
return fixed4(finalColor,1);
}
ENDCG
}
}
}