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
		 }
	 }
 }
相关推荐
AT~2 小时前
unity 使用Socket和protobuf实现网络连接
unity·游戏引擎
怣疯knight9 小时前
Cocos creator判断节点是否能用的方法
unity·cocos2d
tealcwu9 小时前
Google Play的Keystore不可用时的解决方法
unity
呼呼突突9 小时前
Unity使用TouchSocket的RPC
unity·rpc·游戏引擎
qq 180809511 天前
从零构建一个多目标多传感器融合跟踪器
unity
平行云1 天前
实时云渲染支持在网页上运行UE5开发的3A大作Lyra项目
unity·云原生·ue5·webgl·虚拟现实·实时云渲染·像素流送
鹏飞于天1 天前
Shader compiler initialization error: Failed to read D3DCompiler DLL file
unity
wonder135791 天前
UGUI重建流程和优化
unity·游戏开发·ugui
HONT1 天前
Unity Crest Ocean System源码阅读
shader
那个村的李富贵1 天前
Unity打包Webgl后 本地运行测试
unity·webgl