调通usf调用,将usf输出添加到材质。但是现在usf正式算法的输出值无法产生形变

This commit is contained in:
2025-11-12 00:17:23 +08:00
parent f12fef4edd
commit b027ec6487
12 changed files with 275 additions and 16 deletions
@@ -0,0 +1,56 @@
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class MiceShaders : ModuleRules
{
public MiceShaders(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
"MiceShaders/Public"
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
"MiceShaders/Private"
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
// ... add other public dependencies that you statically link with here ...
"RenderCore",
"RHI",
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Projects"
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}
@@ -0,0 +1,34 @@
#include "FDeformComputeShader.h"
#include "GlobalShader.h"
#include "ShaderParameterStruct.h"
#include "RHIStaticStates.h"
#include "Shader.h"
#include "GlobalShader.h"
#include "RHICommandList.h"
#include "RHIResources.h"
#include "ShaderParameterUtils.h"
// 全局参数存储
FThreadSafeDeformationParams GDeformationParams;
// 实现Compute Shader
IMPLEMENT_GLOBAL_SHADER(FDeformComputeShader, "/MiceShaders/Shaders/Private/DeformationCS.usf", "MainCompute", SF_Compute);
// 线程安全参数管理
void FThreadSafeDeformationParams::AddParams(const FDeformationCSDispatchParams& NewParams)
{
FScopeLock Lock(&ParamsCriticalSection);
PendingParams.Add(NewParams);
}
bool FThreadSafeDeformationParams::ConsumeParams(FDeformationCSDispatchParams& OutParams)
{
FScopeLock Lock(&ParamsCriticalSection);
if (PendingParams.Num() > 0)
{
OutParams = PendingParams[0];
PendingParams.RemoveAt(0);
return true;
}
return false;
}
@@ -0,0 +1,24 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "MiceShaders.h"
#include "Interfaces/IPluginManager.h"
#define LOCTEXT_NAMESPACE "FMiceShadersModule"
void FMiceShadersModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
FString PluginShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("MiceShaders"))->GetBaseDir(), TEXT("Shaders"));
AddShaderSourceDirectoryMapping(TEXT("/MiceShaders/Shaders"), PluginShaderDir);
}
void FMiceShadersModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FMiceShadersModule, MiceShaders)
@@ -0,0 +1,103 @@
#pragma once
#include "CoreMinimal.h"
#include "GlobalShader.h"
#include "ShaderParameterStruct.h"
#include "RenderGraphUtils.h" // 必须包含
// Compute Shader参数结构体
struct MICESHADERS_API FDeformationCSDispatchParams
{
// 碰撞参数
FVector ImpactPoint;
FVector ImpactNormal;
float ImpactForce;
float DeformationRadius;
float Plasticity;
// 纹理资源
FTextureRHIRef InputTexture;
FUnorderedAccessViewRHIRef OutputTexture;
// 更新区域 (优化部分更新)
FIntRect UpdateRegion;
// 纹理尺寸
FIntVector TextureSize;
FDeformationCSDispatchParams() : ImpactPoint(0,0,0),
ImpactNormal(0,0,0),
ImpactForce(0.0f),
DeformationRadius(10.0f),
Plasticity(0.7f),
TextureSize(FIntVector::ZeroValue)
{
}
};
// 支持多实例的线程安全参数
struct FThreadSafeDeformationParams
{
FCriticalSection ParamsCriticalSection;
TArray<FDeformationCSDispatchParams> PendingParams;
void AddParams(const FDeformationCSDispatchParams& NewParams);
bool ConsumeParams(FDeformationCSDispatchParams& OutParams);
};
// 全局参数存储
extern FThreadSafeDeformationParams GDeformationParams;
// DeformationCS.h (续)
// Compute Shader类
class MICESHADERS_API FDeformComputeShader : public FGlobalShader
{
DECLARE_GLOBAL_SHADER(FDeformComputeShader);
SHADER_USE_PARAMETER_STRUCT(FDeformComputeShader, FGlobalShader);
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
// 碰撞参数
SHADER_PARAMETER(FVector3f, ImpactPoint)
SHADER_PARAMETER(float, ImpactForce)
SHADER_PARAMETER(FVector3f, ImpactNormal)
SHADER_PARAMETER(float, DeformationRadius)
SHADER_PARAMETER(float, Plasticity)
// 更新区域
SHADER_PARAMETER(FIntVector, TextureSize)
SHADER_PARAMETER(FIntVector4, UpdateRegionParam)
// 纹理参数
SHADER_PARAMETER_RDG_TEXTURE_SRV(Texture2D, InputTexture)
SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D<float4>, OutputTexture)
END_SHADER_PARAMETER_STRUCT()
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)
{
return GetMaxSupportedFeatureLevel(Parameters.Platform) >= ERHIFeatureLevel::SM5;
}
// // 2. ✨ 修复 Precache 错误的关键:实现 ShouldPrecachePermutation
// // 我们告诉引擎:在开发模式下,请始终预缓存这个着色器。
// static EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FGlobalShaderPermutationParameters& Parameters)
// {
// // 如果你的着色器没有使用任何模板或标志,只需返回 E_DEVELOPMENT 或 E_ALL。
// return EShaderPermutationPrecacheRequest::Precached;
// }
static bool ShouldCache(EShaderPlatform Platform)
{
return true;
}
// (假设你的 .usf 文件中线程组大小为 8x8x1)
static FIntVector GetGroupSize()
{
return FIntVector(8, 8, 1);
}
static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment)
{
FGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment);
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZE_X"), 8);
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZE_Y"), 8);
}
};
@@ -0,0 +1,14 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Modules/ModuleManager.h"
class FMiceShadersModule : public IModuleInterface
{
public:
/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};