MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / BuildIndexBuffer

Method BuildIndexBuffer

Source/Engine/Graphics/Models/ModelData.Tool.cpp:284–371  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

282}
283
284void MeshData::BuildIndexBuffer()
285{
286 PROFILE_CPU();
287 Stopwatch stopwatch;
288
289 const int32 vertexCount = Positions.Count();
290 MeshData newMesh;
291 newMesh.Indices.EnsureCapacity(vertexCount);
292 Array<int32> mapping;
293 mapping.Resize(vertexCount);
294 int32 newVertexCounter = 0;
295
296#if USE_SPATIAL_SORT
297 // Set up a SpatialSort to quickly find all vertices close to a given position
298 Assimp::SpatialSort vertexFinder;
299 vertexFinder.Fill((const aiVector3D*)Positions.Get(), vertexCount, sizeof(Float3));
300 std::vector<unsigned int> spatialSortCache;
301#endif
302
303 // Build index buffer
304 for (int32 vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++)
305 {
306 // Find duplicated vertex before the current one
307 const int32 reuseVertexIndex = FindVertex(*this, vertexIndex, 0, vertexIndex, mapping
308#if USE_SPATIAL_SORT
309 , vertexFinder, spatialSortCache
310#endif
311 );
312 if (reuseVertexIndex == INVALID_INDEX)
313 {
314 newMesh.Indices.Add(newVertexCounter);
315 mapping[vertexIndex] = newVertexCounter;
316 newVertexCounter++;
317 }
318 else
319 {
320 // Remove vertex
321 const int32 mapped = mapping[reuseVertexIndex];
322 ASSERT(mapped != INVALID_INDEX && mapped < vertexIndex);
323 newMesh.Indices.Add(mapped);
324 mapping[vertexIndex] = INVALID_INDEX;
325 }
326 }
327
328 // Skip if no change
329 if (vertexCount == newVertexCounter)
330 return;
331
332 // Remove unused vertices
333 newMesh.SwapBuffers(*this);
334#define REMAP_BUFFER(name) RemapBuffer(newMesh.name, name, mapping, newVertexCounter)
335 REMAP_BUFFER(Positions);
336 REMAP_BUFFER(Normals);
337 REMAP_BUFFER(Tangents);
338 REMAP_BUFFER(BitangentSigns);
339 REMAP_BUFFER(Colors);
340 REMAP_BUFFER(BlendIndices);
341 REMAP_BUFFER(BlendWeights);

Callers 1

ProcessMeshFunction · 0.80

Calls 9

RoundTo2DecimalPlacesFunction · 0.85
SwapBuffersMethod · 0.80
CountMethod · 0.45
EnsureCapacityMethod · 0.45
ResizeMethod · 0.45
FillMethod · 0.45
GetMethod · 0.45
AddMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected