From e9ce467eb9e7d42aafdaccecb0a2c7aa3ca3cff3 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 22 Oct 2019 01:43:08 -0700 Subject: [PATCH] Crahs because of ? --- libraries/fbx/src/FBXSerializer_Mesh.cpp | 1 - libraries/hfm/src/hfm/HFM.h | 14 +++++-- libraries/hfm/src/hfm/HFMModelMath.cpp | 9 ++-- libraries/hfm/src/hfm/HFMModelMath.h | 13 +----- .../model-baker/src/model-baker/Baker.cpp | 41 ++++++++++++++++--- .../src/model-networking/ModelCache.cpp | 5 --- 6 files changed, 53 insertions(+), 30 deletions(-) diff --git a/libraries/fbx/src/FBXSerializer_Mesh.cpp b/libraries/fbx/src/FBXSerializer_Mesh.cpp index 895059c6ad..e687f5e9f2 100644 --- a/libraries/fbx/src/FBXSerializer_Mesh.cpp +++ b/libraries/fbx/src/FBXSerializer_Mesh.cpp @@ -174,7 +174,6 @@ void appendIndex(MeshData& data, QVector& indices, int index, bool deduplic data.indices.insert(vertex, newIndex); data.extracted.newIndices.insert(vertexIndex, newIndex); data.extracted.mesh.vertices.append(position); - data.extracted.mesh.positions.emplace_back(position); data.extracted.mesh.originalIndices.append(vertexIndex); data.extracted.mesh.normals.append(normal); data.extracted.mesh.texCoords.append(vertex.texCoord); diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index ee3a25fc46..419f47d680 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -229,14 +229,22 @@ public: bool needTangentSpace() const; }; + +/// Simple Triangle List vertices; + std::vector indices; + std::vector parts; // Offset in the indices, Number of indices +}; + /// A single mesh (with optional blendshapes). class Mesh { public: std::vector parts; - std::vector positions; QVector vertices; + std::vector _vertices; QVector normals; QVector tangents; QVector colors; @@ -255,6 +263,8 @@ public: // Blendshape attributes QVector blendshapes; + // Simple Triangle List Mesh generated during baking + hfm::TriangleListMesh triangleListMesh; QVector originalIndices; // Original indices of the vertices unsigned int meshIndex; // the order the meshes appeared in the object file @@ -262,8 +272,6 @@ public: graphics::MeshPointer _mesh; bool wasCompressed { false }; - std::vector uniqueVertices; - std::vector trianglesIndices; }; /// A single animation frame. diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index f34a43bf19..1678d5d405 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -145,11 +145,12 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s } -const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts) { +const TriangleListMesh generateTriangleListMesh(const std::vector& srcVertices, const std::vector& srcParts) { - MeshIndexedTrianglesPos dest; - // dest.vertices.resize(srcVertices.size()); - dest.vertices = srcVertices; + TriangleListMesh dest; + + // just copy vertices + dest.vertices.insert(dest.vertices.end(), srcVertices.cbegin(), srcVertices.cend()); /* std::vector remap(srcVertices.size()); { diff --git a/libraries/hfm/src/hfm/HFMModelMath.h b/libraries/hfm/src/hfm/HFMModelMath.h index 38d3262042..dc397c5e6f 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.h +++ b/libraries/hfm/src/hfm/HFMModelMath.h @@ -25,8 +25,7 @@ void calculateExtentsForShape(hfm::Shape& shape, const std::vector& m void calculateExtentsForModel(Extents& modelExtents, const std::vector& shapes); -class ReweightedDeformers { -public: +struct ReweightedDeformers { std::vector indices; std::vector weights; uint16_t weightsPerVertex { 0 }; @@ -37,15 +36,7 @@ const uint16_t DEFAULT_SKINNING_WEIGHTS_PER_VERTEX = 4; ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const std::vector skinClusters, const uint16_t weightsPerVertex = DEFAULT_SKINNING_WEIGHTS_PER_VERTEX); - -struct MeshIndexedTrianglesPos { -public: - std::vector vertices; - std::vector indices; - std::vector parts; // Offset in the indices, Number of indices -}; - -const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts); +const TriangleListMesh generateTriangleListMesh(const std::vector& srcVertices, const std::vector& srcParts); }; diff --git a/libraries/model-baker/src/model-baker/Baker.cpp b/libraries/model-baker/src/model-baker/Baker.cpp index c63495c169..c6c8be4bdd 100644 --- a/libraries/model-baker/src/model-baker/Baker.cpp +++ b/libraries/model-baker/src/model-baker/Baker.cpp @@ -23,6 +23,7 @@ #include "CalculateExtentsTask.h" #include "BuildDracoMeshTask.h" #include "ParseFlowDataTask.h" +#include namespace baker { @@ -49,6 +50,29 @@ namespace baker { } }; + class BuildMeshTriangleListTask { + public: + using Input = std::vector; + using Output = std::vector; + using JobModel = Job::ModelIO; + + void run(const BakeContextPointer& context, const Input& input, Output& output) { + const auto& meshesIn = input; + auto& indexedTrianglesMeshOut = output; + indexedTrianglesMeshOut.clear(); + indexedTrianglesMeshOut.resize(meshesIn.size()); + + for (int i = 0; i < meshesIn.size(); i++) { + auto& mesh = meshesIn[i]; + + auto meshPointer = const_cast (&mesh); + meshPointer->_vertices = meshPointer->vertices.toStdVector(); + + indexedTrianglesMeshOut[i] = hfm::generateTriangleListMesh(meshPointer->_vertices, mesh.parts); + } + } + }; + class BuildBlendshapesTask { public: using Input = VaryingSet3, std::vector>; @@ -80,21 +104,23 @@ namespace baker { class BuildMeshesTask { public: - using Input = VaryingSet5, std::vector, NormalsPerMesh, TangentsPerMesh, BlendshapesPerMesh>; + using Input = VaryingSet6, std::vector, std::vector, NormalsPerMesh, TangentsPerMesh, BlendshapesPerMesh>; using Output = std::vector; using JobModel = Job::ModelIO; void run(const BakeContextPointer& context, const Input& input, Output& output) { auto& meshesIn = input.get0(); int numMeshes = (int)meshesIn.size(); - auto& graphicsMeshesIn = input.get1(); - auto& normalsPerMeshIn = input.get2(); - auto& tangentsPerMeshIn = input.get3(); - auto& blendshapesPerMeshIn = input.get4(); + auto& triangleListMeshesIn = input.get1(); + auto& graphicsMeshesIn = input.get2(); + auto& normalsPerMeshIn = input.get3(); + auto& tangentsPerMeshIn = input.get4(); + auto& blendshapesPerMeshIn = input.get5(); auto meshesOut = meshesIn; for (int i = 0; i < numMeshes; i++) { auto& meshOut = meshesOut[i]; + meshOut.triangleListMesh = triangleListMeshesIn[i]; meshOut._mesh = safeGet(graphicsMeshesIn, i); meshOut.normals = QVector::fromStdVector(safeGet(normalsPerMeshIn, i)); meshOut.tangents = QVector::fromStdVector(safeGet(tangentsPerMeshIn, i)); @@ -162,6 +188,9 @@ namespace baker { const auto collectShapeVerticesInputs = CollectShapeVerticesTask::Input(meshesIn, shapesIn, jointsIn, skinDeformersIn).asVarying(); const auto shapeVerticesPerJoint = model.addJob("CollectShapeVertices", collectShapeVerticesInputs); + // Build the slim triangle list mesh for each hfm::mesh + const auto triangleListMeshes = model.addJob("BuildMeshTriangleListTask", meshesIn); + // Build the graphics::MeshPointer for each hfm::Mesh const auto buildGraphicsMeshInputs = BuildGraphicsMeshTask::Input(meshesIn, url, meshIndicesToModelNames, normalsPerMesh, tangentsPerMesh, shapesIn, skinDeformersIn).asVarying(); const auto graphicsMeshes = model.addJob("BuildGraphicsMesh", buildGraphicsMeshInputs); @@ -200,7 +229,7 @@ namespace baker { // Combine the outputs into a new hfm::Model const auto buildBlendshapesInputs = BuildBlendshapesTask::Input(blendshapesPerMeshIn, normalsPerBlendshapePerMesh, tangentsPerBlendshapePerMesh).asVarying(); const auto blendshapesPerMeshOut = model.addJob("BuildBlendshapes", buildBlendshapesInputs); - const auto buildMeshesInputs = BuildMeshesTask::Input(meshesIn, graphicsMeshes, normalsPerMesh, tangentsPerMesh, blendshapesPerMeshOut).asVarying(); + const auto buildMeshesInputs = BuildMeshesTask::Input(meshesIn, triangleListMeshes, graphicsMeshes, normalsPerMesh, tangentsPerMesh, blendshapesPerMeshOut).asVarying(); const auto meshesOut = model.addJob("BuildMeshes", buildMeshesInputs); const auto buildModelInputs = BuildModelTask::Input(hfmModelIn, meshesOut, jointsOut, jointRotationOffsets, jointIndices, flowData, shapeVerticesPerJoint, shapesOut, modelExtentsOut).asVarying(); const auto hfmModelOut = model.addJob("BuildModel", buildModelInputs); diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index f427326f68..e9674d6d26 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -331,16 +331,11 @@ void ModelResource::setGeometryDefinition(HFMModel::Pointer hfmModel, const Mate } std::shared_ptr meshes = std::make_shared(); - std::vector triangleListMeshes = std::vector(); int meshID = 0; for (const HFMMesh& mesh : _hfmModel->meshes) { // Copy mesh pointers meshes->emplace_back(mesh._mesh); meshID++; - - auto simpleMesh = hfm::generateMeshIndexedTrianglePos(mesh.positions, mesh.parts); - - triangleListMeshes.emplace_back(simpleMesh); } _meshes = meshes;