Skip to content

Commit

Permalink
Fix UV map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
huxingyi committed Jan 16, 2023
1 parent b8d517c commit 6581bbe
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 78 deletions.
26 changes: 2 additions & 24 deletions application/sources/component_property_widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ComponentPropertyWidget::ComponentPropertyWidget(Document* document,

QHBoxLayout* topLayout = new QHBoxLayout;

if (hasTextureConfigure()) {
if (!m_componentIds.empty()) {
QPushButton* colorPreviewArea = new QPushButton;
colorPreviewArea->setStyleSheet("QPushButton {background-color: " + m_color.name() + "; border-radius: 0;}");
colorPreviewArea->setFixedSize(Theme::toolIconSize * 1.8, Theme::toolIconSize);
Expand Down Expand Up @@ -309,7 +309,7 @@ ComponentPropertyWidget::ComponentPropertyWidget(Document* document,
}

QGroupBox* colorImageGroupBox = nullptr;
if (hasTextureConfigure()) {
if (!m_componentIds.empty()) {
ImagePreviewWidget* colorImagePreviewWidget = new ImagePreviewWidget;
colorImagePreviewWidget->setFixedSize(Theme::partPreviewImageSize * 2, Theme::partPreviewImageSize * 2);
auto colorImageId = lastColorImageId();
Expand Down Expand Up @@ -507,25 +507,3 @@ void ComponentPropertyWidget::showColorDialog()
}
emit groupOperationAdded();
}

bool ComponentPropertyWidget::hasTextureConfigure()
{
if (!m_partIds.empty())
return true;

for (const auto& componentId : m_componentIds) {
const Document::Component* parentComponent = m_document->findComponent(componentId);
if (nullptr == parentComponent)
continue;
for (const auto& childId : parentComponent->childrenIds) {
const Document::Component* component = m_document->findComponent(childId);
if (nullptr == component)
continue;
if (component->linkToPartId.isNull())
continue;
return true;
}
}

return false;
}
1 change: 0 additions & 1 deletion application/sources/component_property_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public slots:
std::vector<QString> m_cutFaceList;

void updateCutFaceButtonState(size_t index);
bool hasTextureConfigure();
};

#endif
50 changes: 8 additions & 42 deletions application/sources/uv_map_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,74 +93,40 @@ QImage* UvMapGenerator::combineMetalnessRoughnessAmbientOcclusionImages(QImage*
return textureMetalnessRoughnessAmbientOcclusionImage;
}

void UvMapGenerator::collectStitchingLines(const std::map<std::string, std::string>& component)
{
// TODO: Pick the most occurent image id and color
// ...

/*
for (const auto& childId : dust3d::String::split(dust3d::String::valueOrEmpty(component, "children"), ',')) {
if (childId.empty())
continue;
dust3dDebug << childId;
const auto& componentIt = m_snapshot->components.find(childId);
if (componentIt == m_snapshot->components.end())
continue;
if ("partId" != dust3d::String::valueOrEmpty(componentIt->second, "linkDataType")) {
collectStitchingLines(componentIt->second);
continue;
}
const auto& partIt = m_snapshot->parts.find(dust3d::String::valueOrEmpty(componentIt->second, "linkData"));
if (partIt == m_snapshot->parts.end())
continue;
auto partTarget = dust3d::PartTargetFromString(dust3d::String::valueOrEmpty(partIt->second, "target").c_str());
if (dust3d::PartTarget::StitchingLine != partTarget)
continue;
dust3dDebug << childId << partIt->first;
// TODO:
}
*/
}

void UvMapGenerator::packUvs()
{
m_mapPacker = std::make_unique<dust3d::UvMapPacker>();

for (const auto& partIt : m_snapshot->parts) {
auto partTarget = dust3d::PartTargetFromString(dust3d::String::valueOrEmpty(partIt.second, "target").c_str());
if (dust3d::PartTarget::Model != partTarget)
for (const auto& componentTriangleUvIt : m_object->componentTriangleUvs) {
auto componentIt = m_snapshot->components.find(componentTriangleUvIt.first.toString());
if (componentIt == m_snapshot->components.end())
continue;
dust3d::Uuid imageId;
dust3d::Color color(1.0, 1.0, 1.0);
double width = 1.0;
double height = 1.0;
const auto& colorIt = partIt.second.find("color");
if (colorIt != partIt.second.end()) {
const auto& colorIt = componentIt->second.find("color");
if (colorIt != componentIt->second.end()) {
color = dust3d::Color(colorIt->second);
}
const auto& colorImageIdIt = partIt.second.find("colorImageId");
if (colorImageIdIt != partIt.second.end()) {
const auto& colorImageIdIt = componentIt->second.find("colorImageId");
if (colorImageIdIt != componentIt->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.color = color;
part.width = width;
part.height = height;
part.localUv = findUvs->second;
part.localUv = componentTriangleUvIt.second;
m_mapPacker->addPart(part);
}

collectStitchingLines(m_snapshot->rootComponent);

m_mapPacker->addSeams(m_object->seamTriangleUvs);

m_mapPacker->pack();
Expand Down
1 change: 0 additions & 1 deletion application/sources/uv_map_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public slots:
bool m_hasTransparencySettings = false;
static size_t m_textureSize;
void packUvs();
void collectStitchingLines(const std::map<std::string, std::string>& component);
void generateTextureColorImage();
void generateUvCoords();
};
Expand Down
2 changes: 1 addition & 1 deletion dust3d/base/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Object {
std::map<Uuid, ObjectNode> nodeMap;
std::vector<std::vector<size_t>> triangleAndQuads;
std::vector<std::vector<size_t>> triangles;
std::unordered_map<Uuid, std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> partTriangleUvs;
std::unordered_map<Uuid, std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> componentTriangleUvs;
std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs;
std::vector<Vector3> triangleNormals;
std::vector<Color> vertexColors;
Expand Down
14 changes: 7 additions & 7 deletions dust3d/mesh/mesh_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ std::unique_ptr<MeshState> MeshGenerator::combineStitchingMesh(const std::string

const auto& faceUvs = stitchMeshBuilder->generatedFaceUvs();
Uuid componentId = Uuid(componentIdString);
auto& triangleUvs = componentCache.partTriangleUvs[componentId];
auto& triangleUvs = componentCache.componentTriangleUvs[componentId];
for (size_t i = 0; i < faceUvs.size(); ++i) {
const auto& uv = faceUvs[i];
const auto& face = generatedFaces[i];
Expand Down Expand Up @@ -823,7 +823,7 @@ std::unique_ptr<MeshState> MeshGenerator::combineComponentMesh(const std::string
for (const auto& vertex : partCache.vertices)
componentCache.noneSeamVertices.insert(vertex);
collectSharedQuadEdges(partCache.vertices, partCache.faces, &componentCache.sharedQuadEdges);
componentCache.partTriangleUvs.insert({ Uuid(partIdString), partCache.triangleUvs });
componentCache.componentTriangleUvs.insert({ componentId, partCache.triangleUvs });
for (const auto& it : partCache.positionToNodeIdMap)
componentCache.positionToNodeIdMap.emplace(it);
for (const auto& it : partCache.nodeMap)
Expand Down Expand Up @@ -959,8 +959,8 @@ std::unique_ptr<MeshState> MeshGenerator::combineComponentChildGroupMesh(const s
componentCache.noneSeamVertices.insert(vertex);
for (const auto& it : childComponentCache.sharedQuadEdges)
componentCache.sharedQuadEdges.insert(it);
for (const auto& it : childComponentCache.partTriangleUvs)
componentCache.partTriangleUvs.insert({ it.first, it.second });
for (const auto& it : childComponentCache.componentTriangleUvs)
componentCache.componentTriangleUvs.insert({ it.first, it.second });
for (const auto& it : childComponentCache.positionToNodeIdMap)
componentCache.positionToNodeIdMap.emplace(it);
for (const auto& it : childComponentCache.nodeMap)
Expand Down Expand Up @@ -1073,8 +1073,8 @@ void MeshGenerator::collectIncombinableMesh(const MeshState* mesh, const Generat
updateVertexIndices(uncombinedFaces);
updateVertexIndices(uncombinedTriangleAndQuads);

for (const auto& it : componentCache.partTriangleUvs)
m_object->partTriangleUvs.insert({ it.first, it.second });
for (const auto& it : componentCache.componentTriangleUvs)
m_object->componentTriangleUvs.insert({ it.first, it.second });
for (const auto& it : componentCache.positionToNodeIdMap)
m_object->positionToNodeIdMap.emplace(it);
for (const auto& it : componentCache.nodeMap)
Expand Down Expand Up @@ -1267,7 +1267,7 @@ void MeshGenerator::generate()

m_object->positionToNodeIdMap = componentCache.positionToNodeIdMap;
m_object->nodeMap = componentCache.nodeMap;
m_object->partTriangleUvs = componentCache.partTriangleUvs;
m_object->componentTriangleUvs = componentCache.componentTriangleUvs;

std::vector<Vector3> combinedVertices;
std::vector<std::vector<size_t>> combinedFaces;
Expand Down
4 changes: 2 additions & 2 deletions dust3d/mesh/mesh_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MeshGenerator {
struct GeneratedComponent {
std::unique_ptr<MeshState> mesh;
std::set<std::pair<PositionKey, PositionKey>> sharedQuadEdges;
std::unordered_map<Uuid, std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> partTriangleUvs;
std::unordered_map<Uuid, std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> componentTriangleUvs;
std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs;
std::set<PositionKey> noneSeamVertices;
std::map<PositionKey, Uuid> positionToNodeIdMap;
Expand All @@ -80,7 +80,7 @@ class MeshGenerator {
{
mesh.reset();
sharedQuadEdges.clear();
partTriangleUvs.clear();
componentTriangleUvs.clear();
seamTriangleUvs.clear();
noneSeamVertices.clear();
positionToNodeIdMap.clear();
Expand Down

0 comments on commit 6581bbe

Please sign in to comment.