9#include "glm/gtx/norm.hpp"
11#include <meshoptimizer.h>
26 PLOGI <<
"=== MeshPrimitive CONSTRUCTOR STARTED ===";
27 PLOGI <<
"MeshPrimitive constructor - vertices: " <<
vertices.size() <<
", indices: " <<
indices.size();
32 materialName = meshPrimitiveData.getMaterialData().getVulkanSchneeExtension().materialName;
33 materialData = meshPrimitiveData.getMaterialData().getMaterialDataRaw();
34 PLOGI <<
"MeshPrimitive loaded with materialID: " << static_cast<int>( materialName );
38 materialName = PipelineNames::NORMALS_SHADER;
39 PLOGI <<
"MeshPrimitive using default material (normalsShader)";
44 textureIndex = meshPrimitiveData.getColorTextureIndex();
45 colorTexture = meshPrimitiveData.getColorTexturePtr();
47 PLOGI <<
"=== About to generate meshlets... ===";
49 PLOGI <<
"=== Meshlet generation completed - meshlets: " <<
meshlets.size() <<
" ===";
50 PLOGI <<
"=== MeshPrimitive CONSTRUCTOR FINISHED ===";
179 PLOGI <<
"=== GENERATE MESHLETS STARTED ===";
180 PLOGI <<
"Starting meshlet generation - vertices: " <<
vertices.size() <<
", indices: " <<
indices.size();
184 std::vector<uint32_t> remap(
vertices.size() );
185 size_t uniqueVertexCount;
188 uniqueVertexCount = meshopt_generateVertexRemap(
199 std::vector<Vertex> uniqueVertices( uniqueVertexCount );
200 std::vector<uint32_t> uniqueIndices(
indices.size() );
204 meshopt_remapVertexBuffer(
210 meshopt_remapIndexBuffer(
211 uniqueIndices.data(),
indices.data(),
indices.size(), remap.data()
222 meshopt_optimizeVertexCache(
223 this->
indices.data(), this->indices.data(), this->indices.size(), this->vertices.size()
228 meshopt_optimizeVertexFetch(
230 this->indices.data(),
231 this->indices.size(),
232 this->vertices.data(),
233 this->vertices.size(),
239 constexpr size_t maxVerticesPerMeshlet = 252;
240 constexpr size_t maxTrianglesPerMeshlet = 252;
241 constexpr float coneWeight = 0.5f;
247 meshopt_buildMeshletsBound(
indices.size(), maxVerticesPerMeshlet, maxTrianglesPerMeshlet );
257 meshletCount = meshopt_buildMeshlets(
266 maxVerticesPerMeshlet,
267 maxTrianglesPerMeshlet,
275 if ( meshletCount > 0 )
277 const auto & [vertex_offset, triangle_offset, vertex_count, triangle_count] =
285 for (
size_t i = 0; i < meshletCount; ++i )
287 const meshopt_Meshlet & m =
meshlets[i];
288 meshopt_optimizeMeshlet(
309 PLOGI <<
"Generated " <<
meshlets.size() <<
" meshlets for primitive.";
321 uint32_t meshletCount =
meshlets.size();
324 for (
size_t i = 0; i < meshletCount; ++i )
327 const meshopt_Meshlet & m =
meshlets[i];
328 auto meshletBoundsHelper =
332 for ( uint32_t vertexId = 0; vertexId < m.vertex_count; ++vertexId )
335 primitiveBoundsHelper.enlarge( pos );
336 meshletBoundsHelper.enlarge( pos );
357 if ( point.x <
minX.x )
359 if ( point.x >
maxX.x )
361 if ( point.y <
minY.y )
363 if ( point.y >
maxY.y )
365 if ( point.z <
minZ.z )
367 if ( point.z >
maxZ.z )
372 const std::vector<meshopt_Meshlet> &
meshlets,
373 const std::vector<Vertex> &
vertices,
380 float distX = glm::distance2(
minX,
maxX );
381 float distY = glm::distance2(
minY,
maxY );
382 float distZ = glm::distance2(
minZ,
maxZ );
385 if ( distX > distY && distX > distZ )
390 else if ( distY > distZ )
402 glm::vec3 center = ( p1 + p2 ) * 0.5f;
403 float radius = glm::distance( center, p2 );
410 float maxDistSq = 0.0f;
411 glm::vec3 farthestPoint = center;
413 for (
const auto & meshlet :
meshlets )
415 for ( uint32_t vertexId = 0; vertexId < meshlet.vertex_count; ++vertexId )
417 const glm::vec3 & pos =
419 glm::vec3 diff = pos - center;
420 float distSq = glm::dot( diff, diff );
422 if ( distSq > maxDistSq )
431 if ( maxDistSq > radius * radius )
433 float dist = glm::sqrt( maxDistSq );
434 float newRadius = ( radius + dist ) * 0.5f;
435 center += ( farthestPoint - center ) * ( ( dist - radius ) / dist );
441 for (
const auto & meshlet :
meshlets )
443 for ( uint32_t vertexId = 0; vertexId < meshlet.vertex_count; ++vertexId )
445 const glm::vec3 & pos =
447 glm::vec3 diff = pos - center;
448 float distSq = glm::dot( diff, diff );
450 if ( distSq > maxDistSq )
458 if ( maxDistSq > radius * radius )
460 float dist = glm::sqrt( maxDistSq );
461 float newRadius = ( radius + dist ) * 0.5f;
462 center += ( farthestPoint - center ) * ( ( dist - radius ) / dist );
469 {center.x, center.y, center.z, radius},
#define TRACY_ZONE_SCOPED_NAMED(name)
The application context is the core class which stores the basic openxr and vulkan objects.
std::vector< uint8_t > getMeshletTriangles() const
float getBoundingSphereRadius() const
ApplicationContext * context
bool hasCustomTexture() const
std::vector< meshopt_Meshlet > getMeshlets() const
std::vector< Vertex > getVertices() const
PipelineNames getMaterialName() const
uint32_t getTextureIndex() const
ObjectCullingData objectCullingData
const std::vector< uint32_t > & getIndicesByRef() const
const std::vector< MeshletBounds > & getMeshletBoundsByRef() const
uint32_t getMeshletTriangleCount() const
std::vector< uint32_t > meshletVertices
const std::vector< Vertex > & getVerticesByRef() const
std::vector< Vertex > vertices
PipelineNames materialName
const std::vector< uint32_t > & getMeshletVerticesByRef() const
void setMaterialShader(MaterialShader *shader)
std::vector< uint32_t > getIndices() const
std::vector< meshopt_Meshlet > meshlets
std::vector< MeshletBounds > meshletBounds
std::vector< uint8_t > meshletTriangles
MaterialShader * materialShader
const ObjectCullingData & getPrimitiveBoundsByRef() const
uint32_t getMeshletVertexCount() const
uint32_t getMeshletsCount() const
const std::vector< uint8_t > & getMeshletTrianglesByRef() const
MaterialShader * getMaterialShader() const
void setMaterialId(uint32_t materialId)
std::vector< uint32_t > getMeshletVertices() const
std::vector< uint32_t > indices
void generateBoundingSpheres()
const std::vector< meshopt_Meshlet > & getMeshletByRef() const
glm::vec3 getBoundingSphereCenter() const
ObjectCullingData getCullingData() const
Log category system implementation.
bool hasVulkanSchneeExtension() const
Stores intermediate data from loading the asset from disk. These are the vertices,...
GltfMaterialData getMaterialData() const
MeshletBounds getBounds(const std::vector< meshopt_Meshlet > &meshlets, const std::vector< Vertex > &vertices, const std::vector< uint32_t > &meshletVertices) const
void enlarge(glm::vec3 point)
glm::vec4 worldPositionAndRadius
The fundamental building block of all meshes in this engine.