Unity3d Shader(三) Pass(Culling & Depth Testing)透明立方体

剔除是一种通过避免渲染背对观察者的几何体面来提高性能的优化措施。所有几何体都包含正面和反面。剔除基于大多数对象都是封闭的事实;如果你有一个立方体,你不会看到背离你的那一面(总是只有一面在你的前方),因此我们不需要绘制出背面。因此也被称做背面剔除。
 
另一个使得渲染看起来正确的是深度测试。深度测试确保只有场景内的对象的最靠近的表面参与绘制。
 
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.
控制是否将来之对象的像素写入深度缓冲(默认开启),如果你正绘制纯色物体,将此项打开。如果你正绘制半透明效果,关闭深度缓冲,更多细节如下
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.
允许你定义用两个参数深度偏移。因子和单位。Factor 缩放Z的最大斜率,几何体的X和Y也一样,units缩放可计算的深度缓冲值。这允许你迫使一个几何体绘制在另一个的上层,尽管他们实际上是在同一个位置。例如偏移0,-1使得靠近摄像机的几何体忽略几何体的斜率,而偏移-1,-1则会几何体在一个几乎擦过的角度被观察使看起来更近些。
 
例子:
 
Glass Culling 玻璃剔除
控制剔除比背面调试更有用。如果你有透明物体,你经常会想要显示一个对象的背面。如果你不做任何剔除,你会发现有时常有一部分背面会覆盖在前面的一部分上。
下面是一个用于凸物体(球,立方体,车窗)的简单着色器。
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.
  // 渲染对象面向我们的部分

你可能感兴趣的:(ShaderLab)