当前位置:网站首页>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
边栏推荐
- C language from entry to soil function
- From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
- 【LeetCode】11. 盛最多水的容器 - Go 语言题解
- [line test] Figure finding regular questions
- Unity中使用深度和法线纹理
- MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)
- [steering wheel] the super favorite idea efficiency artifact save actions is uninstalled
- C language from entry to soil (II)
- 开发系统选择路线
- numpy.arange
猜你喜欢

Bookkeeping app: xiaoha bookkeeping 2 - production of registration page

编译与调试(gcc,g++,gdb)

Hackingtool of security tools
![[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled](/img/b0/54a826287154be5b758b3850e7fa51.png)
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled

Take you to learn C step by step (second)

Upload excel file

Part II - C language improvement_ 4. Secondary pointer

【时序逻辑电路】——寄存器

cookie_ session

C language from entry to soil (I)
随机推荐
/etc/rc. Local setting UI program startup and self startup
Part I - Fundamentals of C language_ 11. Comprehensive project - greedy snake
【行测】图形找规律类题目
解压主播狂揽4000w+播放,快手美食赛道又添新风向?
C process running permission
[leetcode] 11. Container with the most water - go language solution
The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
传统电商红利消失,怎么进军新型社交电商?
【Word】如何生成左侧的索引目录
第二部分—C语言提高篇_1. C语言概述
numpy.concatenate
定制 or 通用,中国 SaaS 未来发展趋势是什么?
17. What is the situation of using ArrayList or LinkedList?
JMeter笔记2 | JMeter原理及测试计划要素
Jenkins 详细部署
B. Also Try Minecraft
avaScript的流程控制语句
Hackingtool of security tools
cookie_ session
Mqtt learning