From aa057af8869ac161212961f9791d41d8ac69ffac Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Mon, 12 Dec 2022 21:38:28 +1100 Subject: [PATCH] Fix texture generator when no image specified --- application/sources/uv_map_generator.cc | 66 +++++++++++++++---------- dust3d/uv/uv_map_packer.cc | 1 + dust3d/uv/uv_map_packer.h | 3 ++ 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/application/sources/uv_map_generator.cc b/application/sources/uv_map_generator.cc index 2f8870c2..5e79c251 100644 --- a/application/sources/uv_map_generator.cc +++ b/application/sources/uv_map_generator.cc @@ -97,20 +97,31 @@ void UvMapGenerator::packUvs() m_mapPacker = std::make_unique(); for (const auto& partIt : m_snapshot->parts) { + dust3d::Uuid imageId; + dust3d::Color color; + double width = 1.0; + double height = 1.0; + const auto& colorIt = partIt.second.find("color"); + if (colorIt != partIt.second.end()) { + color = dust3d::Color(colorIt->second); + } const auto& colorImageIdIt = partIt.second.find("colorImageId"); - if (colorImageIdIt == partIt.second.end()) - continue; - auto imageId = dust3d::Uuid(colorImageIdIt->second); - const QImage* image = ImageForever::get(imageId); - if (nullptr == image) - continue; + if (colorImageIdIt != partIt.second.end()) { + imageId = dust3d::Uuid(colorImageIdIt->second); + const QImage* image = ImageForever::get(imageId); + if (nullptr != image) { + width = image->width(); + height = image->height(); + } + } const auto& findUvs = m_object->partTriangleUvs.find(dust3d::Uuid(partIt.first)); if (findUvs == m_object->partTriangleUvs.end()) continue; dust3d::UvMapPacker::Part part; part.id = imageId; - part.width = image->width(); - part.height = image->height(); + part.color = color; + part.width = width; + part.height = height; part.localUv = findUvs->second; m_mapPacker->addPart(part); } @@ -132,25 +143,30 @@ void UvMapGenerator::generateTextureColorImage() colorTexturePainter.setPen(Qt::NoPen); for (const auto& layout : m_mapPacker->packedLayouts()) { - const QImage* image = image = ImageForever::get(layout.id); - if (nullptr == image) { - dust3dDebug << "Find image failed:" << layout.id.toString(); - continue; - } QPixmap brushPixmap; - if (layout.flipped) { - auto scaledImage = image->scaled(QSize(layout.height * UvMapGenerator::m_textureSize, - layout.width * UvMapGenerator::m_textureSize)); - QPoint center = scaledImage.rect().center(); - QMatrix matrix; - matrix.translate(center.x(), center.y()); - matrix.rotate(90); - auto rotatedImage = scaledImage.transformed(matrix).mirrored(true, false); - brushPixmap = QPixmap::fromImage(rotatedImage); + if (layout.id.isNull()) { + brushPixmap = QPixmap(layout.width * UvMapGenerator::m_textureSize, layout.height * UvMapGenerator::m_textureSize); + brushPixmap.fill(QColor(QString::fromStdString(layout.color.toString()))); } else { - auto scaledImage = image->scaled(QSize(layout.width * UvMapGenerator::m_textureSize, - layout.height * UvMapGenerator::m_textureSize)); - brushPixmap = QPixmap::fromImage(scaledImage); + const QImage* image = ImageForever::get(layout.id); + if (nullptr == image) { + dust3dDebug << "Find image failed:" << layout.id.toString(); + continue; + } + if (layout.flipped) { + auto scaledImage = image->scaled(QSize(layout.height * UvMapGenerator::m_textureSize, + layout.width * UvMapGenerator::m_textureSize)); + QPoint center = scaledImage.rect().center(); + QMatrix matrix; + matrix.translate(center.x(), center.y()); + matrix.rotate(90); + auto rotatedImage = scaledImage.transformed(matrix).mirrored(true, false); + brushPixmap = QPixmap::fromImage(rotatedImage); + } else { + auto scaledImage = image->scaled(QSize(layout.width * UvMapGenerator::m_textureSize, + layout.height * UvMapGenerator::m_textureSize)); + brushPixmap = QPixmap::fromImage(scaledImage); + } } colorTexturePainter.drawPixmap(layout.left * UvMapGenerator::m_textureSize, layout.top * UvMapGenerator::m_textureSize, diff --git a/dust3d/uv/uv_map_packer.cc b/dust3d/uv/uv_map_packer.cc index fcb79c9f..cad95eec 100644 --- a/dust3d/uv/uv_map_packer.cc +++ b/dust3d/uv/uv_map_packer.cc @@ -105,6 +105,7 @@ void UvMapPacker::pack() auto& flipped = std::get<4>(result); //dust3dDebug << "left:" << left << "top:" << top << "width:" << width << "height:" << height << "flipped:" << flipped; Layout layout; + layout.color = part.color; layout.id = part.id; layout.flipped = flipped; if (flipped) { diff --git a/dust3d/uv/uv_map_packer.h b/dust3d/uv/uv_map_packer.h index 7184229d..eaedd9c4 100644 --- a/dust3d/uv/uv_map_packer.h +++ b/dust3d/uv/uv_map_packer.h @@ -24,6 +24,7 @@ #define DUST3D_UV_MAP_PACKER_H_ #include +#include #include #include #include @@ -37,6 +38,7 @@ class UvMapPacker { public: struct Part { Uuid id; + Color color; double width = 0.0; double height = 0.0; std::map, std::array> localUv; @@ -44,6 +46,7 @@ public: struct Layout { Uuid id; + Color color; double left = 0.0; double top = 0.0; double width = 0.0;