2018-10-09 02:19:12 +00:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QElapsedTimer>
|
2022-07-03 19:00:06 +00:00
|
|
|
#include <QDebug>
|
2021-11-18 14:58:01 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <dust3d/base/ds3_file.h>
|
|
|
|
#include <dust3d/base/snapshot_xml.h>
|
|
|
|
#include "material_previews_generator.h"
|
|
|
|
#include "mesh_generator.h"
|
|
|
|
#include "texture_generator.h"
|
|
|
|
#include "image_forever.h"
|
|
|
|
#include "mesh_result_post_processor.h"
|
2018-10-09 02:19:12 +00:00
|
|
|
|
|
|
|
MaterialPreviewsGenerator::MaterialPreviewsGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialPreviewsGenerator::~MaterialPreviewsGenerator()
|
|
|
|
{
|
|
|
|
for (auto &item: m_previews) {
|
|
|
|
delete item.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
void MaterialPreviewsGenerator::addMaterial(dust3d::Uuid materialId, const std::vector<MaterialLayer> &layers)
|
2018-10-09 02:19:12 +00:00
|
|
|
{
|
|
|
|
m_materials.push_back({materialId, layers});
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
const std::set<dust3d::Uuid> &MaterialPreviewsGenerator::generatedPreviewMaterialIds()
|
2018-10-09 02:19:12 +00:00
|
|
|
{
|
|
|
|
return m_generatedMaterialIds;
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
ModelMesh *MaterialPreviewsGenerator::takePreview(dust3d::Uuid materialId)
|
2018-10-09 02:19:12 +00:00
|
|
|
{
|
2022-09-23 15:54:49 +00:00
|
|
|
ModelMesh *resultMesh = m_previews[materialId];
|
2018-10-09 02:19:12 +00:00
|
|
|
m_previews[materialId] = nullptr;
|
|
|
|
return resultMesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialPreviewsGenerator::generate()
|
|
|
|
{
|
2021-11-18 14:58:01 +00:00
|
|
|
dust3d::Snapshot *snapshot = new dust3d::Snapshot;
|
2018-10-09 02:19:12 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
QFile file(":/resources/material-demo-model.ds3");
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
QByteArray fileData = file.readAll();
|
|
|
|
|
|
|
|
std::vector<dust3d::Uuid> partIds;
|
|
|
|
dust3d::Ds3FileReader ds3Reader((const std::uint8_t *)fileData.data(), fileData.size());
|
2022-10-01 06:26:59 +00:00
|
|
|
for (int i = 0; i < (int)ds3Reader.items().size(); ++i) {
|
2021-11-18 14:58:01 +00:00
|
|
|
dust3d::Ds3ReaderItem item = ds3Reader.items().at(i);
|
2018-10-09 02:19:12 +00:00
|
|
|
if (item.type == "model") {
|
2021-11-18 14:58:01 +00:00
|
|
|
std::vector<std::uint8_t> data;
|
2018-10-09 02:19:12 +00:00
|
|
|
ds3Reader.loadItem(item.name, &data);
|
2021-11-18 14:58:01 +00:00
|
|
|
std::string xmlString((char *)data.data(), data.size());
|
|
|
|
dust3d::loadSnapshotFromXmlString(snapshot, (char *)xmlString.c_str());
|
2018-10-09 02:19:12 +00:00
|
|
|
for (const auto &item: snapshot->parts) {
|
2021-11-18 14:58:01 +00:00
|
|
|
partIds.push_back(dust3d::Uuid(item.first));
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
dust3d::MeshGenerator::GeneratedCacheContext *cacheContext = new dust3d::MeshGenerator::GeneratedCacheContext();
|
2018-10-09 02:19:12 +00:00
|
|
|
MeshGenerator *meshGenerator = new MeshGenerator(snapshot);
|
|
|
|
meshGenerator->setGeneratedCacheContext(cacheContext);
|
|
|
|
|
|
|
|
meshGenerator->generate();
|
|
|
|
for (const auto &mirror: cacheContext->partMirrorIdMap) {
|
2021-11-18 14:58:01 +00:00
|
|
|
partIds.push_back(dust3d::Uuid(mirror.first));
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
dust3d::Object *object = meshGenerator->takeObject();
|
2020-11-17 14:31:51 +00:00
|
|
|
if (nullptr != object) {
|
|
|
|
MeshResultPostProcessor *poseProcessor = new MeshResultPostProcessor(*object);
|
2018-10-26 23:04:45 +00:00
|
|
|
poseProcessor->poseProcess();
|
2020-11-17 14:31:51 +00:00
|
|
|
delete object;
|
|
|
|
object = poseProcessor->takePostProcessedObject();
|
2018-10-26 23:04:45 +00:00
|
|
|
delete poseProcessor;
|
|
|
|
}
|
2018-10-09 02:19:12 +00:00
|
|
|
|
2020-11-17 14:31:51 +00:00
|
|
|
if (nullptr != object) {
|
2019-12-24 23:24:49 +00:00
|
|
|
for (const auto &material: m_materials) {
|
2020-11-17 14:31:51 +00:00
|
|
|
TextureGenerator *textureGenerator = new TextureGenerator(*object);
|
2019-12-24 23:24:49 +00:00
|
|
|
for (const auto &layer: material.second) {
|
|
|
|
for (const auto &mapItem: layer.maps) {
|
|
|
|
const QImage *image = ImageForever::get(mapItem.imageId);
|
|
|
|
if (nullptr == image)
|
|
|
|
continue;
|
|
|
|
for (const auto &partId: partIds) {
|
2021-11-18 14:58:01 +00:00
|
|
|
if (dust3d::TextureType::BaseColor == mapItem.forWhat)
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->addPartColorMap(partId, image, layer.tileScale);
|
2021-11-18 14:58:01 +00:00
|
|
|
else if (dust3d::TextureType::Normal == mapItem.forWhat)
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->addPartNormalMap(partId, image, layer.tileScale);
|
2021-11-18 14:58:01 +00:00
|
|
|
else if (dust3d::TextureType::Metallic == mapItem.forWhat)
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->addPartMetalnessMap(partId, image, layer.tileScale);
|
2021-11-18 14:58:01 +00:00
|
|
|
else if (dust3d::TextureType::Roughness == mapItem.forWhat)
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->addPartRoughnessMap(partId, image, layer.tileScale);
|
2021-11-18 14:58:01 +00:00
|
|
|
else if (dust3d::TextureType::AmbientOcclusion == mapItem.forWhat)
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->addPartAmbientOcclusionMap(partId, image, layer.tileScale);
|
|
|
|
}
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-24 23:24:49 +00:00
|
|
|
textureGenerator->generate();
|
2022-09-23 15:54:49 +00:00
|
|
|
ModelMesh *texturedResultMesh = textureGenerator->takeResultMesh();
|
2019-12-24 23:24:49 +00:00
|
|
|
if (nullptr != texturedResultMesh) {
|
2022-09-23 15:54:49 +00:00
|
|
|
m_previews[material.first] = new ModelMesh(*texturedResultMesh);
|
2019-12-24 23:24:49 +00:00
|
|
|
m_generatedMaterialIds.insert(material.first);
|
|
|
|
delete texturedResultMesh;
|
|
|
|
}
|
|
|
|
delete textureGenerator;
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:31:51 +00:00
|
|
|
delete object;
|
2018-10-09 02:19:12 +00:00
|
|
|
|
|
|
|
delete meshGenerator;
|
|
|
|
delete cacheContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialPreviewsGenerator::process()
|
|
|
|
{
|
|
|
|
QElapsedTimer countTimeConsumed;
|
|
|
|
countTimeConsumed.start();
|
|
|
|
|
|
|
|
generate();
|
|
|
|
|
|
|
|
qDebug() << "The material previews generation took" << countTimeConsumed.elapsed() << "milliseconds";
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
}
|