Skip to content

Commit

Permalink
Fix texture generator when no image specified
Browse files Browse the repository at this point in the history
  • Loading branch information
huxingyi committed Dec 12, 2022
1 parent a182554 commit aa057af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
66 changes: 41 additions & 25 deletions application/sources/uv_map_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
}
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions dust3d/uv/uv_map_packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions dust3d/uv/uv_map_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -37,13 +38,15 @@ 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;
};

struct Layout {
Uuid id;
Color color;
double left = 0.0;
double top = 0.0;
double width = 0.0;
Expand Down

0 comments on commit aa057af

Please sign in to comment.