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
		 }
	 }
 }
相关推荐
KhalilRuan3 小时前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it7 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
10 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生10 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎
程序猿多布11 天前
Unity Addressable使用之检测更新流程
unity·addressable
Bunny Chen11 天前
Unity中的物理单位是真实的吗?
unity·游戏引擎
benben04412 天前
Unity3D仿星露谷物语开发69之动作声音
游戏·ui·unity·c#·游戏引擎
徐子竣12 天前
Unity Shader开发-着色器变体(1)-着色器变体概述
unity·游戏引擎·着色器
向宇it13 天前
【unity游戏开发——热更新】什么是Unity热更新
游戏·unity·编辑器·游戏引擎