diff --git a/111.txt b/111.txt new file mode 100644 index 0000000..ac2b893 --- /dev/null +++ b/111.txt @@ -0,0 +1,48 @@ +// ------------------------------------------------------------------------- + // 4. 坐标计算与边界检查 + // ------------------------------------------------------------------------- + + // DispatchThreadID 从 0 开始,代表在调度区域内的偏移量。 + // 我们将其加上 UpdateRegionParam.xy (Min.X, Min.Y) 得到全局像素坐标。 + int2 PixelCoord = DispatchThreadID.xy + int2(UpdateRegionParam.x, UpdateRegionParam.y); + + // 边界检查:检查是否超出 UpdateRegionParam.zw (Max.X, Max.Y) + if (PixelCoord.x < UpdateRegionParam.x || PixelCoord.x >= UpdateRegionParam.z || + PixelCoord.y < UpdateRegionParam.y || PixelCoord.y >= UpdateRegionParam.w) + { + // 虽然我们调度时已经只调度了需要的区域,但为了安全起见,保留检查。 + // 理论上,如果 C++ 端调度正确,这个检查应该很少触发。 + return; + } + + // ------------------------------------------------------------------------- + // 5. 变形计算逻辑 (与之前相同) + // ------------------------------------------------------------------------- + + // 从输入纹理读取位置 + // Load 需要 int3,第三个分量 (MipLevel) 设为 0 + 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; + } \ No newline at end of file diff --git a/BladeSmith.sln.DotSettings.user b/BladeSmith.sln.DotSettings.user index 571bc59..afc749d 100644 --- a/BladeSmith.sln.DotSettings.user +++ b/BladeSmith.sln.DotSettings.user @@ -2,6 +2,8 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded \ No newline at end of file diff --git a/Content/Materials/M_Iron.uasset b/Content/Materials/M_Iron.uasset index 1912c4e..07ff7ed 100644 Binary files a/Content/Materials/M_Iron.uasset and b/Content/Materials/M_Iron.uasset differ diff --git a/Plugins/MiceShaders/MiceShaders.uplugin b/Plugins/MiceShaders/MiceShaders.uplugin new file mode 100644 index 0000000..6b737d8 --- /dev/null +++ b/Plugins/MiceShaders/MiceShaders.uplugin @@ -0,0 +1,24 @@ +{ + "FileVersion": 3, + "Version": 1, + "VersionName": "1.0", + "FriendlyName": "MiceShaders", + "Description": "", + "Category": "Other", + "CreatedBy": "", + "CreatedByURL": "", + "DocsURL": "", + "MarketplaceURL": "", + "SupportURL": "", + "CanContainContent": true, + "IsBetaVersion": false, + "IsExperimentalVersion": false, + "Installed": false, + "Modules": [ + { + "Name": "MiceShaders", + "Type": "Runtime", + "LoadingPhase": "PostConfigInit" + } + ] +} \ No newline at end of file diff --git a/Plugins/MiceShaders/Resources/Icon128.png b/Plugins/MiceShaders/Resources/Icon128.png new file mode 100644 index 0000000..1231d4a Binary files /dev/null and b/Plugins/MiceShaders/Resources/Icon128.png differ diff --git a/Plugins/MiceShaders/Shaders/Private/DeformationCS.usf b/Plugins/MiceShaders/Shaders/Private/DeformationCS.usf new file mode 100644 index 0000000..87e1e58 --- /dev/null +++ b/Plugins/MiceShaders/Shaders/Private/DeformationCS.usf @@ -0,0 +1,91 @@ +// DeformationCS.usf + +#include "/Engine/Public/Platform.ush" + +// ----------------------------------------------------------------------------- +// 1. 输入输出定义 +// ----------------------------------------------------------------------------- +// 必须与 C++ 的 SHADER_PARAMETER_RDG_TEXTURE_SRV 匹配 +Texture2D InputTexture; +// 必须与 C++ 的 SHADER_PARAMETER_RDG_TEXTURE_UAV 匹配 +RWTexture2D OutputTexture; + +// ----------------------------------------------------------------------------- +// 2. 参数缓冲区 (必须与 C++ FParameters 结构体严格匹配) +// ----------------------------------------------------------------------------- +cbuffer Parameters +{ + float3 ImpactPoint; + float ImpactForce; + float3 ImpactNormal; + float DeformationRadius; + float Plasticity; + int3 TextureSize; + + // 修正后的参数:对应 C++ 的 FIntVector4 UpdateRegionParam + // 分量顺序: (Min.X, Min.Y, Max.X, Max.Y) + int4 UpdateRegionParam; +}; + +// ----------------------------------------------------------------------------- +// 3. 线程组大小和入口点 +// ----------------------------------------------------------------------------- +#define THREADGROUP_SIZE_X 8 +#define THREADGROUP_SIZE_Y 8 +[numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y, 1)] +void MainCompute( // 注意: 建议使用 MainCompute,如果 C++ 注册的是 MainCompute + uint3 GroupID : SV_GroupID, + uint3 GroupThreadID : SV_GroupThreadID, + uint3 DispatchThreadID : SV_DispatchThreadID // 全局线程 ID +) +{ + // ------------------------------------------------------------------------- + // 4. 坐标计算与边界检查 + // ------------------------------------------------------------------------- + + // DispatchThreadID 从 0 开始,代表在调度区域内的偏移量。 + // 我们将其加上 UpdateRegionParam.xy (Min.X, Min.Y) 得到全局像素坐标。 + int2 PixelCoord = DispatchThreadID.xy + int2(UpdateRegionParam.x, UpdateRegionParam.y); + + // 边界检查:检查是否超出 UpdateRegionParam.zw (Max.X, Max.Y) + if (PixelCoord.x < UpdateRegionParam.x || PixelCoord.x >= UpdateRegionParam.z || + PixelCoord.y < UpdateRegionParam.y || PixelCoord.y >= UpdateRegionParam.w) + { + // 虽然我们调度时已经只调度了需要的区域,但为了安全起见,保留检查。 + // 理论上,如果 C++ 端调度正确,这个检查应该很少触发。 + return; + } + + // ------------------------------------------------------------------------- + // 5. 变形计算逻辑 (与之前相同) + // ------------------------------------------------------------------------- + + // 从输入纹理读取位置 + // Load 需要 int3,第三个分量 (MipLevel) 设为 0 + 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; + } + +} \ No newline at end of file diff --git a/Plugins/MiceShaders/Source/MiceShaders/MiceShaders.Build.cs b/Plugins/MiceShaders/Source/MiceShaders/MiceShaders.Build.cs new file mode 100644 index 0000000..17dbe27 --- /dev/null +++ b/Plugins/MiceShaders/Source/MiceShaders/MiceShaders.Build.cs @@ -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 ... + } + ); + } +} diff --git a/Source/BladeSmith/FDeformComputeShader.cpp b/Plugins/MiceShaders/Source/MiceShaders/Private/FDeformComputeShader.cpp similarity index 87% rename from Source/BladeSmith/FDeformComputeShader.cpp rename to Plugins/MiceShaders/Source/MiceShaders/Private/FDeformComputeShader.cpp index 2abd903..0052ddc 100644 --- a/Source/BladeSmith/FDeformComputeShader.cpp +++ b/Plugins/MiceShaders/Source/MiceShaders/Private/FDeformComputeShader.cpp @@ -12,7 +12,7 @@ FThreadSafeDeformationParams GDeformationParams; // 实现Compute Shader -IMPLEMENT_GLOBAL_SHADER(FDeformComputeShader, "/Engine/Private/MiceCola/DeformationCS.usf", "MainCompute", SF_Compute); +IMPLEMENT_GLOBAL_SHADER(FDeformComputeShader, "/MiceShaders/Shaders/Private/DeformationCS.usf", "MainCompute", SF_Compute); // 线程安全参数管理 void FThreadSafeDeformationParams::AddParams(const FDeformationCSDispatchParams& NewParams) diff --git a/Plugins/MiceShaders/Source/MiceShaders/Private/MiceShaders.cpp b/Plugins/MiceShaders/Source/MiceShaders/Private/MiceShaders.cpp new file mode 100644 index 0000000..f3f6797 --- /dev/null +++ b/Plugins/MiceShaders/Source/MiceShaders/Private/MiceShaders.cpp @@ -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) \ No newline at end of file diff --git a/Source/BladeSmith/FDeformComputeShader.h b/Plugins/MiceShaders/Source/MiceShaders/Public/FDeformComputeShader.h similarity index 82% rename from Source/BladeSmith/FDeformComputeShader.h rename to Plugins/MiceShaders/Source/MiceShaders/Public/FDeformComputeShader.h index 40f45a0..468a0c4 100644 --- a/Source/BladeSmith/FDeformComputeShader.h +++ b/Plugins/MiceShaders/Source/MiceShaders/Public/FDeformComputeShader.h @@ -5,7 +5,7 @@ #include "RenderGraphUtils.h" // 必须包含 // Compute Shader参数结构体 -struct FDeformationCSDispatchParams +struct MICESHADERS_API FDeformationCSDispatchParams { // 碰撞参数 FVector ImpactPoint; @@ -47,7 +47,7 @@ extern FThreadSafeDeformationParams GDeformationParams; // DeformationCS.h (续) // Compute Shader类 -class FDeformComputeShader : public FGlobalShader +class MICESHADERS_API FDeformComputeShader : public FGlobalShader { DECLARE_GLOBAL_SHADER(FDeformComputeShader); SHADER_USE_PARAMETER_STRUCT(FDeformComputeShader, FGlobalShader); @@ -59,14 +59,13 @@ class FDeformComputeShader : public FGlobalShader SHADER_PARAMETER(FVector3f, ImpactNormal) SHADER_PARAMETER(float, DeformationRadius) SHADER_PARAMETER(float, Plasticity) - - // 纹理参数 - SHADER_PARAMETER_RDG_TEXTURE_SRV(Texture2D, InputTexture) - SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D, OutputTexture) - // 更新区域 SHADER_PARAMETER(FIntVector, TextureSize) SHADER_PARAMETER(FIntVector4, UpdateRegionParam) + + // 纹理参数 + SHADER_PARAMETER_RDG_TEXTURE_SRV(Texture2D, InputTexture) + SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D, OutputTexture) END_SHADER_PARAMETER_STRUCT() static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters) @@ -74,13 +73,13 @@ class FDeformComputeShader : public FGlobalShader return GetMaxSupportedFeatureLevel(Parameters.Platform) >= ERHIFeatureLevel::SM5; } - // 2. ✨ 修复 Precache 错误的关键:实现 ShouldPrecachePermutation - // 我们告诉引擎:在开发模式下,请始终预缓存这个着色器。 - static EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FGlobalShaderPermutationParameters& Parameters) - { - // 如果你的着色器没有使用任何模板或标志,只需返回 E_DEVELOPMENT 或 E_ALL。 - return EShaderPermutationPrecacheRequest::Precached; - } + // // 2. ✨ 修复 Precache 错误的关键:实现 ShouldPrecachePermutation + // // 我们告诉引擎:在开发模式下,请始终预缓存这个着色器。 + // static EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FGlobalShaderPermutationParameters& Parameters) + // { + // // 如果你的着色器没有使用任何模板或标志,只需返回 E_DEVELOPMENT 或 E_ALL。 + // return EShaderPermutationPrecacheRequest::Precached; + // } static bool ShouldCache(EShaderPlatform Platform) diff --git a/Plugins/MiceShaders/Source/MiceShaders/Public/MiceShaders.h b/Plugins/MiceShaders/Source/MiceShaders/Public/MiceShaders.h new file mode 100644 index 0000000..8b1f9b6 --- /dev/null +++ b/Plugins/MiceShaders/Source/MiceShaders/Public/MiceShaders.h @@ -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; +}; diff --git a/Source/BladeSmith/BladeSmith.Build.cs b/Source/BladeSmith/BladeSmith.Build.cs index 841e79f..057f8d9 100644 --- a/Source/BladeSmith/BladeSmith.Build.cs +++ b/Source/BladeSmith/BladeSmith.Build.cs @@ -23,7 +23,8 @@ public class BladeSmith : ModuleRules "PhysicsCore", "AudioExtensions", "AudioMixer", - "AudioPlatformConfiguration" + "AudioPlatformConfiguration", + "MiceShaders" }); }