Decal 是游戏中常见的一个东西,经常被用在 显示弹痕,地面叠加花纹等。
Decal 绘制流程
Decal可以认为是,将一个面的画面沿Decal的Box的X轴方向投影到物体表面。
Decal的绘制实际只有一个Box绘制。
RHICmdList.DrawIndexedPrimitive(GetUnitCubeIndexBuffer(), 0, 0, 8, 0, UE_ARRAY_COUNT(GCubeIndices) / 3, 1);
为什么不用一个平面来绘制呢?这样做主要是为了,覆盖不同视角,裁剪也变得简单。
Decal Pass是在Base Pass之后,Translucency Pass之前,Decal只能投射在非透明物体上。
Vertex过程
// from component to clip space (for decal frustum)
float4x4 FrustumComponentToClip;
// decal vertex shader
void MainVS(
in float4 InPosition : ATTRIBUTE0,
out float4 OutPosition : SV_POSITION
)
{
OutPosition = mul(InPosition, FrustumComponentToClip);
}
Vertex Shader只是乘以FrustumComponentToClip变换矩阵,变换到Clip空间。
FMatrix FDecalRendering::ComputeComponentToClipMatrix(const FViewInfo& View, const FMatrix& DecalComponentToWorld)
{
FMatrix ComponentToWorldMatrixTrans = DecalComponentToWorld.ConcatTranslation(View.ViewMatrices.GetPreViewTranslation());
return ComponentToWorldMatrixTrans * View.ViewMatrices.GetTranslatedViewProjectionMatrix();
}
FrustumComponentToClip实际就是WorldViewProjection 组合变换矩阵(包含了DecalSize)
FTransform GetTransformIncludingDecalSize() const
{
FTransform Ret = GetComponentToWorld();
Ret.SetScale3D(Ret.GetScale3D(