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

unity摄像机深度图使用[通俗易懂]https://www.jianshu.com/p/80a932d1f11ehttps://www.jianshu.com/p/178f3a065187https://www.cnblogs.com/czaoth/p/5830735.htmlhttps://www.cnblogs.com/jackmaxwell/p/7117909.htmlhttps://docs.unity3d.com/…

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

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#脚本:

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代码:

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
原型如下:

#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原型:

// Z buffer to linear 0..1 depth
inline float Linear01Depth( float z )
{ 
   
    return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
}

这地方的推导有点问题:

// 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原型:

// 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.net/129926.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 动态代理

    动态代理

    2021年7月20日
    54
  • 安卓连接Mqtt服务器「建议收藏」

    安卓连接Mqtt服务器「建议收藏」学习目标:实现app与mqtt服务器连接学习内容1、下载导入jar包2、调用mqtt核心代码handler=newHandler(){@SuppressLint(“SetTextI18n”)publicvoidhandleMessage(Messagemsg){super.handleMessage(msg);switch(msg.what){

    2022年5月22日
    36
  • AP模式和Router模式区别是什么

    AP模式和Router模式区别是什么AP模式和Router模式有什么区别呢?在TP-Link、水星(Mercury)、迅捷(Fast)等品牌的迷你无线路由器上,有AP模式、Router模式、中继和桥接等几个上网模式,本文重点详细介绍AP模式与Router模式的区别。AP(接入点)模式在AP模式下,需要把迷你无线路由器接入到已经可以上网的路由器或者交换机上面,然后迷你无线路由器可以提供无线WiFi,一般的迷你无线路由器出厂

    2022年10月24日
    0
  • 匈牙利算法

    匈牙利算法

    2021年12月15日
    49
  • db4o数据库文件_繁忙的Java开发人员指南db4o,使用db4o进行数据库重构

    db4o数据库文件_繁忙的Java开发人员指南db4o,使用db4o进行数据库重构存档日期:2019年5月13日|首次发布:2007年5月22日重构Java™代码比重构关系数据库要简单得多,但是幸运的是,对象数据库的情况并非如此。在《繁忙的Java开发人员db4o指南》的这一期中,TedNeward向您介绍了他最喜欢的对象数据库的另一个优点:db4o将重构简化到几乎没有用处。此内容不再被更新或维护。全文以PDF格式“按原样”提供。随着技术的飞速发展,…

    2022年7月21日
    10
  • 全局平均池化(global-average-pooling)

    全局平均池化在很多视觉任务中会用到。之前对darknet-53结构分析的时候,特别留意了一下全局平局池化。其实,这个操作就是它的字面意思:把特征图全局平均一下输出一个值,也就是把W*H*D的一个张量变成1*1*D的张量。下列引用来自stackoverflow:WithGlobalpoolingreducesthedimensionalityfrom3Dto1D.The…

    2022年4月9日
    170

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号