当前位置:网站首页>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
边栏推荐
- 论文阅读:HarDNet: A Low Memory Traffic Network
- Traditional e-commerce dividends disappear, how to enter the new social e-commerce?
- FPGA realizes reading and writing of axi4 bus
- Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx
- 17. 什么情况用ArrayList or LinkedList呢?
- 一个怎样的模式能让平台用户发生自助裂变?-链动2+1
- [leetcode] 444. Sequence reconstruction
- Nacos的高级部分
- yocs_ velocity_ Smooth source code compilation
- Chapter007 FPGA learning IIC bus EEPROM reading
猜你喜欢

PyTorch 深度学习实践 第10讲/作业(Basic CNN)

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

avaScript的流程控制语句

SPI - send 16 bit and 8-bit data

Unity中使用深度和法线纹理

Variables and data types (04) end

Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx

【方向盘】IDEA的代码审查能力,来保证代码质量

Paper reading: hardnet: a low memory traffic network
![[sequential logic circuit] - register](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[sequential logic circuit] - register
随机推荐
第二部分—C语言提高篇_3. 指针强化
【PTA】团体程序设计天梯赛-练习集 L3题目总结(不全)
JMeter笔记2 | JMeter原理及测试计划要素
MySql的DDL和DML和DQL的基本语法
OpenCascade笔记:gp包
SPI - send 16 bit and 8-bit data
C language from entry to soil (III)
【方向盘】IDEA的代码审查能力,来保证代码质量
JMeter notes 2 | JMeter principle and test plan elements
numpy.concatenate
win10声音图标有个没有声音
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
【Tips】创建版本控制项目的简单方法
【C语言入门】ZZULIOJ 1011-1015
Hackingtool of security tools
电子商务时代,企业社交电商转型要做什么?
C language start
UNI-APP_小程序或h5页面背景音乐的播放与暂停
C language from entry to soil (II)