VR_Unity的基本Shader的剔除、深度测试、Alpha测试以及基本雾效

 剔除与深度测试(Culling & Depth Testing):

http://www.k2ma.com/support/jishufuwu/2015/1119/174.html


 
        1.1 剔除(Culling)的概念:
       对于实时交互的3D环境而言,现实的速度和效率是非常重要的。虽然现在的硬件能力非常的
快,但是要想保持30FPS的同时处理数十万的三角形,以现在的主流机器来说还是有些困难的。
          For realtime interactive 3D environment, the real speed and efficiency are very important. 
Although the hardware is now very fast, but it is still difficult to maintain the 30FPS's simultaneous 
processing of hundreds of thousands of triangles, and now the mainstream of the machine.

       为了解决这种问题,人们提出了很多方法,其中有LOD,有Culling。这两种方法并不矛盾,而且我们往往需要在culling的基础上再使用LOD进一步解决pipeline的负担。
        In order to solve this problem, people put forward a lot of methods, including LOD, Culling. 
These two methods are not contradictory, and we often need to use the culling based on the LOD 
to further solve the burden of pipeline.


      剔除是一种通过避免渲染背对观察者的几何体面来提高性能的优化措施。所有几何体都包含正面和反面。剔除基于大多数对象都是封闭的事实;如果你有一个立方体,你不会看到背离你的那一面(总是只有一面在你的前方),因此我们不需要绘制出背面。因此也被称做背面剔除。
      The optimization measure is to improve the performance of the observer by avoiding the 
rendering of the geometry of the back of the observer. All geometry contains both positive and 
negative. It is closed to the fact that most objects are closed; if you have a cube, you don't see 
the one that has to deviate from your side (always only one side in front of you), so we don't 
need to draw back. So it is called the reverse side.

      一言以蔽之,所谓剔除,就是被挡住或视角以外的我们看不到的物体,因为它们无关紧要,所以我们就不去绘制,以节省资源,提高场景的运行效率。
        In a word, we can not see objects from the perspective of the object, because they do not 
matter, so we do not draw, to save resources, improve the operating efficiency of the scene.

                   

      1.2 深度测试(Depth Testing)的概念
        在复杂的场景中,通常有多个物体需要绘制,这些物体之间通常会存在遮挡关系,离观察点较远的物体会因为近处物体的者的遮挡而不可见或只有部分可见,Direct3D图形系统提供了深度测试功能来实现这种效果。深度测试可以简化复杂场景的绘制,确保只有场景内的对象的最靠近的表面参与绘制。
        In complex scene, there are usually multiple objects need to be drawn, these objects 
usually of the occlusion relationship, far away from the observation point objects because of 
nearby objects occlusion and not visible or only partially visible, Direct3D graphics system 
provides depth test function to achieve this effect. Depth testing can simplify the rendering of 
complex scenes, ensuring that only the most close to the object in the scene is involved in 
rendering.

       把深度测试看做在一口井的井口处向井中观望。把所有物体都赋予一个深度值,放到井中来显示。深度越深的物体,离井口就越远。深度越浅的物体,离井口就越近。井表面的深度值为
0。离井口近而深度浅的物体,可能会把离井口远的物体遮挡住。最终显示在屏幕上的开启深度测试后的画面,就如在井口处向井中观望里面物体显示出的遮挡与层次的效果一样。当然,离井口的深度就是每个物体在世界坐标系中的矩阵的Z坐标值了。
        The depth of the test as well as the wellhead in a well to see the well. Give a depth value 
to all the objects, and put it in the well. The deeper the object, the farther away from the well. 
The more shallow the depth of the object, the more close to the wellhead. The depth of the 
well surface is 0. An object close to the mouth may be blocked by an object far from the mouth. 
Finally, on the screen to open the depth of the test after the test, as well as in the wellhead to 
see inside the object to see the effect of the same level as the object. Of course, the depth of 
the well head is the Z coordinates of each object in the world coordinate system.

         语句之二:ZWrite On | Off
        此语句用于控制是否将来之对象的像素写入深度缓冲(默认开启),如果需要绘制纯色物
