Files
UnrealEngineUWP/Engine/Plugins/Experimental/MeshModelingToolset/Source/MeshModelingTools/Private/WeldMeshEdgesTool.cpp

228 lines
6.2 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "WeldMeshEdgesTool.h"
#include "InteractiveToolManager.h"
#include "ToolBuilderUtil.h"
#include "ToolSetupUtil.h"
#include "ModelingToolTargetUtil.h"
#include "PreviewMesh.h"
#include "Drawing/MeshElementsVisualizer.h"
#include "DynamicMesh/DynamicMesh3.h"
#include "DynamicMesh/Operations/MergeCoincidentMeshEdges.h"
#include "ExplicitUseGeometryMathTypes.h" // using UE::Geometry::(math types)
using namespace UE::Geometry;
#define LOCTEXT_NAMESPACE "UWeldMeshEdgesTool"
/*
* ToolBuilder
*/
USingleSelectionMeshEditingTool* UWeldMeshEdgesToolBuilder::CreateNewTool(const FToolBuilderState& SceneState) const
{
return NewObject<UWeldMeshEdgesTool>(SceneState.ToolManager);
}
class FWeldMeshEdgesOp : public FDynamicMeshOperator
{
public:
// parameters set by the tool
TSharedPtr<FDynamicMesh3, ESPMode::ThreadSafe> SourceMesh;
double Tolerance;
bool bOnlyUnique;
public:
FWeldMeshEdgesOp() : FDynamicMeshOperator() {}
virtual void CalculateResult(FProgressCancel* Progress) override
{
if ((Progress && Progress->Cancelled()) || !SourceMesh)
{
return;
}
ResultMesh->Copy(*SourceMesh, true, true, true, true);
FMergeCoincidentMeshEdges Merger(ResultMesh.Get());
Merger.MergeVertexTolerance = Tolerance;
Merger.MergeSearchTolerance = 2 * Merger.MergeVertexTolerance;
Merger.OnlyUniquePairs = bOnlyUnique;
if (Merger.Apply() == false)
{
ResultMesh->Copy(*SourceMesh, true, true, true, true);
}
else if (ResultMesh->CheckValidity(true, EValidityCheckFailMode::ReturnOnly) == false)
{
ResultMesh->Copy(*SourceMesh, true, true, true, true);
}
}
void SetTransform(const FTransform& Transform)
{
ResultTransform = (UE::Geometry::FTransform3d)Transform;
}
};
TUniquePtr<FDynamicMeshOperator> UWeldMeshEdgesOperatorFactory::MakeNewOperator()
{
check(WeldMeshEdgesTool);
TUniquePtr<FWeldMeshEdgesOp> MeshOp = MakeUnique<FWeldMeshEdgesOp>();
WeldMeshEdgesTool->UpdateOpParameters(*MeshOp);
return MeshOp;
}
/*
* Tool
*/
UWeldMeshEdgesTool::UWeldMeshEdgesTool()
{
SetToolDisplayName(LOCTEXT("WeldMeshEdgesToolName", "Weld Edges"));
}
void UWeldMeshEdgesTool::Setup()
{
UInteractiveTool::Setup();
SourceMesh = MakeShared<FDynamicMesh3, ESPMode::ThreadSafe>(UE::ToolTarget::GetDynamicMeshCopy(Target, true));
FTransform MeshTransform = (FTransform)UE::ToolTarget::GetLocalToWorldTransform(Target);
OperatorFactory = NewObject<UWeldMeshEdgesOperatorFactory>(this);
OperatorFactory->WeldMeshEdgesTool = this;
PreviewCompute = NewObject<UMeshOpPreviewWithBackgroundCompute>(OperatorFactory);
PreviewCompute->Setup(this->TargetWorld, OperatorFactory);
// Give the preview something to display
PreviewCompute->PreviewMesh->SetTransform(MeshTransform);
PreviewCompute->PreviewMesh->SetTangentsMode(EDynamicMeshComponentTangentsMode::ExternallyProvided);
PreviewCompute->PreviewMesh->UpdatePreview(SourceMesh.Get());
ModelingComponents: Clean up DynamicMeshComponent API. Update Component and Proxy handling of Tangents to use Attribute Overlay if available. Update affected Tools and also convert most of the affected Tools to use UE::ToolTarget helper functions. - Add UE::ToolTarget::CommitMaterialSetUpdate() and ::CommitDynamicMeshUpdate(). ::GetDynamicMeshCopy() can now return tangents if requested. - Add IMeshDescriptionProvider::CalculateAutoGeneratedAttributes(). Default implementation does nothing, UStaticMeshComponentToolTarget implementation initializes auto-generated MeshDescription attributes. Used in ::GetDynamicMeshCopy() to get tangents (but requires a MeshDescription copy). - Clean up handling of Tangents in Simple/OctreeDynamicMeshComponent. Add local MakeTangentsFunc() to generate the Tangents lambda, handle different cases and no-tangents fallbacks consistently. - UDynamicMesh: add optional info arguments to EditMesh() and ChangeInfo struct. Add support for deferring change events from Edit funcs. - Remove UBaseDynamicMeshComponent::InitializeMesh(), ::Bake() APIs, and add ::SetMesh(). Implement in Simple/Octree implementations, update all Tools that used those APIs. - Add USimpleDynamicMeshComponent::ProcessMesh(), EditMesh(). These are now the preferred ways to read/write mesh. - Update USimpleDynamicMeshComponent tangents handling. Externally-computed tangents are now taken directly from the FDynamicMesh3 attribute set. Autogenerated tangents are still computed and stored in an internal FMeshTangentsf, but this is no longer exposed for external updates. - Remove UPreviewMesh pass-through functions for Tangents access, InitializeMesh() and Bake(). Add ProcessMesh() - Update all affected Tools. In most cases these Tools have also been converted to use ModelingToolTargetUtil functions, instead of direct ToolTarget interface casting. #rb none #rnx #jira none #preflight 60c3e71d3e1b3c00015668af [CL 16650666 by Ryan Schmidt in ue5-main branch]
2021-06-11 22:39:18 -04:00
FComponentMaterialSet MaterialSet = UE::ToolTarget::GetMaterialSet(Target);
PreviewCompute->ConfigureMaterials(MaterialSet.Materials,
ToolSetupUtil::GetDefaultWorkingMaterial(GetToolManager()) );
PreviewCompute->SetVisibility(true);
ModelingComponents: Clean up DynamicMeshComponent API. Update Component and Proxy handling of Tangents to use Attribute Overlay if available. Update affected Tools and also convert most of the affected Tools to use UE::ToolTarget helper functions. - Add UE::ToolTarget::CommitMaterialSetUpdate() and ::CommitDynamicMeshUpdate(). ::GetDynamicMeshCopy() can now return tangents if requested. - Add IMeshDescriptionProvider::CalculateAutoGeneratedAttributes(). Default implementation does nothing, UStaticMeshComponentToolTarget implementation initializes auto-generated MeshDescription attributes. Used in ::GetDynamicMeshCopy() to get tangents (but requires a MeshDescription copy). - Clean up handling of Tangents in Simple/OctreeDynamicMeshComponent. Add local MakeTangentsFunc() to generate the Tangents lambda, handle different cases and no-tangents fallbacks consistently. - UDynamicMesh: add optional info arguments to EditMesh() and ChangeInfo struct. Add support for deferring change events from Edit funcs. - Remove UBaseDynamicMeshComponent::InitializeMesh(), ::Bake() APIs, and add ::SetMesh(). Implement in Simple/Octree implementations, update all Tools that used those APIs. - Add USimpleDynamicMeshComponent::ProcessMesh(), EditMesh(). These are now the preferred ways to read/write mesh. - Update USimpleDynamicMeshComponent tangents handling. Externally-computed tangents are now taken directly from the FDynamicMesh3 attribute set. Autogenerated tangents are still computed and stored in an internal FMeshTangentsf, but this is no longer exposed for external updates. - Remove UPreviewMesh pass-through functions for Tangents access, InitializeMesh() and Bake(). Add ProcessMesh() - Update all affected Tools. In most cases these Tools have also been converted to use ModelingToolTargetUtil functions, instead of direct ToolTarget interface casting. #rb none #rnx #jira none #preflight 60c3e71d3e1b3c00015668af [CL 16650666 by Ryan Schmidt in ue5-main branch]
2021-06-11 22:39:18 -04:00
UE::ToolTarget::HideSourceObject(Target);
Settings = NewObject<UWeldMeshEdgesToolProperties>(this);
Settings->RestoreProperties(this);
AddToolPropertySource(Settings);
Settings->WatchProperty(Settings->Tolerance, [this](float) { PreviewCompute->InvalidateResult(); });
Settings->WatchProperty(Settings->bOnlyUnique, [this](bool) { PreviewCompute->InvalidateResult(); });
// create mesh display
MeshElementsDisplay = NewObject<UMeshElementsVisualizer>(this);
MeshElementsDisplay->CreateInWorld(PreviewCompute->PreviewMesh->GetWorld(), PreviewCompute->PreviewMesh->GetTransform());
if (ensure(MeshElementsDisplay->Settings))
{
MeshElementsDisplay->Settings->bShowUVSeams = false;
MeshElementsDisplay->Settings->bShowNormalSeams = false;
MeshElementsDisplay->Settings->bShowColorSeams = false;
MeshElementsDisplay->Settings->RestoreProperties(this, TEXT("WeldEdges"));
AddToolPropertySource(MeshElementsDisplay->Settings);
}
MeshElementsDisplay->SetMeshAccessFunction([this](UMeshElementsVisualizer::ProcessDynamicMeshFunc ProcessFunc) {
PreviewCompute->ProcessCurrentMesh(ProcessFunc);
});
PreviewCompute->OnMeshUpdated.AddLambda([this](UMeshOpPreviewWithBackgroundCompute* Compute)
{
Compute->ProcessCurrentMesh([&](const FDynamicMesh3& ReadMesh)
{
MeshElementsDisplay->NotifyMeshChanged();
});
});
PreviewCompute->InvalidateResult();
SetToolDisplayName(LOCTEXT("ToolName", "Weld Edges"));
GetToolManager()->DisplayMessage(
LOCTEXT("WeldMeshEdgesToolDescription", "Weld overlapping/identical border edges of the selected Mesh, by merging the vertices."),
EToolMessageLevel::UserNotification);
}
void UWeldMeshEdgesTool::Shutdown(EToolShutdownType ShutdownType)
{
Settings->SaveProperties(this);
UE::ToolTarget::ShowSourceObject(Target);
if (ensure(MeshElementsDisplay->Settings))
{
MeshElementsDisplay->Settings->SaveProperties(this, TEXT("WeldEdges"));
}
MeshElementsDisplay->Disconnect();
if (PreviewCompute)
{
FDynamicMeshOpResult Result = PreviewCompute->Shutdown();
if (ShutdownType == EToolShutdownType::Accept)
{
GetToolManager()->BeginUndoTransaction(LOCTEXT("WeldMeshEdgesToolTransactionName", "Weld Edges"));
FDynamicMesh3* DynamicMeshResult = Result.Mesh.Get();
if (ensure(DynamicMeshResult != nullptr))
{
// todo: have not actually modified topology here, but groups-only update is not supported yet
UE::ToolTarget::CommitDynamicMeshUpdate(Target, *DynamicMeshResult, true);
}
GetToolManager()->EndUndoTransaction();
}
}
}
bool UWeldMeshEdgesTool::CanAccept() const
{
return Super::CanAccept() && (PreviewCompute == nullptr || PreviewCompute->HaveValidResult());
}
void UWeldMeshEdgesTool::OnTick(float DeltaTime)
{
PreviewCompute->Tick(DeltaTime);
MeshElementsDisplay->OnTick(DeltaTime);
}
void UWeldMeshEdgesTool::UpdateOpParameters(FWeldMeshEdgesOp& Op) const
{
Op.bOnlyUnique = Settings->bOnlyUnique;
Op.Tolerance = Settings->Tolerance;
Op.SourceMesh = SourceMesh;
FTransform LocalToWorld = (FTransform)UE::ToolTarget::GetLocalToWorldTransform(Target);
Op.SetTransform(LocalToWorld);
}
#undef LOCTEXT_NAMESPACE