ShaderLab syntax: Culling & Depth Testing 语法:面剔除及深度测试
Culling is an optimization that does not render polygons facing away from the viewer. All polygons have a front and a back side. Culling makes use of the fact that most objects are closed; if you have a cube, you will never see the sides facing away from you (there is always a side facing you in front of it) so we don't need to draw the sides facing away. Hence the term: Backface culling.
面剔除是1个可选项,不被观众观察到的多边形面它不会去渲染。所有的多边形都包含1个正面和背面。剔除常用于剔除那些远离你的面,比如说背面通常情况下你是看不见的。所以通常情况下是剔除背面,但是你也可以选择剔除正面。
The other feature that makes rendering looks correct is Depth testing. Depth testing makes sure that only the closest surfaces objects are drawn in a scene.
另外一个使物体渲染看起来正常的是深度测试。深度测试确定在场景中只绘制物体离观察者最近的表面。
Syntax 语法
Cull Back | Front | Off 剔除 面 正面 关闭
Controls which sides of polygons should be culled (not drawn) 控制哪边的面被剔除(不被绘制)
Back Don't render polygons facing away from the viewer (default). 不去渲染观察者看不到的背面
Front Don't render polygons facing towards the viewer. Used for turning objects inside-out. 不去渲染观察者能看的到得正面。常用于由内而外显示物体。
Off Disables culling - all faces are drawn. Used for special effects. 关闭剔除,所有面都可见,用于一些特殊效果
ZWrite On | Off
Controls whether pixels from this object are written to the depth buffer (default is On). If you're drawng solid objects, leave this on. If you're drawing semitransparent effects, switch to ZWrite Off. For more details read below.
控制是否将物体的像素写入深度缓冲中(默认的是开启)。如果你想绘制某个固定物体,这里无用请不要看了。如果你想绘制半透明效果,请关闭ZWrite,详情请见后面。
ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always
How should depth testing be performed. Default is LEqual (draw objects in from or at the distance as existing objects; hide objects behind them).
控制深度测试如何进行。默认是LEqual(依据现有对象的距离绘制对象,隐藏小于设置的值的对象)。
Offset Factor , Units 深度偏移
Allows you specify a depth offset with two parameters. factor and units. Factor scales the maximum Z slope, with respect to X or Y of the polygon, and units scale the minimum resolvable depth buffer value. This allows you to force one polygon to be drawn on top of another although they are actually in the same position. For example Offset 0, -1 pulls the polygon closer to the camera ignoring the polygon's slope, whereas Offset -1, -1 will pull the polygon even closer when looking at a grazing angle.
允许你指定深度偏移的2个参数。factor和units, Factor 设置Z的最大斜率,关于多边形的X或Y。 units设置最小可分辨深度缓冲的值。当2个多边形在同一深度时候,这个可以强制设定一个多边形比另外1个多边形优先绘制。举例 ,offset 0,-1可以强制多边形更接近摄像头(忽略多边形的斜率)。offset -1,-1 在掠射角时候,将更加使多边形接近摄像头。
参考:原创:Unity3D Shader官方教程翻译(六)----Shader语法:Pass的Color, Material, Lighting /html/xwzx/ty/3317.html
Examples
This object will render only the backfaces of an object:
物体被剔除正面仅仅在渲染时候显示背面。
Shader "Show Insides" {
SubShader {
Pass {
Material {
Diffuse (1,1,1,1)
}
Lighting On
Cull Front
}
}
}
Try to apply it to a cube, and notice how the geometry feels all wrong when you orbit around it. This is because you're only seeing the inside parts of the cube.
尝试将它应用到一个立方体,并注意到绕它旋转时候是不是有种几何上错觉。这是因为你只看到立方体的内部。
Debugging Normals 调试法线
The next one is more interesting; first we render the object with normal vertex lighting, then we render the backfaces in bright pink. This has the effects of highlighting anywhere your normals need to be flipped. If you see physically-controlled objects getting 'sucked in' by any meshes, try to assign this shader to them. If any pink parts are visible, these parts will pull in anything unfortunate enough to touch it.
下面将变得十分有趣.首先我们渲染一个物体用顶点法线照明,然后我们将他的背面渲染成亮丽的粉红色。这样将在任何地方拥有高光效果,你的法线需要翻转。如果你观察1个被物理系统控制的物体被网格所吞没(产生碰撞或该物体一部分塞到网格里面去了),给这个物体指派这个Shader。如果其中任何粉色的部分可以被看到,那么这个部分将可以被推到任何地方,我们没办法触碰它。(物理系统模拟问题)
Here we go: 例子如下
Shader "Reveal Backfaces" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" { }
}
SubShader {
// Render the front-facing parts of the object.
// We use a simple white material, and apply the main texture.
Pass {
Material {
Diffuse (1,1,1,1)
}
Lighting On
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
// Now we render the back-facing triangles in the most
// irritating color in the world: BRIGHT PINK!
Pass {
Color (1,0,1,1)
Cull Front
}
}
}
Glass Culling 玻璃剔除
Controlling Culling is useful for more than debugging backfaces. If you have transparent objects, you quite often want to show the backfacing side of an object. If you render without any culling (Cull Off), you'll most likely have some rear faces overlapping some of the front faces.
可以控制的剔除比调试背面更有用。如果你有1个透明物体,你往往要显示这个物体的背面。如果你关闭了剔除,很可能物体后面的一些面和前面的一些面重叠。
Here is a simple shader that will work for convex objects (spheres, cubes, car windscreens).
这里有1个简单的Shader, 它将作用于一些凸面物体(球体,立方体,汽车挡风玻璃) .
Shader "Simple Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,0)
_SpecColor ("Spec Color", Color) = (1,1,1,1)
_Emission ("Emmisive Color", Color) = (0,0,0,0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.7
_MainTex ("Base (RGB)", 2D) = "white" { }
}
SubShader {
// We use the material in many passes by defining them in the subshader.
// Anything defined here becomes default values for all contained passes. 任何在这里定义的东西都会成为默认值(对所有passes有效)。
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
// Set up alpha blending 设置alpha混合
Blend SrcAlpha OneMinusSrcAlpha
// Render the back facing parts of the object.
// If the object is convex, these will always be further away
// than the front-faces.
// 如果物体是凸面物体,这些将比前表面更远
Pass {
Cull Front
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
// Render the parts of the object facing us.
// If the object is convex, these will be closer than the
// back-faces.
// 如果物体是凸面物体,这些将比后表面更近
Pass {
Cull Back
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
}
}
由www.J2meGame.com原创
Culling is an optimization that does not render polygons facing away from the viewer. All polygons have a front and a back side. Culling makes use of the fact that most objects are closed; if you have a cube, you will never see the sides facing away from you (there is always a side facing you in front of it) so we don't need to draw the sides facing away. Hence the term: Backface culling.
面剔除是1个可选项,不被观众观察到的多边形面它不会去渲染。所有的多边形都包含1个正面和背面。剔除常用于剔除那些远离你的面,比如说背面通常情况下你是看不见的。所以通常情况下是剔除背面,但是你也可以选择剔除正面。
The other feature that makes rendering looks correct is Depth testing. Depth testing makes sure that only the closest surfaces objects are drawn in a scene.
另外一个使物体渲染看起来正常的是深度测试。深度测试确定在场景中只绘制物体离观察者最近的表面。
Syntax 语法
Cull Back | Front | Off 剔除 面 正面 关闭
Controls which sides of polygons should be culled (not drawn) 控制哪边的面被剔除(不被绘制)
Back Don't render polygons facing away from the viewer (default). 不去渲染观察者看不到的背面
Front Don't render polygons facing towards the viewer. Used for turning objects inside-out. 不去渲染观察者能看的到得正面。常用于由内而外显示物体。
Off Disables culling - all faces are drawn. Used for special effects. 关闭剔除,所有面都可见,用于一些特殊效果
ZWrite On | Off
Controls whether pixels from this object are written to the depth buffer (default is On). If you're drawng solid objects, leave this on. If you're drawing semitransparent effects, switch to ZWrite Off. For more details read below.
控制是否将物体的像素写入深度缓冲中(默认的是开启)。如果你想绘制某个固定物体,这里无用请不要看了。如果你想绘制半透明效果,请关闭ZWrite,详情请见后面。
ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always
How should depth testing be performed. Default is LEqual (draw objects in from or at the distance as existing objects; hide objects behind them).
控制深度测试如何进行。默认是LEqual(依据现有对象的距离绘制对象,隐藏小于设置的值的对象)。
Offset Factor , Units 深度偏移
Allows you specify a depth offset with two parameters. factor and units. Factor scales the maximum Z slope, with respect to X or Y of the polygon, and units scale the minimum resolvable depth buffer value. This allows you to force one polygon to be drawn on top of another although they are actually in the same position. For example Offset 0, -1 pulls the polygon closer to the camera ignoring the polygon's slope, whereas Offset -1, -1 will pull the polygon even closer when looking at a grazing angle.
允许你指定深度偏移的2个参数。factor和units, Factor 设置Z的最大斜率,关于多边形的X或Y。 units设置最小可分辨深度缓冲的值。当2个多边形在同一深度时候,这个可以强制设定一个多边形比另外1个多边形优先绘制。举例 ,offset 0,-1可以强制多边形更接近摄像头(忽略多边形的斜率)。offset -1,-1 在掠射角时候,将更加使多边形接近摄像头。
参考:原创:Unity3D Shader官方教程翻译(六)----Shader语法:Pass的Color, Material, Lighting /html/xwzx/ty/3317.html
Examples
This object will render only the backfaces of an object:
物体被剔除正面仅仅在渲染时候显示背面。
Shader "Show Insides" {
SubShader {
Pass {
Material {
Diffuse (1,1,1,1)
}
Lighting On
Cull Front
}
}
}
Try to apply it to a cube, and notice how the geometry feels all wrong when you orbit around it. This is because you're only seeing the inside parts of the cube.
尝试将它应用到一个立方体,并注意到绕它旋转时候是不是有种几何上错觉。这是因为你只看到立方体的内部。
Debugging Normals 调试法线
The next one is more interesting; first we render the object with normal vertex lighting, then we render the backfaces in bright pink. This has the effects of highlighting anywhere your normals need to be flipped. If you see physically-controlled objects getting 'sucked in' by any meshes, try to assign this shader to them. If any pink parts are visible, these parts will pull in anything unfortunate enough to touch it.
下面将变得十分有趣.首先我们渲染一个物体用顶点法线照明,然后我们将他的背面渲染成亮丽的粉红色。这样将在任何地方拥有高光效果,你的法线需要翻转。如果你观察1个被物理系统控制的物体被网格所吞没(产生碰撞或该物体一部分塞到网格里面去了),给这个物体指派这个Shader。如果其中任何粉色的部分可以被看到,那么这个部分将可以被推到任何地方,我们没办法触碰它。(物理系统模拟问题)
Here we go: 例子如下
Shader "Reveal Backfaces" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" { }
}
SubShader {
// Render the front-facing parts of the object.
// We use a simple white material, and apply the main texture.
Pass {
Material {
Diffuse (1,1,1,1)
}
Lighting On
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
// Now we render the back-facing triangles in the most
// irritating color in the world: BRIGHT PINK!
Pass {
Color (1,0,1,1)
Cull Front
}
}
}
Glass Culling 玻璃剔除
Controlling Culling is useful for more than debugging backfaces. If you have transparent objects, you quite often want to show the backfacing side of an object. If you render without any culling (Cull Off), you'll most likely have some rear faces overlapping some of the front faces.
可以控制的剔除比调试背面更有用。如果你有1个透明物体,你往往要显示这个物体的背面。如果你关闭了剔除,很可能物体后面的一些面和前面的一些面重叠。
Here is a simple shader that will work for convex objects (spheres, cubes, car windscreens).
这里有1个简单的Shader, 它将作用于一些凸面物体(球体,立方体,汽车挡风玻璃) .
Shader "Simple Glass" {
Properties {
_Color ("Main Color", Color) = (1,1,1,0)
_SpecColor ("Spec Color", Color) = (1,1,1,1)
_Emission ("Emmisive Color", Color) = (0,0,0,0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.7
_MainTex ("Base (RGB)", 2D) = "white" { }
}
SubShader {
// We use the material in many passes by defining them in the subshader.
// Anything defined here becomes default values for all contained passes. 任何在这里定义的东西都会成为默认值(对所有passes有效)。
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
// Set up alpha blending 设置alpha混合
Blend SrcAlpha OneMinusSrcAlpha
// Render the back facing parts of the object.
// If the object is convex, these will always be further away
// than the front-faces.
// 如果物体是凸面物体,这些将比前表面更远
Pass {
Cull Front
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
// Render the parts of the object facing us.
// If the object is convex, these will be closer than the
// back-faces.
// 如果物体是凸面物体,这些将比后表面更近
Pass {
Cull Back
SetTexture [_MainTex] {
Combine Primary * Texture
}
}
}
}
由www.J2meGame.com原创