体,便将此项打开,也就是写上ZWrite On。如果要绘制半透明效果,关闭深度缓冲,则用ZWrite Off。
      This statement is used to control whether the future object pixels are written to the depth buffer 
(enabled by default), if need to draw a solid objects, the open, is to write zwrite on. If you want to 
draw the translucent effect, close the depth buffer, then use Off ZWrite.

        语句之三:ZTest Less |Greater | LEqual | GEqual | Equal | NotEqual | Always

         此语句用于控制深度测试如何执行。
        This statement is used to control the depth of the test.

        缺省值是LEqual (绘制和存在的对象一致或是在其中的对象;隐藏其背后的对象),含义列举对应如下:
       The default value is LEqual (the object that is to be consistent or in the presence of the object; 
the object hidden behind it), and the meaning list is as follows:

        GEqual 只渲染大于等于AlphaValue值的像素;  
        Less 只渲染小于AlphaValue值的像素;
        LEqual 只渲染小于等于AlphaValue值的像素;
        Equal 只渲染等于AlphaValue值的像素;
        NotEqual 只渲染不等于AlphaValue值的像素;
        Always 渲染所有像素,等于关闭透明度测试。等于用AlphaTest Off
        Never 不渲染任何像素
 
        语句之四:Offset Factor ,Units
        此语句用两个参数(Facto和Units)来定义深度偏移。
       Two parameters for this statement (Facto and Units) to define the depth migration.

 
  • Factor参数表示 Z缩放的最大斜率的值。
  • The Factor parameter indicates the maximum slope of the Z scaling.
  • Units参数表示可分辨的最小深度缓冲区的值。
  • The Units parameter represents the value of the minimum depth buffer that can be resolved.

       于是,我们就可以强制使位于同一位置上的两个集合体中的一个几何体绘制在另一个的上
层。例如偏移量Offset 设为0, -1(即Factor=0, Units=-1)的值使得靠近摄像机的几何体忽略几何体的斜率,而偏移量为-1,-1(即Factor =-1, Units=-1)时,则会让几何体偏移一个微小的角度,让观察使看起来更近些。
      Thus, we can force a geometry in the same location as one of the two assemblies to be drawn on the other. For example, the offset Offset is set to 0, and the -1 (i.e. Units=1, Units=1) makes the geometry of the camera close to the Factor=0, while the offset is -1, and the -1 (i.e., =-1), then let 
the geometry offset a small angle, so that the observation can be closer.

        Alpha测试(Alpha testing)
 2.1 Alpha测试的概念:

        Unity中,Alpha测试(Alpha Testing)是阻止像素被写到屏幕的最后机会。在Pineline中Alpha测试的位置如下:
        In Unity, the Alpha test (Testing Alpha) is the last chance to stop the pixel being written to the 
screen. The position of the Alpha test in the Pineline is as follows:

              

        在最终渲染出的颜色被计算出来之后,可选择通过将颜色的透明度值和一个固定值比较。如果比较的结果失败,像素将不会被写到显示输出中。
        After the final render of the color is calculated, it can be selected by comparing the 
transparency value of the color to a fixed value. If the comparison fails, the pixels will not be 
written to the display output.

       Alpha测试在渲染凹形物体的透明部分时非常有用。显卡上有着每个像素写到屏幕上的深度记录。如果一个新的像素比原来的像素的深度深,那么新的像素就不会被写到屏幕中。
       The Alpha test is very useful in rendering the transparent part of the concave object. 
The video card has a depth record of each pixel on the screen. If a new pixel is deeper than 
the depth of the original pixel, then the new pixel will not be written to the screen.

               

      仔细看上图,会发现:

 

            左边的树使用了透明度测试(AlphaTest),注意在它的图形上的像素是如何完全透明或不透明的。
           The tree on the left uses the transparency test (AlphaTest), and note how the pixels in the 
