原理很简单,设置一张黑白的遮罩图,根据遮罩图的某个非alpha通道的颜色从深到浅,逐渐设置透明和溶解的颜色。
直接贴代码:
Shader "MyShader/DissMask" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_MaskTex("Mask Image",2D) = "white"{}
_MainColor("Main Color", Color) = (1,1,1,1)
_LightColor("Light Color", Color) = (1,1,1,1)
_DissFactor("Diss Factor ",Range(0,1)) = 0
_Cutoff("_Cutoff ",Range(0,1)) = 0
}
SubShader {
Tags { "RenderType"="Transparent" }
LOD 200
Cull off
//ZWrite Off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainTex;
sampler2D _MaskTex;
struct Input {
float2 uv_MainTex;
float2 uv_MaskTex;
};
fixed4 _MainColor;
fixed4 _LightColor;
fixed _DissFactor;
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
fixed4 m = tex2D(_MaskTex, IN.uv_MaskTex);
fixed color = m.r;
if (color < _DissFactor)
{
o.Alpha = 0;
}else
{
if (color-_DissFactor<0.1 && _DissFactor!=0)
{
fixed temp = (color - _DissFactor) * 10;
o.Albedo = (_MainColor.rgb * temp + _LightColor.rgb *(1-temp));
o.Alpha = temp;
}
else
{
o.Albedo = c.rgb;
o.Alpha = c.a;
}
}
}
ENDCG
}
FallBack "Diffuse"
}
上面代码在处理消融颜色的时候,感觉效果并不太理想。又不知道怎么修改,希望大神给点建议。
重点记录一下新学到的内容,在刚开始#program是这么写的
因为是三维消融,所以要把双面渲染开启,这样就可以透过前面透明的位置看到后面的内容
开启双面渲染比较简单,只要把CULL属性关闭就好了
在刚开始#pragma是这么写的
#pragma surface surf Lambert alpha:blend
但是这样写会再渲染时产生深度错乱的问题
具体原因请看这篇文章
http://blog.csdn.net/candycat1992/article/details/41599167
于是,把#pragma修改为这样子
#pragma surface surf Lambert alphatest:_Cutoff
==========================================================================
晚上睡觉的时候想到一种很普遍的需求,从上往下或者从下往上消失。这就需要根据顶点坐标来改变像素透明度
没什么技术含量,改了一下代码
Shader "MyShader/DissMask" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_MaskTex("Mask Image",2D) = "white"{}
_MainColor("Main Color", Color) = (1,1,1,1)
_LightColor("Light Color", Color) = (1,1,1,1)
_DissFactor("Diss Factor ",Range(-2,2)) = 0
_Trace("Trace Range",Range(0,0.5))=0
_Cutoff("_Cutoff ",Range(0,1)) = 0
}
SubShader {
Tags { "RenderType"="Transparent" }
LOD 200
Cull off
//ZWrite Off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Lambert alphatest:_Cutoff vertex:vert
sampler2D _MainTex;
sampler2D _MaskTex;
struct Input {
float2 uv_MainTex;
float2 uv_MaskTex;
fixed4 vertColor;
};
fixed4 _MainColor;
fixed4 _LightColor;
fixed _DissFactor;
fixed _Trace;
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.vertColor = v.color;
if (v.vertex.z<_DissFactor)
{
o.vertColor.a = 0;
}
else
{
if (v.vertex.z < _DissFactor + _Trace)
{
o.vertColor.a = 0.5;
}
}
}
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
fixed4 m = tex2D(_MaskTex, IN.uv_MaskTex);
fixed color = m.r;
if (IN.vertColor.a==0.5)
{
o.Albedo = (c.rgb+ m.r*_MainColor+(1-m.r)*_LightColor)/2;
o.Alpha = 1;
}
else
{
o.Albedo = c.rgb;
o.Alpha = IN.vertColor.a;
}
}
ENDCG
}
FallBack "Diffuse"
}
一分钱的过渡效果,调了好长时间都不满意,就先这样吧