2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2017-08-22 16:18:25 -04:00
# include "MeshDescription.h"
Total revamp of mesh element attribute model.
Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages:
- It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures.
- This is more efficient in batch operations which deal with a number of mesh elements in one go.
- These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation.
- The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4).
Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended.
Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible.
The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere.
Various bug fixes.
#rb Alexis.Matte
[CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
# include "MeshAttributes.h"
2019-10-04 13:11:45 -04:00
# include "Algo/Copy.h"
# include "Misc/SecureHash.h"
2018-12-10 09:29:08 -05:00
# include "Serialization/BulkDataReader.h"
# include "Serialization/BulkDataWriter.h"
2019-10-04 13:11:45 -04:00
# include "UObject/EnterpriseObjectVersion.h"
2018-01-23 13:46:41 -05:00
2019-12-19 18:07:47 -05:00
void UDEPRECATED_MeshDescription : : Serialize ( FArchive & Ar )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
Super : : Serialize ( Ar ) ;
2018-05-29 05:36:50 -04:00
2019-12-19 18:07:47 -05:00
if ( ! HasAnyFlags ( RF_ClassDefaultObject ) )
2018-08-07 16:07:44 -04:00
{
2019-12-19 18:07:47 -05:00
UE_LOG ( LogLoad , Error , TEXT ( " UMeshDescription about to be deprecated - please resave %s " ) , * GetPathName ( ) ) ;
2018-08-07 16:07:44 -04:00
}
2019-12-19 18:07:47 -05:00
2018-06-07 16:22:56 -04:00
// Discard the contents
FMeshDescription MeshDescription ;
Ar < < MeshDescription ;
}
2018-06-18 08:47:01 -04:00
2019-10-01 20:41:42 -04:00
FMeshDescription : : FMeshDescription ( )
{
// Minimal requirement is that vertices have a Position attribute
VertexAttributesSet . RegisterAttribute ( MeshAttribute : : Vertex : : Position , 1 , FVector : : ZeroVector , EMeshAttributeFlags : : Lerpable ) ;
}
2019-09-10 11:35:20 -04:00
void FMeshDescription : : Serialize ( FArchive & Ar )
{
Ar . UsingCustomVersion ( FReleaseObjectVersion : : GUID ) ;
Ar . UsingCustomVersion ( FEditorObjectVersion : : GUID ) ;
if ( Ar . IsLoading ( ) & & Ar . CustomVer ( FReleaseObjectVersion : : GUID ) < FReleaseObjectVersion : : MeshDescriptionNewSerialization )
2018-08-07 16:07:44 -04:00
{
2019-09-10 11:35:20 -04:00
UE_LOG ( LogLoad , Warning , TEXT ( " Deprecated serialization format " ) ) ;
2018-08-07 16:07:44 -04:00
}
2019-09-10 11:35:20 -04:00
Ar < < VertexArray ;
Ar < < VertexInstanceArray ;
Ar < < EdgeArray ;
Ar < < PolygonArray ;
Ar < < PolygonGroupArray ;
2017-08-22 16:18:25 -04:00
2019-09-10 11:35:20 -04:00
Ar < < VertexAttributesSet ;
Ar < < VertexInstanceAttributesSet ;
Ar < < EdgeAttributesSet ;
Ar < < PolygonAttributesSet ;
Ar < < PolygonGroupAttributesSet ;
2018-05-28 14:28:16 -04:00
2019-09-10 11:35:20 -04:00
// Serialize new triangle arrays since version MeshDescriptionTriangles
if ( ! Ar . IsLoading ( ) | | Ar . CustomVer ( FEditorObjectVersion : : GUID ) > = FEditorObjectVersion : : MeshDescriptionTriangles )
{
Ar < < TriangleArray ;
Ar < < TriangleAttributesSet ;
}
if ( Ar . IsLoading ( ) & & Ar . CustomVer ( FReleaseObjectVersion : : GUID ) > = FReleaseObjectVersion : : MeshDescriptionNewSerialization )
2018-05-28 14:28:16 -04:00
{
// Populate vertex instance IDs for vertices
2019-09-10 11:35:20 -04:00
for ( const FVertexInstanceID VertexInstanceID : VertexInstanceArray . GetElementIDs ( ) )
2018-05-28 14:28:16 -04:00
{
2019-09-10 11:35:20 -04:00
const FVertexID VertexID = GetVertexInstanceVertex ( VertexInstanceID ) ;
VertexArray [ VertexID ] . VertexInstanceIDs . Add ( VertexInstanceID ) ;
2018-05-28 14:28:16 -04:00
}
// Populate edge IDs for vertices
2019-09-10 11:35:20 -04:00
for ( const FEdgeID EdgeID : EdgeArray . GetElementIDs ( ) )
2018-05-28 14:28:16 -04:00
{
2019-09-10 11:35:20 -04:00
const FVertexID VertexID0 = GetEdgeVertex ( EdgeID , 0 ) ;
const FVertexID VertexID1 = GetEdgeVertex ( EdgeID , 1 ) ;
VertexArray [ VertexID0 ] . ConnectedEdgeIDs . Add ( EdgeID ) ;
VertexArray [ VertexID1 ] . ConnectedEdgeIDs . Add ( EdgeID ) ;
}
if ( Ar . CustomVer ( FEditorObjectVersion : : GUID ) > = FEditorObjectVersion : : MeshDescriptionTriangles )
{
// Make reverse connection from polygons to triangles
for ( const FTriangleID TriangleID : TriangleArray . GetElementIDs ( ) )
{
const FPolygonID PolygonID = TriangleArray [ TriangleID ] . PolygonID ;
PolygonArray [ PolygonID ] . TriangleIDs . Add ( TriangleID ) ;
}
2018-05-28 14:28:16 -04:00
}
// Populate polygon IDs for vertex instances, edges and polygon groups
2019-09-10 11:35:20 -04:00
for ( const FPolygonID PolygonID : PolygonArray . GetElementIDs ( ) )
2018-05-28 14:28:16 -04:00
{
2019-09-10 11:35:20 -04:00
if ( Ar . CustomVer ( FEditorObjectVersion : : GUID ) > = FEditorObjectVersion : : MeshDescriptionTriangles )
2018-05-28 14:28:16 -04:00
{
2019-09-10 11:35:20 -04:00
// If the polygon has no contour serialized, copy it over from the triangle
2019-12-19 18:07:47 -05:00
if ( PolygonArray [ PolygonID ] . VertexInstanceIDs . Num ( ) = = 0 )
2018-05-28 14:28:16 -04:00
{
2019-09-10 11:35:20 -04:00
check ( PolygonArray [ PolygonID ] . TriangleIDs . Num ( ) = = 1 ) ;
const FTriangleID TriangleID = PolygonArray [ PolygonID ] . TriangleIDs [ 0 ] ;
for ( int32 Index = 0 ; Index < 3 ; Index + + )
{
2019-12-19 18:07:47 -05:00
PolygonArray [ PolygonID ] . VertexInstanceIDs . Add ( TriangleArray [ TriangleID ] . GetVertexInstanceID ( Index ) ) ;
2019-09-10 11:35:20 -04:00
}
2018-05-28 14:28:16 -04:00
}
2019-09-10 11:35:20 -04:00
}
2018-05-28 14:28:16 -04:00
2019-09-10 11:35:20 -04:00
const FPolygonGroupID PolygonGroupID = PolygonArray [ PolygonID ] . PolygonGroupID ;
PolygonGroupArray [ PolygonGroupID ] . Polygons . Add ( PolygonID ) ;
2018-05-28 14:28:16 -04:00
}
}
2018-06-07 16:22:56 -04:00
2019-09-10 11:35:20 -04:00
if ( Ar . IsLoading ( ) )
{
if ( Ar . CustomVer ( FEditorObjectVersion : : GUID ) < FEditorObjectVersion : : MeshDescriptionTriangles )
{
TriangleArray . Reset ( ) ;
// If we didn't serialize triangles, generate them from the polygon contour
for ( const FPolygonID PolygonID : PolygonArray . GetElementIDs ( ) )
{
check ( PolygonArray [ PolygonID ] . TriangleIDs . Num ( ) = = 0 ) ;
ComputePolygonTriangulation ( PolygonID ) ;
}
}
else
{
// Otherwise connect existing triangles to vertex instances and edges
for ( const FTriangleID TriangleID : TriangleArray . GetElementIDs ( ) )
{
for ( int32 Index = 0 ; Index < 3 ; + + Index )
{
const FVertexInstanceID VertexInstanceID = GetTriangleVertexInstance ( TriangleID , Index ) ;
const FVertexInstanceID NextVertexInstanceID = GetTriangleVertexInstance ( TriangleID , ( Index + 1 = = 3 ) ? 0 : Index + 1 ) ;
const FVertexID VertexID0 = GetVertexInstanceVertex ( VertexInstanceID ) ;
const FVertexID VertexID1 = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
FEdgeID EdgeID = GetVertexPairEdge ( VertexID0 , VertexID1 ) ;
check ( EdgeID ! = FEdgeID : : Invalid ) ;
VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Add ( TriangleID ) ;
EdgeArray [ EdgeID ] . ConnectedTriangles . Add ( TriangleID ) ;
}
}
}
}
2017-08-22 16:18:25 -04:00
}
2018-03-26 17:55:30 -04:00
2018-06-07 16:22:56 -04:00
void FMeshDescription : : Empty ( )
2017-12-28 17:15:00 -05:00
{
VertexArray . Reset ( ) ;
VertexInstanceArray . Reset ( ) ;
EdgeArray . Reset ( ) ;
2019-09-10 11:35:20 -04:00
TriangleArray . Reset ( ) ;
2017-12-28 17:15:00 -05:00
PolygonArray . Reset ( ) ;
PolygonGroupArray . Reset ( ) ;
2019-12-19 18:07:47 -05:00
// Empty all attributes
VertexAttributesSet . Initialize ( 0 ) ;
VertexInstanceAttributesSet . Initialize ( 0 ) ;
EdgeAttributesSet . Initialize ( 0 ) ;
TriangleAttributesSet . Initialize ( 0 ) ;
PolygonAttributesSet . Initialize ( 0 ) ;
PolygonGroupAttributesSet . Initialize ( 0 ) ;
2017-12-28 17:15:00 -05:00
}
Total revamp of mesh element attribute model.
Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages:
- It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures.
- This is more efficient in batch operations which deal with a number of mesh elements in one go.
- These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation.
- The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4).
Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended.
Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible.
The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere.
Various bug fixes.
#rb Alexis.Matte
[CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
2018-01-17 15:58:38 -05:00
2018-12-10 09:29:08 -05:00
bool FMeshDescription : : IsEmpty ( ) const
{
return VertexArray . GetArraySize ( ) = = 0 & &
VertexInstanceArray . GetArraySize ( ) = = 0 & &
EdgeArray . GetArraySize ( ) = = 0 & &
2019-09-10 11:35:20 -04:00
TriangleArray . GetArraySize ( ) = = 0 & &
2018-12-10 09:29:08 -05:00
PolygonArray . GetArraySize ( ) = = 0 & &
PolygonGroupArray . GetArraySize ( ) = = 0 ;
}
2019-12-19 18:07:47 -05:00
void FMeshDescription : : Compact ( FElementIDRemappings & OutRemappings )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
VertexArray . Compact ( OutRemappings . NewVertexIndexLookup ) ;
VertexInstanceArray . Compact ( OutRemappings . NewVertexInstanceIndexLookup ) ;
EdgeArray . Compact ( OutRemappings . NewEdgeIndexLookup ) ;
TriangleArray . Compact ( OutRemappings . NewTriangleIndexLookup ) ;
PolygonArray . Compact ( OutRemappings . NewPolygonIndexLookup ) ;
PolygonGroupArray . Compact ( OutRemappings . NewPolygonGroupIndexLookup ) ;
2017-08-22 16:18:25 -04:00
2019-12-19 18:07:47 -05:00
RemapAttributes ( OutRemappings ) ;
FixUpElementIDs ( OutRemappings ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
void FMeshDescription : : Remap ( const FElementIDRemappings & Remappings )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
VertexArray . Remap ( Remappings . NewVertexIndexLookup ) ;
VertexInstanceArray . Remap ( Remappings . NewVertexInstanceIndexLookup ) ;
EdgeArray . Remap ( Remappings . NewEdgeIndexLookup ) ;
TriangleArray . Remap ( Remappings . NewTriangleIndexLookup ) ;
PolygonArray . Remap ( Remappings . NewPolygonIndexLookup ) ;
PolygonGroupArray . Remap ( Remappings . NewPolygonGroupIndexLookup ) ;
2017-08-22 16:18:25 -04:00
2019-12-19 18:07:47 -05:00
RemapAttributes ( Remappings ) ;
FixUpElementIDs ( Remappings ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
void FMeshDescription : : RemapAttributes ( const FElementIDRemappings & Remappings )
2019-09-10 11:35:20 -04:00
{
2019-12-19 18:07:47 -05:00
VertexAttributesSet . Remap ( Remappings . NewVertexIndexLookup ) ;
VertexInstanceAttributesSet . Remap ( Remappings . NewVertexInstanceIndexLookup ) ;
EdgeAttributesSet . Remap ( Remappings . NewEdgeIndexLookup ) ;
TriangleAttributesSet . Remap ( Remappings . NewTriangleIndexLookup ) ;
PolygonAttributesSet . Remap ( Remappings . NewPolygonIndexLookup ) ;
PolygonGroupAttributesSet . Remap ( Remappings . NewPolygonGroupIndexLookup ) ;
2019-09-10 11:35:20 -04:00
}
2019-12-19 18:07:47 -05:00
void FMeshDescription : : FixUpElementIDs ( const FElementIDRemappings & Remappings )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
for ( const FVertexID VertexID : VertexArray . GetElementIDs ( ) )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshVertex & Vertex = VertexArray [ VertexID ] ;
2017-08-22 16:18:25 -04:00
// Fix up vertex instance index references in vertices array
2019-12-19 18:07:47 -05:00
for ( FVertexInstanceID & VertexInstanceID : Vertex . VertexInstanceIDs )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
VertexInstanceID = Remappings . GetRemappedVertexInstanceID ( VertexInstanceID ) ;
2017-08-22 16:18:25 -04:00
}
// Fix up edge index references in the vertex array
2019-12-19 18:07:47 -05:00
for ( FEdgeID & EdgeID : Vertex . ConnectedEdgeIDs )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
EdgeID = Remappings . GetRemappedEdgeID ( EdgeID ) ;
2017-08-22 16:18:25 -04:00
}
}
// Fix up vertex index references in vertex instance array
2019-12-19 18:07:47 -05:00
for ( const FVertexInstanceID VertexInstanceID : VertexInstanceArray . GetElementIDs ( ) )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshVertexInstance & VertexInstance = VertexInstanceArray [ VertexInstanceID ] ;
2017-08-22 16:18:25 -04:00
2019-12-19 18:07:47 -05:00
VertexInstance . VertexID = Remappings . GetRemappedVertexID ( VertexInstance . VertexID ) ;
2017-08-22 16:18:25 -04:00
2019-12-19 18:07:47 -05:00
for ( FTriangleID & TriangleID : VertexInstance . ConnectedTriangles )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
TriangleID = Remappings . GetRemappedTriangleID ( TriangleID ) ;
2019-09-10 11:35:20 -04:00
}
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
for ( const FEdgeID EdgeID : EdgeArray . GetElementIDs ( ) )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshEdge & Edge = EdgeArray [ EdgeID ] ;
2017-08-22 16:18:25 -04:00
// Fix up vertex index references in Edges array
2019-12-19 18:07:47 -05:00
for ( int32 Index = 0 ; Index < 2 ; Index + + )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
Edge . VertexIDs [ Index ] = Remappings . GetRemappedVertexID ( Edge . VertexIDs [ Index ] ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
for ( FTriangleID & TriangleID : Edge . ConnectedTriangles )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
TriangleID = Remappings . GetRemappedTriangleID ( TriangleID ) ;
2019-09-10 11:35:20 -04:00
}
}
2019-12-19 18:07:47 -05:00
for ( const FTriangleID TriangleID : TriangleArray . GetElementIDs ( ) )
2019-09-10 11:35:20 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
2019-09-10 11:35:20 -04:00
// Fix up vertex instance references in Triangle
2019-12-19 18:07:47 -05:00
for ( FVertexInstanceID & VertexInstanceID : Triangle . VertexInstanceIDs )
2019-09-10 11:35:20 -04:00
{
2019-12-19 18:07:47 -05:00
VertexInstanceID = Remappings . GetRemappedVertexInstanceID ( VertexInstanceID ) ;
2019-09-10 11:35:20 -04:00
}
2019-12-19 18:07:47 -05:00
Triangle . PolygonID = Remappings . GetRemappedPolygonID ( Triangle . PolygonID ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
for ( const FPolygonID PolygonID : PolygonArray . GetElementIDs ( ) )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
2017-08-22 16:18:25 -04:00
// Fix up references to vertex indices in section polygons' contours
2019-12-19 18:07:47 -05:00
for ( FVertexInstanceID & VertexInstanceID : Polygon . VertexInstanceIDs )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
VertexInstanceID = Remappings . GetRemappedVertexInstanceID ( VertexInstanceID ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
for ( FTriangleID & TriangleID : Polygon . TriangleIDs )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
TriangleID = Remappings . GetRemappedTriangleID ( TriangleID ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
Polygon . PolygonGroupID = Remappings . GetRemappedPolygonGroupID ( Polygon . PolygonGroupID ) ;
2017-08-22 16:18:25 -04:00
}
2019-12-19 18:07:47 -05:00
for ( const FPolygonGroupID PolygonGroupID : PolygonGroupArray . GetElementIDs ( ) )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
FMeshPolygonGroup & PolygonGroup = PolygonGroupArray [ PolygonGroupID ] ;
2017-08-22 16:18:25 -04:00
2019-12-19 18:07:47 -05:00
for ( FPolygonID & Polygon : PolygonGroup . Polygons )
2017-08-22 16:18:25 -04:00
{
2019-12-19 18:07:47 -05:00
Polygon = Remappings . GetRemappedPolygonID ( Polygon ) ;
2017-08-22 16:18:25 -04:00
}
}
}
Total revamp of mesh element attribute model.
Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages:
- It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures.
- This is more efficient in batch operations which deal with a number of mesh elements in one go.
- These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation.
- The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4).
Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended.
Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible.
The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere.
Various bug fixes.
#rb Alexis.Matte
[CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
2019-09-10 11:35:20 -04:00
void FMeshDescription : : CreateVertexInstance_Internal ( const FVertexInstanceID VertexInstanceID , const FVertexID VertexID )
2019-01-18 06:37:35 -05:00
{
2019-09-10 11:35:20 -04:00
VertexInstanceArray [ VertexInstanceID ] . VertexID = VertexID ;
check ( ! VertexArray [ VertexID ] . VertexInstanceIDs . Contains ( VertexInstanceID ) ) ;
VertexArray [ VertexID ] . VertexInstanceIDs . Add ( VertexInstanceID ) ;
VertexInstanceAttributesSet . Insert ( VertexInstanceID ) ;
2019-01-18 06:37:35 -05:00
}
2020-02-03 11:08:35 -05:00
template < template < typename . . . > class TContainer >
void FMeshDescription : : DeleteVertexInstance_Internal ( const FVertexInstanceID VertexInstanceID , TContainer < FVertexID > * InOutOrphanedVerticesPtr )
2019-01-18 06:37:35 -05:00
{
2019-12-19 18:07:47 -05:00
check ( VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Num ( ) = = 0 ) ;
2019-09-10 11:35:20 -04:00
const FVertexID VertexID = VertexInstanceArray [ VertexInstanceID ] . VertexID ;
verify ( VertexArray [ VertexID ] . VertexInstanceIDs . RemoveSingle ( VertexInstanceID ) = = 1 ) ;
if ( InOutOrphanedVerticesPtr & & VertexArray [ VertexID ] . VertexInstanceIDs . Num ( ) = = 0 & & VertexArray [ VertexID ] . ConnectedEdgeIDs . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedVerticesPtr , VertexID ) ;
2019-09-10 11:35:20 -04:00
}
VertexInstanceArray . Remove ( VertexInstanceID ) ;
VertexInstanceAttributesSet . Remove ( VertexInstanceID ) ;
}
2020-02-03 11:08:35 -05:00
void FMeshDescription : : DeleteVertexInstance ( const FVertexInstanceID VertexInstanceID , TArray < FVertexID > * InOutOrphanedVerticesPtr )
{
DeleteVertexInstance_Internal < TArray > ( VertexInstanceID , InOutOrphanedVerticesPtr ) ;
}
2019-09-10 11:35:20 -04:00
void FMeshDescription : : CreateEdge_Internal ( const FEdgeID EdgeID , const FVertexID VertexID0 , const FVertexID VertexID1 )
{
check ( GetVertexPairEdge ( VertexID0 , VertexID1 ) = = FEdgeID : : Invalid ) ;
FMeshEdge & Edge = EdgeArray [ EdgeID ] ;
Edge . VertexIDs [ 0 ] = VertexID0 ;
Edge . VertexIDs [ 1 ] = VertexID1 ;
VertexArray [ VertexID0 ] . ConnectedEdgeIDs . Add ( EdgeID ) ;
VertexArray [ VertexID1 ] . ConnectedEdgeIDs . Add ( EdgeID ) ;
EdgeAttributesSet . Insert ( EdgeID ) ;
}
2020-02-03 11:08:35 -05:00
template < template < typename . . . > class TContainer >
void FMeshDescription : : DeleteEdge_Internal ( const FEdgeID EdgeID , TContainer < FVertexID > * InOutOrphanedVerticesPtr )
2019-09-10 11:35:20 -04:00
{
FMeshEdge & Edge = EdgeArray [ EdgeID ] ;
for ( const FVertexID EdgeVertexID : Edge . VertexIDs )
{
FMeshVertex & Vertex = VertexArray [ EdgeVertexID ] ;
verify ( Vertex . ConnectedEdgeIDs . RemoveSingle ( EdgeID ) = = 1 ) ;
if ( InOutOrphanedVerticesPtr & & Vertex . ConnectedEdgeIDs . Num ( ) = = 0 )
{
check ( Vertex . VertexInstanceIDs . Num ( ) = = 0 ) ; // We must already have deleted any vertex instances
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedVerticesPtr , EdgeVertexID ) ;
2019-09-10 11:35:20 -04:00
}
}
EdgeArray . Remove ( EdgeID ) ;
EdgeAttributesSet . Remove ( EdgeID ) ;
}
2020-02-03 11:08:35 -05:00
void FMeshDescription : : DeleteEdge ( const FEdgeID EdgeID , TArray < FVertexID > * InOutOrphanedVerticesPtr )
{
DeleteEdge_Internal < TArray > ( EdgeID , InOutOrphanedVerticesPtr ) ;
}
2019-09-10 11:35:20 -04:00
void FMeshDescription : : CreateTriangle_Internal ( const FTriangleID TriangleID , const FPolygonGroupID PolygonGroupID , TArrayView < const FVertexInstanceID > VertexInstanceIDs , TArray < FEdgeID > * OutEdgeIDs )
{
if ( OutEdgeIDs )
2019-01-18 06:37:35 -05:00
{
OutEdgeIDs - > Empty ( ) ;
}
2019-09-10 11:35:20 -04:00
// Fill out triangle vertex instances
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
check ( VertexInstanceIDs . Num ( ) = = 3 ) ;
Triangle . SetVertexInstanceID ( 0 , VertexInstanceIDs [ 0 ] ) ;
Triangle . SetVertexInstanceID ( 1 , VertexInstanceIDs [ 1 ] ) ;
Triangle . SetVertexInstanceID ( 2 , VertexInstanceIDs [ 2 ] ) ;
2019-01-18 06:37:35 -05:00
2019-09-10 11:35:20 -04:00
// Make a polygon which will contain this triangle
FPolygonID PolygonID = PolygonArray . Add ( ) ;
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
PolygonAttributesSet . Insert ( PolygonID ) ;
2019-12-19 18:07:47 -05:00
Polygon . VertexInstanceIDs . Reserve ( 3 ) ;
Algo : : Copy ( VertexInstanceIDs , Polygon . VertexInstanceIDs ) ;
2019-09-10 11:35:20 -04:00
Polygon . PolygonGroupID = PolygonGroupID ;
PolygonGroupArray [ PolygonGroupID ] . Polygons . Add ( PolygonID ) ;
Triangle . PolygonID = PolygonID ;
check ( ! Polygon . TriangleIDs . Contains ( TriangleID ) ) ;
Polygon . TriangleIDs . Add ( TriangleID ) ;
TriangleAttributesSet . Insert ( TriangleID ) ;
for ( int32 Index = 0 ; Index < 3 ; + + Index )
2019-01-18 06:37:35 -05:00
{
2019-09-10 11:35:20 -04:00
const FVertexInstanceID VertexInstanceID = Triangle . GetVertexInstanceID ( Index ) ;
const FVertexInstanceID NextVertexInstanceID = Triangle . GetVertexInstanceID ( ( Index = = 2 ) ? 0 : Index + 1 ) ;
2019-01-18 06:37:35 -05:00
2019-09-10 11:35:20 -04:00
const FVertexID ThisVertexID = GetVertexInstanceVertex ( VertexInstanceID ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
2019-01-18 06:37:35 -05:00
2019-09-10 11:35:20 -04:00
FEdgeID EdgeID = GetVertexPairEdge ( ThisVertexID , NextVertexID ) ;
if ( EdgeID = = FEdgeID : : Invalid )
2019-01-18 06:37:35 -05:00
{
2019-09-10 11:35:20 -04:00
EdgeID = CreateEdge ( ThisVertexID , NextVertexID ) ;
if ( OutEdgeIDs )
2019-01-18 06:37:35 -05:00
{
2019-09-10 11:35:20 -04:00
OutEdgeIDs - > Add ( EdgeID ) ;
2019-01-18 06:37:35 -05:00
}
}
2019-09-10 11:35:20 -04:00
check ( ! VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Add ( TriangleID ) ;
check ( ! EdgeArray [ EdgeID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
EdgeArray [ EdgeID ] . ConnectedTriangles . Add ( TriangleID ) ;
2019-01-18 06:37:35 -05:00
}
}
2020-02-03 11:08:35 -05:00
template < template < typename . . . > class TContainer >
void FMeshDescription : : DeleteTriangle_Internal ( const FTriangleID TriangleID , TContainer < FEdgeID > * InOutOrphanedEdgesPtr , TContainer < FVertexInstanceID > * InOutOrphanedVertexInstancesPtr , TContainer < FPolygonGroupID > * InOutOrphanedPolygonGroupsPtr )
Total revamp of mesh element attribute model.
Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages:
- It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures.
- This is more efficient in batch operations which deal with a number of mesh elements in one go.
- These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation.
- The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4).
Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended.
Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible.
The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere.
Various bug fixes.
#rb Alexis.Matte
[CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
{
2019-09-10 11:35:20 -04:00
const FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
const FPolygonID PolygonID = Triangle . PolygonID ;
// Delete this triangle from the polygon
verify ( PolygonArray [ PolygonID ] . TriangleIDs . RemoveSingle ( TriangleID ) = = 1 ) ;
if ( PolygonArray [ PolygonID ] . TriangleIDs . Num ( ) = = 0 )
{
// If it was the only triangle in the polygon, delete the polygon too
for ( int32 Index = 0 ; Index < 3 ; + + Index )
{
const FVertexInstanceID VertexInstanceID = Triangle . GetVertexInstanceID ( Index ) ;
const FVertexInstanceID NextVertexInstanceID = Triangle . GetVertexInstanceID ( ( Index = = 2 ) ? 0 : Index + 1 ) ;
const FVertexID VertexID0 = GetVertexInstanceVertex ( VertexInstanceID ) ;
const FVertexID VertexID1 = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
FEdgeID EdgeID = GetVertexPairEdge ( VertexID0 , VertexID1 ) ;
check ( EdgeID ! = FEdgeID : : Invalid ) ;
verify ( VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
verify ( EdgeArray [ EdgeID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
if ( InOutOrphanedVertexInstancesPtr & & VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedVertexInstancesPtr , VertexInstanceID ) ;
2019-09-10 11:35:20 -04:00
}
if ( InOutOrphanedEdgesPtr & & EdgeArray [ EdgeID ] . ConnectedTriangles . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedEdgesPtr , EdgeID ) ;
2019-09-10 11:35:20 -04:00
}
}
// Remove the polygon
const FPolygonGroupID PolygonGroupID = PolygonArray [ PolygonID ] . PolygonGroupID ;
verify ( PolygonGroupArray [ PolygonGroupID ] . Polygons . RemoveSingle ( PolygonID ) = = 1 ) ;
if ( InOutOrphanedPolygonGroupsPtr & & PolygonGroupArray [ PolygonGroupID ] . Polygons . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedPolygonGroupsPtr , PolygonGroupID ) ;
2019-09-10 11:35:20 -04:00
}
2019-12-19 18:07:47 -05:00
PolygonArray . Remove ( PolygonID ) ;
PolygonAttributesSet . Remove ( PolygonID ) ;
2019-09-10 11:35:20 -04:00
}
else
{
// @todo: Handle this properly when deleting a triangle which forms part of an n-gon
// Either it needs to shave off the triangle from the contour and update the contour vertex instances,
// or it should just refuse to delete the triangle.
check ( false ) ;
}
TriangleArray . Remove ( TriangleID ) ;
TriangleAttributesSet . Remove ( TriangleID ) ;
}
2020-02-03 11:08:35 -05:00
void FMeshDescription : : DeleteTriangle ( const FTriangleID TriangleID , TArray < FEdgeID > * InOutOrphanedEdgesPtr , TArray < FVertexInstanceID > * InOutOrphanedVertexInstancesPtr , TArray < FPolygonGroupID > * InOutOrphanedPolygonGroupsPtr )
{
DeleteTriangle_Internal < TArray > ( TriangleID , InOutOrphanedEdgesPtr , InOutOrphanedVertexInstancesPtr , InOutOrphanedPolygonGroupsPtr ) ;
}
void FMeshDescription : : DeleteTriangles ( const TArray < FTriangleID > & Triangles )
{
TSet < FEdgeID > OrphanedEdges ;
TSet < FVertexInstanceID > OrphanedVertexInstances ;
TSet < FPolygonGroupID > OrphanedPolygonGroups ;
TSet < FVertexID > OrphanedVertices ;
for ( FTriangleID TriangleID : Triangles )
{
DeleteTriangle_Internal < TSet > ( TriangleID , & OrphanedEdges , & OrphanedVertexInstances , & OrphanedPolygonGroups ) ;
}
for ( FPolygonGroupID PolygonGroupID : OrphanedPolygonGroups )
{
DeletePolygonGroup ( PolygonGroupID ) ;
}
for ( FVertexInstanceID VertexInstanceID : OrphanedVertexInstances )
{
DeleteVertexInstance_Internal < TSet > ( VertexInstanceID , & OrphanedVertices ) ;
}
for ( FEdgeID EdgeID : OrphanedEdges )
{
DeleteEdge_Internal < TSet > ( EdgeID , & OrphanedVertices ) ;
}
for ( FVertexID VertexID : OrphanedVertices )
{
DeleteVertex ( VertexID ) ;
}
}
2019-09-10 11:35:20 -04:00
void FMeshDescription : : CreatePolygon_Internal ( const FPolygonID PolygonID , const FPolygonGroupID PolygonGroupID , TArrayView < const FVertexInstanceID > VertexInstanceIDs , TArray < FEdgeID > * OutEdgeIDs )
{
if ( OutEdgeIDs )
{
OutEdgeIDs - > Empty ( ) ;
}
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
const int32 NumVertices = VertexInstanceIDs . Num ( ) ;
2019-12-19 18:07:47 -05:00
Polygon . VertexInstanceIDs . SetNumUninitialized ( NumVertices ) ;
2019-09-10 11:35:20 -04:00
for ( int32 Index = 0 ; Index < NumVertices ; + + Index )
{
const FVertexInstanceID ThisVertexInstanceID = VertexInstanceIDs [ Index ] ;
const FVertexInstanceID NextVertexInstanceID = VertexInstanceIDs [ ( Index + 1 = = NumVertices ) ? 0 : Index + 1 ] ;
const FVertexID ThisVertexID = GetVertexInstanceVertex ( ThisVertexInstanceID ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
2019-12-19 18:07:47 -05:00
Polygon . VertexInstanceIDs [ Index ] = ThisVertexInstanceID ;
2019-09-10 11:35:20 -04:00
FEdgeID EdgeID = GetVertexPairEdge ( ThisVertexID , NextVertexID ) ;
if ( EdgeID = = FEdgeID : : Invalid )
{
EdgeID = CreateEdge ( ThisVertexID , NextVertexID ) ;
if ( OutEdgeIDs )
{
OutEdgeIDs - > Add ( EdgeID ) ;
}
}
}
2019-11-22 11:21:51 -05:00
check ( PolygonGroupID ! = FPolygonGroupID : : Invalid ) ;
2019-09-10 11:35:20 -04:00
Polygon . PolygonGroupID = PolygonGroupID ;
PolygonGroupArray [ PolygonGroupID ] . Polygons . Add ( PolygonID ) ;
check ( Polygon . TriangleIDs . Num ( ) = = 0 ) ;
ComputePolygonTriangulation ( PolygonID ) ;
PolygonAttributesSet . Insert ( PolygonID ) ;
}
2020-02-03 11:08:35 -05:00
template < template < typename . . . > class TContainer >
void FMeshDescription : : DeletePolygon_Internal ( const FPolygonID PolygonID , TContainer < FEdgeID > * InOutOrphanedEdgesPtr , TContainer < FVertexInstanceID > * InOutOrphanedVertexInstancesPtr , TContainer < FPolygonGroupID > * InOutOrphanedPolygonGroupsPtr )
2019-09-10 11:35:20 -04:00
{
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
// Remove constituent triangles
for ( const FTriangleID TriangleID : Polygon . TriangleIDs )
{
const FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
for ( int32 Index = 0 ; Index < 3 ; + + Index )
{
const FVertexInstanceID ThisVertexInstanceID = Triangle . GetVertexInstanceID ( Index ) ;
const FVertexInstanceID NextVertexInstanceID = Triangle . GetVertexInstanceID ( ( Index = = 2 ) ? 0 : Index + 1 ) ;
const FVertexID ThisVertexID = GetVertexInstanceVertex ( ThisVertexInstanceID ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
const FEdgeID EdgeID = GetVertexPairEdge ( ThisVertexID , NextVertexID ) ;
2020-02-03 11:08:35 -05:00
2019-09-10 11:35:20 -04:00
// If a valid edge isn't found, we deem this to be because it's an internal edge which was already removed
// in a previous iteration through the triangle array.
if ( EdgeID ! = FEdgeID : : Invalid )
{
if ( IsEdgeInternal ( EdgeID ) )
{
// Remove internal edges
for ( const FVertexID EdgeVertexID : EdgeArray [ EdgeID ] . VertexIDs )
{
verify ( VertexArray [ EdgeVertexID ] . ConnectedEdgeIDs . RemoveSingle ( EdgeID ) = = 1 ) ;
}
EdgeArray . Remove ( EdgeID ) ;
EdgeAttributesSet . Remove ( EdgeID ) ;
}
else
{
verify ( EdgeArray [ EdgeID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
if ( InOutOrphanedEdgesPtr & & EdgeArray [ EdgeID ] . ConnectedTriangles . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedEdgesPtr , EdgeID ) ;
2019-09-10 11:35:20 -04:00
}
}
}
verify ( VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
if ( InOutOrphanedVertexInstancesPtr & & VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . Num ( ) = = 0 )
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedVertexInstancesPtr , ThisVertexInstanceID ) ;
2019-09-10 11:35:20 -04:00
}
}
TriangleArray . Remove ( TriangleID ) ;
TriangleAttributesSet . Remove ( TriangleID ) ;
}
2019-12-19 18:07:47 -05:00
FMeshPolygonGroup & PolygonGroup = PolygonGroupArray [ Polygon . PolygonGroupID ] ;
verify ( PolygonGroup . Polygons . RemoveSingle ( PolygonID ) = = 1 ) ;
2019-09-10 11:35:20 -04:00
2019-12-19 18:07:47 -05:00
if ( InOutOrphanedPolygonGroupsPtr & & PolygonGroup . Polygons . Num ( ) = = 0 )
2019-09-10 11:35:20 -04:00
{
2020-02-03 11:08:35 -05:00
AddUnique ( * InOutOrphanedPolygonGroupsPtr , Polygon . PolygonGroupID ) ;
2019-09-10 11:35:20 -04:00
}
2019-12-19 18:07:47 -05:00
PolygonArray . Remove ( PolygonID ) ;
PolygonAttributesSet . Remove ( PolygonID ) ;
Total revamp of mesh element attribute model.
Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages:
- It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures.
- This is more efficient in batch operations which deal with a number of mesh elements in one go.
- These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation.
- The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4).
Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended.
Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible.
The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere.
Various bug fixes.
#rb Alexis.Matte
[CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
}
2017-12-28 17:15:00 -05:00
2020-02-03 11:08:35 -05:00
void FMeshDescription : : DeletePolygon ( const FPolygonID PolygonID , TArray < FEdgeID > * InOutOrphanedEdgesPtr , TArray < FVertexInstanceID > * InOutOrphanedVertexInstancesPtr , TArray < FPolygonGroupID > * InOutOrphanedPolygonGroupsPtr )
{
DeletePolygon_Internal < TArray > ( PolygonID , InOutOrphanedEdgesPtr , InOutOrphanedVertexInstancesPtr , InOutOrphanedPolygonGroupsPtr ) ;
}
void FMeshDescription : : DeletePolygons ( const TArray < FPolygonID > & Polygons )
{
TSet < FEdgeID > OrphanedEdges ;
TSet < FVertexInstanceID > OrphanedVertexInstances ;
TSet < FPolygonGroupID > OrphanedPolygonGroups ;
TSet < FVertexID > OrphanedVertices ;
for ( FPolygonID PolygonID : Polygons )
{
DeletePolygon_Internal < TSet > ( PolygonID , & OrphanedEdges , & OrphanedVertexInstances , & OrphanedPolygonGroups ) ;
}
for ( FPolygonGroupID PolygonGroupID : OrphanedPolygonGroups )
{
DeletePolygonGroup ( PolygonGroupID ) ;
}
for ( FVertexInstanceID VertexInstanceID : OrphanedVertexInstances )
{
DeleteVertexInstance_Internal < TSet > ( VertexInstanceID , & OrphanedVertices ) ;
}
for ( FEdgeID EdgeID : OrphanedEdges )
{
DeleteEdge_Internal < TSet > ( EdgeID , & OrphanedVertices ) ;
}
for ( FVertexID VertexID : OrphanedVertices )
{
DeleteVertex ( VertexID ) ;
}
}
2017-12-28 17:15:00 -05:00
2019-12-19 18:07:47 -05:00
bool FMeshDescription : : IsVertexOrphaned ( const FVertexID VertexID ) const
2018-01-29 17:04:17 -05:00
{
2019-12-19 18:07:47 -05:00
for ( const FVertexInstanceID VertexInstanceID : VertexArray [ VertexID ] . VertexInstanceIDs )
2018-01-29 17:04:17 -05:00
{
2019-12-19 18:07:47 -05:00
if ( VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Num ( ) > 0 )
2018-01-29 17:04:17 -05:00
{
return false ;
}
}
return true ;
}
2019-09-10 11:35:20 -04:00
FEdgeID FMeshDescription : : GetVertexPairEdge ( const FVertexID VertexID0 , const FVertexID VertexID1 ) const
2018-01-29 17:04:17 -05:00
{
2019-09-10 11:35:20 -04:00
for ( const FEdgeID VertexConnectedEdgeID : VertexArray [ VertexID0 ] . ConnectedEdgeIDs )
2017-12-28 17:15:00 -05:00
{
2019-09-10 11:35:20 -04:00
const FVertexID EdgeVertexID0 = EdgeArray [ VertexConnectedEdgeID ] . VertexIDs [ 0 ] ;
const FVertexID EdgeVertexID1 = EdgeArray [ VertexConnectedEdgeID ] . VertexIDs [ 1 ] ;
if ( ( EdgeVertexID0 = = VertexID0 & & EdgeVertexID1 = = VertexID1 ) | | ( EdgeVertexID0 = = VertexID1 & & EdgeVertexID1 = = VertexID0 ) )
{
return VertexConnectedEdgeID ;
}
2017-12-28 17:15:00 -05:00
}
2019-09-10 11:35:20 -04:00
return FEdgeID : : Invalid ;
2017-12-28 17:15:00 -05:00
}
2018-01-29 17:04:17 -05:00
2019-09-10 11:35:20 -04:00
FEdgeID FMeshDescription : : GetVertexInstancePairEdge ( const FVertexInstanceID VertexInstanceID0 , const FVertexInstanceID VertexInstanceID1 ) const
2017-12-28 17:15:00 -05:00
{
2019-09-10 11:35:20 -04:00
const FVertexID VertexID0 = VertexInstanceArray [ VertexInstanceID0 ] . VertexID ;
const FVertexID VertexID1 = VertexInstanceArray [ VertexInstanceID1 ] . VertexID ;
for ( const FEdgeID VertexConnectedEdgeID : VertexArray [ VertexID0 ] . ConnectedEdgeIDs )
{
const FVertexID EdgeVertexID0 = EdgeArray [ VertexConnectedEdgeID ] . VertexIDs [ 0 ] ;
const FVertexID EdgeVertexID1 = EdgeArray [ VertexConnectedEdgeID ] . VertexIDs [ 1 ] ;
if ( ( EdgeVertexID0 = = VertexID0 & & EdgeVertexID1 = = VertexID1 ) | | ( EdgeVertexID0 = = VertexID1 & & EdgeVertexID1 = = VertexID0 ) )
{
return VertexConnectedEdgeID ;
}
}
return FEdgeID : : Invalid ;
2017-12-28 17:15:00 -05:00
}
2018-06-07 16:22:56 -04:00
2019-09-10 11:35:20 -04:00
void FMeshDescription : : SetPolygonVertexInstance ( const FPolygonID PolygonID , const int32 PerimeterIndex , const FVertexInstanceID VertexInstanceID )
{
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
2019-12-19 18:07:47 -05:00
check ( PerimeterIndex > = 0 & & PerimeterIndex < Polygon . VertexInstanceIDs . Num ( ) ) ;
2019-09-10 11:35:20 -04:00
// Disconnect old vertex instance from polygon, and connect new one
2019-12-19 18:07:47 -05:00
const FVertexInstanceID OldVertexInstanceID = Polygon . VertexInstanceIDs [ PerimeterIndex ] ;
2019-09-10 11:35:20 -04:00
2019-12-19 18:07:47 -05:00
Polygon . VertexInstanceIDs [ PerimeterIndex ] = VertexInstanceID ;
2019-09-10 11:35:20 -04:00
// Fix up triangle list
for ( const FTriangleID TriangleID : Polygon . TriangleIDs )
2017-12-28 17:15:00 -05:00
{
2019-09-10 11:35:20 -04:00
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
for ( int32 VertexIndex = 0 ; VertexIndex < 3 ; + + VertexIndex )
{
if ( Triangle . GetVertexInstanceID ( VertexIndex ) = = OldVertexInstanceID )
{
verify ( VertexInstanceArray [ OldVertexInstanceID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
check ( ! VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . Add ( TriangleID ) ;
Triangle . SetVertexInstanceID ( VertexIndex , VertexInstanceID ) ;
}
}
2017-12-28 17:15:00 -05:00
}
}
2018-06-07 16:22:56 -04:00
FPlane FMeshDescription : : ComputePolygonPlane ( const FPolygonID PolygonID ) const
2017-12-28 17:15:00 -05:00
{
2019-06-07 11:22:52 -04:00
// NOTE: This polygon plane computation code is partially based on the implementation of "Newell's method" from Real-Time
2017-12-28 17:15:00 -05:00
// Collision Detection by Christer Ericson, published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc
// @todo mesheditor perf: For polygons that are just triangles, use a cross product to get the normal fast!
// @todo mesheditor perf: We could skip computing the plane distance when we only need the normal
// @todo mesheditor perf: We could cache these computed polygon normals; or just use the normal of the first three vertices' triangle if it is satisfactory in all cases
// @todo mesheditor: For non-planar polygons, the result can vary. Ideally this should use the actual polygon triangulation as opposed to the arbitrary triangulation used here.
FVector Centroid = FVector : : ZeroVector ;
FVector Normal = FVector : : ZeroVector ;
2019-06-07 11:22:52 -04:00
TArray < FVertexID > PerimeterVertexIDs ;
2019-09-10 11:35:20 -04:00
GetPolygonVertices ( PolygonID , /* Out */ PerimeterVertexIDs ) ;
2017-12-28 17:15:00 -05:00
2018-06-07 16:22:56 -04:00
// @todo Maybe this shouldn't be in FMeshDescription but in a utility class, as it references a specific attribute name
New attribute array API.
Fixed some flaws in the original API, deprecated various methods, and introduced some new features.
- Now attribute arrays are accessed via TAttributesRef or TAttributesView (and corresponding const versions). These value types hold references to attribute arrays, and individual elements can be accessed by their element ID and attribute index. Using a value type is safer than the previous method which required assignment to a const-ref (and not doing so would take a temporary copy of the attribute array).
- The attribute set has been totally flattened, so all attributes of different types are added to the same container. This greatly improves compile times, prevents attributes from being created with the same name but different types, and permits the view feature.
- The class hierarchy has changed to have generic base classes where possible with no particular ElementID type. This reduces the code footprint by no longer generating nearly identical copies of templated methods.
- A TAttributesView allows the user to access an attribute array by the type of their choosing, regardless of its actual type. For example, the Normal attribute may be registered with type FPackedVector, but accessed as if it was an FVector. This allows us to move away from very strong typing, and instead transparently store attributes of a more efficient size, while the user is not affected.
- A transient attribute flag has been added, to denote that a particular attribute should not be saved.
#rb none
[CL 4226081 by Richard TalbotWatkin in Dev-Editor branch]
2018-07-20 18:58:37 -04:00
TVertexAttributesConstRef < FVector > VertexPositions = VertexAttributes ( ) . GetAttributesRef < FVector > ( MeshAttribute : : Vertex : : Position ) ;
2017-12-28 17:15:00 -05:00
// Use 'Newell's Method' to compute a robust 'best fit' plane from the vertices of this polygon
2018-06-07 16:22:56 -04:00
for ( int32 VertexNumberI = PerimeterVertexIDs . Num ( ) - 1 , VertexNumberJ = 0 ; VertexNumberJ < PerimeterVertexIDs . Num ( ) ; VertexNumberI = VertexNumberJ , VertexNumberJ + + )
2017-12-28 17:15:00 -05:00
{
2018-06-07 16:22:56 -04:00
const FVertexID VertexIDI = PerimeterVertexIDs [ VertexNumberI ] ;
const FVector PositionI = VertexPositions [ VertexIDI ] ;
2017-12-28 17:15:00 -05:00
2018-06-07 16:22:56 -04:00
const FVertexID VertexIDJ = PerimeterVertexIDs [ VertexNumberJ ] ;
const FVector PositionJ = VertexPositions [ VertexIDJ ] ;
2017-12-28 17:15:00 -05:00
Centroid + = PositionJ ;
2018-06-07 16:22:56 -04:00
Normal . X + = ( PositionJ . Y - PositionI . Y ) * ( PositionI . Z + PositionJ . Z ) ;
Normal . Y + = ( PositionJ . Z - PositionI . Z ) * ( PositionI . X + PositionJ . X ) ;
Normal . Z + = ( PositionJ . X - PositionI . X ) * ( PositionI . Y + PositionJ . Y ) ;
2017-12-28 17:15:00 -05:00
}
Normal . Normalize ( ) ;
// Construct a plane from the normal and centroid
2018-06-07 16:22:56 -04:00
return FPlane ( Normal , FVector : : DotProduct ( Centroid , Normal ) / ( float ) PerimeterVertexIDs . Num ( ) ) ;
2017-12-28 17:15:00 -05:00
}
2018-06-07 16:22:56 -04:00
FVector FMeshDescription : : ComputePolygonNormal ( const FPolygonID PolygonID ) const
2017-12-28 17:15:00 -05:00
{
// @todo mesheditor: Polygon normals are now computed and cached when changes are made to a polygon.
// In theory, we can just return that cached value, but we need to check that there is nothing which relies on the value being correct before
// the cache is updated at the end of a modification.
2018-06-07 16:22:56 -04:00
const FPlane PolygonPlane = ComputePolygonPlane ( PolygonID ) ;
const FVector PolygonNormal ( PolygonPlane . X , PolygonPlane . Y , PolygonPlane . Z ) ;
2017-12-28 17:15:00 -05:00
return PolygonNormal ;
}
2018-06-07 16:22:56 -04:00
2019-09-10 11:35:20 -04:00
/** Returns true if the triangle formed by the specified three positions has a normal that is facing the opposite direction of the reference normal */
static bool IsTriangleFlipped ( const FVector ReferenceNormal , const FVector VertexPositionA , const FVector VertexPositionB , const FVector VertexPositionC )
{
const FVector TriangleNormal = FVector : : CrossProduct (
VertexPositionC - VertexPositionA ,
VertexPositionB - VertexPositionA ) . GetSafeNormal ( ) ;
return ( FVector : : DotProduct ( ReferenceNormal , TriangleNormal ) < = 0.0f ) ;
}
/** Given three direction vectors, indicates if A and B are on the same 'side' of Vec. */
static bool VectorsOnSameSide ( const FVector & Vec , const FVector & A , const FVector & B , const float SameSideDotProductEpsilon )
{
const FVector CrossA = FVector : : CrossProduct ( Vec , A ) ;
const FVector CrossB = FVector : : CrossProduct ( Vec , B ) ;
float DotWithEpsilon = SameSideDotProductEpsilon + FVector : : DotProduct ( CrossA , CrossB ) ;
return ! FMath : : IsNegativeFloat ( DotWithEpsilon ) ;
}
/** Util to see if P lies within triangle created by A, B and C. */
static bool PointInTriangle ( const FVector & A , const FVector & B , const FVector & C , const FVector & P , const float InsideTriangleDotProductEpsilon )
{
// Cross product indicates which 'side' of the vector the point is on
// If its on the same side as the remaining vert for all edges, then its inside.
return ( VectorsOnSameSide ( B - A , P - A , C - A , InsideTriangleDotProductEpsilon ) & &
VectorsOnSameSide ( C - B , P - B , A - B , InsideTriangleDotProductEpsilon ) & &
VectorsOnSameSide ( A - C , P - C , B - C , InsideTriangleDotProductEpsilon ) ) ;
}
void FMeshDescription : : ComputePolygonTriangulation ( const FPolygonID PolygonID )
2017-12-28 17:15:00 -05:00
{
// NOTE: This polygon triangulation code is partially based on the ear cutting algorithm described on
// page 497 of the book "Real-time Collision Detection", published in 2005.
2019-09-10 11:35:20 -04:00
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
2019-12-19 18:07:47 -05:00
const TArray < FVertexInstanceID > & PolygonVertexInstanceIDs = Polygon . VertexInstanceIDs ;
2017-12-28 17:15:00 -05:00
// Polygon must have at least three vertices/edges
const int32 PolygonVertexCount = PolygonVertexInstanceIDs . Num ( ) ;
check ( PolygonVertexCount > = 3 ) ;
2019-09-10 11:35:20 -04:00
// If polygon was already triangulated, and only has three vertices, no need to do anything here
if ( Polygon . TriangleIDs . Num ( ) = = 1 & & PolygonVertexCount = = 3 )
{
return ;
}
// Remove currently configured triangles
for ( const FTriangleID TriangleID : Polygon . TriangleIDs )
{
// Disconnect triangles from vertex instances
for ( const FVertexInstanceID VertexInstanceID : GetTriangleVertexInstances ( TriangleID ) )
{
verify ( VertexInstanceArray [ VertexInstanceID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
}
// Disconnect triangles from perimeter edges, and delete internal edges
for ( const FEdgeID EdgeID : GetTriangleEdges ( TriangleID ) )
{
if ( EdgeID ! = FEdgeID : : Invalid )
{
// The edge may be invalid if it was an internal edge which was deleted in a previous iteration through the triangles.
// So only do something with valid edges
if ( IsEdgeInternal ( EdgeID ) )
{
// Remove internal edges completely (the first time they are seen)
for ( const FVertexID VertexID : GetEdgeVertices ( EdgeID ) )
{
// Disconnect edge from vertices
verify ( VertexArray [ VertexID ] . ConnectedEdgeIDs . RemoveSingle ( EdgeID ) = = 1 ) ;
}
EdgeArray . Remove ( EdgeID ) ;
EdgeAttributesSet . Remove ( EdgeID ) ;
}
else
{
// Don't remove perimeter edge, but disconnect this triangle from it
verify ( EdgeArray [ EdgeID ] . ConnectedTriangles . RemoveSingle ( TriangleID ) = = 1 ) ;
}
}
}
TriangleArray . Remove ( TriangleID ) ;
TriangleAttributesSet . Remove ( TriangleID ) ;
}
Polygon . TriangleIDs . Reset ( ) ;
// If perimeter only has 3 vertices, just add a single triangle and return
2018-06-07 18:49:50 -04:00
if ( PolygonVertexCount = = 3 )
{
2019-09-10 11:35:20 -04:00
const FTriangleID TriangleID = TriangleArray . Add ( ) ;
TriangleAttributesSet . Insert ( TriangleID ) ;
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
Triangle . PolygonID = PolygonID ;
Polygon . TriangleIDs . Add ( TriangleID ) ;
2018-06-07 18:49:50 -04:00
2019-09-10 11:35:20 -04:00
for ( int32 Index = 0 ; Index < 3 ; + + Index )
{
const FVertexInstanceID ThisVertexInstanceID = PolygonVertexInstanceIDs [ Index ] ;
const FVertexInstanceID NextVertexInstanceID = PolygonVertexInstanceIDs [ ( Index = = 2 ) ? 0 : Index + 1 ] ;
const FVertexID ThisVertexID = GetVertexInstanceVertex ( ThisVertexInstanceID ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
const FEdgeID EdgeID = GetVertexPairEdge ( ThisVertexID , NextVertexID ) ;
check ( EdgeID ! = FEdgeID : : Invalid ) ;
Triangle . SetVertexInstanceID ( Index , ThisVertexInstanceID ) ;
check ( ! EdgeArray [ EdgeID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
EdgeArray [ EdgeID ] . ConnectedTriangles . Add ( TriangleID ) ;
check ( ! VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . Add ( TriangleID ) ;
}
2018-06-07 18:49:50 -04:00
return ;
}
2019-09-10 11:35:20 -04:00
// @todo mesheditor: Perhaps should always attempt to triangulate by splitting polygons along the shortest edge, for better determinism.
2017-12-28 17:15:00 -05:00
// First figure out the polygon normal. We need this to determine which triangles are convex, so that
// we can figure out which ears to clip
const FVector PolygonNormal = ComputePolygonNormal ( PolygonID ) ;
// Make a simple linked list array of the previous and next vertex numbers, for each vertex number
// in the polygon. This will just save us having to iterate later on.
2019-09-10 11:35:20 -04:00
TArray < int32 > PrevVertexNumbers ;
TArray < int32 > NextVertexNumbers ;
TArray < FVector > VertexPositions ;
2017-12-28 17:15:00 -05:00
{
New attribute array API.
Fixed some flaws in the original API, deprecated various methods, and introduced some new features.
- Now attribute arrays are accessed via TAttributesRef or TAttributesView (and corresponding const versions). These value types hold references to attribute arrays, and individual elements can be accessed by their element ID and attribute index. Using a value type is safer than the previous method which required assignment to a const-ref (and not doing so would take a temporary copy of the attribute array).
- The attribute set has been totally flattened, so all attributes of different types are added to the same container. This greatly improves compile times, prevents attributes from being created with the same name but different types, and permits the view feature.
- The class hierarchy has changed to have generic base classes where possible with no particular ElementID type. This reduces the code footprint by no longer generating nearly identical copies of templated methods.
- A TAttributesView allows the user to access an attribute array by the type of their choosing, regardless of its actual type. For example, the Normal attribute may be registered with type FPackedVector, but accessed as if it was an FVector. This allows us to move away from very strong typing, and instead transparently store attributes of a more efficient size, while the user is not affected.
- A transient attribute flag has been added, to denote that a particular attribute should not be saved.
#rb none
[CL 4226081 by Richard TalbotWatkin in Dev-Editor branch]
2018-07-20 18:58:37 -04:00
const TVertexAttributesRef < FVector > MeshVertexPositions = VertexAttributes ( ) . GetAttributesRef < FVector > ( MeshAttribute : : Vertex : : Position ) ;
2017-12-28 17:15:00 -05:00
PrevVertexNumbers . SetNumUninitialized ( PolygonVertexCount , false ) ;
NextVertexNumbers . SetNumUninitialized ( PolygonVertexCount , false ) ;
VertexPositions . SetNumUninitialized ( PolygonVertexCount , false ) ;
for ( int32 VertexNumber = 0 ; VertexNumber < PolygonVertexCount ; + + VertexNumber )
{
PrevVertexNumbers [ VertexNumber ] = VertexNumber - 1 ;
NextVertexNumbers [ VertexNumber ] = VertexNumber + 1 ;
VertexPositions [ VertexNumber ] = MeshVertexPositions [ GetVertexInstanceVertex ( PolygonVertexInstanceIDs [ VertexNumber ] ) ] ;
}
PrevVertexNumbers [ 0 ] = PolygonVertexCount - 1 ;
NextVertexNumbers [ PolygonVertexCount - 1 ] = 0 ;
}
int32 EarVertexNumber = 0 ;
int32 EarTestCount = 0 ;
for ( int32 RemainingVertexCount = PolygonVertexCount ; RemainingVertexCount > = 3 ; )
{
bool bIsEar = true ;
// If we're down to only a triangle, just treat it as an ear. Also, if we've tried every possible candidate
// vertex looking for an ear, go ahead and just treat the current vertex as an ear. This can happen when
// vertices are colinear or other degenerate cases.
if ( RemainingVertexCount > 3 & & EarTestCount < RemainingVertexCount )
{
const FVector PrevVertexPosition = VertexPositions [ PrevVertexNumbers [ EarVertexNumber ] ] ;
const FVector EarVertexPosition = VertexPositions [ EarVertexNumber ] ;
const FVector NextVertexPosition = VertexPositions [ NextVertexNumbers [ EarVertexNumber ] ] ;
2019-09-10 11:35:20 -04:00
// Figure out whether the potential ear triangle is facing the same direction as the polygon
// itself. If it's facing the opposite direction, then we're dealing with a concave triangle
// and we'll skip it for now.
if ( ! IsTriangleFlipped ( PolygonNormal , PrevVertexPosition , EarVertexPosition , NextVertexPosition ) )
{
int32 TestVertexNumber = NextVertexNumbers [ NextVertexNumbers [ EarVertexNumber ] ] ;
do
{
// Test every other remaining vertex to make sure that it doesn't lie inside our potential ear
// triangle. If we find a vertex that's inside the triangle, then it cannot actually be an ear.
const FVector TestVertexPosition = VertexPositions [ TestVertexNumber ] ;
if ( PointInTriangle ( PrevVertexPosition , EarVertexPosition , NextVertexPosition , TestVertexPosition , SMALL_NUMBER ) )
{
bIsEar = false ;
break ;
}
TestVertexNumber = NextVertexNumbers [ TestVertexNumber ] ;
} while ( TestVertexNumber ! = PrevVertexNumbers [ EarVertexNumber ] ) ;
}
else
{
bIsEar = false ;
}
}
if ( bIsEar )
{
// OK, we found an ear! Let's save this triangle in our output buffer.
// This will also create any missing internal edges.
{
// Add a new triangle
const FTriangleID TriangleID = TriangleArray . Add ( ) ;
TriangleAttributesSet . Insert ( TriangleID ) ;
// Set its vertex instances and connect it to its parent polygon
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
Triangle . SetVertexInstanceID ( 0 , PolygonVertexInstanceIDs [ PrevVertexNumbers [ EarVertexNumber ] ] ) ;
Triangle . SetVertexInstanceID ( 1 , PolygonVertexInstanceIDs [ EarVertexNumber ] ) ;
Triangle . SetVertexInstanceID ( 2 , PolygonVertexInstanceIDs [ NextVertexNumbers [ EarVertexNumber ] ] ) ;
Triangle . PolygonID = PolygonID ;
check ( ! Polygon . TriangleIDs . Contains ( TriangleID ) ) ;
Polygon . TriangleIDs . Add ( TriangleID ) ;
// Now generate internal edges and connected vertex instances to the new triangle
for ( int32 Index = 0 ; Index < 3 ; + + Index )
{
const FVertexInstanceID ThisVertexInstanceID = Triangle . GetVertexInstanceID ( Index ) ;
const FVertexInstanceID NextVertexInstanceID = Triangle . GetVertexInstanceID ( ( Index = = 2 ) ? 0 : Index + 1 ) ;
const FVertexID ThisVertexID = GetVertexInstanceVertex ( ThisVertexInstanceID ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( NextVertexInstanceID ) ;
FEdgeID EdgeID = GetVertexPairEdge ( ThisVertexID , NextVertexID ) ;
if ( EdgeID = = FEdgeID : : Invalid )
{
// This must be an internal edge (as perimeter edges will already be defined)
EdgeID = CreateEdge ( ThisVertexID , NextVertexID ) ;
}
check ( ! VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
VertexInstanceArray [ ThisVertexInstanceID ] . ConnectedTriangles . Add ( TriangleID ) ;
check ( ! EdgeArray [ EdgeID ] . ConnectedTriangles . Contains ( TriangleID ) ) ;
EdgeArray [ EdgeID ] . ConnectedTriangles . Add ( TriangleID ) ;
}
}
// Update our linked list. We're effectively cutting off the ear by pointing the ear vertex's neighbors to
// point at their next sequential neighbor, and reducing the remaining vertex count by one.
{
NextVertexNumbers [ PrevVertexNumbers [ EarVertexNumber ] ] = NextVertexNumbers [ EarVertexNumber ] ;
PrevVertexNumbers [ NextVertexNumbers [ EarVertexNumber ] ] = PrevVertexNumbers [ EarVertexNumber ] ;
- - RemainingVertexCount ;
}
// Move on to the previous vertex in the list, now that this vertex was cut
EarVertexNumber = PrevVertexNumbers [ EarVertexNumber ] ;
EarTestCount = 0 ;
}
else
{
// The vertex is not the ear vertex, because it formed a triangle that either had a normal which pointed in the opposite direction
// of the polygon, or at least one of the other polygon vertices was found to be inside the triangle. Move on to the next vertex.
EarVertexNumber = NextVertexNumbers [ EarVertexNumber ] ;
// Keep track of how many ear vertices we've tested, so that if we exhaust all remaining vertices, we can
// fall back to clipping the triangle and adding it to our mesh anyway. This is important for degenerate cases.
+ + EarTestCount ;
}
}
check ( Polygon . TriangleIDs . Num ( ) > 0 ) ;
}
2019-10-01 20:41:42 -04:00
FBoxSphereBounds FMeshDescription : : GetBounds ( ) const
{
FBoxSphereBounds BoundingBoxAndSphere ;
FBox BoundingBox ;
BoundingBox . Init ( ) ;
TVertexAttributesConstRef < FVector > VertexPositions = VertexAttributes ( ) . GetAttributesRef < FVector > ( MeshAttribute : : Vertex : : Position ) ;
for ( const FVertexID VertexID : Vertices ( ) . GetElementIDs ( ) )
{
if ( ! IsVertexOrphaned ( VertexID ) )
{
BoundingBox + = VertexPositions [ VertexID ] ;
}
}
BoundingBox . GetCenterAndExtents ( BoundingBoxAndSphere . Origin , BoundingBoxAndSphere . BoxExtent ) ;
// Calculate the bounding sphere, using the center of the bounding box as the origin.
BoundingBoxAndSphere . SphereRadius = 0.0f ;
for ( const FVertexID VertexID : Vertices ( ) . GetElementIDs ( ) )
{
if ( ! IsVertexOrphaned ( VertexID ) )
{
BoundingBoxAndSphere . SphereRadius = FMath : : Max ( ( VertexPositions [ VertexID ] - BoundingBoxAndSphere . Origin ) . Size ( ) , BoundingBoxAndSphere . SphereRadius ) ;
}
}
return BoundingBoxAndSphere ;
}
2018-06-07 16:22:56 -04:00
void FMeshDescription : : TriangulateMesh ( )
2017-12-28 17:15:00 -05:00
{
// Perform triangulation directly into mesh polygons
2018-06-07 16:22:56 -04:00
for ( const FPolygonID PolygonID : Polygons ( ) . GetElementIDs ( ) )
2017-12-28 17:15:00 -05:00
{
2019-09-10 11:35:20 -04:00
ComputePolygonTriangulation ( PolygonID ) ;
2017-12-28 17:15:00 -05:00
}
2018-01-04 15:57:54 -05:00
}
2018-06-07 16:22:56 -04:00
2019-10-01 20:41:42 -04:00
namespace MeshAttribute_
{
namespace Vertex
{
const FName CornerSharpness ( " CornerSharpness " ) ;
}
namespace VertexInstance
{
const FName TextureCoordinate ( " TextureCoordinate " ) ;
const FName Normal ( " Normal " ) ;
const FName Tangent ( " Tangent " ) ;
const FName BinormalSign ( " BinormalSign " ) ;
const FName Color ( " Color " ) ;
}
namespace Edge
{
const FName IsHard ( " IsHard " ) ;
const FName IsUVSeam ( " IsUVSeam " ) ;
const FName CreaseSharpness ( " CreaseSharpness " ) ;
}
namespace Polygon
{
const FName Normal ( " Normal " ) ;
const FName Tangent ( " Tangent " ) ;
const FName Binormal ( " Binormal " ) ;
const FName Center ( " Center " ) ;
}
namespace PolygonGroup
{
const FName ImportedMaterialSlotName ( " ImportedMaterialSlotName " ) ;
const FName EnableCollision ( " EnableCollision " ) ;
const FName CastShadow ( " CastShadow " ) ;
}
}
2018-06-07 16:22:56 -04:00
float FMeshDescription : : GetPolygonCornerAngleForVertex ( const FPolygonID PolygonID , const FVertexID VertexID ) const
2018-03-22 16:02:58 -04:00
{
2019-09-10 11:35:20 -04:00
const FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
2018-03-22 16:02:58 -04:00
// Lambda function which returns the inner angle at a given index on a polygon contour
2019-12-19 18:07:47 -05:00
auto GetContourAngle = [ this ] ( const TArray < FVertexInstanceID > & VertexInstanceIDs , const int32 ContourIndex )
2018-03-22 16:02:58 -04:00
{
2019-12-19 18:07:47 -05:00
const int32 NumVertices = VertexInstanceIDs . Num ( ) ;
2018-03-22 16:02:58 -04:00
const int32 PrevIndex = ( ContourIndex + NumVertices - 1 ) % NumVertices ;
const int32 NextIndex = ( ContourIndex + 1 ) % NumVertices ;
2019-12-19 18:07:47 -05:00
const FVertexID PrevVertexID = GetVertexInstanceVertex ( VertexInstanceIDs [ PrevIndex ] ) ;
const FVertexID ThisVertexID = GetVertexInstanceVertex ( VertexInstanceIDs [ ContourIndex ] ) ;
const FVertexID NextVertexID = GetVertexInstanceVertex ( VertexInstanceIDs [ NextIndex ] ) ;
2018-03-22 16:02:58 -04:00
New attribute array API.
Fixed some flaws in the original API, deprecated various methods, and introduced some new features.
- Now attribute arrays are accessed via TAttributesRef or TAttributesView (and corresponding const versions). These value types hold references to attribute arrays, and individual elements can be accessed by their element ID and attribute index. Using a value type is safer than the previous method which required assignment to a const-ref (and not doing so would take a temporary copy of the attribute array).
- The attribute set has been totally flattened, so all attributes of different types are added to the same container. This greatly improves compile times, prevents attributes from being created with the same name but different types, and permits the view feature.
- The class hierarchy has changed to have generic base classes where possible with no particular ElementID type. This reduces the code footprint by no longer generating nearly identical copies of templated methods.
- A TAttributesView allows the user to access an attribute array by the type of their choosing, regardless of its actual type. For example, the Normal attribute may be registered with type FPackedVector, but accessed as if it was an FVector. This allows us to move away from very strong typing, and instead transparently store attributes of a more efficient size, while the user is not affected.
- A transient attribute flag has been added, to denote that a particular attribute should not be saved.
#rb none
[CL 4226081 by Richard TalbotWatkin in Dev-Editor branch]
2018-07-20 18:58:37 -04:00
TVertexAttributesConstRef < FVector > VertexPositions = VertexAttributes ( ) . GetAttributesRef < FVector > ( MeshAttribute : : Vertex : : Position ) ;
2018-03-22 16:02:58 -04:00
const FVector PrevVertexPosition = VertexPositions [ PrevVertexID ] ;
const FVector ThisVertexPosition = VertexPositions [ ThisVertexID ] ;
const FVector NextVertexPosition = VertexPositions [ NextVertexID ] ;
const FVector Direction1 = ( PrevVertexPosition - ThisVertexPosition ) . GetSafeNormal ( ) ;
const FVector Direction2 = ( NextVertexPosition - ThisVertexPosition ) . GetSafeNormal ( ) ;
return FMath : : Acos ( FVector : : DotProduct ( Direction1 , Direction2 ) ) ;
} ;
const FVertexInstanceArray & VertexInstancesRef = VertexInstances ( ) ;
auto IsVertexInstancedFromThisVertex = [ & VertexInstancesRef , VertexID ] ( const FVertexInstanceID VertexInstanceID )
{
return VertexInstancesRef [ VertexInstanceID ] . VertexID = = VertexID ;
} ;
// First look for the vertex instance in the perimeter
2019-12-19 18:07:47 -05:00
int32 ContourIndex = Polygon . VertexInstanceIDs . IndexOfByPredicate ( IsVertexInstancedFromThisVertex ) ;
2018-03-22 16:02:58 -04:00
if ( ContourIndex ! = INDEX_NONE )
{
// Return the internal angle if found
2019-12-19 18:07:47 -05:00
return GetContourAngle ( Polygon . VertexInstanceIDs , ContourIndex ) ;
2018-03-22 16:02:58 -04:00
}
// Found nothing; return 0
return 0.0f ;
}
2019-10-04 13:11:45 -04:00
FBox FMeshDescription : : ComputeBoundingBox ( ) const
{
FBox BoundingBox ( ForceInit ) ;
TVertexAttributesConstRef < FVector > VertexPositions = VertexAttributes ( ) . GetAttributesRef < FVector > ( MeshAttribute : : Vertex : : Position ) ;
for ( const FVertexID VertexID : Vertices ( ) . GetElementIDs ( ) )
{
BoundingBox + = VertexPositions [ VertexID ] ;
}
return BoundingBox ;
}
2018-06-07 16:22:56 -04:00
2018-03-29 05:42:07 -04:00
2018-06-07 16:22:56 -04:00
void FMeshDescription : : ReversePolygonFacing ( const FPolygonID PolygonID )
2018-01-04 15:57:54 -05:00
{
2019-09-10 11:35:20 -04:00
// Build a reverse perimeter
FMeshPolygon & Polygon = PolygonArray [ PolygonID ] ;
2019-12-19 18:07:47 -05:00
for ( int32 i = 0 ; i < Polygon . VertexInstanceIDs . Num ( ) / 2 ; + + i )
2018-01-04 15:57:54 -05:00
{
2019-12-19 18:07:47 -05:00
Polygon . VertexInstanceIDs . Swap ( i , Polygon . VertexInstanceIDs . Num ( ) - i - 1 ) ;
2018-01-04 15:57:54 -05:00
}
2019-06-07 11:22:52 -04:00
2019-10-04 13:11:45 -04:00
// Update the polygon's triangle vertex instance ids with the reversed ids
for ( FTriangleID TriangleID : GetPolygonTriangleIDs ( PolygonID ) )
{
FMeshTriangle & Triangle = TriangleArray [ TriangleID ] ;
2019-12-19 18:07:47 -05:00
Swap ( Triangle . VertexInstanceIDs [ 0 ] , Triangle . VertexInstanceIDs [ 2 ] ) ;
2019-10-04 13:11:45 -04:00
}
2018-01-04 15:57:54 -05:00
}
2018-06-07 16:22:56 -04:00
void FMeshDescription : : ReverseAllPolygonFacing ( )
2018-01-04 15:57:54 -05:00
{
// Perform triangulation directly into mesh polygons
for ( const FPolygonID PolygonID : Polygons ( ) . GetElementIDs ( ) )
{
ReversePolygonFacing ( PolygonID ) ;
}
2018-12-10 09:29:08 -05:00
}
2019-09-10 11:35:20 -04:00
void FMeshDescription : : RemapPolygonGroups ( const TMap < FPolygonGroupID , FPolygonGroupID > & Remap )
{
2019-10-01 20:41:42 -04:00
TPolygonGroupAttributesRef < FName > PolygonGroupNames = PolygonGroupAttributes ( ) . GetAttributesRef < FName > ( MeshAttribute_ : : PolygonGroup : : ImportedMaterialSlotName ) ;
2019-09-10 11:35:20 -04:00
struct FOldPolygonGroupData
{
FName Name ;
TArray < FPolygonID > Polygons ;
} ;
TMap < FPolygonGroupID , FOldPolygonGroupData > OldData ;
for ( const FPolygonGroupID & PolygonGroupID : PolygonGroups ( ) . GetElementIDs ( ) )
{
if ( ! Remap . Contains ( PolygonGroupID ) | | PolygonGroupID = = Remap [ PolygonGroupID ] )
{
//No need to change this one
continue ;
}
FOldPolygonGroupData & PolygonGroupData = OldData . FindOrAdd ( PolygonGroupID ) ;
PolygonGroupData . Name = PolygonGroupNames [ PolygonGroupID ] ;
FMeshPolygonGroup & PolygonGroup = PolygonGroupArray [ PolygonGroupID ] ;
PolygonGroupData . Polygons = PolygonGroup . Polygons ;
PolygonGroup . Polygons . Empty ( ) ;
DeletePolygonGroup ( PolygonGroupID ) ;
}
for ( auto Kvp : OldData )
{
FPolygonGroupID GroupID = Kvp . Key ;
FPolygonGroupID ToGroupID = Remap [ GroupID ] ;
if ( ! PolygonGroups ( ) . IsValid ( ToGroupID ) )
{
CreatePolygonGroupWithID ( ToGroupID ) ;
}
TArray < FPolygonID > & Polygons = PolygonGroupArray [ ToGroupID ] . Polygons ;
Polygons . Append ( Kvp . Value . Polygons ) ;
PolygonGroupNames [ ToGroupID ] = Kvp . Value . Name ;
for ( FPolygonID PolygonID : Polygons )
{
PolygonArray [ PolygonID ] . PolygonGroupID = ToGroupID ;
}
}
}
2018-12-10 09:29:08 -05:00
# if WITH_EDITORONLY_DATA
void FMeshDescriptionBulkData : : Serialize ( FArchive & Ar , UObject * Owner )
{
2019-02-07 05:53:07 -05:00
Ar . UsingCustomVersion ( FEditorObjectVersion : : GUID ) ;
2019-10-04 13:11:45 -04:00
Ar . UsingCustomVersion ( FEnterpriseObjectVersion : : GUID ) ;
2019-02-07 05:53:07 -05:00
if ( Ar . IsTransacting ( ) )
2019-02-02 12:38:12 -05:00
{
2019-02-07 05:53:07 -05:00
// If transacting, keep these members alive the other side of an undo, otherwise their values will get lost
CustomVersions . Serialize ( Ar ) ;
Ar < < bBulkDataUpdated ;
}
else
{
if ( Ar . IsLoading ( ) )
{
// If loading, take a copy of the package custom version container, so it can be applied when unpacking
// MeshDescription from the bulk data.
CustomVersions = Ar . GetCustomVersions ( ) ;
}
else if ( Ar . IsSaving ( ) )
{
// If the bulk data hasn't been updated since this was loaded, there's a possibility that it has old versioning.
// Explicitly load and resave the FMeshDescription so that its version is in sync with the FMeshDescriptionBulkData.
if ( ! bBulkDataUpdated )
{
FMeshDescription MeshDescription ;
LoadMeshDescription ( MeshDescription ) ;
SaveMeshDescription ( MeshDescription ) ;
}
}
2019-02-02 12:38:12 -05:00
}
2018-12-10 09:29:08 -05:00
BulkData . Serialize ( Ar , Owner ) ;
2018-12-10 19:34:09 -05:00
if ( Ar . IsLoading ( ) & & Ar . CustomVer ( FEditorObjectVersion : : GUID ) < FEditorObjectVersion : : MeshDescriptionBulkDataGuid )
{
FPlatformMisc : : CreateGuid ( Guid ) ;
}
else
{
Ar < < Guid ;
}
2019-10-04 13:11:45 -04:00
// MeshDescriptionBulkData contains a bGuidIsHash so we can benefit from DDC caching.
if ( Ar . IsLoading ( ) & & Ar . CustomVer ( FEnterpriseObjectVersion : : GUID ) < FEnterpriseObjectVersion : : MeshDescriptionBulkDataGuidIsHash )
{
bGuidIsHash = false ;
}
else
{
Ar < < bGuidIsHash ;
}
2018-12-10 09:29:08 -05:00
}
void FMeshDescriptionBulkData : : SaveMeshDescription ( FMeshDescription & MeshDescription )
{
2019-10-04 13:11:45 -04:00
TRACE_CPUPROFILER_EVENT_SCOPE ( FMeshDescriptionBulkData : : SaveMeshDescription ) ;
2018-12-10 09:29:08 -05:00
BulkData . RemoveBulkData ( ) ;
if ( ! MeshDescription . IsEmpty ( ) )
{
const bool bIsPersistent = true ;
FBulkDataWriter Ar ( BulkData , bIsPersistent ) ;
Ar < < MeshDescription ;
2019-07-09 09:41:08 -04:00
// Preserve CustomVersions at save time so we can reuse the same ones when reloading direct from memory
CustomVersions = Ar . GetCustomVersions ( ) ;
2018-12-10 09:29:08 -05:00
}
2018-12-10 19:34:09 -05:00
2019-10-04 13:11:45 -04:00
if ( bGuidIsHash )
{
UseHashAsGuid ( ) ;
}
else
{
FPlatformMisc : : CreateGuid ( Guid ) ;
}
2019-02-07 05:53:07 -05:00
// Mark the MeshDescriptionBulkData as having been updated.
// This means we know that its version is up-to-date.
bBulkDataUpdated = true ;
2018-12-10 09:29:08 -05:00
}
void FMeshDescriptionBulkData : : LoadMeshDescription ( FMeshDescription & MeshDescription )
{
MeshDescription . Empty ( ) ;
if ( BulkData . GetElementCount ( ) > 0 )
{
// Get a lock on the bulk data and read it into the mesh description
{
const bool bIsPersistent = true ;
FBulkDataReader Ar ( BulkData , bIsPersistent ) ;
2019-02-02 12:38:12 -05:00
// Propagate the custom version information from the package to the bulk data, so that the MeshDescription
// is serialized with the same versioning.
2019-02-07 05:46:47 -05:00
Ar . SetCustomVersions ( CustomVersions ) ;
2018-12-10 09:29:08 -05:00
Ar < < MeshDescription ;
}
// Unlock bulk data when we leave scope
// Throw away the bulk data allocation as we don't need it now we have its contents as a FMeshDescription
2018-12-12 07:00:54 -05:00
// @todo: revisit this
// BulkData.UnloadBulkData();
2018-12-10 09:29:08 -05:00
}
}
void FMeshDescriptionBulkData : : Empty ( )
{
BulkData . RemoveBulkData ( ) ;
}
2018-12-10 19:34:09 -05:00
FString FMeshDescriptionBulkData : : GetIdString ( ) const
{
2019-10-04 13:11:45 -04:00
FString GuidString = Guid . ToString ( ) ;
if ( bGuidIsHash )
{
GuidString + = TEXT ( " X " ) ;
}
return GuidString ;
}
void FMeshDescriptionBulkData : : UseHashAsGuid ( )
{
uint32 Hash [ 5 ] = { } ;
if ( BulkData . GetBulkDataSize ( ) > 0 )
{
bGuidIsHash = true ;
void * Buffer = BulkData . Lock ( LOCK_READ_ONLY ) ;
FSHA1 : : HashBuffer ( Buffer , BulkData . GetBulkDataSize ( ) , ( uint8 * ) Hash ) ;
BulkData . Unlock ( ) ;
}
Guid = FGuid ( Hash [ 0 ] ^ Hash [ 4 ] , Hash [ 1 ] , Hash [ 2 ] , Hash [ 3 ] ) ;
2018-12-10 19:34:09 -05:00
}
2018-12-10 09:29:08 -05:00
# endif // #if WITH_EDITORONLY_DATA