image are completely transparent or opaque.

 
        中间的树只使用透明度混合(Alpha Blending)来渲染,注意由于深度缓冲的缘故靠近分支的透明部分是如何覆盖更远的叶子。
           
The intermediate tree uses only Blending Alpha to render, note that the transparent 
portion of the branch's transparent portion is covered further by the depth buffer.

 
        最右边的树是通过下文实战中的第六个Shader“6.简单的植被Shader”着色器来渲染的,实现了透明度混合和透明度测试的组合,细节没有粗糙之处,浑然天成。
         
The rightmost tree is by the sixth shader below actual combat. Simple vegetation shader 
"shaders to render to achieve the combination of blended transparency and alpha testing, no 
details of the rough place, unaware Tiancheng.

      Alpha测试相关句法
       语句之一:AlphaTest Off                 
       
        此语句用于渲染所有像素(缺省)
       This statement is used to render all pixels (default)

        语句之二:AlphaTest comparison AlphaValue

        此语句用于设定透明度测试只渲染在某一确定范围内的透明度值的像素。而AlphaValue为一个范围在0到1之间的浮点值。也可以是一个指向浮点属性或是一个范围属性,在后一种情况下需要使用标准的方括号写法标注索引名字,如([变量名]).
        This statement is used to set the transparency test only for rendering the pixels in a certain 
range of transparency values. While AlphaValue is a floating point value between 1 and 0. Can 
also be a point to floating point attributes, and a wide range of properties, in a need to label name 
index using the standard written in square brackets, as variables [name]).

         基本雾效(Fog)设置

    3.1 雾效相关背景和概念理解

      雾效(Fog)参数用于控制雾相关的渲染。
      Fog effect (Fog) parameters used to control fog related rendering.

       在计算机图形学中,雾化是通过混合已生成的像素的颜色和基于到镜头的距离来确定的最终的颜色来完成的。雾化不会改变已经混合的像素的透明度值,只是改变RGB值。
       In computer graphics, the atomization is accomplished by mixing the colors of the pixels that 
have been generated and the final color based on the distance from the lens. Atomization will not 
change the transparency of the already mixed pixels, just to change the RGB value.

                

        Unity中的缺省雾设定是基于Edit->RenderSettings中的设置的,且雾模式既可以是Exp2也可以是关闭;密度和颜色完全取自设定。
       The default setting in Unity is based on Edit>RenderSettings, and the fog mode can either be 
Exp2 or off; the density and color are completely from the set.

        注意如果使用了片段着色器的话,着色器中的雾设定仍然是有效的。如果平台没有对固定管线的雾功能支持,Unity会实时补上着色器以支持需要的雾模式。
        Note that if you use the word fragment shader shader, the fog in the set is still valid. If there is 
no platform for fixing pipeline fog function support, Unity will need to support real-time patch 
shaders fog mode.

        雾效相关的句法
      
        语句之一:Fog { FogCommands }
 
        此语句用于设定雾命令的内容,具体的填在大括号之中的内容见下面的语句。
        This statement is used to set up the contents of the fog command, and the specific contents of 
the large brackets are shown below.

         语句之二:Mode Off | Global | Linear | Exp | Exp2
         此语句用于定义雾模式。缺省是全局的,依据雾在渲染设定中是否打开确定可从无变化到平方值
         This statement is used to define the fog mode. The default is global, according to set whether 
to open the fog in the rendering can be determined from the change to the square value

          语句之三:Color ColorValue
          此语句用于设定雾的颜色值。
         This statement is used to set the color value of the fog.

          语句之四:Density FloatValue
          此语句以指数的方式设定雾的密度。
         This sentence by way of setting density index of fog.

           语句之五:Range FloatValue , FloatValue
           此语句用于为linear类型的雾设定远近距离值。
          This statement is used to set the distance values for the linear type of fog.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值