Unity3D Shader官方教程翻译(七)----Shader语法:Pass的Culling & Depth Testing





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  unitsFactor 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原创,转载请说明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 完美剔除和遮挡剔除都是计算机图形学中的优化技术,目的是在场景中只渲染可见的物体,从而提高渲染效率。 完美剔除是指通过检测物体是否在视野内来剔除不需要渲染的物体。这种剔除方式需要使用一系列算法来检测物体是否可见,包括视锥剔除、反向相交剔除等等。完美剔除的优点是可以剔除不必要的渲染,从而提高帧率和性能,但同时也需要较大的计算开销。 遮挡剔除则是在完美剔除的基础上,通过对物体的相互遮挡关系的检测来进一步提高渲染效率。在遮挡剔除中,通常会使用空间划分树等数据结构来优化遮挡关系的计算。遮挡剔除的优点是可以剔除视线被阻挡的物体,从而降低渲染负载,但是需要在每一帧进行遮挡检测,计算复杂度较高。 总的来说,完美剔除和遮挡剔除都是提高渲染效率的有效方法,但具体应用取决于具体的场景和计算需求。 ### 回答2: Perfect culling - occlusion culling,也叫视野遮挡剔除,是计算机图形学中的一项技术,利用算法判断物体是否在视野或视线范围内,决定哪些物体需要渲染,哪些可以被剔除或隐藏,从而加速计算机图形的处理速度和优化渲染。 完美剔除是指完全的剔除,即仅在无需绘制的情况下进行剔除,从而节省系统资源。通过分析场景中的物体和环境,可以减少未被看到的物体的绘制,从而提高帧率和性能。此外,完美剔除还可以消除隐藏物体对深度测试产生的影响,提高渲染质量。 视野遮挡剔除(Occlusion Culling)是一种先进的完美剔除技术,它基于场景的几何形状,通过计算相机的视线路径对场景中的物体进行剔除。通常,OC会对游戏场景进行预处理,并将相机的视锥体与场景的几何体进行比较,从而确定哪些物体可以被遮挡,从而进行动态的渲染。 综上所述,完美剔除-视野遮挡剔除是一种计算机图形学中高效的技术,可以显着提高渲染效率和性能,并使绘制的图形更加真实和准确。它在游戏开发、虚拟现实等领域有着广泛的应用前景。 ### 回答3: 完美的剔除技术——遮挡剔除 完美的剔除技术通常包括两种类型,一种是静态剔除,另一种是动态剔除。 遮挡剔除是一种静态剔除技术,它的基本原理是在场景渲染前,对场景进行逐像素的遮挡分析,找出被遮挡物体,将其剔除掉,只渲染视图内可见的物体,从而达到提高渲染性能的效果。这种技术可以应用于虚拟现实、游戏开发等领域,尤其是对于大规模场景和复杂场景的渲染,遮挡剔除可以显著降低渲染的开销,提高场景的渲染效率。 具体来讲,遮挡剔除通过采用空间分区、视景体裁剪等技术,将场景以及被渲染物体分为多个区域,然后逐个区域对相邻的物体进行深度比较和遮挡分析,找出被遮挡物体,将其剔除掉。如果采用了合适的剔除策略和算法,遮挡剔除可以大幅减少被渲染对象的数量,从而降低了渲染的开销,提高了渲染效率和帧率。 总的来说,遮挡剔除技术在现代计算机图形学中是非常重要的技术之一,它可以有效地提高场景渲染效率,增加场景的视觉真实感和交互性,是图形学领域中不可或缺的技术之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值