RuntimeMeshShapeGenerator.cpp
Go to the documentation of this file.
1 // Copyright 2016-2018 Chris Conway (Koderz). All Rights Reserved.
2 
5 
6 
7 
8 void URuntimeMeshShapeGenerator::ConvertQuadToTriangles(TArray<int32>& Triangles, int32 Vert0, int32 Vert1, int32 Vert2, int32 Vert3)
9 {
10  Triangles.Add(Vert0);
11  Triangles.Add(Vert1);
12  Triangles.Add(Vert3);
13 
14  Triangles.Add(Vert1);
15  Triangles.Add(Vert2);
16  Triangles.Add(Vert3);
17 }
18 
19 
20 #define CREATEBOX_NUMVERTS 24
21 #define CREATEBOX_NUMTRIS 36
22 #define CREATEGRIDMESHTRIANGLES_NUMVERTS(NumX, NumY) (((NumX) + 1) * ((NumY) + 1))
23 #define CREATEGRIDMESHTRIANGLES_NUMTRIS(NumX, NumY) ((NumX) * (NumY) * 2 * 3)
24 
25 
26 
27 void URuntimeMeshShapeGenerator::CreateBoxMesh(FVector BoxRadius, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FRuntimeMeshTangent>& Tangents)
28 {
29  Vertices.Empty(CREATEBOX_NUMVERTS);
30  Triangles.Empty(CREATEBOX_NUMTRIS);
31  Normals.Empty(CREATEBOX_NUMVERTS);
32  UVs.Empty(CREATEBOX_NUMVERTS);
33  Tangents.Empty(CREATEBOX_NUMVERTS);
34 
35  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
36  {
37  Vertices.Add(Position);
38  Normals.Add(Normal);
39  Tangents.Add(Tangent);
40  UVs.Add(UV0);
41  };
42 
43  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
44  {
45  Triangles.Add(Index);
46  };
47 
48  CreateBoxMesh(BoxRadius, VerticesBuilder, TrianglesBuilder);
49 }
50 
51 void URuntimeMeshShapeGenerator::CreateBoxMeshPacked(FVector BoxRadius, TArray<FRuntimeMeshBlueprintVertexSimple>& Vertices, TArray<int32>& Triangles)
52 {
53  Vertices.Empty(CREATEBOX_NUMVERTS);
54  Triangles.Empty(CREATEBOX_NUMTRIS);
55 
56  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
57  {
58  Vertices.Add(FRuntimeMeshBlueprintVertexSimple(Position, Normal, Tangent, UV0));
59  };
60 
61  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
62  {
63  Triangles.Add(Index);
64  };
65 
66  CreateBoxMesh(BoxRadius, VerticesBuilder, TrianglesBuilder);
67 }
68 
69 void URuntimeMeshShapeGenerator::CreateBoxMesh(FVector BoxRadius, TArray<FRuntimeMeshVertexSimple>& Vertices, TArray<int32>& Triangles)
70 {
71  Vertices.Empty(CREATEBOX_NUMVERTS);
72  Triangles.Empty(CREATEBOX_NUMTRIS);
73 
74  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
75  {
76  Vertices.Add(FRuntimeMeshVertexSimple(Position, Normal, Tangent, UV0));
77  };
78 
79  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
80  {
81  Triangles.Add(Index);
82  };
83 
84  CreateBoxMesh(BoxRadius, VerticesBuilder, TrianglesBuilder);
85 }
86 
87 void URuntimeMeshShapeGenerator::CreateBoxMesh(FVector BoxRadius, const TSharedPtr<FRuntimeMeshAccessor>& MeshBuilder)
88 {
89  MeshBuilder->EmptyVertices(CREATEBOX_NUMVERTS);
90  MeshBuilder->EmptyIndices(CREATEBOX_NUMTRIS);
91  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
92  {
93  int32 NewVertex = MeshBuilder->AddVertex(Position);
94  MeshBuilder->SetNormalTangent(NewVertex, Normal, Tangent);
95  MeshBuilder->SetUV(NewVertex, UV0);
96  };
97 
98  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
99  {
100  MeshBuilder->AddIndex(Index);
101  };
102 
103  CreateBoxMesh(BoxRadius, VerticesBuilder, TrianglesBuilder);
104 }
105 
107 {
108  MeshBuilder.EmptyVertices(CREATEBOX_NUMVERTS);
109  MeshBuilder.EmptyIndices(CREATEBOX_NUMTRIS);
110  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
111  {
112  int32 NewVertex = MeshBuilder.AddVertex(Position);
113  MeshBuilder.SetNormalTangent(NewVertex, Normal, Tangent);
114  MeshBuilder.SetUV(NewVertex, UV0);
115  };
116 
117  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
118  {
119  MeshBuilder.AddIndex(Index);
120  };
121 
122  CreateBoxMesh(BoxRadius, VerticesBuilder, TrianglesBuilder);
123 }
124 
125 
126 void URuntimeMeshShapeGenerator::CreateGridMeshTriangles(int32 NumX, int32 NumY, bool bWinding, TArray<int32>& Triangles)
127 {
128  Triangles.Reset(CREATEGRIDMESHTRIANGLES_NUMTRIS(NumX - 1, NumY - 1));
129 
130  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
131  {
132  Triangles.Add(Index);
133  };
134 
135  CreateGridMeshTriangles(NumX, NumY, bWinding, TrianglesBuilder);
136 }
137 
138 
139 
140 void URuntimeMeshShapeGenerator::CreateGridMesh(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FRuntimeMeshTangent>& Tangents)
141 {
142  Vertices.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
143  Triangles.Empty(CREATEGRIDMESHTRIANGLES_NUMTRIS(NumSubdivisionsX, NumSubdivisionsY));
144  Normals.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
145  UVs.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
146  Tangents.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
147 
148  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
149  {
150  Vertices.Add(Position);
151  Normals.Add(Normal);
152  Tangents.Add(Tangent);
153  UVs.Add(UV0);
154  };
155 
156  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
157  {
158  Triangles.Add(Index);
159  };
160 
161  CreateGridMesh(Width, Height, NumSubdivisionsX, NumSubdivisionsY, VerticesBuilder, TrianglesBuilder);
162 }
163 
164 void URuntimeMeshShapeGenerator::CreateGridMeshPacked(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, TArray<FRuntimeMeshBlueprintVertexSimple>& Vertices, TArray<int32>& Triangles)
165 {
166  Vertices.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
167  Triangles.Empty(CREATEGRIDMESHTRIANGLES_NUMTRIS(NumSubdivisionsX, NumSubdivisionsY));
168 
169  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
170  {
171  Vertices.Add(FRuntimeMeshBlueprintVertexSimple(Position, Normal, Tangent, UV0));
172  };
173 
174  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
175  {
176  Triangles.Add(Index);
177  };
178 
179  CreateGridMesh(Width, Height, NumSubdivisionsX, NumSubdivisionsY, VerticesBuilder, TrianglesBuilder);
180 }
181 
182 void URuntimeMeshShapeGenerator::CreateGridMesh(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, TArray<FRuntimeMeshVertexSimple>& Vertices, TArray<int32>& Triangles)
183 {
184  Vertices.Empty(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
185  Triangles.Empty(CREATEGRIDMESHTRIANGLES_NUMTRIS(NumSubdivisionsX, NumSubdivisionsY));
186 
187  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
188  {
189  Vertices.Add(FRuntimeMeshVertexSimple(Position, Normal, Tangent, UV0));
190  };
191 
192  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
193  {
194  Triangles.Add(Index);
195  };
196 
197  CreateGridMesh(Width, Height, NumSubdivisionsX, NumSubdivisionsY, VerticesBuilder, TrianglesBuilder);
198 }
199 
200 void URuntimeMeshShapeGenerator::CreateGridMesh(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, const TSharedPtr<FRuntimeMeshAccessor>& MeshBuilder)
201 {
202  MeshBuilder->EmptyVertices(CREATEGRIDMESHTRIANGLES_NUMVERTS(NumSubdivisionsX, NumSubdivisionsY));
203  MeshBuilder->EmptyIndices(CREATEGRIDMESHTRIANGLES_NUMTRIS(NumSubdivisionsX, NumSubdivisionsY));
204  FVerticesBuilderFunction VerticesBuilder = [&](const FVector& Position, const FVector& Normal, const FRuntimeMeshTangent& Tangent, const FVector2D& UV0)
205  {
206  int32 NewVertex = MeshBuilder->AddVertex(Position);
207  MeshBuilder->SetNormalTangent(NewVertex, Normal, Tangent);
208  MeshBuilder->SetUV(NewVertex, UV0);
209  };
210 
211  FTrianglesBuilderFunction TrianglesBuilder = [&](int32 Index)
212  {
213  MeshBuilder->AddIndex(Index);
214  };
215 
216  CreateGridMesh(Width, Height, NumSubdivisionsX, NumSubdivisionsY, VerticesBuilder, TrianglesBuilder);
217 }
218 
219 void URuntimeMeshShapeGenerator::ConvertQuadToTriangles(TFunction<void(int32 Index)> TrianglesBuilder, int32 Vert0, int32 Vert1, int32 Vert2, int32 Vert3)
220 {
221  TrianglesBuilder(Vert0);
222  TrianglesBuilder(Vert1);
223  TrianglesBuilder(Vert3);
224 
225  TrianglesBuilder(Vert1);
226  TrianglesBuilder(Vert2);
227  TrianglesBuilder(Vert3);
228 }
229 
230 void URuntimeMeshShapeGenerator::CreateBoxMesh(FVector BoxRadius, FVerticesBuilderFunction VerticesBuilder, FTrianglesBuilderFunction TrianglesBuilder)
231 {
232  // Generate verts
233  FVector BoxVerts[8];
234  BoxVerts[0] = FVector(-BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
235  BoxVerts[1] = FVector(BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
236  BoxVerts[2] = FVector(BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
237  BoxVerts[3] = FVector(-BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
238 
239  BoxVerts[4] = FVector(-BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
240  BoxVerts[5] = FVector(BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
241  BoxVerts[6] = FVector(BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
242  BoxVerts[7] = FVector(-BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
243 
244  FVector Normal;
245  FRuntimeMeshTangent Tangent;
246 
247  // Pos Z
248  Normal = FVector(0.0f, 0.0f, 1.0f);
249  Tangent.TangentX = FVector(0.0f, -1.0f, 0.0f);
250  VerticesBuilder(BoxVerts[0], Normal, Tangent, FVector2D(0.0f, 0.0f));
251  VerticesBuilder(BoxVerts[1], Normal, Tangent, FVector2D(0.0f, 1.0f));
252  VerticesBuilder(BoxVerts[2], Normal, Tangent, FVector2D(1.0f, 1.0f));
253  VerticesBuilder(BoxVerts[3], Normal, Tangent, FVector2D(1.0f, 0.0f));
254  ConvertQuadToTriangles(TrianglesBuilder, 0, 1, 2, 3);
255 
256  // Neg X
257  Normal = FVector(-1.0f, 0.0f, 0.0f);
258  Tangent.TangentX = FVector(0.0f, -1.0f, 0.0f);
259  VerticesBuilder(BoxVerts[4], Normal, Tangent, FVector2D(0.0f, 0.0f));
260  VerticesBuilder(BoxVerts[0], Normal, Tangent, FVector2D(0.0f, 1.0f));
261  VerticesBuilder(BoxVerts[3], Normal, Tangent, FVector2D(1.0f, 1.0f));
262  VerticesBuilder(BoxVerts[7], Normal, Tangent, FVector2D(1.0f, 0.0f));
263  ConvertQuadToTriangles(TrianglesBuilder, 4, 5, 6, 7);
264 
265  // Pos Y
266  Normal = FVector(0.0f, 1.0f, 0.0f);
267  Tangent.TangentX = FVector(-1.0f, 0.0f, 0.0f);
268  VerticesBuilder(BoxVerts[5], Normal, Tangent, FVector2D(0.0f, 0.0f));
269  VerticesBuilder(BoxVerts[1], Normal, Tangent, FVector2D(0.0f, 1.0f));
270  VerticesBuilder(BoxVerts[0], Normal, Tangent, FVector2D(1.0f, 1.0f));
271  VerticesBuilder(BoxVerts[4], Normal, Tangent, FVector2D(1.0f, 0.0f));
272  ConvertQuadToTriangles(TrianglesBuilder, 8, 9, 10, 11);
273 
274  // Pos X
275  Normal = FVector(1.0f, 0.0f, 0.0f);
276  Tangent.TangentX = FVector(0.0f, 1.0f, 0.0f);
277  VerticesBuilder(BoxVerts[6], Normal, Tangent, FVector2D(0.0f, 0.0f));
278  VerticesBuilder(BoxVerts[2], Normal, Tangent, FVector2D(0.0f, 1.0f));
279  VerticesBuilder(BoxVerts[1], Normal, Tangent, FVector2D(1.0f, 1.0f));
280  VerticesBuilder(BoxVerts[5], Normal, Tangent, FVector2D(1.0f, 0.0f));
281  ConvertQuadToTriangles(TrianglesBuilder, 12, 13, 14, 15);
282 
283  // Neg Y
284  Normal = FVector(0.0f, -1.0f, 0.0f);
285  Tangent.TangentX = FVector(1.0f, 0.0f, 0.0f);
286  VerticesBuilder(BoxVerts[7], Normal, Tangent, FVector2D(0.0f, 0.0f));
287  VerticesBuilder(BoxVerts[3], Normal, Tangent, FVector2D(0.0f, 1.0f));
288  VerticesBuilder(BoxVerts[2], Normal, Tangent, FVector2D(1.0f, 1.0f));
289  VerticesBuilder(BoxVerts[6], Normal, Tangent, FVector2D(1.0f, 0.0f));
290  ConvertQuadToTriangles(TrianglesBuilder, 16, 17, 18, 19);
291 
292  // Neg Z
293  Normal = FVector(0.0f, 0.0f, -1.0f);
294  Tangent.TangentX = FVector(0.0f, 1.0f, 0.0f);
295  VerticesBuilder(BoxVerts[7], Normal, Tangent, FVector2D(0.0f, 0.0f));
296  VerticesBuilder(BoxVerts[6], Normal, Tangent, FVector2D(0.0f, 1.0f));
297  VerticesBuilder(BoxVerts[5], Normal, Tangent, FVector2D(1.0f, 1.0f));
298  VerticesBuilder(BoxVerts[4], Normal, Tangent, FVector2D(1.0f, 0.0f));
299  ConvertQuadToTriangles(TrianglesBuilder, 20, 21, 22, 23);
300 }
301 
302 void URuntimeMeshShapeGenerator::CreateGridMeshTriangles(int32 NumX, int32 NumY, bool bWinding, FTrianglesBuilderFunction TrianglesBuilder)
303 {
304  if (NumX >= 2 && NumY >= 2)
305  {
306  // Build Quads
307  for (int XIdx = 0; XIdx < NumX - 1; XIdx++)
308  {
309  for (int YIdx = 0; YIdx < NumY - 1; YIdx++)
310  {
311  const int32 I0 = (XIdx + 0)*NumY + (YIdx + 0);
312  const int32 I1 = (XIdx + 1)*NumY + (YIdx + 0);
313  const int32 I2 = (XIdx + 1)*NumY + (YIdx + 1);
314  const int32 I3 = (XIdx + 0)*NumY + (YIdx + 1);
315 
316  if (bWinding)
317  {
318  ConvertQuadToTriangles(TrianglesBuilder, I0, I1, I2, I3);
319  }
320  else
321  {
322  ConvertQuadToTriangles(TrianglesBuilder, I0, I3, I2, I1);
323  }
324  }
325  }
326  }
327 }
328 
329 void URuntimeMeshShapeGenerator::CreateGridMesh(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, FVerticesBuilderFunction VerticesBuilder, FTrianglesBuilderFunction TrianglesBuilder)
330 {
331  static const FVector Normal(0.0f, 0.0f, 1.0f);
332  static const FVector Tangent(0.0f, -1.0f, 0.0f);
333 
334 
335  const float HalfX = Width * 0.5f;
336  const float HalfY = Height * 0.5f;
337 
338  int32 NumVerticesX = NumSubdivisionsX + 1;
339  int32 NumVerticesY = NumSubdivisionsY + 1;
340 
341  for (int32 X = 0; X < NumVerticesX; X++)
342  {
343  for (int32 Y = 0; Y < NumVerticesY; Y++)
344  {
345  FVector2D Position;
346  Position.X = -HalfX + ((X / (float)NumSubdivisionsX) * Width);
347  Position.Y = -HalfY + ((Y / (float)NumSubdivisionsY) * Height);
348 
349  FVector2D UV;
350  UV.X = X / (float)NumSubdivisionsX;
351  UV.Y = Y / (float)NumSubdivisionsY;
352 
353  VerticesBuilder(FVector(Position, 0.0f), Normal, Tangent, UV);
354  }
355  }
356 
357  CreateGridMeshTriangles(NumVerticesX, NumVerticesY, false, TrianglesBuilder);
358 }
359 
360 
static void CreateGridMesh(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, TArray< FVector > &Vertices, TArray< int32 > &Triangles, TArray< FVector > &Normals, TArray< FVector2D > &UVs, TArray< FRuntimeMeshTangent > &Tangents)
static void CreateGridMeshTriangles(int32 NumX, int32 NumY, bool bWinding, TArray< int32 > &Triangles)
static void CreateBoxMeshPacked(FVector BoxRadius, TArray< FRuntimeMeshBlueprintVertexSimple > &Vertices, TArray< int32 > &Triangles)
TFunction< void(const FVector &Position, const FVector &Normal, const FRuntimeMeshTangent &Tangent, const FVector2D &UV0)> FVerticesBuilderFunction
int32 AddVertex(FVector InPosition)
#define CREATEGRIDMESHTRIANGLES_NUMVERTS(NumX, NumY)
#define CREATEGRIDMESHTRIANGLES_NUMTRIS(NumX, NumY)
GLdouble f
static void CreateBoxMesh(FVector BoxRadius, TArray< FVector > &Vertices, TArray< int32 > &Triangles, TArray< FVector > &Normals, TArray< FVector2D > &UVs, TArray< FRuntimeMeshTangent > &Tangents)
static void ConvertQuadToTriangles(UPARAM(ref) TArray< int32 > &Triangles, int32 Vert0, int32 Vert1, int32 Vert2, int32 Vert3)
TFunction< void(int32 Index)> FTrianglesBuilderFunction
#define CREATEBOX_NUMTRIS
void SetNormalTangent(int32 Index, FVector Normal, FRuntimeMeshTangent Tangent)
void SetUV(int32 Index, const FVector2D &Value)
struct Index Index
Definition: sqlite3.c:11789
list Y
Definition: rmse.py:132
list X
Definition: rmse.py:131
#define CREATEBOX_NUMVERTS
static void CreateGridMeshPacked(float Width, float Height, int32 NumSubdivisionsX, int32 NumSubdivisionsY, TArray< FRuntimeMeshBlueprintVertexSimple > &Vertices, TArray< int32 > &Triangles)
int32 AddIndex(int32 NewIndex)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41