查看: 5107|回复: 1
打印 上一主题 下一主题

[Unity 组件参考手册]着色器参考之高级ShaderLab主题:使用深度纹理

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

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

跳转到指定楼层
楼主
发表于 2013-2-26 17:34:21 |只看该作者 |倒序浏览
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"s***ct 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
    }
}
}
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

1

听众

849

积分

初级设计师

Rank: 3Rank: 3

纳金币
1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-7-21 15:11:48 |只看该作者
学shader应该从哪里开始啊。。。
回复

使用道具 举报

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

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-20 09:21 , Processed in 0.086608 second(s), 32 queries .

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

© 2008-2019 Narkii Inc.

回顶部