当前位置:网站首页>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
边栏推荐
- 记账APP:小哈记账1——欢迎页的制作
- [USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
- 17. 什么情况用ArrayList or LinkedList呢?
- 找工作备忘
- 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
- MongoDB应用场景及选型(海量数据存储选型)
- 第一部分—C语言基础篇_11. 综合项目-贪吃蛇
- 2D目标检测综述之学习策略篇(终章)
- 解压主播狂揽4000w+播放,快手美食赛道又添新风向?
- "Big factory interview" JVM Chapter 21 questions and answers
猜你喜欢

Redis master-slave mechanism

周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录

QoS服务质量三DiffServ模型报文的标记及PHB

Variables and data types (03)

C语言中extern,static, register,volatile 关键字的作用;保姆级教学!
![[waveform / signal generator] Based on stc1524k32s4 for C on Keil](/img/62/8bf96bf7617cc9b2682b5a32711878.png)
[waveform / signal generator] Based on stc1524k32s4 for C on Keil

在线问题反馈模块实战(十二):实现图片删除功能

Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary

Filter 过滤器

What kind of mode can make platform users self-help fission- Chain 2+1
随机推荐
Libc.so.6/glibc cross compilation
Redis 分片集群
Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary
你是谁由你自己决定!
C language from entry to soil (III)
Introduction to pyqt5 - student management system
Basic syntax of MySQL DDL and DML and DQL
C语言中extern,static, register,volatile 关键字的作用;保姆级教学!
处理树形结构数据
5. Template cache. Drawing a square can only move within the triangle
上传excel文件
C language from entry to soil function
上传图片base64
Neo4j modify tag name
MySql的DDL和DML和DQL的基本语法
C language to achieve three chess? Gobang? No, it's n-chess
C language from introduction to soil -- super detailed summary of operators
变量和数据类型(04)完结
Who you are is up to you!
Tensorflow Einstein function