在游戏中,有很多用到贴画的地方。比如:地面污渍、地面喷漆、地面血迹、魔法阵、地裂等效果。
我们在这篇文章中,来用深度图实现一下贴画的效果。
TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);
float4 depthVS = 1;
float2 screenUV = i.positionCS.xy / _ScreenParams.xy;
half depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,screenUV);
half depthZ = LinearEyeDepth(depthTex,_ZBufferParams);
depthVS.z = depthZ;
float3 positionVS : TEXCOORD3;
o.positionVS = TransformWorldToView(TransformObjectToWorld(o.positionOS));
我们先在
X
o
Z
XoZ
XoZ 平面下求出我们的
W
x
W_x
Wx? 值
P
z
W
z
=
P
x
W
x
\frac{P_z}{W_z}=\frac{P_x}{W_x}
Wz?Pz??=Wx?Px??
W
x
=
P
x
W
z
P
z
W_x = \frac{P_xW_z}{P_z}
Wx?=Pz?Px?Wz??
P
z
P_z
Pz? 为负数,
W
z
W_z
Wz?为正数
我们需要的比值只是一个长度关系。所以,需要乘以一个
?
1
-1
?1
W
x
=
P
x
W
z
?
P
z
W_x = \frac{P_xW_z}{-P_z}
Wx?=?Pz?Px?Wz??
然后,在
Y
o
Z
YoZ
YoZ 平面下求出我们的
W
y
W_y
Wy? 值
W
y
=
P
y
W
z
?
P
z
W_y = \frac{P_yW_z}{-P_z}
Wy?=?Pz?Py?Wz??
depthVS.xy = i.positionVS.xy*depthZ/-i.positionVS.z;
float4 depthWS = mul(unity_CameraToWorld,depthVS);
float4 depthOS = mul(unity_WorldToObject,depthWS);
float2 uv = depthOS.xy+0.5;
half4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,uv);
//深度贴花
Shader "MyShader/URP/P4_4_2"
{
Properties
{
[Header(MainTex)]
_MainTex("MainTex",2D) = "white"{}
}
SubShader
{
Tags
{
//告诉引擎,该Shader只用于 URP 渲染管线
"RenderPipeline"="UniversalPipeline"
//渲染类型
"RenderType"="Transparent"
//渲染队列
"Queue"="Transparent"
}
Pass
{
Blend One One
ZWrite Off
Name "Unlit"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
// Pragmas
#pragma target 2.0
// Includes
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_CameraDepthTexture);
SAMPLER(sampler_CameraDepthTexture);
//struct appdata
//顶点着色器的输入
struct Attributes
{
float3 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
//struct v2f
//片元着色器的输入
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float fogCoord : TEXCOORD1;
float3 positionOS : TEXCOORD2;
float3 positionVS : TEXCOORD3;
};
//v2f vert(Attributes v)
//顶点着色器
Varyings vert(Attributes v)
{
Varyings o = (Varyings)0;
o.positionCS = TransformObjectToHClip(v.positionOS);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.fogCoord = ComputeFogFactor(o.positionCS.z);
o.positionOS = v.positionOS;
//2、通过模型面片的求出像素在观察空间下的坐标值
o.positionVS = TransformWorldToView(TransformObjectToWorld(o.positionOS));
return o;
}
//fixed4 frag(v2f i) : SV_TARGET
//片元着色器
half4 frag(Varyings i) : SV_TARGET
{
//思路:
float4 depthVS = 1;
//1、通过深度图求出像素所在视图空间的Z值
float2 screenUV = i.positionCS.xy / _ScreenParams.xy;
half4 depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV);
half depthZ = LinearEyeDepth(depthTex.r, _ZBufferParams);
//2、通过模型面片的求出像素在观察空间下的坐标值
//这个在顶点着色器中完成
//3、结合两者求出 深度图中像素的 XYZ值
depthVS.z = depthZ;
depthVS.xy = i.positionVS.xy * depthZ / -i.positionVS.z;
//4、再将此坐标转换到模型的本地空间,把XY作为UV来进行纹理采样
float4 depthWS = mul(unity_CameraToWorld, depthVS);
float4 depthOS = mul(unity_WorldToObject, depthWS);
float2 uv = depthOS.xy + 0.5;
half4 col = 0;
half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
col += mainTex;
return col;
}
ENDHLSL
}
}
}