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
		 }
	 }
 }
相关推荐
雪下的新火10 小时前
Blender:法线图&黑白图
游戏·unity·游戏引擎·blender·笔记分享
HahaGiver66612 小时前
从0到1做一个“字母拼词”Unity小游戏(含源码/GIF)- 实现多单词顺序通关进度逻辑
unity·游戏引擎·游戏程序
Dr.勿忘17 小时前
Unity一分钟思路---UI任务条:宝箱位置如何准确卡在百分比位置上
ui·unity·游戏程序·屏幕适配
weixin_4242946717 小时前
Unity 实现 ScrollBar 值变化控制 Panel 位置的方法
unity·游戏引擎
在路上看风景1 天前
## 2.2 状态同步
unity
霜绛1 天前
Unity:lua热更新(一)——AB包AssetBundle、Lua语法
笔记·学习·游戏·unity·lua
霜绛1 天前
Unity:lua热更新(二)——Lua语法(续)
笔记·学习·unity·游戏引擎·lua
yi碗汤园1 天前
【一文了解】C#反射
开发语言·unity·c#
HahaGiver6661 天前
Unity Shader Graph 3D 实例 - 基础的模型贴图渲染
3d·unity·游戏程序·贴图·游戏美术
HahaGiver6661 天前
Unity Shader Graph 3D 实例 - 一个简单的3D打印效果
3d·unity·游戏引擎