2019-12-26 14:45:42 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-10-01 20:41:42 -04:00
|
|
|
|
|
|
|
|
#include "StaticMeshDescription.h"
|
|
|
|
|
#include "StaticMeshAttributes.h"
|
|
|
|
|
#include "StaticMeshOperations.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UStaticMeshDescription::RegisterAttributes()
|
|
|
|
|
{
|
2020-11-11 11:25:09 -04:00
|
|
|
RequiredAttributes = MakeUnique<FStaticMeshAttributes>(GetMeshDescription());
|
2019-10-01 20:41:42 -04:00
|
|
|
RequiredAttributes->Register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FVector2D UStaticMeshDescription::GetVertexInstanceUV(FVertexInstanceID VertexInstanceID, int32 UVIndex) const
|
|
|
|
|
{
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().IsVertexInstanceValid(VertexInstanceID))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("GetVertexInstanceUV: VertexInstanceID %d doesn't exist."), VertexInstanceID.GetValue());
|
|
|
|
|
return FVector2D::ZeroVector;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().VertexInstanceAttributes().HasAttribute(MeshAttribute::VertexInstance::TextureCoordinate))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("GetVertexInstanceUV: VertexInstanceAttribute TextureCoordinate doesn't exist."));
|
|
|
|
|
return FVector2D::ZeroVector;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
return (FVector2D)GetMeshDescription().VertexInstanceAttributes().GetAttribute<FVector2f>(VertexInstanceID, MeshAttribute::VertexInstance::TextureCoordinate, UVIndex);
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UStaticMeshDescription::SetVertexInstanceUV(FVertexInstanceID VertexInstanceID, FVector2D UV, int32 UVIndex)
|
|
|
|
|
{
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().IsVertexInstanceValid(VertexInstanceID))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("SetVertexInstanceUV: VertexInstanceID %d doesn't exist."), VertexInstanceID.GetValue());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().VertexInstanceAttributes().HasAttribute(MeshAttribute::VertexInstance::TextureCoordinate))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("SetVertexInstanceUV: VertexInstanceAttribute TextureCoordinate doesn't exist."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
GetMeshDescription().VertexInstanceAttributes().SetAttribute(VertexInstanceID, MeshAttribute::VertexInstance::TextureCoordinate, UVIndex, FVector2f(UV));
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UStaticMeshDescription::SetPolygonGroupMaterialSlotName(FPolygonGroupID PolygonGroupID, const FName& SlotName)
|
|
|
|
|
{
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().IsPolygonGroupValid(PolygonGroupID))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("SetPolygonGroupMaterialSlotName: PolygonGroupID %d doesn't exist."), PolygonGroupID.GetValue());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 11:25:09 -04:00
|
|
|
if (!GetMeshDescription().PolygonGroupAttributes().HasAttribute(MeshAttribute::PolygonGroup::ImportedMaterialSlotName))
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
UE_LOG(LogMeshDescription, Warning, TEXT("SetPolygonGroupMaterialSlotName: PolygonGroupAttribute ImportedMaterialSlotName doesn't exist."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 11:25:09 -04:00
|
|
|
GetMeshDescription().PolygonGroupAttributes().SetAttribute(PolygonGroupID, MeshAttribute::PolygonGroup::ImportedMaterialSlotName, 0, SlotName);
|
2019-10-01 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UStaticMeshDescription::CreateCube(FVector Center, FVector HalfExtents, FPolygonGroupID PolygonGroup,
|
|
|
|
|
FPolygonID& PolygonID_PlusX,
|
|
|
|
|
FPolygonID& PolygonID_MinusX,
|
|
|
|
|
FPolygonID& PolygonID_PlusY,
|
|
|
|
|
FPolygonID& PolygonID_MinusY,
|
|
|
|
|
FPolygonID& PolygonID_PlusZ,
|
|
|
|
|
FPolygonID& PolygonID_MinusZ)
|
|
|
|
|
{
|
2020-11-11 11:25:09 -04:00
|
|
|
FMeshDescription& MeshDescription = GetMeshDescription();
|
2021-05-05 15:07:25 -04:00
|
|
|
TVertexAttributesRef<FVector3f> Positions = GetVertexPositions();
|
2019-10-01 20:41:42 -04:00
|
|
|
|
|
|
|
|
FVertexID VertexIDs[8];
|
|
|
|
|
|
|
|
|
|
MeshDescription.ReserveNewVertices(8);
|
|
|
|
|
for (int32 Index = 0; Index < 8; ++Index)
|
|
|
|
|
{
|
|
|
|
|
VertexIDs[Index] = MeshDescription.CreateVertex();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 07:59:31 -05:00
|
|
|
const FVector3f CenterPlusHalfExtent(Center + HalfExtents); //LWC_TODO: Precision loss
|
|
|
|
|
|
|
|
|
|
Positions[VertexIDs[0]] = CenterPlusHalfExtent * FVector3f( 1.0f, -1.0f, 1.0f);
|
|
|
|
|
Positions[VertexIDs[1]] = CenterPlusHalfExtent * FVector3f( 1.0f, 1.0f, 1.0f);
|
|
|
|
|
Positions[VertexIDs[2]] = CenterPlusHalfExtent * FVector3f(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
Positions[VertexIDs[3]] = CenterPlusHalfExtent * FVector3f(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
Positions[VertexIDs[4]] = CenterPlusHalfExtent * FVector3f(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
Positions[VertexIDs[5]] = CenterPlusHalfExtent * FVector3f(-1.0f, -1.0f, -1.0f);
|
|
|
|
|
Positions[VertexIDs[6]] = CenterPlusHalfExtent * FVector3f( 1.0f, -1.0f, -1.0f);
|
|
|
|
|
Positions[VertexIDs[7]] = CenterPlusHalfExtent * FVector3f( 1.0f, 1.0f, -1.0f);
|
2019-10-01 20:41:42 -04:00
|
|
|
|
2020-11-11 11:25:09 -04:00
|
|
|
auto MakePolygon = [this, &MeshDescription, &VertexIDs, PolygonGroup](int32 P0, int32 P1, int32 P2, int32 P3) -> FPolygonID
|
2019-10-01 20:41:42 -04:00
|
|
|
{
|
|
|
|
|
FVertexInstanceID VertexInstanceIDs[4];
|
|
|
|
|
VertexInstanceIDs[0] = MeshDescription.CreateVertexInstance(VertexIDs[P0]);
|
|
|
|
|
VertexInstanceIDs[1] = MeshDescription.CreateVertexInstance(VertexIDs[P1]);
|
|
|
|
|
VertexInstanceIDs[2] = MeshDescription.CreateVertexInstance(VertexIDs[P2]);
|
|
|
|
|
VertexInstanceIDs[3] = MeshDescription.CreateVertexInstance(VertexIDs[P3]);
|
|
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
TVertexInstanceAttributesRef<FVector2f> UVs = GetVertexInstanceUVs();
|
|
|
|
|
UVs[VertexInstanceIDs[0]] = FVector2f(0.0f, 0.0f);
|
|
|
|
|
UVs[VertexInstanceIDs[1]] = FVector2f(1.0f, 0.0f);
|
|
|
|
|
UVs[VertexInstanceIDs[2]] = FVector2f(1.0f, 1.0f);
|
|
|
|
|
UVs[VertexInstanceIDs[3]] = FVector2f(0.0f, 1.0f);
|
2020-01-27 20:11:15 -05:00
|
|
|
|
2021-09-22 10:01:48 -04:00
|
|
|
TVertexInstanceAttributesRef<FVector4f> Colors = GetVertexInstanceColors();
|
2022-01-27 03:30:41 -05:00
|
|
|
Colors[VertexInstanceIDs[0]] = (FVector4f)FLinearColor::Red;
|
|
|
|
|
Colors[VertexInstanceIDs[1]] = (FVector4f)FLinearColor::Green;
|
|
|
|
|
Colors[VertexInstanceIDs[2]] = (FVector4f)FLinearColor::Blue;
|
|
|
|
|
Colors[VertexInstanceIDs[3]] = (FVector4f)FLinearColor::White;
|
2021-07-12 05:33:04 -04:00
|
|
|
|
2019-10-01 20:41:42 -04:00
|
|
|
TArray<FEdgeID> EdgeIDs;
|
|
|
|
|
EdgeIDs.Reserve(4);
|
|
|
|
|
|
|
|
|
|
FPolygonID PolygonID = MeshDescription.CreatePolygon(PolygonGroup, VertexInstanceIDs, &EdgeIDs);
|
|
|
|
|
|
|
|
|
|
for (FEdgeID EdgeID : EdgeIDs)
|
|
|
|
|
{
|
|
|
|
|
GetEdgeHardnesses()[EdgeID] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PolygonID;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PolygonID_PlusX = MakePolygon(0, 1, 7, 6);
|
|
|
|
|
PolygonID_MinusX = MakePolygon(2, 3, 5, 4);
|
|
|
|
|
PolygonID_PlusY = MakePolygon(1, 2, 4, 7);
|
|
|
|
|
PolygonID_MinusY = MakePolygon(3, 0, 6, 5);
|
|
|
|
|
PolygonID_PlusZ = MakePolygon(1, 0, 3, 2);
|
2020-01-27 20:11:15 -05:00
|
|
|
PolygonID_MinusZ = MakePolygon(6, 7, 4, 5);
|
2019-10-01 20:41:42 -04:00
|
|
|
|
First pass of MeshDescription API and format refactor.
- Removed hardcoded element type arrays (Vertices, Edges, Triangles etc.). Mesh element types can now be arbitrarily added, with any number of channels.
- Mesh element containers have a much leaner format; instead of sparse arrays, they are now represented by a simple bitarray, determining whether an index is used or not. Consequently, mesh topology is now entirely described with the attribute system, e.g. edge start and end vertices, triangle vertices, etc.
- Support added for attributes of arbitrary dimensions, e.g. float[4] or int[2].
- Support added for attributes which index into another mesh element container.
- Added FMeshElementIndexer: this is an efficient container for maintaining backward references from one element type to another; for example, edges have an attribute specifying which vertices are at each end (an attribute of type FVertexID[2]). With an indexer, it is possible to look up which edges contain a given vertex, even though this is not explicitly stored. Indexers are designed to do minimal allocations and update lazily and in batch when necessary.
- Added support for preserving UV topology in static meshes. UVs are now a first-class element type which may be indexed directly from triangles.
- Added the facility to access the underlying array in an attribute array directly.
- Triangles now directly reference their vertex, edge and UV IDs. Vertex instances are to be deprecated.
- Changed various systems to be triangle-centric rather than polygon-centric, as this is faster. Triangles are presumed to be the elementary face type in a MeshDescription, even if polygons are still supported. The concept of polygons will be somewhat shifted to mean a group of triangles which should be treated collectively for editing purposes.
- Optimised normal/tangent generation and FBX import.
- Deprecated EditableMesh, MeshEditor and StaticMeshEditorExtension plugins - these are to be removed, but they still have certain hooks in place which need removing.
#rb
[CL 13568702 by Richard TalbotWatkin in ue5-main branch]
2020-05-28 10:56:57 -04:00
|
|
|
FStaticMeshOperations::ComputeTriangleTangentsAndNormals(MeshDescription);
|
2019-10-01 20:41:42 -04:00
|
|
|
FStaticMeshOperations::ComputeTangentsAndNormals(MeshDescription, EComputeNTBsFlags::Normals | EComputeNTBsFlags::Tangents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|