查看: 2704|回复: 0
打印 上一主题 下一主题

[Unity 组件参考手册]着色器参考之着色器语法:Culling & Depth Testing

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-25 17:07:05 |只看该作者 |倒序浏览
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.剔除是一种通过避免渲染背对观察者的几何体面来提高性能的优化措施。所有几何体都包含正面和反面。剔除基于大多数对象都是封闭的事实;如果你有一个立方体,你不会看到背离你的那一面(总是只有一面在你的前方),因此我们不需要绘制出背面。因此也被称做背面剔除。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.
    控制是否将来之对象的像素写入深度缓冲(默认开启),如果你正绘制纯色物体,将此项打开。如果你正绘制半透明效果,关闭深度缓冲,更多细节如下
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则会几何体在一个几乎擦过的角度被观察使看起来更近些。  [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.下一个更加有趣;首先我们使用法线顶点光照渲染对象,然后我们使用亮粉处渲染背面。任何你的法线需要跳起的地方都会有高光的效果,如果你期望物理控制的对象有任意网格的吸入的效果时,可以尝试使用这个着色器。如果任何粉红色的部分显示,这些部分可能会不幸的带入任何事物。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
            }
        }
    }
}
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-10 13:04 , Processed in 0.141456 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部