打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Shader: 获取深度贴图数据

Lately I’ve been working more with depth-based image effects and I often had to search through my archive to find examples of using the camera’s depth texture. Therefore, this Shader Bits post will be a bit different from the other ones. Since the subject is more relevant to image effects, this post won’t have the same format with the different code for vertex-fragment and surface shaders. Instead, I will present some shader code snippets to make use of the camera’s depth and normal textures.

More information on depth textures can be found in this manual from unity. You should also take a look at this manual page as well.

Also, keep in mind that in order for some of the stuff here to work you may have to change your camera’s depth texture mode.

Linear eye depth

Getting the linear eye depth is made easy using Unity’s built-in methods and macros. All you have to do basically is this:


sampler2D _CameraDepthTexture;

fixed4 frag (v2f i) : SV_Target

{

    fixed4 col = tex2D(_MainTex, i.uv);

    float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);

    depth = LinearEyeDepth(depth);

    return col;

}

I think it’s pretty straightforward. Obviously lines 6 and 7 could be combined but they’re like that for the sake of clarity.

    这里获取的是真实值, 不是归一化的值

Linear 0-1 depth

As mentioned before, having things in the [0,1] spectrum is amazingly useful. In order to get the camera’s depth in a [0,1] spectrum Unity gives us the “Linear01Depth” method, which was shown in the Firewatch fog post. Fun fact: you can use the EXACT same code snippet as the linear eye depth, but instead of “LinearEyeDepth(depth)” in line 7 you use “Linear01Depth(depth)”. It’s that simple. However, as in the Firewatch fog post, you might come across a different implementation of the same method, which I will present here, just so you have it and know that it exists:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sampler2D _CameraDepthTexture;
struct v2f
{
    float2 uv : TEXCOORD0;
    float4 vertex : SV_POSITION;
    float4 scrPos : TEXCOORD1;
};
v2f vert (appdata v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = v.uv;
    o.scrPos = ComputeScreenPos(o.vertex);
    return o;
}
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    float depth = (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)));
    depth = Linear01Depth(depth);
    return col;
}

This has a bit more hassle, as you have to also calculate the screen position, so I’d use the aforementioned way. But that works too.

获取的是归一化的值. -- 比较少的计算量

PS : 这个东西使用的是屏幕坐标进行获取 _CameraDepthTexture 的深度值, 可是通过偏移屏幕坐标得到的是错误的数据, 在使用上限制太多, 非常不适合使用......

Depth and normals

There is also a way to get the camera’s depth along with the normal information for each pixel, which could be useful for SO many effects. Here’s it:

1
2
3
4
5
6
7
8
9
10
sampler2D _CameraDepthNormalsTexture;
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    half3 normal;
    float depth;
    DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv), depth, normal);
    return col;
}

The “DecodeDepthNormal” method when called will change the values of the “depth” and “normal” parameters, similarly to what a method would do to parameters with the “ref” or “out” keyword. Do keep in mind that these normal vectors could need some kind of matrix transformation.

That’s about it! Hopefully you’ll put those bits to good use, cause depth based effects can be hella awesome! See you in the next one!

P.S.: Recently came across Ryan Brucks’ amazing work in his site, http://shaderbits.com. Awkwardly enough, I found out about the site after I posted the first two “Shader Bits” posts. Nevertheless, while his work is relative to UE4, it’s still awesome and I’d suggest you give it a look, even just for the inspiration.

获取归一化的值, 如果是为了获取深度和向量的话使用它, 计算量较大, 如果只为了获取深度值的话, 建议用第二个减少计算量.




Footer

The code in the shader tutorials is under the CC0 license, so you can freely use it in your commercial or non-commercial projects without the need for any attribution/credits.

These posts will never go behind a paywall. But if you really really super enjoyed this post, consider buying me a coffee, or becoming my patron to get exciting benefits!



Or don't, I'm not the boss of you.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
William Chyr | Unity Shaders
UnityShader学习教程之<UsePass和#include>
Unity制作深度相机
猫都能学会的Unity3D Shader入门指南(一)
Unity shader 官网文档全方位学习(一)
RenderDoc[03] 还原粒子特效shader
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服