2022-10-02 19:45:46 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
#include "mesh_generator.h"
|
|
|
|
#include "cut_face_preview.h"
|
2022-10-18 09:35:04 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <dust3d/mesh/smooth_normal.h>
|
|
|
|
#include <dust3d/mesh/trim_vertices.h>
|
2021-11-18 14:58:01 +00:00
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
MeshGenerator::MeshGenerator(dust3d::Snapshot* snapshot)
|
|
|
|
: dust3d::MeshGenerator(snapshot)
|
2021-11-18 14:58:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshGenerator::~MeshGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
std::map<dust3d::Uuid, std::unique_ptr<ModelMesh>>* MeshGenerator::takeComponentPreviewMeshes()
|
2021-11-18 14:58:01 +00:00
|
|
|
{
|
2022-10-02 19:45:46 +00:00
|
|
|
return m_componentPreviewMeshes.release();
|
2021-11-18 14:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
MonochromeMesh* MeshGenerator::takeWireframeMesh()
|
2022-09-23 15:54:49 +00:00
|
|
|
{
|
|
|
|
return m_wireframeMesh.release();
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
void MeshGenerator::process()
|
|
|
|
{
|
|
|
|
QElapsedTimer countTimeConsumed;
|
|
|
|
countTimeConsumed.start();
|
2022-10-18 09:35:04 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
generate();
|
2022-10-18 09:35:04 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
if (nullptr != m_object)
|
2022-10-02 19:45:46 +00:00
|
|
|
m_resultMesh = std::make_unique<ModelMesh>(*m_object);
|
2022-10-18 09:35:04 +00:00
|
|
|
|
2022-10-02 19:45:46 +00:00
|
|
|
m_componentPreviewMeshes = std::make_unique<std::map<dust3d::Uuid, std::unique_ptr<ModelMesh>>>();
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& componentId : m_generatedPreviewComponentIds) {
|
2022-10-02 19:45:46 +00:00
|
|
|
auto it = m_generatedComponentPreviews.find(componentId);
|
|
|
|
if (it == m_generatedComponentPreviews.end())
|
2021-11-18 14:58:01 +00:00
|
|
|
continue;
|
2022-10-23 03:14:30 +00:00
|
|
|
std::vector<std::array<dust3d::Vector2, 3>> triangleUvs;
|
|
|
|
if (!it->second.triangleUvs.empty()) {
|
|
|
|
triangleUvs.resize(it->second.triangles.size());
|
|
|
|
for (size_t i = 0; i < it->second.triangles.size(); ++i) {
|
|
|
|
const auto& triangle = it->second.triangles[i];
|
|
|
|
auto findUv = it->second.triangleUvs.find({ dust3d::PositionKey(it->second.vertices[triangle[0]]),
|
|
|
|
dust3d::PositionKey(it->second.vertices[triangle[1]]),
|
|
|
|
dust3d::PositionKey(it->second.vertices[triangle[2]]) });
|
|
|
|
if (findUv != it->second.triangleUvs.end()) {
|
|
|
|
triangleUvs[i] = findUv->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 19:45:46 +00:00
|
|
|
dust3d::trimVertices(&it->second.vertices, true);
|
2022-10-18 09:35:04 +00:00
|
|
|
for (auto& it : it->second.vertices) {
|
2022-10-02 19:45:46 +00:00
|
|
|
it *= 2.0;
|
|
|
|
}
|
|
|
|
std::vector<dust3d::Vector3> previewTriangleNormals;
|
|
|
|
previewTriangleNormals.reserve(it->second.triangles.size());
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& face : it->second.triangles) {
|
2022-10-02 19:45:46 +00:00
|
|
|
previewTriangleNormals.emplace_back(dust3d::Vector3::normal(
|
|
|
|
it->second.vertices[face[0]],
|
|
|
|
it->second.vertices[face[1]],
|
2022-10-18 09:35:04 +00:00
|
|
|
it->second.vertices[face[2]]));
|
2022-10-02 19:45:46 +00:00
|
|
|
}
|
|
|
|
std::vector<std::vector<dust3d::Vector3>> previewTriangleVertexNormals;
|
|
|
|
dust3d::smoothNormal(it->second.vertices,
|
|
|
|
it->second.triangles,
|
|
|
|
previewTriangleNormals,
|
|
|
|
0,
|
|
|
|
&previewTriangleVertexNormals);
|
|
|
|
(*m_componentPreviewMeshes)[componentId] = std::make_unique<ModelMesh>(it->second.vertices,
|
2021-11-18 14:58:01 +00:00
|
|
|
it->second.triangles,
|
2022-10-02 19:45:46 +00:00
|
|
|
previewTriangleVertexNormals,
|
2021-11-18 14:58:01 +00:00
|
|
|
it->second.color,
|
|
|
|
it->second.metalness,
|
2022-10-07 20:26:30 +00:00
|
|
|
it->second.roughness,
|
2022-10-23 03:14:30 +00:00
|
|
|
it->second.vertexProperties.empty() ? nullptr : &it->second.vertexProperties,
|
|
|
|
triangleUvs.empty() ? nullptr : &triangleUvs);
|
2021-11-18 14:58:01 +00:00
|
|
|
}
|
2022-09-23 15:54:49 +00:00
|
|
|
|
|
|
|
if (nullptr != m_object)
|
|
|
|
m_wireframeMesh = std::make_unique<MonochromeMesh>(*m_object);
|
2022-10-18 09:35:04 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
qDebug() << "The mesh generation took" << countTimeConsumed.elapsed() << "milliseconds";
|
2022-10-18 09:35:04 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
ModelMesh* MeshGenerator::takeResultMesh()
|
2021-11-18 14:58:01 +00:00
|
|
|
{
|
2022-10-02 19:45:46 +00:00
|
|
|
return m_resultMesh.release();
|
2021-11-18 14:58:01 +00:00
|
|
|
}
|