Fix seam UV

master
Jeremy HU 2022-11-03 08:22:12 +11:00
parent da7536ea8f
commit 3e81a4e45f
3 changed files with 9 additions and 52 deletions

View File

@ -132,28 +132,10 @@ void UvMapGenerator::generateTextureColorImage()
colorTexturePainter.setPen(Qt::NoPen); colorTexturePainter.setPen(Qt::NoPen);
for (const auto& layout : m_mapPacker->packedLayouts()) { for (const auto& layout : m_mapPacker->packedLayouts()) {
const QImage* image = nullptr; const QImage* image = image = ImageForever::get(layout.id);
std::unique_ptr<QImage> seamImage; if (nullptr == image) {
if (layout.isSeam) { dust3dDebug << "Find image failed:" << layout.id.toString();
seamImage = std::make_unique<QImage>(layout.imageWidth, layout.imageHeight, QImage::Format_ARGB32); continue;
seamImage->fill(Qt::red);
for (const auto& it : layout.uvCopyMap) {
const auto& sourceImageId = m_mapPacker->packedLayouts()[it.first].id;
const QImage* sourceImage = ImageForever::get(sourceImageId);
if (nullptr == sourceImage) {
dust3dDebug << "Find image failed:" << sourceImageId.toString();
continue;
}
// TODO: copy triangle UV from source to seam image
}
//dust3dDebug << "layout.imageWidth:" << layout.imageWidth << "layout.imageHeight:" << layout.imageHeight;
image = seamImage.get();
} else {
image = ImageForever::get(layout.id);
if (nullptr == image) {
dust3dDebug << "Find image failed:" << layout.id.toString();
continue;
}
} }
QPixmap brushPixmap; QPixmap brushPixmap;
if (layout.flipped) { if (layout.flipped) {

View File

@ -54,41 +54,26 @@ void UvMapPacker::resolveSeamUvs()
for (const auto& it : part.localUv) { for (const auto& it : part.localUv) {
halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> { halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> {
it.first[1], it.first[0] }, it.first[1], it.first[0] },
TriangleUv { partIndex, it.second })); TriangleUv { partIndex, { it.second[1], it.second[0], it.second[2] } }));
halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> { halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> {
it.first[2], it.first[1] }, it.first[2], it.first[1] },
TriangleUv { partIndex, it.second })); TriangleUv { partIndex, { it.second[2], it.second[1], it.second[0] } }));
halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> { halfEdgeToUvMap.insert(std::make_pair(std::array<PositionKey, 2> {
it.first[0], it.first[2] }, it.first[0], it.first[2] },
TriangleUv { partIndex, it.second })); TriangleUv { partIndex, { it.second[0], it.second[2], it.second[1] } }));
} }
} }
for (size_t seamIndex = 0; seamIndex < m_seams.size(); ++seamIndex) { for (size_t seamIndex = 0; seamIndex < m_seams.size(); ++seamIndex) {
const auto& seam = m_seams[seamIndex]; const auto& seam = m_seams[seamIndex];
double seamUvMapWidth = 0.0;
double seamUvMapHeight = 0.0;
std::unordered_map<size_t, std::array<std::array<Vector2, 3>, 2>> uvCopyMap;
for (const auto& triangle : seam) { for (const auto& triangle : seam) {
auto findUv = halfEdgeToUvMap.find({ triangle.first[0], triangle.first[1] }); auto findUv = halfEdgeToUvMap.find({ triangle.first[0], triangle.first[1] });
if (findUv == halfEdgeToUvMap.end()) if (findUv == halfEdgeToUvMap.end())
continue; continue;
const auto& triangleUv = findUv->second; const auto& triangleUv = findUv->second;
const auto& part = m_partTriangleUvs[triangleUv.partIndex]; m_partTriangleUvs[triangleUv.partIndex].localUv.insert({ triangle.first,
seamUvMapWidth += std::abs(triangleUv.uv[0].x() - triangleUv.uv[1].x()) * part.width; triangleUv.uv });
seamUvMapHeight += std::abs(triangleUv.uv[0].y() - triangleUv.uv[1].y()) * part.height;
uvCopyMap.insert({ triangleUv.partIndex, { triangleUv.uv, triangle.second } });
} }
if (uvCopyMap.empty())
continue;
Part newPart;
newPart.isSeam = true;
newPart.width = seamUvMapWidth;
newPart.height = seamUvMapHeight;
newPart.uvCopyMap = std::move(uvCopyMap);
newPart.localUv = seam;
m_partTriangleUvs.emplace_back(newPart);
// dust3dDebug << "Seam uv map size:" << seamUvMapWidth << seamUvMapHeight;
} }
} }
@ -121,10 +106,6 @@ void UvMapPacker::pack()
//dust3dDebug << "left:" << left << "top:" << top << "width:" << width << "height:" << height << "flipped:" << flipped; //dust3dDebug << "left:" << left << "top:" << top << "width:" << width << "height:" << height << "flipped:" << flipped;
Layout layout; Layout layout;
layout.id = part.id; layout.id = part.id;
layout.isSeam = part.isSeam;
layout.imageWidth = part.width;
layout.imageHeight = part.height;
layout.uvCopyMap = part.uvCopyMap;
layout.flipped = flipped; layout.flipped = flipped;
if (flipped) { if (flipped) {
layout.left = left; layout.left = left;

View File

@ -37,10 +37,8 @@ class UvMapPacker {
public: public:
struct Part { struct Part {
Uuid id; Uuid id;
bool isSeam = false;
double width = 0.0; double width = 0.0;
double height = 0.0; double height = 0.0;
std::unordered_map<size_t, std::array<std::array<Vector2, 3>, 2>> uvCopyMap;
std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> localUv; std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> localUv;
}; };
@ -51,10 +49,6 @@ public:
double width = 0.0; double width = 0.0;
double height = 0.0; double height = 0.0;
bool flipped = false; bool flipped = false;
bool isSeam = false;
size_t imageWidth = 0;
size_t imageHeight = 0;
std::unordered_map<size_t, std::array<std::array<Vector2, 3>, 2>> uvCopyMap;
std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> globalUv; std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> globalUv;
}; };