您的位置:首页 > 移动开发 > Unity3D

Unity3D翻译——Using Depth Textures

2012-05-29 22:20 387 查看
Using Depth Textures      
使用深度纹理

原文地址:http://unity3d.com/support/documentation/Components/SL-DepthTextures.html
如有翻译不当之处,还请帮忙指出!
 
Desktop
台式机

 
It is possible to create
Render Textures where each pixel contains a high precision "depth"
value (see RenderTextureFormat.Depth). This is mostly
used when some effects need scene's depth to be available (for example, soft particles, screen space ambient occlusion, translucency would all need scene's depth).

可以创建一种渲染纹理,其中每个像素都包含一个高精度的"深度"值(见
RenderTextureFormat.Depth)。这主要是用于一些需要场景深度信息的特效制作中(例如软粒子、屏幕空间的环境遮挡,半透明的特效均需要场景的深度信息)。
Pixel values in the depth texture range from 0 to 1 with a nonlinear distribution. Precision is usually 24 or 16 bits, depending on depth buffer used. When reading from depth texture, a high precision
value in 0..1 range is returned. If you need to get distance from the camera, or otherwise linear value, you should compute that manually.

深度纹理中每个像素所记录的深度值是从0
到1 非线性分布的。精度通常是 24
位或16
位,这主要取决于所使用的深度缓冲区。当读取深度纹理时,我们可以得到一个0-1范围内的高精度值。如果你需要获取到达相机的距离或者其他线性关系的值,那么你需要手动计算它。
Depth textures in Unity are implemented differently on different platforms.

Unity中的深度纹理在不同的平台上所执行的机制也是不尽相同的:


      
On Direct3D 9 (Windows), depth texture is either a native depth buffer, or a single channel 32 bit floating point texture ("R32F" Direct3D format).


      
在Direct3D 9 (Windows)中,深度纹理不仅是一个深度缓存,也是一个32位浮点纹理通道("R32F"
Direct3D 格式).。
·        
Graphics card must support either native depth buffer (INTZ format) or floating point render textures in order for them to work.

·        
为了让它们正常工作,显卡必须支持深度缓存(INTZ格式)或浮点型的渲染纹理。
·        
When rendering into the depth texture,
fragment program must output the value needed.

·        
当渲染到纹理时,片段程序必须能够输出需要的数值。
·        
When reading from depth texture, red component of the color contains the high precision value.

·        
当读取深度纹理时,颜色的红色部分包含高精度数值。


      
On OpenGL (Mac OS X), depth texture is the native OpenGL depth texture (see
ARB_depth_texture).


      
在OpenGL (Mac OS X)中,深度纹理是一个OpenGL深度纹理(见ARB_depth_texture
·        
Graphics card must support OpenGL 1.4 or
ARB_depth_texture extension.

·        
显卡必须支持OpenGL 1.4或者ARB_depth_texture扩展包。
·        
Depth texture corresponds to Z buffer contents that are rendered, it
does not use the result from the fragment program.
·        
深度纹理对应于Z缓冲区所渲染的内容,它并不使用片段程序所输出的结果。
 
Using depth texture helper macros   
使用深度纹理帮助宏

Most of the time depth textures are used to render depth from the camera.
UnityCG.cginc helper include file contains some macros to deal with the above complexity in this case:

大部分时间里,深度纹理都是用来渲染到达照相机的深度值。在此情况下,UnityCG.cginc
帮助器中的文件包含一些宏来应付上述情况:


      
UNITY_TRANSFER_DEPTH(o): computes eye space depth of the vertex and outputs it in
o (which must be a float2). Use it in a vertex program when

          rendering into a depth texture. On OpenGL this macro does nothing at all, because Z buffer value is rendered implicitly.

          UNITY_TRANSFER_DEPTH(o):计算顶点在眼空间的深度,并输出到o中(这必须是
float2类型)。当渲染到深度纹理时,在顶点程序中使用它。在
          OpenGL中,该宏不会起任何作用,因为Z
缓冲区的值是隐式地渲染出来的。


      
UNITY_OUTPUT_DEPTH(i): returns eye space depth from
i (which must be a float2). Use it in a fragment program when rendering into a depth texture.

          On OpenGL this macro always returns zero, because Z buffer value is rendered implicitly.


      
UNITY_OUTPUT_DEPTH(i):返回在i中的眼空间深度(这必须是
float2类型)。当渲染到深度纹理时,在片段程序中使用它。在OpenGL中,该宏始终返回
         0,因为Z
缓冲区的值是隐式地渲染出来的。


      
COMPUTE_EYEDEPTH(i): computes eye space depth of the vertex and outputs it in
o. Use it in a vertex program when
not rendering into a depth
         texture.


      
COMPUTE_EYEDEPTH(i):计算顶点在眼空间的深度,并输出到o中。当不渲染到深度纹理时,在顶点程序中使用它。


      
DECODE_EYEDEPTH(i): given high precision value from depth texture
i, returns corresponding eye space depth. This macro just returns
i*FarPlane on

         Direct3D. On OpenGL it linearizes and expands the value to match camera's range.


      
DECODE_EYEDEPTH(i):对于深度纹理i中的高精度值,返回其相应的眼空间深度。该宏在Direct3D中仅仅返回i*FarPlane。在OpenGL,它线性化并扩展
         该值来匹配相机的范围。
For example, this shader would render depth of its objects:

举个例子,这个着色器将渲染物体的深度值:
 
Shader "Render Depth" {

SubShader {

Tags { "RenderType"="Opaque" }

Pass {

Fog { Mode Off }

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "UnityCG.cginc"
struct v2f {

float4 pos : SV_POSITION;

float2 depth : TEXCOORD0;

};

v2f vert (appdata_base v) {

v2f o;

o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

UNITY_TRANSFER_DEPTH(o.depth);

return o;

}

half4 frag(v2f i) : COLOR {

UNITY_OUTPUT_DEPTH(i.depth);

}

ENDCG

}

}

}


iOS
 
This feature is not supported for iOS targets.

该功能暂不支持iOS设备。
 
Android
 
This feature is not supported for Android targets.

该功能暂不支持Android设备。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息