在之前的文章中,我们实现了URP下的深度图使用。
在这篇文章中,我们来看一下BRP下深度图怎么单独使用。
sampler2D _CameraDepthTexture;
float2 uv = i.positionCS/ _ScreenParams.xy;
float4 cameraDepthTex = tex2D(_CameraDepthTexture,uv);
float depthTex = LinearEyeDepth(cameraDepthTex);
return frac(depthTex);
Camera.main.depthTextureMode = DepthTextureMode.Depth;
Shader "MyShader/URP/P4_1"
{
Properties
{
_Color("Color",Color) = (0,0,0,0)
_MainTex("MainTex",2D) = "white"{}
}
SubShader
{
Tags
{
//渲染类型
"RenderType"="Transparent"
//渲染队列
"Queue"="Transparent"
}
//Blend One One
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Pragmas
#pragma target 2.0
// Includes
#include "UnityCG.cginc"
half4 _Color;
sampler2D _MainTex;float4 _MainTex_ST;
sampler2D _CameraDepthTexture;
struct appdata
{
float3 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.positionCS = UnityObjectToClipPos(v.positionOS);
o.uv = TRANSFORM_TEX(v.uv,_MainTex);
o.screenPos = ComputeScreenPos(o.positionCS);
return o;
}
//fixed4 frag(v2f i) : SV_TARGET
//片元着色器
half4 frag(v2f i) : SV_TARGET
{
half4 c;
float4 mainTex = tex2D(_MainTex,i.uv);
//c = _Color * mainTex;
//深度图
//float2 uv = i.screenPos.xy / i.screenPos.w;
float2 uv = i.positionCS/ _ScreenParams.xy;
float4 cameraDepthTex = tex2D(_CameraDepthTexture,uv);
float depthTex = LinearEyeDepth(cameraDepthTex);
return frac(depthTex);
}
ENDCG
}
}
}