Files
UnrealEngineUWP/Engine/Plugins/Experimental/MeshModelingToolset/Source/ModelingComponents/Private/SimpleDynamicMeshComponent.cpp
jimmy andrews ff37523e51 fix crash on edit normals if mesh has duplicate triangles
#jira UE-91276
#rb none
#rnx

#ROBOMERGE-SOURCE: CL 12480856 in //UE4/Release-4.25/... via CL 12480860 via CL 12480865
#ROBOMERGE-BOT: RELEASE (Release-Engine-Staging -> Main) (v673-12478461)

[CL 12487324 by jimmy andrews in Main branch]
2020-03-30 12:04:36 -04:00

377 lines
9.0 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "SimpleDynamicMeshComponent.h"
#include "RenderingThread.h"
#include "RenderResource.h"
#include "PrimitiveViewRelevance.h"
#include "PrimitiveSceneProxy.h"
#include "VertexFactory.h"
#include "MaterialShared.h"
#include "Engine/CollisionProfile.h"
#include "Materials/Material.h"
#include "LocalVertexFactory.h"
#include "SceneManagement.h"
#include "DynamicMeshBuilder.h"
#include "EngineGlobals.h"
#include "Engine/Engine.h"
#include "StaticMeshResources.h"
#include "StaticMeshAttributes.h"
#include "DynamicMeshAttributeSet.h"
#include "MeshNormals.h"
#include "MeshDescriptionToDynamicMesh.h"
#include "DynamicMeshToMeshDescription.h"
#include "Changes/MeshVertexChange.h"
#include "Changes/MeshChange.h"
#include "DynamicMeshChangeTracker.h"
// default proxy for this component
#include "SimpleDynamicMeshSceneProxy.h"
USimpleDynamicMeshComponent::USimpleDynamicMeshComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = false;
SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
InitializeNewMesh();
}
void USimpleDynamicMeshComponent::InitializeMesh(FMeshDescription* MeshDescription)
{
FMeshDescriptionToDynamicMesh Converter;
Mesh->Clear();
Converter.Convert(MeshDescription, *Mesh);
if (TangentsType == EDynamicMeshTangentCalcType::ExternallyCalculated)
{
Converter.CopyTangents(MeshDescription, Mesh.Get(), Tangents);
}
NotifyMeshUpdated();
}
TUniquePtr<FDynamicMesh3> USimpleDynamicMeshComponent::ExtractMesh(bool bNotifyUpdate)
{
TUniquePtr<FDynamicMesh3> CurMesh = MoveTemp(Mesh);
InitializeNewMesh();
if (bNotifyUpdate)
{
NotifyMeshUpdated();
}
return CurMesh;
}
void USimpleDynamicMeshComponent::InitializeNewMesh()
{
Mesh = MakeUnique<FDynamicMesh3>();
// discard any attributes/etc initialized by default
Mesh->Clear();
Tangents.SetMesh(Mesh.Get());
}
void USimpleDynamicMeshComponent::Bake(FMeshDescription* MeshDescription, bool bHaveModifiedTopology, const FConversionToMeshDescriptionOptions& ConversionOptions)
{
FDynamicMeshToMeshDescription Converter(ConversionOptions);
if (bHaveModifiedTopology == false && Converter.HaveMatchingElementCounts(Mesh.Get(), MeshDescription))
{
if (ConversionOptions.bUpdatePositions)
{
Converter.Update(Mesh.Get(), *MeshDescription, ConversionOptions.bUpdateNormals, ConversionOptions.bUpdateUVs);
}
else
{
Converter.UpdateAttributes(Mesh.Get(), *MeshDescription, ConversionOptions.bUpdateNormals, ConversionOptions.bUpdateUVs);
}
}
else
{
Converter.Convert(Mesh.Get(), *MeshDescription);
//UE_LOG(LogTemp, Warning, TEXT("MeshDescription has %d instances"), MeshDescription->VertexInstances().Num());
}
}
FMeshTangentsf* USimpleDynamicMeshComponent::GetTangents()
{
if (TangentsType == EDynamicMeshTangentCalcType::NoTangents)
{
return nullptr;
}
if (TangentsType == EDynamicMeshTangentCalcType::AutoCalculated)
{
if (bTangentsValid == false && Mesh->HasAttributes())
{
FDynamicMeshUVOverlay* UVOverlay = Mesh->Attributes()->PrimaryUV();
FDynamicMeshNormalOverlay* NormalOverlay = Mesh->Attributes()->PrimaryNormals();
if (UVOverlay != nullptr && NormalOverlay != nullptr)
{
Tangents.ComputePerTriangleTangents(NormalOverlay, UVOverlay);
bTangentsValid = true;
}
}
return (bTangentsValid) ? &Tangents : nullptr;
}
// in this mode we assume the tangents are valid
check(TangentsType == EDynamicMeshTangentCalcType::ExternallyCalculated);
if (TangentsType == EDynamicMeshTangentCalcType::ExternallyCalculated)
{
// if you hit this, you did not request ExternallyCalculated tangents before initializing this PreviewMesh
check(Tangents.GetTangents().Num() > 0)
}
return &Tangents;
}
void USimpleDynamicMeshComponent::SetDrawOnTop(bool bSet)
{
bDrawOnTop = bSet;
bUseEditorCompositing = bSet;
}
void USimpleDynamicMeshComponent::NotifyMeshUpdated()
{
// Need to recreate scene proxy to send it over
MarkRenderStateDirty();
LocalBounds = Mesh->GetCachedBounds();
UpdateBounds();
if (TangentsType != EDynamicMeshTangentCalcType::ExternallyCalculated)
{
bTangentsValid = false;
}
}
void USimpleDynamicMeshComponent::FastNotifyColorsUpdated()
{
FSimpleDynamicMeshSceneProxy* Proxy = GetCurrentSceneProxy();
if (Proxy != nullptr)
{
if (TriangleColorFunc != nullptr && Proxy->bUsePerTriangleColor == false )
{
Proxy->bUsePerTriangleColor = true;
Proxy->PerTriangleColorFunc = [this](const FDynamicMesh3* MeshIn, int TriangleID) { return GetTriangleColor(MeshIn, TriangleID); };
}
else if (TriangleColorFunc == nullptr && Proxy->bUsePerTriangleColor == true)
{
Proxy->bUsePerTriangleColor = false;
Proxy->PerTriangleColorFunc = nullptr;
}
Proxy->FastUpdateVertices(false, false, true, false);
//MarkRenderDynamicDataDirty();
}
else
{
NotifyMeshUpdated();
}
}
void USimpleDynamicMeshComponent::FastNotifyPositionsUpdated(bool bNormals, bool bColors, bool bUVs)
{
if (GetCurrentSceneProxy() != nullptr)
{
GetCurrentSceneProxy()->FastUpdateVertices(true, bNormals, bColors, bUVs);
//MarkRenderDynamicDataDirty();
MarkRenderTransformDirty();
LocalBounds = Mesh->GetCachedBounds();
UpdateBounds();
}
else
{
NotifyMeshUpdated();
}
}
void USimpleDynamicMeshComponent::FastNotifyUVsUpdated()
{
if (GetCurrentSceneProxy() != nullptr)
{
GetCurrentSceneProxy()->FastUpdateVertices(false, false, false, true);
//MarkRenderDynamicDataDirty();
MarkRenderTransformDirty();
LocalBounds = Mesh->GetCachedBounds();
UpdateBounds();
}
else
{
NotifyMeshUpdated();
}
}
void USimpleDynamicMeshComponent::FastNotifySecondaryTrianglesChanged()
{
if (GetCurrentSceneProxy() != nullptr)
{
GetCurrentSceneProxy()->FastUpdateAllIndexBuffers();
}
else
{
NotifyMeshUpdated();
}
}
FPrimitiveSceneProxy* USimpleDynamicMeshComponent::CreateSceneProxy()
{
check(GetCurrentSceneProxy() == nullptr);
FSimpleDynamicMeshSceneProxy* NewProxy = nullptr;
if (Mesh->TriangleCount() > 0)
{
NewProxy = new FSimpleDynamicMeshSceneProxy(this);
if (TriangleColorFunc)
{
NewProxy->bUsePerTriangleColor = true;
NewProxy->PerTriangleColorFunc = [this](const FDynamicMesh3* MeshIn, int TriangleID) { return GetTriangleColor(MeshIn, TriangleID); };
}
if (SecondaryTriFilterFunc)
{
NewProxy->bUseSecondaryTriBuffers = true;
NewProxy->SecondaryTriFilterFunc = [this](const FDynamicMesh3* MeshIn, int32 TriangleID)
{
return (SecondaryTriFilterFunc) ? SecondaryTriFilterFunc(MeshIn, TriangleID) : false;
};
}
NewProxy->Initialize();
}
return NewProxy;
}
void USimpleDynamicMeshComponent::NotifyMaterialSetUpdated()
{
if (GetCurrentSceneProxy() != nullptr)
{
GetCurrentSceneProxy()->UpdatedReferencedMaterials();
}
}
void USimpleDynamicMeshComponent::EnableSecondaryTriangleBuffers(TUniqueFunction<bool(const FDynamicMesh3*, int32)> SecondaryTriFilterFuncIn)
{
SecondaryTriFilterFunc = MoveTemp(SecondaryTriFilterFuncIn);
NotifyMeshUpdated();
}
void USimpleDynamicMeshComponent::DisableSecondaryTriangleBuffers()
{
SecondaryTriFilterFunc = nullptr;
NotifyMeshUpdated();
}
FColor USimpleDynamicMeshComponent::GetTriangleColor(const FDynamicMesh3* MeshIn, int TriangleID)
{
if (TriangleColorFunc)
{
return TriangleColorFunc(MeshIn, TriangleID);
}
else
{
return (TriangleID % 2 == 0) ? FColor::Red : FColor::White;
}
}
FBoxSphereBounds USimpleDynamicMeshComponent::CalcBounds(const FTransform& LocalToWorld) const
{
// can get a tighter box by calculating in world space, but we care more about performance
FBox LocalBoundingBox = (FBox)LocalBounds;
FBoxSphereBounds Ret(LocalBoundingBox.TransformBy(LocalToWorld));
Ret.BoxExtent *= BoundsScale;
Ret.SphereRadius *= BoundsScale;
return Ret;
}
void USimpleDynamicMeshComponent::ApplyChange(const FMeshVertexChange* Change, bool bRevert)
{
int32 NV = Change->Vertices.Num();
const TArray<FVector3d>& Positions = (bRevert) ? Change->OldPositions : Change->NewPositions;
for (int32 k = 0; k < NV; ++k)
{
int32 vid = Change->Vertices[k];
if (Mesh->IsVertex(vid))
{
Mesh->SetVertex(vid, Positions[k]);
}
}
if (Change->bHaveOverlayNormals && Mesh->HasAttributes() && Mesh->Attributes()->PrimaryNormals() )
{
FDynamicMeshNormalOverlay* Overlay = Mesh->Attributes()->PrimaryNormals();
int32 NumNormals = Change->Normals.Num();
const TArray<FVector3f>& UseNormals = (bRevert) ? Change->OldNormals : Change->NewNormals;
for (int32 k = 0; k < NumNormals; ++k)
{
int32 elemid = Change->Normals[k];
if (Overlay->IsElement(elemid))
{
Overlay->SetElement(elemid, UseNormals[k]);
}
}
}
NotifyMeshUpdated();
OnMeshChanged.Broadcast();
}
void USimpleDynamicMeshComponent::ApplyChange(const FMeshChange* Change, bool bRevert)
{
Change->DynamicMeshChange->Apply(Mesh.Get(), bRevert);
NotifyMeshUpdated();
OnMeshChanged.Broadcast();
}
void USimpleDynamicMeshComponent::ApplyChange(const FMeshReplacementChange* Change, bool bRevert)
{
Mesh->Copy(*Change->GetMesh(bRevert));
NotifyMeshUpdated();
OnMeshChanged.Broadcast();
}