当前位置:网站首页>Unity中使用深度和法线纹理
Unity中使用深度和法线纹理
2022-07-24 06:39:00 【whr12】
1、开启深度和法线纹理
通过设置Camera中的depthTextureMode变量。
camera.depthTextureMode = DepthTextureMode.None; //不生成深度纹理
camera.depthTextureMode = DepthTextureMode.Depth; //生成深度纹理
camera.depthTextureMode = DepthTextureMode.DepthNormals; //生成深度法线纹理
camera.depthTextureMode = DepthTextureMode.MotionVectors; //指定是否应渲染运动矢量
//可通过或指令同时指定生成多个纹理
camera.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.DepthNormals;
2、访问深度和法线纹理
在shader中,使用变量名_CameraDepthTexture和_CameraDepthNormalsTexture访问对应纹理。
sampler2D _CameraDepthTexture; //深度纹理,深度值存储在r通道,范围[0,1]。通过MVP矩阵变化后的z值得到
sampler2D _CameraDepthNormalsTexture; //深度法线纹理
深度纹理的线性化
对于透视投影,存储的深度值是非线性的,要得到线性的深度值,需要进行转化,可通过LinearEyeDepth和Linear01Depth完成这个过程
float depth = tex2D(_CameraDepthTexture, uv).r; //获取到存储的深度值
float depth_view = LinearEyeDepth(depth); //获取视角空间下的深度值,范围在近平面和远平面之间,[z_near, z_far]
float depth_01 = Linear01Depth(depth); //获取[0,1]范围内线性分布的深度值, 0处于摄像机位置,1处于远平面位置
深度法线纹理的使用
深度法线纹理是32bit编码(每个通道8bit),法线信息存储在R和G通道,深度信息存储在B和A通道。深度值是16bit的数据,分别存储在两个8bit通道内。
因此,要使用深度法线纹理,在采样后需要对其解码才能使用。可使用UnityCG.cginc中的DecodeDepthNormal进行解码。
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的最终结果为[0,1]范围的线性分布
float3 normal; //normal为屏幕空间下的法线方向
DecodeDepthNormal(samplerValue, depth, normal);
3、显示深度值和法线值
深度纹理显示:
float4 samplerValue = tex2D(_CameraDepthNormalsTexture, i.uv);
float depth; //depth的最终结果为[0,1]范围的线性分布
float3 normal; //normal为屏幕空间下的法线方向
DecodeDepthNormal(samplerValue, depth, normal);
return fixed4(depth, depth, depth,1);
![[0,1]范围的深度值](/img/ab/6831eb4edcc0c3f15b6026f7e4c766.png)
法线纹理显示:
float4 samplerValue = tex2D(_CameraDepthNormalsTexture, i.uv);
float depth; //depth的最终结果为[0,1]范围的线性分布
float3 normal; //normal为屏幕空间下的法线方向
DecodeDepthNormal(samplerValue, depth, normal);
return fixed4(normal*0.5+0.5,1);
![映射到[0,1]范围内的视角空间下的法线纹理](/img/3d/0c5ca44272305ac8bf58f84df282d1.png)
4、参考资料
[1] 冯乐乐 《Unity Shader入门精要》页269-272
[2] Unity手册 https://docs.unity3d.com/2020.2/Documentation/Manual/SL-CameraDepthTexture.html
边栏推荐
- C language from entry to soil (I)
- 第二部分—C语言提高篇_4. 二级指针
- 找工作备忘
- 【方向盘】超爱的IDEA提效神器Save Actions,卸载了
- After grouping, return to the last record group in each group_ Use of concat
- Libevent and multithreading
- "Big factory interview" JVM Chapter 21 questions and answers
- The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
- Vs debugging
- Redis 持久化
猜你喜欢

安全工具之hackingtool

C language from entry to soil (III)

C language from entry to soil (II)

C language from entry to soil (I)

C语言中extern,static, register,volatile 关键字的作用;保姆级教学!

Can recursion still play like this? Recursive implementation of minesweeping game

上传excel文件

周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录
![[C language] operator details (in-depth understanding + sorting and classification)](/img/0a/e726126f6a55590d5ae73c49757cdb.png)
[C language] operator details (in-depth understanding + sorting and classification)

PyTorch 深度学习实践 第10讲/作业(Basic CNN)
随机推荐
一个怎样的模式能让平台用户发生自助裂变?-链动2+1
Basic syntax of MySQL DDL and DML and DQL
记账APP:小哈记账1——欢迎页的制作
电子商务时代,企业社交电商转型要做什么?
Don't care too much about others' eyes, it will erase your glory
Redis 主从机制
Design a function print to print the string. If only the string parameter s is passed, the string length is compared with 10, greater than 10, print the first 10 characters, less than 10, and output a
【时序逻辑电路】——寄存器
Mqtt learning
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
Input some data and find the maximum output. (keyboard and file reading)
Libevent and multithreading
django.db.utils.OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
Upload excel file
Use dichotomy to find specific values from the array
[wechat applet] understand conditional rendering, list rendering and wxss template style
Input the names of 10 people and output them in descending order
Part II - C language improvement_ 4. Secondary pointer
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
第二部分—C语言提高篇_1. C语言概述