当前位置:网站首页>Using depth and normal textures in unity
Using depth and normal textures in unity
2022-07-24 07:18:00 【whr12】
1、 Turn on depth and normal textures
By setting Camera Medium depthTextureMode Variable .
camera.depthTextureMode = DepthTextureMode.None; // Do not generate depth texture
camera.depthTextureMode = DepthTextureMode.Depth; // Generate depth texture
camera.depthTextureMode = DepthTextureMode.DepthNormals; // Generate depth normal texture
camera.depthTextureMode = DepthTextureMode.MotionVectors; // Specifies whether motion vectors should be rendered
// You can specify to generate multiple textures at the same time by or instructions
camera.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.DepthNormals;
2、 Access depth and normal textures
stay shader in , Use variable names _CameraDepthTexture and _CameraDepthNormalsTexture Access the corresponding texture .
sampler2D _CameraDepthTexture; // Depth texture , The depth value is stored in r passageway , Range [0,1]. adopt MVP Matrix changes z Worthy of
sampler2D _CameraDepthNormalsTexture; // Depth normal texture
Linearization of depth texture
For perspective projection , The stored depth value is nonlinear , To get a linear depth value , Need to transform , It can be done by LinearEyeDepth and Linear01Depth Complete the process
float depth = tex2D(_CameraDepthTexture, uv).r; // Get the stored depth value
float depth_view = LinearEyeDepth(depth); // Get the depth value in the perspective space , The range is between the near plane and the far plane ,[z_near, z_far]
float depth_01 = Linear01Depth(depth); // obtain [0,1] Depth value of linear distribution in the range , 0 In camera position ,1 In the far plane
Use of depth normal texture
The depth normal texture is 32bit code ( Every channel 8bit), Normal information is stored in R and G passageway , Depth information is stored in B and A passageway . The depth value is 16bit The data of , Stored separately in two 8bit In the channel .
therefore , To use a depth normal texture , After sampling, it needs to be decoded before it can be used . You can use UnityCG.cginc Medium DecodeDepthNormal decode .
inline void DecodeDepthNormal( float4 enc, out float depth, out float3 normal )
{
depth = DecodeFloatRG (enc.zw);
normal = DecodeViewNormalStereo (enc);
}
float4 samplerValue = tex2D(_CameraDepthNormalsTexture, i.uv);
float depth; //depth The end result is [0,1] Linear distribution of ranges
float3 normal; //normal Is the normal direction in screen space
DecodeDepthNormal(samplerValue, depth, normal);
3、 Display the depth value and normal value
Depth texture display :
float4 samplerValue = tex2D(_CameraDepthNormalsTexture, i.uv);
float depth; //depth The end result is [0,1] Linear distribution of ranges
float3 normal; //normal Is the normal direction in screen space
DecodeDepthNormal(samplerValue, depth, normal);
return fixed4(depth, depth, depth,1);
![[0,1] Depth value of the range](/img/ab/6831eb4edcc0c3f15b6026f7e4c766.png)
Normal texture display :
float4 samplerValue = tex2D(_CameraDepthNormalsTexture, i.uv);
float depth; //depth The end result is [0,1] Linear distribution of ranges
float3 normal; //normal Is the normal direction in screen space
DecodeDepthNormal(samplerValue, depth, normal);
return fixed4(normal*0.5+0.5,1);
![Mapping to [0,1] Normal texture in the view space of the range](/img/3d/0c5ca44272305ac8bf58f84df282d1.png)
4、 Reference material
[1] Feng Lele 《Unity Shader Introduction to topic 》 page 269-272
[2] Unity manual https://docs.unity3d.com/2020.2/Documentation/Manual/SL-CameraDepthTexture.html
边栏推荐
猜你喜欢
随机推荐
编译与调试(gcc,g++,gdb)
Jay Chou's live broadcast was watched by more than 6.54 million people, with a total interaction volume of 450million, helping Kwai break the record again
JS_实现多行文本根据换行分隔成数组
Can recursion still play like this? Recursive implementation of minesweeping game
Unity中使用深度和法线纹理
拉普拉斯(Laplace)分布
C language to achieve three chess? Gobang? No, it's n-chess
Take you to learn C step by step (second)
Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?
Basic syntax of MySQL DDL and DML and DQL
Empty cup mentality, start again
Chapter007 FPGA learning IIC bus EEPROM reading
解压主播狂揽4000w+播放,快手美食赛道又添新风向?
【行测】图形找规律类题目
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
My creation anniversary
django.db.utils. OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
Paper reading: hardnet: a low memory traffic network
MITRE ATT&CK超详细学习笔记-02(大量案例)
numpy.inf









