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
		 }
	 }
 }
相关推荐
★YUI★18 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
不绝1911 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
玩代码1 天前
Unity里的加力
开发语言·unity
贵州晓智信息科技1 天前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
UWA1 天前
UWA DAY 2025 游戏开发者大会|全议程
游戏·unity·性能优化·游戏开发·uwa·unreal engine
unicrom_深圳市由你创科技1 天前
Unity 的UI动画调节
ui·unity·游戏引擎
咩咩觉主1 天前
Unity编辑器拓展 IMGUI与部分Utility知识总结(代码+思维导图)
unity·c#·编辑器·游戏引擎
龚子亦2 天前
【Unity开发】数据存储——XML
xml·unity·游戏引擎·数据存储·游戏开发
write_the_code2 天前
Unity国际版下载链接分享(非c1国内版)
unity·游戏引擎
Bulling_2 天前
unity动态背景制作
unity·游戏引擎