Fix texture generator when no image specified

master
Jeremy HU 2022-12-12 21:38:28 +11:00
parent a1825549bc
commit aa057af886
3 changed files with 45 additions and 25 deletions

View File

@ -97,20 +97,31 @@ void UvMapGenerator::packUvs()
m_mapPacker = std::make_unique<dust3d::UvMapPacker>();
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);
if (colorImageIdIt != partIt.second.end()) {
imageId = dust3d::Uuid(colorImageIdIt->second);
const QImage* image = ImageForever::get(imageId);
if (nullptr == image)
continue;
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,12 +143,16 @@ void UvMapGenerator::generateTextureColorImage()
colorTexturePainter.setPen(Qt::NoPen);
for (const auto& layout : m_mapPacker->packedLayouts()) {
const QImage* image = image = ImageForever::get(layout.id);
QPixmap brushPixmap;
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 {
const QImage* 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));
@ -152,6 +167,7 @@ void UvMapGenerator::generateTextureColorImage()
layout.height * UvMapGenerator::m_textureSize));
brushPixmap = QPixmap::fromImage(scaledImage);
}
}
colorTexturePainter.drawPixmap(layout.left * UvMapGenerator::m_textureSize,
layout.top * UvMapGenerator::m_textureSize,
brushPixmap);

View File

@ -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) {

View File

@ -24,6 +24,7 @@
#define DUST3D_UV_MAP_PACKER_H_
#include <array>
#include <dust3d/base/color.h>
#include <dust3d/base/position_key.h>
#include <dust3d/base/uuid.h>
#include <dust3d/base/vector2.h>
@ -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<PositionKey, 3>, std::array<Vector2, 3>> localUv;
@ -44,6 +46,7 @@ public:
struct Layout {
Uuid id;
Color color;
double left = 0.0;
double top = 0.0;
double width = 0.0;