IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> UE4 Decal实现简介 -> 正文阅读

[游戏开发]UE4 Decal实现简介

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() * DecalSize);

		return Ret;
	}

Pixel 处理过程

先获取屏幕UV

float2 ScreenUV = SvPositionToBufferUV(In.SvPosition);

SvPosition 是从Vertex Shader里输出,插值后 再传入Pixel Shader,

void MainPS
    (
#if PIXELSHADEROUTPUT_INTERPOLANTS || PIXELSHADEROUTPUT_BASEPASS
        FVertexFactoryInterpolantsVSToPS Interpolants,
#endif
#if PIXELSHADEROUTPUT_BASEPASS
        FBasePassInterpolantsVSToPS BasePassInterpolants,
#elif PIXELSHADEROUTPUT_MESHDECALPASS
        FMeshDecalInterpolants MeshDecalInterpolants,
#endif

        in INPUT_POSITION_QUALIFIERS float4 SvPosition : SV_Position        // after all interpolators

SvPositionToBufferUV函数是将SvPosition变换到屏幕空间UV坐标。

float2 SvPositionToBufferUV(float4 SvPosition)
{
    return SvPosition.xy * View.BufferSizeAndInvSize.zw;
}

注意SvPosition.xy是FrameBuffer像素坐标xy

获取设备Z(深度缓存里Depth值),这样可以还原那个像素点的WorldPosition。

// make SvPosition appear to be rasterized with the depth from the depth buffer
    In.SvPosition.z = LookupDeviceZ(ScreenUV);

转换到Decal Box局部坐标系

float4 DecalVectorHom = mul(float4(In.SvPosition.xyz,1), SvPositionToDecal);
        OSPosition = DecalVectorHom.xyz / DecalVectorHom.w;

        // clip content outside the decal
        // not needed if we stencil out the decal but we do that only on large (screen space) ones
        clip(OSPosition.xyz + 1.0f);
        clip(1.0f - OSPosition.xyz);

        // todo: TranslatedWorld would be better for quality
        WSPosition = SvPositionToWorld(In.SvPosition);

有两个clip,裁剪掉DecalBox以外的东西(不是Decal自己的像素点,前面有个步骤还原获取了原来Scene里的DeviceZ(In.SvPosition.z = LookupDeviceZ(ScreenUV)))
如下图所示

在这里插入图片描述

红色是X轴,绿色是Y轴,蓝色是Z轴。中心点坐标是(0, 0, 0)。

			FVector2D InvViewSize = FVector2D(1.0f / View.ViewRect.Width(), 1.0f / View.ViewRect.Height());

			// setup a matrix to transform float4(SvPosition.xyz,1) directly to Decal (quality, performance as we don't need to convert or use interpolator)

			//	new_xy = (xy - ViewRectMin.xy) * ViewSizeAndInvSize.zw * float2(2,-2) + float2(-1, 1);

			//  transformed into one MAD:  new_xy = xy * ViewSizeAndInvSize.zw * float2(2,-2)      +       (-ViewRectMin.xy) * ViewSizeAndInvSize.zw * float2(2,-2) + float2(-1, 1);

			float Mx = 2.0f * InvViewSize.X;
			float My = -2.0f * InvViewSize.Y;
			float Ax = -1.0f - 2.0f * View.ViewRect.Min.X * InvViewSize.X;
			float Ay = 1.0f + 2.0f * View.ViewRect.Min.Y * InvViewSize.Y;

			// todo: we could use InvTranslatedViewProjectionMatrix and TranslatedWorldToComponent for better quality
			const FMatrix SvPositionToDecalValue = 
				FMatrix(
					FPlane(Mx,  0,   0,  0),
					FPlane( 0, My,   0,  0),
					FPlane( 0,  0,   1,  0),
					FPlane(Ax, Ay,   0,  1)
				) * View.ViewMatrices.GetInvViewProjectionMatrix() * WorldToComponent;

先将Sv_Position(Scene的Sv_Position,不是Decal Box的)转换到Projection空间,再InvViewProjectionMatrix 转换到WorldSpace,最后WorldToComponent到Decal局部坐标系。

DecalVectorHom.xyz / DecalVectorHom.w 从其次坐标系,到正常的坐标空间。

DecalUV的计算

// can be optimized
    float3 DecalVector = OSPosition * 0.5f + 0.5f;

    // Swizzle so that DecalVector.xy are perpendicular to the projection direction and DecalVector.z is distance along the projection direction
    float3 SwizzlePos = DecalVector.zyx;

    // By default, map textures using the vectors perpendicular to the projection direction
    float2 DecalUVs = SwizzlePos.xy;

首先将-1 到 1范围的,转换到0到1。

在这里插入图片描述

最后是输出颜色和Alpha。

不接受Decal投射的物体处理

不接受Decal的物件,绘制的时候,写入0x80到Stencil Buffer里。

	if (bEnableReceiveDecalOutput)
	{
		const uint8 StencilValue = (PrimitiveSceneProxy && !PrimitiveSceneProxy->ReceivesDecals() ? 0x01 : 0x00);
		
		DrawRenderState.SetDepthStencilState(TStaticDepthStencilState<
				true, CF_DepthNearOrEqual,
				true, CF_Always, SO_Keep, SO_Keep, SO_Replace,
				false, CF_Always, SO_Keep, SO_Keep, SO_Keep,
				// decals atm are singe user of stencil in mobile base pass
				// don't use masking as it has significant performance hit on Mali GPUs (T860MP2)
				0x00, 0xff /*GET_STENCIL_BIT_MASK(RECEIVE_DECAL, 1)*/ >::GetRHI());

		DrawRenderState.SetStencilRef(GET_STENCIL_BIT_MASK(RECEIVE_DECAL, StencilValue)); // we hash the stencil group because we only have 6 bits.
	}
	else
	{
		// default depth state should be already set
	}

Decal在绘制的时候,设置Read Mask 0x80,和0比较相等,如果不等于就不进行绘制。

						GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<
							false, CF_DepthNearOrEqual,
							true, CF_Equal, SO_Keep, SO_Keep, SO_Keep,
							false, CF_Always, SO_Keep, SO_Keep, SO_Keep,
							GET_STENCIL_BIT_MASK(RECEIVE_DECAL, 1), 0x00>::GetRHI();
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-03 16:26:28  更:2022-01-03 16:26:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/24 22:34:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码