2019-12-27 09:26:59 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-10-01 20:41:42 -04:00
|
|
|
|
|
|
|
|
#include "SimplifyMeshTool.h"
|
|
|
|
|
#include "InteractiveToolManager.h"
|
2020-02-14 17:00:18 -05:00
|
|
|
#include "Properties/RemeshProperties.h"
|
2021-06-15 17:05:40 -04:00
|
|
|
#include "Properties/MeshStatisticsProperties.h"
|
|
|
|
|
#include "Drawing/MeshElementsVisualizer.h"
|
2019-10-01 20:41:42 -04:00
|
|
|
#include "ToolBuilderUtil.h"
|
2021-06-15 17:05:40 -04:00
|
|
|
#include "MeshDescriptionToDynamicMesh.h"
|
|
|
|
|
#include "ModelingToolTargetUtil.h"
|
2019-10-17 20:26:57 -04:00
|
|
|
#include "ToolSetupUtil.h"
|
2021-06-15 17:05:40 -04:00
|
|
|
#include "DynamicMesh/DynamicMesh3.h"
|
|
|
|
|
#include "DynamicMesh/DynamicMeshAABBTree3.h"
|
2020-01-27 20:11:15 -05:00
|
|
|
#include "Util/ColorConstants.h"
|
2019-10-01 20:41:42 -04:00
|
|
|
|
2021-03-24 11:11:02 -04:00
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
//#include "ProfilingDebugging/ScopedTimers.h" // enable this to use the timer.
|
2019-10-17 20:26:57 -04:00
|
|
|
#include "Modules/ModuleManager.h"
|
2019-10-01 20:41:42 -04:00
|
|
|
#include "IMeshReductionManagerModule.h"
|
|
|
|
|
#include "IMeshReductionInterfaces.h"
|
2019-10-17 20:26:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
#include "Misc/ScopedSlowTask.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-03-09 19:33:56 -04:00
|
|
|
using namespace UE::Geometry;
|
|
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
#define LOCTEXT_NAMESPACE "USimplifyMeshTool"
|
|
|
|
|
|
|
|
|
|
DEFINE_LOG_CATEGORY_STATIC(LogMeshSimplification, Log, All);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ToolBuilder
|
|
|
|
|
*/
|
2021-03-24 11:11:02 -04:00
|
|
|
USingleSelectionMeshEditingTool* USimplifyMeshToolBuilder::CreateNewTool(const FToolBuilderState& SceneState) const
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
2021-03-24 11:11:02 -04:00
|
|
|
return NewObject<USimplifyMeshTool>(SceneState.ToolManager);
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Tool
|
|
|
|
|
*/
|
|
|
|
|
USimplifyMeshToolProperties::USimplifyMeshToolProperties()
|
|
|
|
|
{
|
|
|
|
|
SimplifierType = ESimplifyType::QEM;
|
|
|
|
|
TargetMode = ESimplifyTargetType::Percentage;
|
|
|
|
|
TargetPercentage = 50;
|
2021-09-17 17:05:14 -04:00
|
|
|
TargetTriangleCount = 1000;
|
|
|
|
|
TargetVertexCount = 1000;
|
|
|
|
|
MinimalAngleThreshold = 0.01;
|
2019-10-01 20:41:42 -04:00
|
|
|
TargetEdgeLength = 5.0;
|
|
|
|
|
bReproject = false;
|
|
|
|
|
bPreventNormalFlips = true;
|
|
|
|
|
bDiscardAttributes = false;
|
2021-05-11 13:17:19 -04:00
|
|
|
bGeometricConstraint = false;
|
2020-01-27 20:11:15 -05:00
|
|
|
bShowGroupColors = false;
|
2020-02-14 17:00:18 -05:00
|
|
|
GroupBoundaryConstraint = EGroupBoundaryConstraint::Ignore;
|
|
|
|
|
MaterialBoundaryConstraint = EMaterialBoundaryConstraint::Ignore;
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USimplifyMeshTool::Setup()
|
|
|
|
|
{
|
|
|
|
|
UInteractiveTool::Setup();
|
|
|
|
|
|
2019-10-17 20:26:57 -04:00
|
|
|
// hide component and create + show preview
|
2021-06-15 17:05:40 -04:00
|
|
|
UE::ToolTarget::HideSourceObject(Target);
|
|
|
|
|
|
|
|
|
|
Preview = NewObject<UMeshOpPreviewWithBackgroundCompute>(this);
|
2022-01-28 10:18:10 -05:00
|
|
|
Preview->Setup(GetTargetWorld(), this);
|
2021-10-07 22:25:54 -04:00
|
|
|
ToolSetupUtil::ApplyRenderingConfigurationToPreview(Preview->PreviewMesh, nullptr);
|
2021-06-15 17:05:40 -04:00
|
|
|
FComponentMaterialSet MaterialSet = UE::ToolTarget::GetMaterialSet(Target);
|
2019-12-19 18:07:47 -05:00
|
|
|
Preview->ConfigureMaterials( MaterialSet.Materials,
|
2019-10-17 20:26:57 -04:00
|
|
|
ToolSetupUtil::GetDefaultWorkingMaterial(GetToolManager())
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-15 17:05:40 -04:00
|
|
|
// some of this could be done async...
|
2019-10-17 20:26:57 -04:00
|
|
|
{
|
|
|
|
|
// if in editor, create progress indicator dialog because building mesh copies can be slow (for very large meshes)
|
|
|
|
|
// this is especially needed because of the copy we make of the meshdescription; for Reasons, copying meshdescription is pretty slow
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
static const FText SlowTaskText = LOCTEXT("SimplifyMeshInit", "Building mesh simplification data...");
|
|
|
|
|
|
|
|
|
|
FScopedSlowTask SlowTask(3.0f, SlowTaskText);
|
|
|
|
|
SlowTask.MakeDialog();
|
|
|
|
|
|
|
|
|
|
// Declare progress shortcut lambdas
|
|
|
|
|
auto EnterProgressFrame = [&SlowTask](int Progress)
|
2020-01-07 15:54:23 -05:00
|
|
|
{
|
2019-10-17 20:26:57 -04:00
|
|
|
SlowTask.EnterProgressFrame((float)Progress);
|
|
|
|
|
};
|
|
|
|
|
#else
|
|
|
|
|
auto EnterProgressFrame = [](int Progress) {};
|
|
|
|
|
#endif
|
ModelingTools: Add support for reading and writing to a specific LOD via IMeshDescriptionProvider/Committer. Refactor various other aspects of MeshDescription access. Update MeshTransferTool to be able to transfer from and to specific LODs.
Add new MeshTargetInterfaceTypes.h file, move EStaticMeshEditingLOD there and rename to EMeshLODIdentifier. Add FGetMeshParameters and FCommitMeshParameters types.
IMeshDescriptionProvider::GetMeshDescription() now takes FGetMeshParameters to optionally specify LOD.
Added IMeshDescriptionProvider::GetMeshDescriptionCopy() function, to handle copy-case.
removed IMeshDescriptionProvider::CalculateAutoGeneratedAttributes(). This was only being used to force Tangents computation, which can now be done via GetMeshDescriptionCopy() and FGetMeshParameters.bWantMeshTangents option
IMeshDescriptionCommitter commit functions now take a FCommitMeshParameters to optionally specify target LOD.
StaticMeshComponentToolTarget::GetMeshDescriptionCopy() added, optionally computes auto-generated MeshDescription attributes on the copy
StaticMesh(Component)ToolTarget updated to support FCommitMeshParameters target-LOD.
SkeletalMesh, Volume, and DynamicMesh ToolTargets updated for new APIs but do not currently support any of the new LOD parameters. These should never be called w/ LOD parameters in the current codebase, ensures added to catch any errors (they would be non-fatal).
UE::ToolTarget::GetMeshDescription() and GetMeshDescriptionCopy() in now take FGetMeshParameters argument.
Removed bWantMeshTangents param to GetMeshDescriptionCopy(), now done via FGetMeshParameters. Updated call sites.
TransferMeshesTool now supports specifying read and write LOD (numbered or HiRes) for StaticMeshComponent source/target, via above functions.
TransferMeshesTool can now also target a new LOD in a StaticMeshComponent, index is 1 larger than current maximum source LOD.
#rb lonnie.li, rinat.abdrashitov
#rnx
#jira none
#preflight 61b8d56b2e65a1df046aa5e1
#ROBOMERGE-AUTHOR: ryan.schmidt
#ROBOMERGE-SOURCE: CL 18461686 in //UE5/Release-5.0/... via CL 18461725
#ROBOMERGE-BOT: STARSHIP (Release-Engine-Staging -> Release-Engine-Test) (v899-18417669)
[CL 18461778 by ryan schmidt in ue5-release-engine-test branch]
2021-12-14 18:40:01 -05:00
|
|
|
FGetMeshParameters GetMeshParams;
|
|
|
|
|
GetMeshParams.bWantMeshTangents = true;
|
|
|
|
|
OriginalMeshDescription = MakeShared<FMeshDescription, ESPMode::ThreadSafe>(UE::ToolTarget::GetMeshDescriptionCopy(Target, GetMeshParams));
|
2021-06-15 17:05:40 -04:00
|
|
|
|
2019-10-17 20:26:57 -04:00
|
|
|
EnterProgressFrame(1);
|
2021-06-15 17:05:40 -04:00
|
|
|
// UE::ToolTarget::GetDynamicMeshCopy() would recompute the tangents a second time here
|
2021-02-17 11:50:23 -04:00
|
|
|
OriginalMesh = MakeShared<FDynamicMesh3, ESPMode::ThreadSafe>();
|
2021-01-20 14:44:42 -04:00
|
|
|
FMeshDescriptionToDynamicMesh Converter;
|
2021-06-15 17:05:40 -04:00
|
|
|
Converter.Convert(OriginalMeshDescription.Get(), *OriginalMesh, true); // convert with tangent overlay
|
|
|
|
|
|
2019-10-17 20:26:57 -04:00
|
|
|
EnterProgressFrame(2);
|
2021-02-17 11:50:23 -04:00
|
|
|
OriginalMeshSpatial = MakeShared<FDynamicMeshAABBTree3, ESPMode::ThreadSafe>(OriginalMesh.Get(), true);
|
2019-10-17 20:26:57 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 17:05:40 -04:00
|
|
|
Preview->PreviewMesh->SetTransform((FTransform)UE::ToolTarget::GetLocalToWorldTransform(Target));
|
2021-06-11 22:42:32 -04:00
|
|
|
Preview->PreviewMesh->SetTangentsMode(EDynamicMeshComponentTangentsMode::AutoCalculated);
|
2019-10-17 20:26:57 -04:00
|
|
|
Preview->PreviewMesh->UpdatePreview(OriginalMesh.Get());
|
2019-10-01 20:41:42 -04:00
|
|
|
|
|
|
|
|
// initialize our properties
|
|
|
|
|
SimplifyProperties = NewObject<USimplifyMeshToolProperties>(this);
|
2020-03-10 16:59:50 -04:00
|
|
|
SimplifyProperties->RestoreProperties(this);
|
2019-10-01 20:41:42 -04:00
|
|
|
AddToolPropertySource(SimplifyProperties);
|
|
|
|
|
|
2020-04-18 18:42:59 -04:00
|
|
|
SimplifyProperties->WatchProperty(SimplifyProperties->bShowGroupColors,
|
|
|
|
|
[this](bool bNewValue) { UpdateVisualization(); });
|
2020-01-27 20:11:15 -05:00
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
MeshStatisticsProperties = NewObject<UMeshStatisticsProperties>(this);
|
|
|
|
|
AddToolPropertySource(MeshStatisticsProperties);
|
|
|
|
|
|
2021-06-15 17:05:40 -04:00
|
|
|
MeshElementsDisplay = NewObject<UMeshElementsVisualizer>(this);
|
|
|
|
|
MeshElementsDisplay->CreateInWorld(Preview->PreviewMesh->GetWorld(), Preview->PreviewMesh->GetTransform());
|
|
|
|
|
if (ensure(MeshElementsDisplay->Settings))
|
|
|
|
|
{
|
|
|
|
|
MeshElementsDisplay->Settings->bShowWireframe = true;
|
|
|
|
|
MeshElementsDisplay->Settings->RestoreProperties(this, TEXT("Simplify"));
|
|
|
|
|
AddToolPropertySource(MeshElementsDisplay->Settings);
|
|
|
|
|
}
|
|
|
|
|
MeshElementsDisplay->SetMeshAccessFunction([this](UMeshElementsVisualizer::ProcessDynamicMeshFunc ProcessFunc) {
|
|
|
|
|
Preview->ProcessCurrentMesh(ProcessFunc);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2019-10-17 20:26:57 -04:00
|
|
|
Preview->OnMeshUpdated.AddLambda([this](UMeshOpPreviewWithBackgroundCompute* Compute)
|
|
|
|
|
{
|
2021-06-15 17:05:40 -04:00
|
|
|
Compute->ProcessCurrentMesh([&](const FDynamicMesh3& ReadMesh)
|
|
|
|
|
{
|
|
|
|
|
MeshStatisticsProperties->Update(ReadMesh);
|
|
|
|
|
MeshElementsDisplay->NotifyMeshChanged();
|
|
|
|
|
});
|
2019-10-17 20:26:57 -04:00
|
|
|
});
|
|
|
|
|
|
2020-03-06 16:56:32 -05:00
|
|
|
UpdateVisualization();
|
2019-10-17 20:26:57 -04:00
|
|
|
Preview->InvalidateResult();
|
2020-09-24 00:43:27 -04:00
|
|
|
|
2021-02-08 17:02:09 -04:00
|
|
|
SetToolDisplayName(LOCTEXT("ToolName", "Simplify"));
|
2020-09-24 00:43:27 -04:00
|
|
|
GetToolManager()->DisplayMessage(
|
|
|
|
|
LOCTEXT("OnStartTool", "Reduce the number of triangles in the selected Mesh using various strategies."),
|
|
|
|
|
EToolMessageLevel::UserNotification);
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-24 18:42:39 -04:00
|
|
|
bool USimplifyMeshTool::CanAccept() const
|
|
|
|
|
{
|
|
|
|
|
return Super::CanAccept() && Preview->HaveValidResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-28 18:40:54 -05:00
|
|
|
void USimplifyMeshTool::OnShutdown(EToolShutdownType ShutdownType)
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
2020-03-10 16:59:50 -04:00
|
|
|
SimplifyProperties->SaveProperties(this);
|
2021-06-15 17:05:40 -04:00
|
|
|
|
|
|
|
|
if (ensure(MeshElementsDisplay->Settings))
|
|
|
|
|
{
|
|
|
|
|
MeshElementsDisplay->Settings->SaveProperties(this, TEXT("Simplify"));
|
|
|
|
|
}
|
|
|
|
|
MeshElementsDisplay->Disconnect();
|
|
|
|
|
|
|
|
|
|
UE::ToolTarget::ShowSourceObject(Target);
|
2019-11-01 17:39:56 -04:00
|
|
|
FDynamicMeshOpResult Result = Preview->Shutdown();
|
2020-03-10 16:59:50 -04:00
|
|
|
if (ShutdownType == EToolShutdownType::Accept)
|
|
|
|
|
{
|
2021-06-15 17:05:40 -04:00
|
|
|
GetToolManager()->BeginUndoTransaction(LOCTEXT("SimplifyMeshToolTransactionName", "Simplify Mesh"));
|
|
|
|
|
UE::ToolTarget::CommitDynamicMeshUpdate(Target, *Result.Mesh, true);
|
|
|
|
|
GetToolManager()->EndUndoTransaction();
|
2020-03-10 16:59:50 -04:00
|
|
|
}
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-18 18:42:59 -04:00
|
|
|
void USimplifyMeshTool::OnTick(float DeltaTime)
|
2019-10-17 20:26:57 -04:00
|
|
|
{
|
|
|
|
|
Preview->Tick(DeltaTime);
|
2021-06-15 17:05:40 -04:00
|
|
|
MeshElementsDisplay->OnTick(DeltaTime);
|
2019-10-17 20:26:57 -04:00
|
|
|
}
|
|
|
|
|
|
2019-11-01 17:39:56 -04:00
|
|
|
TUniquePtr<FDynamicMeshOperator> USimplifyMeshTool::MakeNewOperator()
|
2019-10-17 20:26:57 -04:00
|
|
|
{
|
2019-11-01 17:39:56 -04:00
|
|
|
TUniquePtr<FSimplifyMeshOp> Op = MakeUnique<FSimplifyMeshOp>();
|
2019-10-17 20:26:57 -04:00
|
|
|
|
|
|
|
|
Op->bDiscardAttributes = SimplifyProperties->bDiscardAttributes;
|
2022-01-16 10:42:36 -05:00
|
|
|
// We always want attributes enabled on result even if we discard them initially
|
|
|
|
|
Op->bResultMustHaveAttributesEnabled = true;
|
2019-10-17 20:26:57 -04:00
|
|
|
Op->bPreventNormalFlips = SimplifyProperties->bPreventNormalFlips;
|
2020-01-27 20:11:15 -05:00
|
|
|
Op->bPreserveSharpEdges = SimplifyProperties->bPreserveSharpEdges;
|
2020-08-11 01:36:57 -04:00
|
|
|
Op->bAllowSeamCollapse = !SimplifyProperties->bPreserveSharpEdges;
|
2019-10-17 20:26:57 -04:00
|
|
|
Op->bReproject = SimplifyProperties->bReproject;
|
|
|
|
|
Op->SimplifierType = SimplifyProperties->SimplifierType;
|
2021-09-17 17:05:14 -04:00
|
|
|
Op->TargetCount = ( SimplifyProperties->TargetMode == ESimplifyTargetType::VertexCount) ? SimplifyProperties->TargetVertexCount : SimplifyProperties->TargetTriangleCount;
|
|
|
|
|
Op->MinimalPlanarAngleThresh = SimplifyProperties->MinimalAngleThreshold;
|
2019-10-17 20:26:57 -04:00
|
|
|
Op->TargetEdgeLength = SimplifyProperties->TargetEdgeLength;
|
|
|
|
|
Op->TargetMode = SimplifyProperties->TargetMode;
|
|
|
|
|
Op->TargetPercentage = SimplifyProperties->TargetPercentage;
|
2020-01-27 20:11:15 -05:00
|
|
|
Op->MeshBoundaryConstraint = (EEdgeRefineFlags)SimplifyProperties->MeshBoundaryConstraint;
|
|
|
|
|
Op->GroupBoundaryConstraint = (EEdgeRefineFlags)SimplifyProperties->GroupBoundaryConstraint;
|
|
|
|
|
Op->MaterialBoundaryConstraint = (EEdgeRefineFlags)SimplifyProperties->MaterialBoundaryConstraint;
|
2021-05-11 13:17:19 -04:00
|
|
|
Op->bGeometricDeviationConstraint = SimplifyProperties->bGeometricConstraint;
|
|
|
|
|
Op->GeometricTolerance = SimplifyProperties->GeometricTolerance;
|
2021-09-17 19:39:12 -04:00
|
|
|
Op->PolyEdgeAngleTolerance = SimplifyProperties->PolyEdgeAngleTolerance;
|
2021-06-15 17:05:40 -04:00
|
|
|
FTransform LocalToWorld = (FTransform)UE::ToolTarget::GetLocalToWorldTransform(Target);
|
2019-10-17 20:26:57 -04:00
|
|
|
Op->SetTransform(LocalToWorld);
|
|
|
|
|
|
|
|
|
|
Op->OriginalMeshDescription = OriginalMeshDescription;
|
|
|
|
|
Op->OriginalMesh = OriginalMesh;
|
|
|
|
|
Op->OriginalMeshSpatial = OriginalMeshSpatial;
|
|
|
|
|
|
|
|
|
|
IMeshReductionManagerModule& MeshReductionModule = FModuleManager::Get().LoadModuleChecked<IMeshReductionManagerModule>("MeshReductionInterface");
|
|
|
|
|
Op->MeshReduction = MeshReductionModule.GetStaticMeshReductionInterface();
|
|
|
|
|
|
|
|
|
|
return Op;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
|
2020-01-07 15:54:23 -05:00
|
|
|
void USimplifyMeshTool::OnPropertyModified(UObject* PropertySet, FProperty* Property)
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
2020-03-06 16:56:32 -05:00
|
|
|
if ( Property )
|
|
|
|
|
{
|
2021-06-15 17:05:40 -04:00
|
|
|
if ( Property->GetFName() == GET_MEMBER_NAME_CHECKED(USimplifyMeshToolProperties, bShowGroupColors) )
|
2020-03-06 16:56:32 -05:00
|
|
|
{
|
|
|
|
|
UpdateVisualization();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Preview->InvalidateResult();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 20:11:15 -05:00
|
|
|
void USimplifyMeshTool::UpdateVisualization()
|
|
|
|
|
{
|
|
|
|
|
FComponentMaterialSet MaterialSet;
|
|
|
|
|
if (SimplifyProperties->bShowGroupColors)
|
|
|
|
|
{
|
|
|
|
|
MaterialSet.Materials = {ToolSetupUtil::GetSelectionMaterial(GetToolManager())};
|
|
|
|
|
Preview->PreviewMesh->SetTriangleColorFunction([this](const FDynamicMesh3* Mesh, int TriangleID)
|
|
|
|
|
{
|
|
|
|
|
return LinearColors::SelectFColor(Mesh->GetTriangleGroup(TriangleID));
|
|
|
|
|
},
|
|
|
|
|
UPreviewMesh::ERenderUpdateMode::FastUpdate);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-06-15 17:05:40 -04:00
|
|
|
MaterialSet = UE::ToolTarget::GetMaterialSet(Target);
|
2020-01-27 20:11:15 -05:00
|
|
|
Preview->PreviewMesh->ClearTriangleColorFunction(UPreviewMesh::ERenderUpdateMode::FastUpdate);
|
|
|
|
|
}
|
|
|
|
|
Preview->ConfigureMaterials(MaterialSet.Materials,
|
|
|
|
|
ToolSetupUtil::GetDefaultWorkingMaterial(GetToolManager()));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#undef LOCTEXT_NAMESPACE
|