ShaderLab: Culling & Depth Testing
ShaderLab: 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.
cull是一种优化,它不会呈现出从观察者面前面对的多边形。所有多边形都有一个正面和背面。cull的原因是大多数物体都是封闭的,如果你有一个立方体,你就永远不会看到你的面朝你的方向(在它面前总是有一个面朝你的),所以我们不需要把两边都画出来。因此,这一术语是“cull
back”。
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
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
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.
控制这个对象的像素是否被写入深度缓冲区(默认值是On)。如果你在绘制固体物体,请把它留在这里。如果你正在绘制半透的效果,请切换到ZWrite。下面将详细介绍更多的细节。
ZTest
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
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.
允许使用两个参数指定深度偏移量。Offset和
Units。考虑到多边形的X或Y的最大Z斜率,单位可以缩放最小可解的深度缓冲值。这使得你可以强迫一个多边形在另一个位置上画,尽管它们实际上处于相同的位置。例如,偏移0,-1将多边形靠近摄像机,忽略多边形的斜率,而偏移--1,-1,在观察一个放牧的角度时,会使多边形更加接近。
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.
尝试应用它的多维数据集,并注意周围的几何感觉时所有错误的轨道。这是因为你’只是看到多维数据集的内部零件。
Transparent shader with depth writes 有深度的透明着色器
Usually semitransparent shaders do not write into the depth buffer. However, this can create draw order problems, especially with complex non-convex meshes. If you want to fade in & out meshes like that, then using a shader that fills in the depth buffer before rendering transparency might be useful.
通常,半闪烁的着色器不会写入深度缓冲。然而,这可能会导致绘制顺序问题,尤其是在复杂的非凸网格中。如果你想要像那样在网格中淡出,那么使用一个在渲染透明度之前填充深度缓冲的着色器是很有用的。
Semitransparent object; left: standard Transparent/Diffuse shader; right: shader that writes to depth buffer.半透明对象;左:标准的透明度/漫反射的着色器;右:写到深度缓冲的着色器。
Shader "Transparent/Diffuse ZWrite" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
// extra pass that renders to depth buffer only
Pass {
ZWrite On
ColorMask 0
}
// paste in forward rendering passes from Transparent/Diffuse
UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}
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.
下一个更有趣;首先我们用普通的顶点光照来渲染对象,然后用亮粉色渲染后面。这将会突出显示你的法线需要被翻转的任何地方。如果你看到物理控制的物体被任何一个网格“吸收”,试着给它们分配这个阴影。如果任何粉色的部件都是可见的,那么这些部件就会在任何不幸的地方碰到它。
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.
控制筛选比调试背景信息更有用。如果你有透明的对象,你通常会想要显示对象的后面。如果你没有进行任何扑杀(扑杀),你很可能会有一些后脸重叠部分的正面。
Here is a simple shader that will work for convex objects (spheres, cubes, car windscreens).
这是一个简单的着色器,它适用于凸对象(球体、立方体、汽车挡风玻璃)。
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.
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
// Set up alpha blending
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
}
}
}
}