初始化
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// DeformationCS.usf
|
||||
#include "/Engine/Public/Platform.ush"
|
||||
// 输入输出定义
|
||||
Texture2D<float4> InputTexture;
|
||||
RWTexture2D<float4> OutputTexture;
|
||||
// 参数缓冲区
|
||||
cbuffer Parameters
|
||||
{
|
||||
float3 ImpactPoint;
|
||||
float ImpactForce;
|
||||
float3 ImpactNormal;
|
||||
float DeformationRadius;
|
||||
float Plasticity;
|
||||
int3 TextureSize;
|
||||
int3 UpdateRegionMin;
|
||||
int3 UpdateRegionSize;
|
||||
};
|
||||
// 线程组大小
|
||||
#define THREADGROUP_SIZE_X 8
|
||||
#define THREADGROUP_SIZE_Y 8
|
||||
[numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y, 1)]
|
||||
void MainCS(
|
||||
uint3 GroupID : SV_GroupID,
|
||||
uint3 GroupThreadID : SV_GroupThreadID,
|
||||
uint3 DispatchThreadID : SV_DispatchThreadID
|
||||
)
|
||||
{
|
||||
// 计算全局像素坐标
|
||||
int2 PixelCoord = DispatchThreadID.xy + int2(UpdateRegionMin.xy);
|
||||
|
||||
// 检查是否在有效区域内
|
||||
if (PixelCoord.x >= UpdateRegionMin.x + UpdateRegionSize.x ||
|
||||
PixelCoord.y >= UpdateRegionMin.y + UpdateRegionSize.y)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 从输入纹理读取位置
|
||||
float4 OriginalPos = InputTexture.Load(int3(PixelCoord, 0));
|
||||
|
||||
// 计算到碰撞点的距离
|
||||
float dist = distance(OriginalPos.xyz, ImpactPoint);
|
||||
|
||||
// 在变形半径内处理
|
||||
if (dist < DeformationRadius)
|
||||
{
|
||||
// 计算变形衰减 (高斯衰减)
|
||||
float falloff = exp(-(dist * dist) / (2.0 * DeformationRadius * DeformationRadius));
|
||||
|
||||
// 计算变形量 (考虑法线方向)
|
||||
float3 deformation = ImpactNormal * ImpactForce * falloff * 0.01;
|
||||
|
||||
// 应用塑性变形
|
||||
float3 newPos = OriginalPos.xyz + deformation * Plasticity;
|
||||
|
||||
// 写入新位置
|
||||
OutputTexture[PixelCoord] = float4(newPos, OriginalPos.w);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 保持原位置
|
||||
OutputTexture[PixelCoord] = OriginalPos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user