Files
UnrealEngineUWP/Engine/Plugins/Experimental/MeshModelingToolset/Source/MeshModelingToolsEditorOnly/Private/ParameterizeMeshTool.cpp
Ryan Schmidt 52ab1f5537 ModelingTools: Clean up AutoUV Tools and add support for Polygroup Layers.
- Refactor recompute-UVs-for-groups/islands out of ParameterizeMeshOp/Tool into RecomputeUVsOp/Tool. RecomputeUVsTool supports selection of active PolygroupLayer when applicable. Add support for auto-repacking.
- Clean up ParameterizeMeshOp/Tool, remove functionality related to recomputation that was moved to Recompute tool, add separate property sets for UVAtlas and XAtlas
- Add FDynamicMeshUVEditor::QuickPack() function to compute UV layout
- Add support for Polygroup Layer selection in SetCollisionGeometryTool, if only one source object is in use

#rb none
#rnx
#jira none
#preflight 60ced4b0be81e800012cc135

[CL 16724375 by Ryan Schmidt in ue5-main branch]
2021-06-20 16:57:02 -04:00

177 lines
5.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "ParameterizeMeshTool.h"
#include "InteractiveToolManager.h"
#include "ToolBuilderUtil.h"
#include "ToolSetupUtil.h"
#include "ModelingToolTargetUtil.h"
#include "DynamicMesh/DynamicMesh3.h"
#include "FaceGroupUtil.h"
#include "ParameterizationOps/ParameterizeMeshOp.h"
#include "ExplicitUseGeometryMathTypes.h" // using UE::Geometry::(math types)
using namespace UE::Geometry;
#define LOCTEXT_NAMESPACE "UParameterizeMeshTool"
/*
* ToolBuilder
*/
USingleSelectionMeshEditingTool* UParameterizeMeshToolBuilder::CreateNewTool(const FToolBuilderState& SceneState) const
{
UParameterizeMeshTool* NewTool = NewObject<UParameterizeMeshTool>(SceneState.ToolManager);
return NewTool;
}
/*
* Tool
*/
void UParameterizeMeshTool::Setup()
{
UInteractiveTool::Setup();
InputMesh = MakeShared<FDynamicMesh3, ESPMode::ThreadSafe>();
*InputMesh = UE::ToolTarget::GetDynamicMeshCopy(Target);
Preview = NewObject<UMeshOpPreviewWithBackgroundCompute>(this);
Preview->Setup(this->TargetWorld, this);
Preview->PreviewMesh->SetTangentsMode(EDynamicMeshComponentTangentsMode::AutoCalculated);
Preview->PreviewMesh->ReplaceMesh(*InputMesh);
Preview->ConfigureMaterials(UE::ToolTarget::GetMaterialSet(Target).Materials,
ToolSetupUtil::GetDefaultWorkingMaterial(GetToolManager()));
Preview->PreviewMesh->SetTransform((FTransform)UE::ToolTarget::GetLocalToWorldTransform(Target));
Preview->OnMeshUpdated.AddLambda([this](UMeshOpPreviewWithBackgroundCompute* Op)
{
MaterialSettings->UpdateMaterials();
});
UE::ToolTarget::HideSourceObject(Target);
// initialize our properties
UVChannelProperties = NewObject<UMeshUVChannelProperties>(this);
UVChannelProperties->RestoreProperties(this);
UVChannelProperties->Initialize(InputMesh.Get(), false);
UVChannelProperties->ValidateSelection(true);
UVChannelProperties->WatchProperty(UVChannelProperties->UVChannel, [this](const FString& NewValue)
{
MaterialSettings->UVChannel = UVChannelProperties->GetSelectedChannelIndex(true);
});
AddToolPropertySource(UVChannelProperties);
Settings = NewObject<UParameterizeMeshToolProperties>(this);
Settings->RestoreProperties(this);
AddToolPropertySource(Settings);
Settings->WatchProperty(Settings->Method, [&](EParameterizeMeshUVMethod) { OnMethodTypeChanged(); });
UVAtlasProperties = NewObject<UParameterizeMeshToolUVAtlasProperties>(this);
UVAtlasProperties->RestoreProperties(this);
AddToolPropertySource(UVAtlasProperties);
SetToolPropertySourceEnabled(UVAtlasProperties, false);
XAtlasProperties = NewObject<UParameterizeMeshToolXAtlasProperties>(this);
XAtlasProperties->RestoreProperties(this);
AddToolPropertySource(XAtlasProperties);
SetToolPropertySourceEnabled(XAtlasProperties, false);
MaterialSettings = NewObject<UExistingMeshMaterialProperties>(this);
MaterialSettings->MaterialMode = ESetMeshMaterialMode::Checkerboard;
MaterialSettings->RestoreProperties(this, TEXT("ModelingUVTools"));
AddToolPropertySource(MaterialSettings);
// force update
MaterialSettings->UpdateMaterials();
Preview->OverrideMaterial = MaterialSettings->GetActiveOverrideMaterial();
Preview->InvalidateResult(); // start compute
SetToolDisplayName(LOCTEXT("ToolNameGlobal", "AutoUV"));
GetToolManager()->DisplayMessage(
LOCTEXT("OnStartTool_Global", "Automatically partition the selected Mesh into UV islands, flatten, and pack into a single UV chart"),
EToolMessageLevel::UserNotification);
}
void UParameterizeMeshTool::OnPropertyModified(UObject* PropertySet, FProperty* Property)
{
if (PropertySet != MaterialSettings)
{
Preview->InvalidateResult();
}
MaterialSettings->UpdateMaterials();
Preview->OverrideMaterial = MaterialSettings->GetActiveOverrideMaterial();
}
void UParameterizeMeshTool::OnMethodTypeChanged()
{
SetToolPropertySourceEnabled(UVAtlasProperties, Settings->Method == EParameterizeMeshUVMethod::UVAtlas);
SetToolPropertySourceEnabled(XAtlasProperties, Settings->Method == EParameterizeMeshUVMethod::XAtlas);
Preview->InvalidateResult();
}
void UParameterizeMeshTool::Shutdown(EToolShutdownType ShutdownType)
{
UVChannelProperties->SaveProperties(this);
Settings->SaveProperties(this);
MaterialSettings->SaveProperties(this, TEXT("ModelingUVTools"));
FDynamicMeshOpResult Result = Preview->Shutdown();
// Restore (unhide) the source meshes
UE::ToolTarget::ShowSourceObject(Target);
if (ShutdownType == EToolShutdownType::Accept)
{
GetToolManager()->BeginUndoTransaction(LOCTEXT("ParameterizeMesh", "Auto UVs"));
FDynamicMesh3* NewDynamicMesh = Result.Mesh.Get();
if (ensure(NewDynamicMesh))
{
UE::ToolTarget::CommitDynamicMeshUVUpdate(Target, NewDynamicMesh);
}
GetToolManager()->EndUndoTransaction();
}
}
void UParameterizeMeshTool::OnTick(float DeltaTime)
{
Preview->Tick(DeltaTime);
}
bool UParameterizeMeshTool::CanAccept() const
{
return Super::CanAccept() && Preview->HaveValidResult();
}
TUniquePtr<FDynamicMeshOperator> UParameterizeMeshTool::MakeNewOperator()
{
FAxisAlignedBox3d MeshBounds = Preview->PreviewMesh->GetMesh()->GetBounds();
TUniquePtr<FParameterizeMeshOp> ParameterizeMeshOp = MakeUnique<FParameterizeMeshOp>();
ParameterizeMeshOp->InputMesh = InputMesh;
ParameterizeMeshOp->UVLayer = UVChannelProperties->GetSelectedChannelIndex(true);
ParameterizeMeshOp->Method = (EParamOpBackend)(int)Settings->Method;
// uvatlas options
ParameterizeMeshOp->Stretch = UVAtlasProperties->ChartStretch;
ParameterizeMeshOp->NumCharts = UVAtlasProperties->NumCharts;
// xatlas options
ParameterizeMeshOp->XAtlasMaxIterations = XAtlasProperties->MaxIterations;
ParameterizeMeshOp->SetTransform(UE::ToolTarget::GetLocalToWorldTransform(Target));
return ParameterizeMeshOp;
}
#undef LOCTEXT_NAMESPACE