转载链接:Shader代码转载地址
1、效果图
2、简述
这里是通过改变材质的透明度来实现 渐入渐出的效果,使用系统自带的shader似乎是无法达到这样的效果,如设置shader为Standard,Rendering Mode 设置为Transparent,将透明度调制0,最后的效果还是看得到模型。
3、 Shader代码
Shader "Transparent/VertexLit with Z" {
Properties{
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Spec Color", Color) = (1,1,1,0)
_Emission("Emissive Color", Color) = (0.15,0.15,0.15,0)
_Shininess("Shininess", Range(0.1, 1)) = 0.7
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader{
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
// Render into depth buffer only
Pass{
ColorMask 0
}
// Render normally
Pass{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
Material{
Diffuse[_Color]
Ambient[_Color]
Shininess[_Shininess]
Specular[_SpecColor]
Emission[_Emission]
}
Lighting On
SetTexture[_MainTex]{
Combine texture * primary DOUBLE, texture * primary
}
}
}
}
4、逻辑代码
很简单的一段代码,就不写注释了
public class NewBehaviourScript : MonoBehaviour
{
private Material _mat;
private float _alpha = 0;
private bool _isFadeIn = true;
void Start()
{
_mat = this.GetComponent<Renderer>().material;
_mat.color = new Color(0, 1, 0, _alpha);
}
void Update()
{
_alpha += (_isFadeIn ? 1 : -1) * Time.deltaTime / 2;
_mat.color = new Color(0, 1, 0, _alpha);
if (_alpha > 1) _isFadeIn = false;
if (_alpha < 0) _isFadeIn = true;
}
}
5、Demo下载
使用的是unity5.5.1版本,如果打不开,请拷贝上面代码进行测试
【点击下载】FadeTest.unitypackage