From 6e53452dc84e82454b1653d03fc472fc4f69f37a Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Sun, 4 Dec 2022 03:58:33 +1100 Subject: [PATCH] Fix bone preview mesh --- application/sources/bone_generator.cc | 38 +++++++++++++++++++++++++++ application/sources/bone_generator.h | 4 +++ application/sources/document.cc | 7 +++-- dust3d/base/object.h | 2 +- dust3d/mesh/mesh_generator.cc | 26 +----------------- dust3d/mesh/mesh_generator.h | 1 - dust3d/rig/bone_generator.cc | 35 +++++++++++++----------- dust3d/rig/bone_generator.h | 10 +++++-- 8 files changed, 76 insertions(+), 47 deletions(-) diff --git a/application/sources/bone_generator.cc b/application/sources/bone_generator.cc index 5a1ffd4d..0fa059da 100644 --- a/application/sources/bone_generator.cc +++ b/application/sources/bone_generator.cc @@ -1,7 +1,15 @@ #include "bone_generator.h" +#include +#include #include #include +BoneGenerator::BoneGenerator(std::unique_ptr object, std::unique_ptr snapshot) + : m_object(std::move(object)) + , m_snapshot(std::move(snapshot)) +{ +} + std::map>* BoneGenerator::takeBonePreviewMeshes() { return m_bonePreviewMeshes.release(); @@ -9,6 +17,34 @@ std::map>* BoneGenerator::takeBonePrevi void BoneGenerator::process() { + QElapsedTimer countTimeConsumed; + countTimeConsumed.start(); + + setVertices(m_object->vertices); + setTriangles(m_object->triangles); + setPositionToNodeMap(m_object->positionToNodeIdMap); + + for (const auto& it : m_object->nodeMap) { + Node node; + node.position = it.second.origin; + addNode(it.first, node); + } + + for (const auto& it : m_snapshot->boneIdList) { + Bone bone; + addBone(dust3d::Uuid(it), bone); + } + + for (const auto& it : m_snapshot->nodes) { + NodeBinding nodeBinding; + for (const auto& boneIdString : dust3d::String::split(dust3d::String::valueOrEmpty(it.second, "boneIdList"), ',')) { + if (boneIdString.empty()) + continue; + nodeBinding.boneIds.insert(dust3d::Uuid(boneIdString)); + } + addNodeBinding(dust3d::Uuid(it.first), nodeBinding); + } + generate(); m_bonePreviewMeshes = std::make_unique>>(); @@ -36,5 +72,7 @@ void BoneGenerator::process() previewTriangleVertexNormals); } + qDebug() << "The bone generation took" << countTimeConsumed.elapsed() << "milliseconds"; + emit finished(); } diff --git a/application/sources/bone_generator.h b/application/sources/bone_generator.h index 39f045ea..4a93bbf4 100644 --- a/application/sources/bone_generator.h +++ b/application/sources/bone_generator.h @@ -3,6 +3,7 @@ #include "model_mesh.h" #include +#include #include #include #include @@ -10,6 +11,7 @@ class BoneGenerator : public QObject, public dust3d::BoneGenerator { Q_OBJECT public: + BoneGenerator(std::unique_ptr object, std::unique_ptr snapshot); std::map>* takeBonePreviewMeshes(); public slots: void process(); @@ -18,6 +20,8 @@ signals: private: std::unique_ptr>> m_bonePreviewMeshes; + std::unique_ptr m_object; + std::unique_ptr m_snapshot; }; #endif diff --git a/application/sources/document.cc b/application/sources/document.cc index 5966f13d..39cbe226 100644 --- a/application/sources/document.cc +++ b/application/sources/document.cc @@ -3036,10 +3036,13 @@ void Document::generateBone() emit boneGenerating(); - // TODO: + auto object = std::make_unique(*m_uvMappedObject); + + auto snapshot = std::make_unique(); + toSnapshot(snapshot.get()); QThread* thread = new QThread; - m_boneGenerator = std::make_unique(); + m_boneGenerator = std::make_unique(std::move(object), std::move(snapshot)); m_boneGenerator->moveToThread(thread); connect(thread, &QThread::started, m_boneGenerator.get(), &BoneGenerator::process); connect(m_boneGenerator.get(), &BoneGenerator::finished, this, &Document::boneReady); diff --git a/dust3d/base/object.h b/dust3d/base/object.h index 5701b905..c23d7363 100644 --- a/dust3d/base/object.h +++ b/dust3d/base/object.h @@ -40,7 +40,7 @@ namespace dust3d { struct ObjectNode { //Uuid partId; //Uuid nodeId; - //Vector3 origin; + Vector3 origin; //float radius = 0.0; Color color; //float colorSolubility = 0.0; diff --git a/dust3d/mesh/mesh_generator.cc b/dust3d/mesh/mesh_generator.cc index 78b2ef56..f3584947 100644 --- a/dust3d/mesh/mesh_generator.cc +++ b/dust3d/mesh/mesh_generator.cc @@ -641,7 +641,7 @@ std::unique_ptr MeshGenerator::combinePartMesh(const std::string& par partCache.nodeMap.clear(); for (const auto& meshNode : meshNodes) { partCache.nodeMap.emplace(std::make_pair(meshNode.sourceId, - ObjectNode { partColor })); + ObjectNode { meshNode.origin, partColor })); } if (PartTarget::Model == target) { @@ -767,30 +767,6 @@ CombineMode MeshGenerator::componentCombineMode(const std::map* component) -{ - if (nullptr == component) - return std::string(); - std::string linkDataType = String::valueOrEmpty(*component, "linkDataType"); - if ("partId" == linkDataType) { - std::string partIdString = String::valueOrEmpty(*component, "linkData"); - auto findPart = m_snapshot->parts.find(partIdString); - if (findPart == m_snapshot->parts.end()) { - return std::string(); - } - auto& part = findPart->second; - std::string colorSolubility = String::valueOrEmpty(part, "colorSolubility"); - if (!colorSolubility.empty()) { - return std::string("+"); - } - std::string colorName = String::valueOrEmpty(part, "color"); - if (colorName.empty()) - return std::string("-"); - return colorName; - } - return std::string(); -} - std::unique_ptr MeshGenerator::combineComponentMesh(const std::string& componentIdString, CombineMode* combineMode) { std::unique_ptr mesh; diff --git a/dust3d/mesh/mesh_generator.h b/dust3d/mesh/mesh_generator.h index ac98807d..1a0d7a71 100644 --- a/dust3d/mesh/mesh_generator.h +++ b/dust3d/mesh/mesh_generator.h @@ -163,7 +163,6 @@ private: std::unique_ptr combineStitchingMesh(const std::vector& partIdStrings, const std::vector& componentIdStrings, GeneratedComponent& componentCache); - std::string componentColorName(const std::map* component); void collectUncombinedComponent(const std::string& componentIdString); void cutFaceStringToCutTemplate(const std::string& cutFaceString, std::vector& cutTemplate); void postprocessObject(Object* object); diff --git a/dust3d/rig/bone_generator.cc b/dust3d/rig/bone_generator.cc index 4730e991..37f4c98d 100644 --- a/dust3d/rig/bone_generator.cc +++ b/dust3d/rig/bone_generator.cc @@ -21,7 +21,6 @@ */ #include -#include namespace dust3d { @@ -161,25 +160,29 @@ std::map& BoneGenerator::bonePreviews() return m_bonePreviews; } +void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview, + std::unordered_map& oldToNewVertexMap, + const std::vector& triangle) +{ + std::vector newTriangle(3); + for (size_t i = 0; i < 3; ++i) { + auto findVertex = oldToNewVertexMap.find(triangle[i]); + if (findVertex == oldToNewVertexMap.end()) { + oldToNewVertexMap.insert(std::make_pair(triangle[i], bonePreview.vertices.size())); + newTriangle[i] = bonePreview.vertices.size(); + bonePreview.vertices.push_back(m_vertices[triangle[i]]); + } else { + newTriangle[i] = findVertex->second; + } + } + bonePreview.triangles.emplace_back(newTriangle); +} + void BoneGenerator::generateBonePreviews() { for (const auto& it : m_boneVertices) { BonePreview bonePreview; std::unordered_map oldToNewVertexMap; - auto addTriangleAsLocal = [&](const std::vector& globalTriangle) { - std::vector newTriangle(3); - for (size_t i = 0; i < 3; ++i) { - auto findVertex = oldToNewVertexMap.find(globalTriangle[i]); - if (findVertex == oldToNewVertexMap.end()) { - oldToNewVertexMap.insert(std::make_pair(globalTriangle[i], bonePreview.vertices.size())); - newTriangle[i] = bonePreview.vertices.size(); - bonePreview.vertices.push_back(m_vertices[globalTriangle[i]]); - } else { - newTriangle[i] = findVertex->first; - } - } - bonePreview.triangles.emplace_back(newTriangle); - }; for (const auto& triangle : m_triangles) { size_t countedPoints = 0; @@ -189,7 +192,7 @@ void BoneGenerator::generateBonePreviews() } if (0 == countedPoints) continue; - addTriangleAsLocal(triangle); + addBonePreviewTriangle(bonePreview, oldToNewVertexMap, triangle); } m_bonePreviews.emplace(std::make_pair(it.first, std::move(bonePreview))); diff --git a/dust3d/rig/bone_generator.h b/dust3d/rig/bone_generator.h index 6d9591fb..709f7d34 100644 --- a/dust3d/rig/bone_generator.h +++ b/dust3d/rig/bone_generator.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -64,14 +65,16 @@ public: }; BoneGenerator(); + void generate(); + std::map& bonePreviews(); + +protected: void setVertices(const std::vector& vertices); void setTriangles(const std::vector>& triangles); void setPositionToNodeMap(const std::map& positionToNodeMap); void addBone(const Uuid& boneId, const Bone& bone); void addNodeBinding(const Uuid& nodeId, const NodeBinding& nodeBidning); void addNode(const Uuid& nodeId, const Node& node); - void generate(); - std::map& bonePreviews(); private: std::vector m_vertices; @@ -92,6 +95,9 @@ private: void buildBoneJoints(); void assignVerticesToBoneJoints(); void generateBonePreviews(); + void addBonePreviewTriangle(BonePreview& bonePreview, + std::unordered_map& oldToNewVertexMap, + const std::vector& triangle); }; }