地址
重点
代码一
Shader "Cg shader using blending" {
SubShader {
Tags { "Queue" = "Transparent" }
// draw after all opaque geometry has been drawn
//此处表示渲染顺序,这个渲染队列在几何体队列之后被渲染,采用由后到前的次序
Pass {
ZWrite Off // don't write to depth buffer
// in order not to occlude other objects
//渲染不透明物体是要进行深度比较,比较后不将深度值写到DepthBuffer
Blend SrcAlpha OneMinusSrcAlpha // use alpha blending 颜色混合
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 vertexPos : POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_MVP, vertexPos);
}
float4 frag(void) : COLOR
{
return float4(0.0, 1.0, 0.0, 0.3);
// the fourth component (alpha) is important:
// this is semitransparent green
}
ENDCG
}
}
}
1.Tags 子着色器使用标签来告诉渲染引擎期望何时和如何渲染对象。
Rendering Order - Queue tag
渲染次序 - 队列 标签
You can determine in which order your objects are drawn using the Queue tag. A Shader decides which render queue its objects belong to, this way any Transparent shaders make sure they are drawn after all opaque objects and so on.
你能使用 Queue 标签决定对象被渲染的次序。着色器决定它所归属的对象的渲染队列,任何透明渲染器可以通过这个办法确认可以在所有不透明对象渲染后再进行渲染。
There are four pre-defined render queues, but there can be more queues in between the predefined ones. The predefined queues are:
有四种预定义的渲染队列,在预定义队列之间还可以定义更多队列
-
Background - this render queue is rendered before any others. It is used for skyboxes and the like.
后台 - 这个渲染队列在所有队列之前被渲染,被用于渲染天空盒之类的对象。 -
Geometry (default) - this is used for most objects. Opaque geometry uses this queue.
几何体(缺省) - 这个队列被用于大多数对象。 不透明的几何体使用这个队列。 -
Transparent - this render queue is rendered after Geometry, in back-to-front order. Anything alpha-blended (i.e. shaders that don't write to depth buffer) should go here (glass, particle effects).
透明 - 这个渲染队列在几何体队列之后被渲染,采用由后到前的次序。任何采用alpha混合的对象(不对深度缓冲产生写操作的着色器)应该在这里渲染(玻璃,粒子效果) -
Overlay - this render queue is meant for overlay effects. Anything rendered last should go here (e.g. lens flares).
覆盖 - 这个渲染队列被用于实现叠加效果。任何需要最后渲染的对象应该放置在此处。(如镜头光晕) - 参考:http://www.ceeger.com/Components/SL-SubshaderTags.html
代码二
Shader "Cg shader using blending" {
SubShader {
Tags { "Queue" = "Transparent" }
// draw after all opaque geometry has been drawn
Pass {
Cull Front // first pass renders only back faces
// (the "inside")
ZWrite Off // don't write to depth buffer
// in order not to occlude other objects
Blend SrcAlpha OneMinusSrcAlpha // use alpha blending
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 vertexPos : POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_MVP, vertexPos);
}
float4 frag(void) : COLOR
{
return float4(1.0, 0.0, 0.0, 0.3);
// the fourth component (alpha) is important:
// this is semitransparent red
}
ENDCG
}
Pass {
Cull Back // second pass renders only front faces
// (the "outside")
ZWrite Off // don't write to depth buffer
// in order not to occlude other objects
Blend SrcAlpha OneMinusSrcAlpha // use alpha blending
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 vertexPos : POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_MVP, vertexPos);
}
float4 frag(void) : COLOR
{
return float4(0.0, 1.0, 0.0, 0.3);
// the fourth component (alpha) is important:
// this is semitransparent green
}
ENDCG
}
}
}
这部分相比前一部分只是添加了通道的处理,类似前一篇的内容