Files
UnrealEngine/Engine/Plugins/Runtime/MeshModelingToolset/Source/MeshModelingTools/Private/DynamicMeshBrushTool.cpp
2025-05-18 13:04:45 +08:00

95 lines
2.3 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "DynamicMeshBrushTool.h"
#include "InteractiveToolManager.h"
#include "ToolBuilderUtil.h"
#include "ToolSetupUtil.h"
#include "ModelingToolTargetUtil.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(DynamicMeshBrushTool)
using namespace UE::Geometry;
// localization namespace
#define LOCTEXT_NAMESPACE "UDynamicMeshBrushTool"
/*
* Tool
*/
UDynamicMeshBrushTool::UDynamicMeshBrushTool()
{
}
void UDynamicMeshBrushTool::Setup()
{
PreviewMesh = NewObject<UPreviewMesh>(this);
PreviewMesh->bBuildSpatialDataStructure = true;
PreviewMesh->CreateInWorld(GetTargetWorld(), FTransform::Identity);
FTransformSRT3d LocalToWorldTransform = UE::ToolTarget::GetLocalToWorldTransform(Target);
PreviewMesh->SetTransform((FTransform)LocalToWorldTransform);
ToolSetupUtil::ApplyRenderingConfigurationToPreview(PreviewMesh, Target);
FComponentMaterialSet MaterialSet = UE::ToolTarget::GetMaterialSet(Target);
PreviewMesh->SetMaterials(MaterialSet.Materials);
PreviewMesh->ReplaceMesh(UE::ToolTarget::GetDynamicMeshCopy(Target));
OnBaseMeshComponentChangedHandle = PreviewMesh->GetOnMeshChanged().Add(
FSimpleMulticastDelegate::FDelegate::CreateUObject(this, &UDynamicMeshBrushTool::OnBaseMeshComponentChanged));
// call this here so that base tool can estimate target dimension
InputMeshBoundsLocal = PreviewMesh->GetPreviewDynamicMesh()->GetBounds();
double ScaledDim = LocalToWorldTransform.TransformVector(FVector::OneVector).Size();
this->WorldToLocalScale = FMathd::Sqrt3 / FMathd::Max(FMathf::ZeroTolerance, ScaledDim);
UBaseBrushTool::Setup();
UE::ToolTarget::HideSourceObject(Target);
}
double UDynamicMeshBrushTool::EstimateMaximumTargetDimension()
{
return InputMeshBoundsLocal.MaxDim();
}
void UDynamicMeshBrushTool::Shutdown(EToolShutdownType ShutdownType)
{
UBaseBrushTool::Shutdown(ShutdownType);
LongTransactions.CloseAll(GetToolManager());
UE::ToolTarget::ShowSourceObject(Target);
if (PreviewMesh != nullptr)
{
PreviewMesh->GetOnMeshChanged().Remove(OnBaseMeshComponentChangedHandle);
OnShutdown(ShutdownType);
PreviewMesh->SetVisible(false);
PreviewMesh->Disconnect();
PreviewMesh = nullptr;
}
}
bool UDynamicMeshBrushTool::HitTest(const FRay& Ray, FHitResult& OutHit)
{
return PreviewMesh->FindRayIntersection(FRay3d(Ray), OutHit);
}
#undef LOCTEXT_NAMESPACE