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

402 lines
13 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "CutMeshWithMeshTool.h"
#include "CompositionOps/BooleanMeshesOp.h"
#include "ToolSetupUtil.h"
Gizmos: refactor Modeling Mode gizmo creation out of InteractiveGizmoManager. Editor will use other "default" transform gizmo implementations, and so the UTransformGizmo creation helper functions do not belong in GizmoManager. Instead a UTransformGizmoContextObject now provides this functionality. ModelingToolsEditorMode (and any other modes/systems that want to use these gizmo convenience functions) creates an instance of UTransformGizmoContextObject and registers it with the ContextObjectStore. Calling code can spawn a new UTransformGizmo by looking this object up in the ContextStore and calling it's helper functions. Static versions of the helper functions in the UE::TransformGizmoUtil:: namespace provide a single-line interface that replaces the previous GizmoManager call sites in the MeshModelingTools library. IntervalGizmo is now just registered and unregistered as needed by the MeshSpaceDeformerTool, as this is the only place it is currently used. Previous implementation in InteractiveGizmoManager is left intact as there are a few uses outside of MeshModelingTools that need to be cleaned up before it can be deleted. UTransformGizmo now requires it's Builder to tell it which sub-gizmo identifier strings to pass to the GizmoManager to create axis/plane/rotation sub-gizmos (and the code that registers the Builder must then provide these strings). This cleans up previous explicit references to UInteractiveGizmoManager static strings from UTransformGizmo. #rb Christina.TempelaarL, david.hill #rnx #jira none [CL 16409673 by Ryan Schmidt in ue5-main branch]
2021-05-20 16:39:39 -04:00
#include "BaseGizmos/TransformGizmoUtil.h"
#include "Selection/ToolSelectionUtil.h"
#include "ModelingObjectsCreationAPI.h"
#include "DynamicMesh/DynamicMesh3.h"
#include "DynamicMesh/MeshTransforms.h"
#include "MeshDescriptionToDynamicMesh.h"
#include "DynamicMeshToMeshDescription.h"
#include "Async/Async.h"
#include "TargetInterfaces/MaterialProvider.h"
#include "TargetInterfaces/MeshDescriptionCommitter.h"
#include "TargetInterfaces/MeshDescriptionProvider.h"
#include "TargetInterfaces/PrimitiveComponentBackedTarget.h"
#include "TargetInterfaces/AssetBackedTarget.h"
#include "ToolTargetManager.h"
ModelingTools: add support for creating Volumes directly from DrawPolygon, DrawRevolve, DrawPolyPath, and AddPrimitive, CombineMeshes, CutMeshWithMesh, PlaneCut, BaseCreateFromSelected Tools. Improve support for Editing volumes, eg handling mesh/volume interactions, and add configurable auto-simplification for volumes to avoid painful Editor hangs. - Move ToolTarget implementations, DynamicMeshToVolume to ModelingComponentsEditorOnly module - move VolumeToDynamicMesh, DynamicMeshProvider/Commiter interfaces to ModelingComponents module - Add UCreateMeshObjectTypeProperties property set to expose mesh/volume options - Add FCreateMeshObjectParams::TypeHintClass to allow AVolume type (or other UClass hints) to be passed to creation APIs - Add UE::ToolTarget::ConfigureCreateMeshObjectParams() util function in ModelingToolTargetUtil, tries to determine output type in a FCreateMeshObjectParams based on input ToolTarget - Add UEditorModelingObjectsCreationAPI::CreateVolume() implementation - Add UEditorModelingObjectsCreationAPI::FilterMaterials() that strips out any internal materials and replaces with WorldGridMaterial. This occurs when (eg) subtracting a Volume from a StaticMesh, because the temporary volume mesh gets assigned internal materials, but the Tools don't know this. Use in EditorModelingObjectsCreationAPI when creating new objects. UStaticMeshComponentToolTarget also does this filtering in ::CommitMaterialSetUpdate(). - Add ::ComponentTypeSupportsCollision() function to ComponentCollisionUtil, use to avoid checks/ensures for Volume targets - Add support for automatic mesh simplification in DynamicMeshToVolume. Add CVar to VolumeDynamicMeshToolTarget.h to control max triangle count (default 500). Apply auto-simplify when creating or updating an AVolume. This prevents the Editor from blocking for long periods on meshes that are too high-res for volumes (even 500 is quite high). - DynamicMeshToVolume now emits polygroup-faces that contain holes (ie multiple boundary loops) as a set of triangles, rather than emitting separate overlapping faces for each boundary loop #rb none #rnx #jira none #preflight 60ba50632c42ea0001cb54c5 [CL 16561742 by Ryan Schmidt in ue5-main branch]
2021-06-04 16:04:03 -04:00
#include "ModelingToolTargetUtil.h"
#include "ExplicitUseGeometryMathTypes.h" // using UE::Geometry::(math types)
using namespace UE::Geometry;
namespace
{
// probably should be something defined for the whole tool framework...
#if WITH_EDITOR
static EAsyncExecution CutMeshWithMeshToolAsyncExecTarget = EAsyncExecution::LargeThreadPool;
#else
static EAsyncExecution CutMeshWithMeshToolAsyncExecTarget = EAsyncExecution::ThreadPool;
#endif
}
#define LOCTEXT_NAMESPACE "UCutMeshWithMeshTool"
void UCutMeshWithMeshTool::SetupProperties()
{
Super::SetupProperties();
CutProperties = NewObject<UCutMeshWithMeshToolProperties>(this);
CutProperties->RestoreProperties(this);
AddToolPropertySource(CutProperties);
SetToolDisplayName(LOCTEXT("CutMeshWithMeshToolName", "Cut With Mesh"));
GetToolManager()->DisplayMessage(
LOCTEXT("OnStartTool", "Cut first mesh with second. Use the transform gizmos to tweak the positions of the input objects (can help to resolve errors/failures)"),
EToolMessageLevel::UserNotification);
// create intersection preview mesh object
IntersectPreviewMesh = NewObject<UPreviewMesh>(this);
IntersectPreviewMesh->CreateInWorld(TargetWorld, FTransform::Identity);
IntersectPreviewMesh->SetVisible(true);
//IntersectPreviewMesh->SetMaterial(MaterialProperties->Material.Get());
//IntersectPreviewMesh->SetMaterial(ToolSetupUtil::GetDefaultMaterial(nullptr));
IntersectPreviewMesh->SetMaterial(ToolSetupUtil::GetDefaultBrushVolumeMaterial(GetToolManager()));
}
void UCutMeshWithMeshTool::SaveProperties()
{
Super::SaveProperties();
CutProperties->SaveProperties(this);
IntersectPreviewMesh->Disconnect();
}
void UCutMeshWithMeshTool::ConvertInputsAndSetPreviewMaterials(bool bSetPreviewMesh)
{
// disable output options
// (this property set is not registered yet in SetupProperties() above)
SetToolPropertySourceEnabled(HandleSourcesProperties, false);
FComponentMaterialSet AllMaterialSet;
TMap<UMaterialInterface*, int> KnownMaterials;
TArray<TArray<int>> MaterialRemap; MaterialRemap.SetNum(Targets.Num());
if (!CutProperties->bOnlyUseFirstMeshMaterials)
{
for (int ComponentIdx = 0; ComponentIdx < Targets.Num(); ComponentIdx++)
{
FComponentMaterialSet ComponentMaterialSet;
TargetMaterialInterface(ComponentIdx)->GetMaterialSet(ComponentMaterialSet);
for (UMaterialInterface* Mat : ComponentMaterialSet.Materials)
{
int* FoundMatIdx = KnownMaterials.Find(Mat);
int MatIdx;
if (FoundMatIdx)
{
MatIdx = *FoundMatIdx;
}
else
{
MatIdx = AllMaterialSet.Materials.Add(Mat);
KnownMaterials.Add(Mat, MatIdx);
}
MaterialRemap[ComponentIdx].Add(MatIdx);
}
}
}
else
{
TargetMaterialInterface(0)->GetMaterialSet(AllMaterialSet);
for (int MatIdx = 0; MatIdx < AllMaterialSet.Materials.Num(); MatIdx++)
{
MaterialRemap[0].Add(MatIdx);
}
for (int ComponentIdx = 1; ComponentIdx < Targets.Num(); ComponentIdx++)
{
MaterialRemap[ComponentIdx].Init(0, TargetMaterialInterface(ComponentIdx)->GetNumMaterials());
}
}
for (int ComponentIdx = 0; ComponentIdx < Targets.Num(); ComponentIdx++)
{
TSharedPtr<FDynamicMesh3, ESPMode::ThreadSafe> Mesh = MakeShared<FDynamicMesh3, ESPMode::ThreadSafe>();
FMeshDescriptionToDynamicMesh Converter;
Converter.Convert(TargetMeshProviderInterface(ComponentIdx)->GetMeshDescription(), *Mesh);
// ensure materials and attributes are always enabled
Mesh->EnableAttributes();
Mesh->Attributes()->EnableMaterialID();
FDynamicMeshMaterialAttribute* MaterialIDs = Mesh->Attributes()->GetMaterialID();
for (int TID : Mesh->TriangleIndicesItr())
{
MaterialIDs->SetValue(TID, MaterialRemap[ComponentIdx][MaterialIDs->GetValue(TID)]);
}
if (ComponentIdx == 0)
{
OriginalTargetMesh = Mesh;
}
else
{
OriginalCuttingMesh = Mesh;
}
}
Preview->ConfigureMaterials(AllMaterialSet.Materials, ToolSetupUtil::GetDefaultWorkingMaterial(GetToolManager()));
// check if we have the same mesh on both inputs
ModelingTools: add support for creating Volumes directly from DrawPolygon, DrawRevolve, DrawPolyPath, and AddPrimitive, CombineMeshes, CutMeshWithMesh, PlaneCut, BaseCreateFromSelected Tools. Improve support for Editing volumes, eg handling mesh/volume interactions, and add configurable auto-simplification for volumes to avoid painful Editor hangs. - Move ToolTarget implementations, DynamicMeshToVolume to ModelingComponentsEditorOnly module - move VolumeToDynamicMesh, DynamicMeshProvider/Commiter interfaces to ModelingComponents module - Add UCreateMeshObjectTypeProperties property set to expose mesh/volume options - Add FCreateMeshObjectParams::TypeHintClass to allow AVolume type (or other UClass hints) to be passed to creation APIs - Add UE::ToolTarget::ConfigureCreateMeshObjectParams() util function in ModelingToolTargetUtil, tries to determine output type in a FCreateMeshObjectParams based on input ToolTarget - Add UEditorModelingObjectsCreationAPI::CreateVolume() implementation - Add UEditorModelingObjectsCreationAPI::FilterMaterials() that strips out any internal materials and replaces with WorldGridMaterial. This occurs when (eg) subtracting a Volume from a StaticMesh, because the temporary volume mesh gets assigned internal materials, but the Tools don't know this. Use in EditorModelingObjectsCreationAPI when creating new objects. UStaticMeshComponentToolTarget also does this filtering in ::CommitMaterialSetUpdate(). - Add ::ComponentTypeSupportsCollision() function to ComponentCollisionUtil, use to avoid checks/ensures for Volume targets - Add support for automatic mesh simplification in DynamicMeshToVolume. Add CVar to VolumeDynamicMeshToolTarget.h to control max triangle count (default 500). Apply auto-simplify when creating or updating an AVolume. This prevents the Editor from blocking for long periods on meshes that are too high-res for volumes (even 500 is quite high). - DynamicMeshToVolume now emits polygroup-faces that contain holes (ie multiple boundary loops) as a set of triangles, rather than emitting separate overlapping faces for each boundary loop #rb none #rnx #jira none #preflight 60ba50632c42ea0001cb54c5 [CL 16561742 by Ryan Schmidt in ue5-main branch]
2021-06-04 16:04:03 -04:00
if (Cast<IAssetBackedTarget>(Targets[0]) != nullptr && Cast<IAssetBackedTarget>(Targets[0])->HasSameSourceData(Targets[1]))
{
GetToolManager()->DisplayMessage(
LOCTEXT("SameSourceError", "WARNING: Target Mesh has same Asset as Cutting Mesh, both inputs will be affected"),
EToolMessageLevel::UserWarning);
}
}
class FCutMeshWithMeshOp : public FDynamicMeshOperator
{
public:
virtual ~FCutMeshWithMeshOp() {}
TSharedPtr<const FDynamicMesh3, ESPMode::ThreadSafe> TargetMesh;
FTransform TargetMeshTransform;
TSharedPtr<const FDynamicMesh3, ESPMode::ThreadSafe> CuttingMesh;
FTransform CuttingMeshTransform;
bool bAttemptToFixHoles = true;
bool bCollapseExtraEdges = true;
virtual void CalculateResult(FProgressCancel* Progress)
{
TUniquePtr<FBooleanMeshesOp> SubtractOp = MakeUnique<FBooleanMeshesOp>();
SubtractOp->CSGOperation = ECSGOperation::DifferenceAB;
SubtractOp->bAttemptFixHoles = bAttemptToFixHoles;
SubtractOp->bTryCollapseExtraEdges = bCollapseExtraEdges;
SubtractOp->Meshes.Add(TargetMesh);
SubtractOp->Transforms.Add(TargetMeshTransform);
SubtractOp->Meshes.Add(CuttingMesh);
SubtractOp->Transforms.Add(CuttingMeshTransform);
TUniquePtr<FBooleanMeshesOp> IntersectOp = MakeUnique<FBooleanMeshesOp>();
IntersectOp->CSGOperation = ECSGOperation::Intersect;
IntersectOp->bAttemptFixHoles = bAttemptToFixHoles;
IntersectOp->bTryCollapseExtraEdges = bCollapseExtraEdges;
IntersectOp->Meshes.Add(TargetMesh);
IntersectOp->Transforms.Add(TargetMeshTransform);
IntersectOp->Meshes.Add(CuttingMesh);
IntersectOp->Transforms.Add(CuttingMeshTransform);
TFuture<void> SubtractFuture = Async(CutMeshWithMeshToolAsyncExecTarget, [&]()
{
SubtractOp->CalculateResult(Progress);
});
TFuture<void> IntersectFuture = Async(CutMeshWithMeshToolAsyncExecTarget, [&]()
{
IntersectOp->CalculateResult(Progress);
});
SubtractFuture.Wait();
IntersectFuture.Wait();
this->ResultMesh = SubtractOp->ExtractResult();
SetResultTransform(SubtractOp->GetResultTransform());
IntersectMesh = IntersectOp->ExtractResult();
CreatedSubtractBoundaryEdges = SubtractOp->GetCreatedBoundaryEdges();
CreatedIntersectBoundaryEdges = IntersectOp->GetCreatedBoundaryEdges();
}
TUniquePtr<FDynamicMesh3> IntersectMesh;
TArray<int> CreatedSubtractBoundaryEdges;
TArray<int> CreatedIntersectBoundaryEdges;
};
void UCutMeshWithMeshTool::SetPreviewCallbacks()
{
DrawnLineSet = NewObject<ULineSetComponent>(Preview->PreviewMesh->GetRootComponent());
DrawnLineSet->SetupAttachment(Preview->PreviewMesh->GetRootComponent());
DrawnLineSet->SetLineMaterial(ToolSetupUtil::GetDefaultLineComponentMaterial(GetToolManager()));
DrawnLineSet->RegisterComponent();
Preview->OnOpCompleted.AddLambda(
[this](const FDynamicMeshOperator* Op)
{
const FCutMeshWithMeshOp* CuttingOp = (const FCutMeshWithMeshOp*)(Op);
CreatedSubtractBoundaryEdges = CuttingOp->CreatedSubtractBoundaryEdges;
CreatedIntersectBoundaryEdges = CuttingOp->CreatedIntersectBoundaryEdges;
IntersectionMesh = *CuttingOp->IntersectMesh; // cannot steal this here because it is const...
IntersectPreviewMesh->UpdatePreview(&IntersectionMesh);
IntersectPreviewMesh->SetTransform((FTransform)Op->GetResultTransform());
}
);
Preview->OnMeshUpdated.AddLambda(
[this](const UMeshOpPreviewWithBackgroundCompute*)
{
GetToolManager()->PostInvalidation();
UpdateVisualization();
}
);
}
void UCutMeshWithMeshTool::UpdateVisualization()
{
FColor BoundaryEdgeColor(240, 15, 15);
float BoundaryEdgeThickness = 2.0;
float BoundaryEdgeDepthBias = 2.0f;
DrawnLineSet->Clear();
if (CutProperties->bShowNewBoundaryEdges)
{
const FDynamicMesh3* TargetMesh = Preview->PreviewMesh->GetPreviewDynamicMesh();
FVector3d A, B;
for (int EID : CreatedSubtractBoundaryEdges)
{
TargetMesh->GetEdgeV(EID, A, B);
DrawnLineSet->AddLine((FVector)A, (FVector)B, BoundaryEdgeColor, BoundaryEdgeThickness, BoundaryEdgeDepthBias);
}
for (int EID : CreatedIntersectBoundaryEdges)
{
IntersectionMesh.GetEdgeV(EID, A, B);
DrawnLineSet->AddLine((FVector)A, (FVector)B, BoundaryEdgeColor, BoundaryEdgeThickness, BoundaryEdgeDepthBias);
}
}
}
TUniquePtr<FDynamicMeshOperator> UCutMeshWithMeshTool::MakeNewOperator()
{
TUniquePtr<FCutMeshWithMeshOp> CuttingOp = MakeUnique<FCutMeshWithMeshOp>();
CuttingOp->TargetMesh = OriginalTargetMesh;
CuttingOp->TargetMeshTransform = TransformProxies[0]->GetTransform();
CuttingOp->CuttingMesh = OriginalCuttingMesh;
CuttingOp->CuttingMeshTransform = TransformProxies[1]->GetTransform();
CuttingOp->bAttemptToFixHoles = CutProperties->bAttemptFixHoles;
CuttingOp->bCollapseExtraEdges = CutProperties->bCollapseExtraEdges;
return CuttingOp;
}
void UCutMeshWithMeshTool::OnPropertyModified(UObject* PropertySet, FProperty* Property)
{
if (Property && (Property->GetFName() == GET_MEMBER_NAME_CHECKED(UCutMeshWithMeshToolProperties, bOnlyUseFirstMeshMaterials)))
{
if (!AreAllTargetsValid())
{
GetToolManager()->DisplayMessage(LOCTEXT("InvalidTargets", "Target meshes are no longer valid"), EToolMessageLevel::UserWarning);
return;
}
ConvertInputsAndSetPreviewMaterials(false);
Preview->InvalidateResult();
}
else if (Property && (Property->GetFName() == GET_MEMBER_NAME_CHECKED(UCutMeshWithMeshToolProperties, bShowNewBoundaryEdges)))
{
GetToolManager()->PostInvalidation();
UpdateVisualization();
}
else
{
Super::OnPropertyModified(PropertySet, Property);
}
}
FString UCutMeshWithMeshTool::GetCreatedAssetName() const
{
return TEXT("Boolean");
}
FText UCutMeshWithMeshTool::GetActionName() const
{
return LOCTEXT("CutMeshWithMeshActionName", "Boolean Meshes");
}
void UCutMeshWithMeshTool::Shutdown(EToolShutdownType ShutdownType)
{
SaveProperties();
HandleSourcesProperties->SaveProperties(this);
TransformProperties->SaveProperties(this);
FDynamicMeshOpResult OpResult = Preview->Shutdown();
// Restore (unhide) the source meshes
for ( int32 ci = 0; ci < Targets.Num(); ++ci)
{
TargetComponentInterface(ci)->SetOwnerVisibility(true);
}
if (ShutdownType == EToolShutdownType::Accept)
{
GetToolManager()->BeginUndoTransaction(GetActionName());
TArray<AActor*> SelectActors;
FComponentMaterialSet MaterialSet;
MaterialSet.Materials = GetOutputMaterials();
// update subtract asset
IPrimitiveComponentBackedTarget* UpdateTarget = TargetComponentInterface(0);
FTransform3d TargetToWorld = (FTransform3d)UpdateTarget->GetWorldTransform();
{
if (OpResult.Mesh->TriangleCount() > 0)
{
MeshTransforms::ApplyTransform(*OpResult.Mesh, OpResult.Transform);
MeshTransforms::ApplyTransformInverse(*OpResult.Mesh, TargetToWorld);
TargetMeshCommitterInterface(0)->CommitMeshDescription([&](const IMeshDescriptionCommitter::FCommitterParams& CommitParams)
{
FDynamicMeshToMeshDescription Converter;
Converter.Convert(OpResult.Mesh.Get(), *CommitParams.MeshDescriptionOut);
});
TargetMaterialInterface(0)->CommitMaterialSetUpdate(MaterialSet, true);
}
}
SelectActors.Add(UpdateTarget->GetOwnerActor());
// create intersection asset
if ( IntersectionMesh.TriangleCount() > 0)
{
MeshTransforms::ApplyTransform(IntersectionMesh, OpResult.Transform);
MeshTransforms::ApplyTransformInverse(IntersectionMesh, TargetToWorld);
FTransform3d NewTransform = TargetToWorld;
FString CurName = UE::Modeling::GetComponentAssetBaseName(UpdateTarget->GetOwnerComponent());
FString UseBaseName = FString::Printf(TEXT("%s_%s"), *CurName, TEXT("CutPart") );
FCreateMeshObjectParams NewMeshObjectParams;
NewMeshObjectParams.TargetWorld = TargetWorld;
NewMeshObjectParams.Transform = (FTransform)NewTransform;
NewMeshObjectParams.BaseName = UseBaseName;
NewMeshObjectParams.Materials = GetOutputMaterials();
NewMeshObjectParams.SetMesh(&IntersectionMesh);
ModelingTools: add support for creating Volumes directly from DrawPolygon, DrawRevolve, DrawPolyPath, and AddPrimitive, CombineMeshes, CutMeshWithMesh, PlaneCut, BaseCreateFromSelected Tools. Improve support for Editing volumes, eg handling mesh/volume interactions, and add configurable auto-simplification for volumes to avoid painful Editor hangs. - Move ToolTarget implementations, DynamicMeshToVolume to ModelingComponentsEditorOnly module - move VolumeToDynamicMesh, DynamicMeshProvider/Commiter interfaces to ModelingComponents module - Add UCreateMeshObjectTypeProperties property set to expose mesh/volume options - Add FCreateMeshObjectParams::TypeHintClass to allow AVolume type (or other UClass hints) to be passed to creation APIs - Add UE::ToolTarget::ConfigureCreateMeshObjectParams() util function in ModelingToolTargetUtil, tries to determine output type in a FCreateMeshObjectParams based on input ToolTarget - Add UEditorModelingObjectsCreationAPI::CreateVolume() implementation - Add UEditorModelingObjectsCreationAPI::FilterMaterials() that strips out any internal materials and replaces with WorldGridMaterial. This occurs when (eg) subtracting a Volume from a StaticMesh, because the temporary volume mesh gets assigned internal materials, but the Tools don't know this. Use in EditorModelingObjectsCreationAPI when creating new objects. UStaticMeshComponentToolTarget also does this filtering in ::CommitMaterialSetUpdate(). - Add ::ComponentTypeSupportsCollision() function to ComponentCollisionUtil, use to avoid checks/ensures for Volume targets - Add support for automatic mesh simplification in DynamicMeshToVolume. Add CVar to VolumeDynamicMeshToolTarget.h to control max triangle count (default 500). Apply auto-simplify when creating or updating an AVolume. This prevents the Editor from blocking for long periods on meshes that are too high-res for volumes (even 500 is quite high). - DynamicMeshToVolume now emits polygroup-faces that contain holes (ie multiple boundary loops) as a set of triangles, rather than emitting separate overlapping faces for each boundary loop #rb none #rnx #jira none #preflight 60ba50632c42ea0001cb54c5 [CL 16561742 by Ryan Schmidt in ue5-main branch]
2021-06-04 16:04:03 -04:00
UE::ToolTarget::ConfigureCreateMeshObjectParams(Targets[0], NewMeshObjectParams);
FCreateMeshObjectResult Result = UE::Modeling::CreateMeshObject(GetToolManager(), MoveTemp(NewMeshObjectParams));
if (Result.IsOK() && Result.NewActor != nullptr)
{
SelectActors.Add(Result.NewActor);
}
}
ToolSelectionUtil::SetNewActorSelection(GetToolManager(), SelectActors);
GetToolManager()->EndUndoTransaction();
}
UInteractiveGizmoManager* GizmoManager = GetToolManager()->GetPairedGizmoManager();
GizmoManager->DestroyAllGizmosByOwner(this);
}
#undef LOCTEXT_NAMESPACE