纳金网

标题: [Unity 组件参考手册]着色器参考之高级ShaderLab主题:摄像机的深度纹理 [打印本页]

作者: .    时间: 2013-2-26 17:34
标题: [Unity 组件参考手册]着色器参考之高级ShaderLab主题:摄像机的深度纹理
In Unity a Camera can generate a depth or depth+normals texture. This is a minimalistic G-buffer texture that can be used for post-processing effects or to implement custom lighting models (e.g. light pre-pass). Camera actually builds the depth texture using Shader Replacement feature, so it's entirely possible to do that yourself, in case you need a different G-buffer setup.在Unity中,相机可以生成深度纹理或者深度+法线纹理。这是一个简化的G-buffer纹理,它可以用于一些后处理特效或者执行自定义的光照模式 (例如light pre-pass)。照相机实际上使用着色器替换功能来生成深度纹理,因此当你需要一个不同的G-buffer设置时,你完全可以自己生成一个深度纹理。Camera's depth texture can be turned on using Camera.depthTextureMode variable from script.相机的深度纹理可以通过在脚本使用 Camera.depthTextureMode 变量来打开。There are two possible depth texture modes:目前有两种可能的深度纹理模式:    DepthTextureMode.Depth: a depth texture.
    DepthTextureMode.Depth:一个深度纹理。
    DepthTextureMode.DepthNormals: depth and view space normals packed into one texture.
    DepthTextureMode.DepthNormals:将深度与视空间法线结合在一张纹理中。 DepthTextureMode.Depth textureThis builds a screen-sized depth texture.这是生成一个屏幕尺寸的深度纹理。
DepthTextureMode.DepthNormals textureThis builds a screen-sized 32 bit (8 bit/channel) texture, where view space normals are encoded into R&G channels, and depth is encoded in B&A channels. Normals are encoded using Stereographic projection, and depth is 16 bit value packed into two 8 bit channels.这生成一个屏幕大小的32 位(8位/通道)纹理,这里视空间的法线记录在红蓝通道中,深度记录在蓝色和半透明通道中。法线使用平射投影,深度为16位数值,并被放进两个8位通道中。UnityCG.cginc Cg include file has a helper function DecodeDepthNormal to decode depth and normal from the encoded pixel value. Returned depth is in 0..1 range.UnityCG.cginc Cg包含文件中含有一个帮助函数DecodeDepthNormal,它可以用来从指定像素中编码深度与法线值,得到的深度值在0-1区间。For examples on how to use the depth and normals texture, please refer to the EdgeDetection image effect in the Shader Replacement example project or SSAO Image Effect.比如,如何使用深度法线纹理,请参考EdgeDetection着色器替换例子中图像特效或SSAO图像特效。
Tips & Tricks 提示&技巧When implementing complex shaders or Image Effects, keep Rendering Differences Between Platforms in mind. In particular, using depth texture in an Image Effect often needs special handling on Direct3D + Anti-Aliasing.当你编写复杂的着色器或图像特效时,要切记不同平台间的渲染差异特性。特别地,在开启反走样特效的Direct3D平台上,如果在图像特效中使用深度纹理需要一些特别的处理。In some cases, the depth texture might come directly from the native Z buffer. If you see artifacts in your depth texture, make sure that the shaders that use it do not write into the Z buffer (use ZWrite Off).在某些情况下,深度纹理本身就是Z缓存。如果你在你的深度纹理中看到了一些瑕疵(artifacts),请确保那些使用它的着色器不会重写Z缓存(使用ZWrite Off)
Under the hood 在底层Depth texture can come directly from the actual depth buffer, or be rendered in a separate pass, depending on the rendering path used and the hardware. When the depth texture is rendered in a separate pass, this is done through Shader Replacement. Hence it is important to have correct "RenderType" tag in your shaders.深度纹理可以直接从深度缓冲器或在单独通道被渲染,取决于使用的渲染路径和硬件。当深度纹理在单独的通道渲染,是通过着色器替换来完成。因此,重要的是着色器有正确的“RenderType”标签。 【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/




欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5