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 depthGetting the linear eye depth is made easy using Unity’s built-in methods and macros. All you have to do basically is this:
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 depthAs 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:
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 normalsThere 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:
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://. 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. 获取归一化的值, 如果是为了获取深度和向量的话使用它, 计算量较大, 如果只为了获取深度值的话, 建议用第二个减少计算量. FooterThe 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. |
|
来自: tiancaiwrk > 《Unity3d》