当前位置:网站首页>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
边栏推荐
- Redis 持久化
- 上传excel文件
- 【PTA】团体程序设计天梯赛-练习集 L3题目总结(不全)
- 【LeetCode】11. 盛最多水的容器 - Go 语言题解
- Processing tree structured data
- Part II - C language improvement_ 4. Secondary pointer
- Accumulation of project problems
- Input the names of 10 people and output them in descending order
- Vs debugging
- What kind of mode can make platform users self-help fission- Chain 2+1
猜你喜欢
随机推荐
Prompt for garbled code when CHM file is opened
ROS starts non native nodes
文件上传下载Demo
第二部分—C语言提高篇_1. C语言概述
Upload pictures Base64
In the era of e-commerce, what should enterprises do in the transformation of social e-commerce?
Part II - C language improvement_ 1. Overview of C language
Input the names of 10 people and output them in descending order
在线问题反馈模块实战(十二):实现图片删除功能
Chinese manufacturers may catch up with the humanoid robot Optimus "Optimus Prime", which has been highly hyped by musk
Use of redis
(笔记整理未完成)【图论:求单源最短路径】
Accumulation of project problems
别太在意别人的眼光,那会抹杀你的光彩
You don't have to waste your life on others' standards
C语言中extern,static, register,volatile 关键字的作用;保姆级教学!
【LeetCode】11. 盛最多水的容器 - Go 语言题解
You can't satisfy everyone!
17. What is the situation of using ArrayList or LinkedList?
Libevent and multithreading









