前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >unity摄像机深度图使用[通俗易懂]

unity摄像机深度图使用[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-10 10:56:25
发布2022-08-10 10:56:25
1.7K00
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

https://www.jianshu.com/p/80a932d1f11e https://www.jianshu.com/p/178f3a065187 https://www.cnblogs.com/czaoth/p/5830735.html https://www.cnblogs.com/jackmaxwell/p/7117909.html https://docs.unity3d.com/540/Documentation/Manual/SL-Pass.html http://www.lsngo.net/2018/01/20/unity_depthtextureprojector/ https://blog.csdn.net/puppet_master/article/details/77489948

本篇博客主要是解决,深度图的原理与例子实现问题。 下面我们直接用unity的脚本和shader,介绍如何使用unity给我们提供的深度图。 C#脚本:

代码语言:javascript
代码运行次数:0
运行
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class DepthTextureTest : MonoBehaviour
{ 
   
    private Material postEffectMat = null;
    private Camera currentCamera = null;

    void Awake()
    { 
   
        currentCamera = GetComponent<Camera>();
    }

    void OnEnable()
    { 
   
        if (postEffectMat == null)
            postEffectMat = new Material(Shader.Find("DepthTexture/DepthTextureTest"));
        currentCamera.depthTextureMode |= DepthTextureMode.Depth;
    }

    void OnDisable()
    { 
   
        currentCamera.depthTextureMode &= ~DepthTextureMode.Depth;
    }

    void OnRenderImage(RenderTexture source, RenderTexture destination)
    { 
   
        if (postEffectMat == null)
        { 
   
            Graphics.Blit(source, destination);
        }
        else
        { 
   
            Graphics.Blit(source, destination, postEffectMat);
        }
    }
}

shader代码:

代码语言:javascript
代码运行次数:0
运行
复制
Shader "DepthTexture/DepthTextureTest"
{ 
   
	CGINCLUDE
		#include "UnityCG.cginc"
		sampler2D _CameraDepthTexture;

		fixed4 frag_depth(v2f_img i) : SV_Target
		{ 
   
			float depthTextureValue = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
			//float linear01EyeDepth = LinearEyeDepth(depthTextureValue) * _ProjectionParams.w;
			float linear01EyeDepth = Linear01Depth(depthTextureValue);
			return fixed4(linear01EyeDepth, linear01EyeDepth, linear01EyeDepth, 1.0);
		}
	ENDCG

	SubShader
	{ 
   
		Pass
		{ 
   
			ZTest Off
			Cull Off
			ZWrite Off
			Fog{ 
    Mode Off }

			CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag_depth
			ENDCG
		}
	}
}

最终结果:

上面用到了这个宏:SAMPLE_DEPTH_TEXTURE 原型如下:

代码语言:javascript
代码运行次数:0
运行
复制
#define SAMPLE_DEPTH_TEXTURE(sampler, uv) (tex2D(sampler, uv).r)

这句代码也可以写为: tex2D(_CameraDepthTexture, i.uv).r;

从视空间深度转化到屏幕空间深度的公式如下: a = F/(F – N) b = NF/(N – F) 最终depth(屏幕空间)=(aZ + b)/Z (Z为视空间深度)

我们在屏幕空间使用屏幕空间的坐标进行插值,得到逐像素的坐标。 屏幕空间的深度是和1/z成正比的。 那么经过透视变换、透视投影之后,得到的是屏幕空间的深度值,而我们需要使用视空间下的z才好计算,所以需要反推得到视空间下的深度z。

Linear01Depth原型:

代码语言:javascript
代码运行次数:0
运行
复制
// Z buffer to linear 0..1 depth
inline float Linear01Depth( float z )
{ 
   
    return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
}

这地方的推导有点问题:

代码语言:javascript
代码运行次数:0
运行
复制
// Values used to linearize the Z buffer (http://www.humus.name/temp/Linearize%20depth.txt)
// x = 1-far/near
// y = far/near
// z = x/far
// w = y/far
// or in case of a reversed depth buffer (UNITY_REVERSED_Z is 1)
// x = -1+far/near
// y = 1
// z = x/far
// w = 1/far
float4 _ZBufferParams;

LinearEyeDepth原型:

代码语言:javascript
代码运行次数:0
运行
复制
// Z buffer to linear depth
inline float LinearEyeDepth( float z )
{ 
   
    return 1.0 / (_ZBufferParams.z * z + _ZBufferParams.w);
}

z&1/z 通过上面的深度图具体的使用,我们发现,实际上真正使用的深度,是从顶点的视空间在,经过投影变成一个1/Z成正比的值(屏幕空间Depth),然后在使用时,再通过投影变换时的计算公式反推回对应视空间像素的位置Z。 https://developer.nvidia.com/content/depth-precision-visualized

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129926.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档