URP - 深度图

在unity中开启深度图,这样才能在Shader中引用_CameraDepthTexture

如何查看画面中的深度信息?

半透明物体不会渲染深度信息,数值小于2500的为不透明渲染队列,数值大于2500为半透明渲染队列

所以想要一个物体的深度信息不出现在深度图中,就可以将该物体的Shader的渲染队列改为 "Queue" = "Transparent"


如何在Shader中应用深度图?

在Pass中声明深度图:

TEXTURE2D(_CameraDepthTexture);

SAMPLER(sampler_CameraDepthTexture);

使用面片模型在屏幕空间中的坐标来采样深度图(因为深度图生成是基于屏幕)

//i.positionCS为屏幕映射后的值(也就是屏幕空间下的坐标),_ScreenParams.xy分别代表屏幕的宽高

//i.positionCS/_ScreenParams.xy的作用就是将模型的坐标转换到屏幕坐标系下并映射到0-1的范围内
float2 uv = i.positionCS/_ScreenParams.xy;
//此时uv代表模型在屏幕空间下的坐标值,用此值来采样深度图(此时为非线性的深度图)
float4 depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,uv);
//因为深度是一个非线性的值域,所以需要将此深度图转换到一个线性的空间(并将值映射到0-1的范围内)
float depth = Linear01Depth(depthTex,_ZBufferParams);

复制代码
Shader"unity/DepthShader"
{
	Properties
	{
		_MainTex("MainTex",2D)="white"{}
	 }

	SubShader
	{

		Tags
		{
			"RenderPipeline"="UniversialPipeline"

			"RenderType"="Transparent"

			"Queue"="Transparent"
		}

		ZWrite Off

		Pass
		{
			HLSLPROGRAM

			#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
			#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"

			#pragma vertex vert
			#pragma fragment frag

			TEXTURE2D(_MainTex);
			#define sampler_MainTex samplerState_Linear_Repeat
			SAMPLER(sampler_MainTex);
			TEXTURE2D(_CameraDepthTexture);
			SAMPLER(sampler_CameraDepthTexture);

			struct Attributes
			{
				float4 positionOS : POSITION;
				float2 uv : TEXCOORD;
			 };

			struct Varyings
			{
				float4 positionCS : SV_POSITION;
				float2 uv : TEXCOORD;
				float4 screenPos : TEXCOORD1;
			 };

			Varyings vert(Attributes v)
			{
				Varyings o = (Varyings)0;

				o.positionCS = TransformObjectToHClip(v.positionOS);

				o.uv = v.uv;

				// o.screenPos = ComputeScreenPos(o.positionCS);

				return o;
			 }

			float4 frag(Varyings i):SV_Target
			{
				// float2 uv = i.screenPos.xy/i.screenPos.w;

				//i.positionCS为屏幕映射后的值(也就是屏幕空间下的坐标),_ScreenParams.xy分别代表屏幕的宽高
				//i.positionCS/_ScreenParams.xy的作用就是将模型的坐标转换到屏幕坐标系下并映射到0-1的范围内
				float2 uv = i.positionCS/_ScreenParams.xy;

				float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv);

				//此时uv代表模型在屏幕空间下的坐标值,用此值来采样深度图(此时为非线性的深度图)
				float4 depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,uv);

				//因为深度是一个非线性的值域,所以需要将此深度图转换到一个线性的空间(并将值映射到0-1的范围内)
				float depth = Linear01Depth(depthTex,_ZBufferParams);

				return depth;
			 }

			ENDHLSL
		 }
	 }
 }
相关推荐
FAREWELL0007518 小时前
Unity基础学习(十二)Unity 物理系统之范围检测
学习·unity·c#·游戏引擎·范围检测
NRatel1 天前
Unity 中实现首尾无限循环的 ListView
unity·listview·无限循环·首尾循环
两水先木示1 天前
【Unity】模型渐变技术 BlendShapes变形
unity·游戏引擎
|Ringleader|1 天前
【Unity博客节选】Playable Graph Monitor 安装使用
unity·timeline·playable·graph monitor·visualizer
向宇it2 天前
【unity游戏开发——编辑器扩展】EditorApplication公共类处理编辑器生命周期事件、播放模式控制以及各种编辑器状态查询
开发语言·ui·unity·编辑器·游戏引擎
向宇it2 天前
【unity游戏开发——编辑器扩展】AssetDatabase公共类在编辑器环境中管理和操作项目中的资源
游戏·ui·unity·编辑器·游戏引擎
benben0442 天前
Unity3D仿星露谷物语开发55之保存游戏到文件
游戏·ui·unity·游戏引擎
子燕若水2 天前
Unity 快捷键
unity·游戏引擎
下次见咯!2 天前
Unity-QFramework框架学习-MVC、Command、Event、Utility、System、BindableProperty
unity·mvc·框架·qframework
Tech Synapse2 天前
开发AR导航助手:ARKit+Unity+Mapbox全流程实战教程
unity·游戏引擎·ar