Fix object color

master
Jeremy HU 2022-11-14 21:27:06 +11:00
parent 5fd836bfdd
commit 65c793e670
4 changed files with 56 additions and 37 deletions

View File

@ -130,12 +130,6 @@ ModelMesh::ModelMesh(dust3d::Object& object)
m_vertices = object.vertices; m_vertices = object.vertices;
m_faces = object.triangleAndQuads; m_faces = object.triangleAndQuads;
//std::map<std::pair<dust3d::Uuid, dust3d::Uuid>, const dust3d::ObjectNode*> nodeMap;
//for (size_t i = 0; i < object.nodes.size(); ++i) {
// const auto& node = object.nodes[i];
// nodeMap.insert({ { node.partId, node.nodeId }, &node });
//}
m_triangleVertexCount = (int)object.triangles.size() * 3; m_triangleVertexCount = (int)object.triangles.size() * 3;
m_triangleVertices = new ModelOpenGLVertex[m_triangleVertexCount]; m_triangleVertices = new ModelOpenGLVertex[m_triangleVertexCount];
int destIndex = 0; int destIndex = 0;
@ -146,10 +140,10 @@ ModelMesh::ModelMesh(dust3d::Object& object)
const dust3d::Vector2 defaultUv = dust3d::Vector2(0, 0); const dust3d::Vector2 defaultUv = dust3d::Vector2(0, 0);
const dust3d::Vector3 defaultTangent = dust3d::Vector3(0, 0, 0); const dust3d::Vector3 defaultTangent = dust3d::Vector3(0, 0, 0);
for (size_t i = 0; i < object.triangles.size(); ++i) { for (size_t i = 0; i < object.triangles.size(); ++i) {
const auto& triangleColor = &object.triangleColors[i];
for (auto j = 0; j < 3; j++) { for (auto j = 0; j < 3; j++) {
int vertexIndex = (int)object.triangles[i][j]; int vertexIndex = (int)object.triangles[i][j];
const dust3d::Vector3* srcVert = &object.vertices[vertexIndex]; const dust3d::Vector3* srcVert = &object.vertices[vertexIndex];
const dust3d::Color* srcColor = &object.vertexColors[vertexIndex];
const dust3d::Vector3* srcNormal = &defaultNormal; const dust3d::Vector3* srcNormal = &defaultNormal;
if (triangleVertexNormals) if (triangleVertexNormals)
srcNormal = &(*triangleVertexNormals)[i][j]; srcNormal = &(*triangleVertexNormals)[i][j];
@ -160,10 +154,10 @@ ModelMesh::ModelMesh(dust3d::Object& object)
if (triangleTangents) if (triangleTangents)
srcTangent = &(*triangleTangents)[i]; srcTangent = &(*triangleTangents)[i];
ModelOpenGLVertex* dest = &m_triangleVertices[destIndex]; ModelOpenGLVertex* dest = &m_triangleVertices[destIndex];
dest->colorR = triangleColor->r(); dest->colorR = srcColor->r();
dest->colorG = triangleColor->g(); dest->colorG = srcColor->g();
dest->colorB = triangleColor->b(); dest->colorB = srcColor->b();
dest->alpha = triangleColor->alpha(); dest->alpha = srcColor->alpha();
dest->posX = srcVert->x(); dest->posX = srcVert->x();
dest->posY = srcVert->y(); dest->posY = srcVert->y();
dest->posZ = srcVert->z(); dest->posZ = srcVert->z();

View File

@ -38,32 +38,33 @@
namespace dust3d { namespace dust3d {
struct ObjectNode { struct ObjectNode {
Uuid partId; //Uuid partId;
Uuid nodeId; //Uuid nodeId;
Vector3 origin; //Vector3 origin;
float radius = 0.0; //float radius = 0.0;
Color color; Color color;
float colorSolubility = 0.0; //float colorSolubility = 0.0;
float metalness = 0.0; //float metalness = 0.0;
float roughness = 1.0; //float roughness = 1.0;
Uuid materialId; //Uuid materialId;
bool countershaded = false; //bool countershaded = false;
Uuid mirrorFromPartId; //Uuid mirrorFromPartId;
Uuid mirroredByPartId; //Uuid mirroredByPartId;
Vector3 direction; //Vector3 direction;
bool joined = true; //bool joined = true;
}; };
class Object { class Object {
public: public:
std::vector<Vector3> vertices; std::vector<Vector3> vertices;
std::map<PositionKey, Uuid> sourceNodeMap; std::map<PositionKey, Uuid> positionToNodeIdMap;
std::map<Uuid, ObjectNode> nodeMap;
std::vector<std::vector<size_t>> triangleAndQuads; std::vector<std::vector<size_t>> triangleAndQuads;
std::vector<std::vector<size_t>> triangles; 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>>> partTriangleUvs;
std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs; std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs;
std::vector<Vector3> triangleNormals; std::vector<Vector3> triangleNormals;
std::vector<Color> triangleColors; std::vector<Color> vertexColors;
bool alphaEnabled = false; bool alphaEnabled = false;
uint64_t meshId = 0; uint64_t meshId = 0;

View File

@ -639,6 +639,12 @@ std::unique_ptr<MeshState> MeshGenerator::combinePartMesh(const std::string& par
partCache.isSuccessful = false; partCache.isSuccessful = false;
partCache.joined = (target == PartTarget::Model && !isDisabled); partCache.joined = (target == PartTarget::Model && !isDisabled);
partCache.nodeMap.clear();
for (const auto& meshNode : meshNodes) {
partCache.nodeMap.emplace(std::make_pair(meshNode.sourceId,
ObjectNode { partColor }));
}
if (PartTarget::Model == target) { if (PartTarget::Model == target) {
if (1 == meshNodes.size()) { if (1 == meshNodes.size()) {
size_t subdivideTimes = subdived ? 1 : 0; size_t subdivideTimes = subdived ? 1 : 0;
@ -684,7 +690,7 @@ std::unique_ptr<MeshState> MeshGenerator::combinePartMesh(const std::string& par
} }
const auto& vertexSources = tubeMeshBuilder->generatedVertexSources(); const auto& vertexSources = tubeMeshBuilder->generatedVertexSources();
for (size_t i = 0; i < vertexSources.size(); ++i) { for (size_t i = 0; i < vertexSources.size(); ++i) {
partCache.sourceNodeMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i])); partCache.positionToNodeIdMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i]));
} }
} }
} }
@ -828,8 +834,10 @@ std::unique_ptr<MeshState> MeshGenerator::combineComponentMesh(const std::string
componentCache.noneSeamVertices.insert(vertex); componentCache.noneSeamVertices.insert(vertex);
collectSharedQuadEdges(partCache.vertices, partCache.faces, &componentCache.sharedQuadEdges); collectSharedQuadEdges(partCache.vertices, partCache.faces, &componentCache.sharedQuadEdges);
componentCache.partTriangleUvs.insert({ Uuid(partIdString), partCache.triangleUvs }); componentCache.partTriangleUvs.insert({ Uuid(partIdString), partCache.triangleUvs });
for (const auto& it : partCache.sourceNodeMap) for (const auto& it : partCache.positionToNodeIdMap)
componentCache.sourceNodeMap.emplace(it); componentCache.positionToNodeIdMap.emplace(it);
for (const auto& it : partCache.nodeMap)
componentCache.nodeMap.emplace(it);
} }
if (!partCache.joined) { if (!partCache.joined) {
if (mesh) if (mesh)
@ -963,8 +971,10 @@ std::unique_ptr<MeshState> MeshGenerator::combineComponentChildGroupMesh(const s
componentCache.sharedQuadEdges.insert(it); componentCache.sharedQuadEdges.insert(it);
for (const auto& it : childComponentCache.partTriangleUvs) for (const auto& it : childComponentCache.partTriangleUvs)
componentCache.partTriangleUvs.insert({ it.first, it.second }); componentCache.partTriangleUvs.insert({ it.first, it.second });
for (const auto& it : childComponentCache.sourceNodeMap) for (const auto& it : childComponentCache.positionToNodeIdMap)
componentCache.sourceNodeMap.emplace(it); componentCache.positionToNodeIdMap.emplace(it);
for (const auto& it : childComponentCache.nodeMap)
componentCache.nodeMap.emplace(it);
if (nullptr == subMesh || subMesh->isNull()) { if (nullptr == subMesh || subMesh->isNull()) {
continue; continue;
@ -1029,7 +1039,16 @@ void MeshGenerator::postprocessObject(Object* object)
object->triangleNormals = combinedFacesNormals; object->triangleNormals = combinedFacesNormals;
object->triangleColors.resize(object->triangles.size(), Color::createWhite()); object->vertexColors.resize(object->vertices.size(), Color::createWhite());
for (size_t i = 0; i < object->vertices.size(); ++i) {
auto findSourceNode = object->positionToNodeIdMap.find(object->vertices[i]);
if (findSourceNode == object->positionToNodeIdMap.end())
continue;
auto findObjectNode = object->nodeMap.find(findSourceNode->second);
if (findObjectNode == object->nodeMap.end())
continue;
object->vertexColors[i] = findObjectNode->second.color;
}
std::vector<std::vector<Vector3>> triangleVertexNormals; std::vector<std::vector<Vector3>> triangleVertexNormals;
smoothNormal(object->vertices, smoothNormal(object->vertices,
@ -1247,7 +1266,8 @@ void MeshGenerator::generate()
const auto& componentCache = m_cacheContext->components[to_string(Uuid())]; const auto& componentCache = m_cacheContext->components[to_string(Uuid())];
m_object->sourceNodeMap = componentCache.sourceNodeMap; m_object->positionToNodeIdMap = componentCache.positionToNodeIdMap;
m_object->nodeMap = componentCache.nodeMap;
m_object->partTriangleUvs = componentCache.partTriangleUvs; m_object->partTriangleUvs = componentCache.partTriangleUvs;
std::vector<Vector3> combinedVertices; std::vector<Vector3> combinedVertices;

View File

@ -44,7 +44,8 @@ public:
struct GeneratedPart { struct GeneratedPart {
std::vector<Vector3> vertices; std::vector<Vector3> vertices;
std::map<PositionKey, Uuid> sourceNodeMap; std::map<PositionKey, Uuid> positionToNodeIdMap;
std::map<Uuid, ObjectNode> nodeMap;
std::vector<std::vector<size_t>> faces; std::vector<std::vector<size_t>> faces;
std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> triangleUvs; std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>> triangleUvs;
Color color = Color(1.0, 1.0, 1.0); Color color = Color(1.0, 1.0, 1.0);
@ -57,7 +58,8 @@ public:
vertices.clear(); vertices.clear();
faces.clear(); faces.clear();
triangleUvs.clear(); triangleUvs.clear();
sourceNodeMap.clear(); positionToNodeIdMap.clear();
nodeMap.clear();
color = Color(1.0, 1.0, 1.0); color = Color(1.0, 1.0, 1.0);
metalness = 0.0; metalness = 0.0;
roughness = 1.0; roughness = 1.0;
@ -72,7 +74,8 @@ public:
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>>> partTriangleUvs;
std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs; std::vector<std::map<std::array<PositionKey, 3>, std::array<Vector2, 3>>> seamTriangleUvs;
std::set<PositionKey> noneSeamVertices; std::set<PositionKey> noneSeamVertices;
std::map<PositionKey, Uuid> sourceNodeMap; std::map<PositionKey, Uuid> positionToNodeIdMap;
std::map<Uuid, ObjectNode> nodeMap;
void reset() void reset()
{ {
mesh.reset(); mesh.reset();
@ -80,7 +83,8 @@ public:
partTriangleUvs.clear(); partTriangleUvs.clear();
seamTriangleUvs.clear(); seamTriangleUvs.clear();
noneSeamVertices.clear(); noneSeamVertices.clear();
sourceNodeMap.clear(); positionToNodeIdMap.clear();
nodeMap.clear();
} }
}; };