UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GeometryCollectionUtility.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
8#include "Async/ParallelFor.h"
9
10namespace GeometryCollection
11{
12 /***
13 * Add the geometry group to a collection. Mostly for backwards compatibility with older files.
14 */
15 void
18
19 /****
20 * MakeMeshElement
21 * Utility to create an arbitrary triangulated mesh using the FGeometryCollection format.
22 */
23 template <class TV3_PTS, class TV3_NORM, class TV2, class TV_INT3>
28 const TArray<TV2>& UVsIn,
29 const FTransform& Xf,
31 const int NumberOfMaterials = 2)
32 {
33 FGeometryCollection* RestCollection = new FGeometryCollection();
37
38 TManagedArray<FVector3f>& Vertices = RestCollection->Vertex;
39 TManagedArray<FVector3f>& Normals = RestCollection->Normal;
40 TManagedArray<FVector3f>& TangentU = RestCollection->TangentU;
41 TManagedArray<FVector3f>& TangentV = RestCollection->TangentV;
42 TManagedArray<FVector2f>& UV0 = *RestCollection->FindUVLayer(0);
43 TManagedArray<FLinearColor>& Colors = RestCollection->Color;
44 TManagedArray<FIntVector>& Indices = RestCollection->Indices;
45 TManagedArray<bool>& Visible = RestCollection->Visible;
46 TManagedArray<int32>& MaterialIndex = RestCollection->MaterialIndex;
47 TManagedArray<int32>& MaterialID = RestCollection->MaterialID;
48 TManagedArray<bool>& Internal = RestCollection->Internal;
50 TManagedArray<int32>& SimulationType = RestCollection->SimulationType;
51 TManagedArray<int32>& BoneMap = RestCollection->BoneMap;
52
53 // Set particle info
55 Transform[0].NormalizeRotation();
57
58 // Set vertex info
59 for (int32 Idx = 0; Idx < PointsIn.Num(); ++Idx)
60 {
61 Vertices[Idx] = (FVector3f)GeoXf.TransformPosition(FVector(PointsIn[Idx][0], PointsIn[Idx][1], PointsIn[Idx][2])); // transform points by GeoXf
62 Normals[Idx] = NormalsIn.Num() > Idx ? FVector3f(NormalsIn[Idx][0], NormalsIn[Idx][1], NormalsIn[Idx][2]) : FVector3f(0);
63
64 FVector2D UV = UVsIn.Num() > Idx ? FVector2D(UVsIn[Idx][0], UVsIn[Idx][1]) : FVector2D(0);
65 UV0[Idx] = FVector2f(UV);
66
67 Colors[Idx] = FLinearColor::White;
68 BoneMap[Idx] = 0;
69 }
70
71 // Set face info
73 for (int32 Idx = 0; Idx < TrianglesIn.Num(); ++Idx)
74 {
75 const auto& Tri = TrianglesIn[Idx];
76 Indices[Idx] = FIntVector(Tri[0], Tri[1], Tri[2]);
77
78 Visible[Idx] = true;
79 Internal[Idx] = false;
80 MaterialIndex[Idx] = Idx;
81 MaterialID[Idx] = Idx / NumberOfEachMaterial;
82
83 for (int32 Axis = 0; Axis < 3; ++Axis)
84 {
85 const FVector3f& Normal = Normals[Tri[Axis]];
86 const FVector3f Edge = (Vertices[Tri[(Axis + 1) % 3]] - Vertices[Tri[Axis]]);
87 TangentU[Tri[Axis]] = (Edge ^ Normal).GetSafeNormal();
88 TangentV[Tri[Axis]] = (Normal ^ TangentU[Tri[Axis]]).GetSafeNormal();
89 }
90 }
91
92 // GeometryGroup
94
95 // Add the materail sections to simulate NumberOfMaterials on the object
96 TManagedArray<FGeometryCollectionSection>& Sections = RestCollection->Sections;
97
98 // the first 6 indices are material 0
100 for (int Element = 0; Element < NumberOfMaterials; Element++)
101 {
102 Sections[Element].MaterialID = Element;
103 Sections[Element].FirstIndex = (Element * NumberOfEachMaterial) * 3;
104 Sections[Element].NumTriangles = NumberOfEachMaterial;
105 Sections[Element].MinVertexIndex = 0;
106 Sections[Element].MaxVertexIndex = Vertices.Num() - 1;
107 }
108
109 return TSharedPtr<FGeometryCollection>(RestCollection);
110 }
111
112 /****
113 * MakeCubeElement
114 * Utility to create a triangulated unit cube using the FGeometryCollection format.
115 */
117 CHAOS_API
119
120 /****
121 * SetupCubeGridExample
122 * Utility to create a grid (10x10x10) of triangulated unit cube using the FGeometryCollection format.
123 */
124 void
125 CHAOS_API
127
128
129 /****
130 * Setup Nested Hierarchy Example
131 */
132 void
135
136 /****
137 * Setup Two Clustered Cubes :
138 * ... geometry { (-9,0,0) && (9,0,0)}
139 * ... center of mass { (-10,0,0) && (10,0,0)}
140 */
141 void
144
145 /***
146 * Ensure Material indices are setup correctly. Mostly for backwards compatibility with older files.
147 */
148 void
149 CHAOS_API
151
152 /***
153 * Transfers attributes from one collection to another based on the nearest vertex
154 * #todo(dmp): We can add a lot of modes here, such as:
155 * - transfer between different attribute groups
156 * - derive attribute values based on different proximity based kernels
157 */
158 template<class T>
159 void
161
162 /***
163 * Generate GUID in the FTransformCollection::TransformGroup for id tracking.
164 * The GUIDs will not be saved during serialization, and can be used to bind
165 * and entry to an location in the collection. NOTE: GUIDs are expensive to
166 * maintain so they should not be kept for cooked content.
167 */
168 void
171
172 /***
173 * Compute inner and outer radius from a set of vertices
174 * @param Vertices array containing the vertices used to computer the radii
175 * @param VertexStart start index of the range of vertex to use within the array
176 * @param VertexCount Number of vertices to use from VertexStart
177 * @param VertexCount Total number of vertices to use (from VertexStart)
178 * @param OutInnerRadius Computed InnerRadius ( existing value will be overriden )
179 * @param OutOuterRadius Computed outerRadius ( existing value will be overriden )
180 */
181 void
183 ComputeInnerAndOuterRadiiFromGeometryVertices(const TManagedArray<FVector3f>& Vertices, const int32 VertexStart, const int32 VertexCount, float& OutInnerRadius, float& OutOuterRadius);
184
185};
186
187// AttributeTransfer implementation
188template<class T>
190{
191 // #todo(dmp): later on we will support different attribute groups for transfer
194
197
198 // for each vertex in ToCollection, find the closest in FromCollection based on vertex position
199 // #todo(dmp): should we be evaluating the transform hierarchy here, or just do it in local space?
200 // #todo(dmp): use spatial hash rather than n^2 lookup
202 {
203 int32 ClosestFromIndex = -1;
204 Chaos::FReal ClosestDist = UE_MAX_FLT;
205 for (int32 FromIndex = 0, ni = FromVertex.Num(); FromIndex < ni ; ++FromIndex)
206 {
207 Chaos::FReal CurrDist = FVector3f::DistSquared(FromVertex[FromIndex], ToVertex[ToIndex]);
208 if (CurrDist < ClosestDist)
209 {
210 ClosestDist = CurrDist;
211 ClosestFromIndex = FromIndex;
212 }
213 }
214
215 // If there is a valid position in FromCollection, transfer attribute
216 if (ClosestFromIndex != -1)
217 {
218 ToAttribute[ToIndex] = FromAttribute[ClosestFromIndex];
219 }
220 });
221}
@ Normal
Definition AndroidInputInterface.h:116
void ParallelFor(int32 Num, TFunctionRef< void(int32)> Body, bool bForceSingleThread, bool bPumpRenderingThread=false)
Definition ParallelFor.h:481
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FORCEINLINE uint32 ToIndex(FHairStrandsTiles::ETileType Type)
Definition HairStrandsData.h:93
#define FVector
Definition IOSSystemIncludes.h:8
UE::Math::TVector2< float > FVector2f
Definition MathFwd.h:74
UE::Math::TVector< float > FVector3f
Definition MathFwd.h:73
UE::Math::TTransform< float > FTransform3f
Definition MathFwd.h:79
UE::Math::TVector2< double > FVector2D
Definition MathFwd.h:48
FInt32Vector3 FIntVector
Definition MathFwd.h:115
if(Failed) console_printf("Failed.\n")
Definition GeometryCollection.h:32
TManagedArray< bool > Internal
Definition GeometryCollection.h:347
static CHAOS_API const FName FacesGroup
Definition GeometryCollection.h:100
TManagedArray< int32 > SimulationType
Definition GeometryCollection.h:307
TManagedArray< FLinearColor > Color
Definition GeometryCollection.h:336
TManagedArray< FVector3f > TangentV
Definition GeometryCollection.h:338
TManagedArray< FVector3f > TangentU
Definition GeometryCollection.h:337
@ FST_Rigid
Definition GeometryCollection.h:133
TManagedArray< FIntVector > Indices
Definition GeometryCollection.h:343
TManagedArray< int32 > MaterialID
Definition GeometryCollection.h:346
TManagedArray< FGeometryCollectionSection > Sections
Definition GeometryCollection.h:360
TManagedArray< int32 > MaterialIndex
Definition GeometryCollection.h:345
static CHAOS_API const FName MaterialGroup
Definition GeometryCollection.h:103
TManagedArray< FVector3f > Vertex
Definition GeometryCollection.h:313
TManagedArray< bool > Visible
Definition GeometryCollection.h:344
TManagedArray< int32 > BoneMap
Definition GeometryCollection.h:340
TManagedArray< FVector3f > Normal
Definition GeometryCollection.h:339
TManagedArray< FVector2f > * FindUVLayer(int32 UVLayer)
Definition GeometryCollection.h:327
static CHAOS_API const FName VerticesGroup
Definition GeometryCollection.h:99
Definition NameTypes.h:617
static CHAOS_API const FName TransformGroup
Definition TransformCollection.h:49
TManagedArray< FTransform3f > Transform
Definition TransformCollection.h:130
Definition Array.h:670
FORCEINLINE int32 Num() const override
Definition ManagedArray.h:519
Definition ManagedArray.h:1099
Definition SharedPointer.h:692
Definition CollectionBoundsFacade.cpp:13
TSharedPtr< FGeometryCollection > MakeCubeElement(const FTransform &center, FVector Scale, int NumberOfMaterials)
Definition GeometryCollectionUtility.cpp:18
void AttributeTransfer(const FGeometryCollection *FromCollection, FGeometryCollection *ToCollection, const FName FromAttributeName, const FName ToAttributeName)
Definition GeometryCollectionUtility.h:189
void SetupTwoClusteredCubesCollection(FGeometryCollection *Collection)
Definition GeometryCollectionUtility.cpp:200
void ComputeInnerAndOuterRadiiFromGeometryVertices(const TManagedArray< FVector3f > &Vertices, const int32 VertexStart, const int32 VertexCount, float &OutInnerRadius, float &OutOuterRadius)
Definition GeometryCollectionUtility.cpp:262
void GenerateTemporaryGuids(FManagedArrayCollection *Collection, int32 StartIdx, bool bForceInit)
Definition GeometryCollectionUtility.cpp:491
void AddGeometryProperties(FManagedArrayCollection *InCollection)
Definition GeometryCollectionUtility.cpp:290
void SetupNestedBoneCollection(FGeometryCollection *Collection)
Definition GeometryCollectionUtility.cpp:238
TSharedPtr< FGeometryCollection > MakeMeshElement(const TArray< TV3_PTS > &PointsIn, const TArray< TV3_NORM > &NormalsIn, const TArray< TV_INT3 > &TrianglesIn, const TArray< TV2 > &UVsIn, const FTransform &Xf, const FTransform &GeoXf=FTransform::Identity, const int NumberOfMaterials=2)
Definition GeometryCollectionUtility.h:24
void SetupCubeGridExample(TSharedPtr< FGeometryCollection > RestCollectionIn)
Definition GeometryCollectionUtility.cpp:167
void MakeMaterialsContiguous(FGeometryCollection *Collection)
Definition GeometryCollectionUtility.cpp:460
Definition ByteSwap.h:14
static CORE_API const FLinearColor White
Definition Color.h:456
Definition ManagedArrayCollection.h:56
CHAOS_API int32 AddElements(int32 NumberElements, FName Group)
Definition ManagedArrayCollection.cpp:246
static CORE_API const TTransform< double > Identity
Definition TransformNonVectorized.h:58