diff --git a/dust3d/mesh/tube_mesh_builder.cc b/dust3d/mesh/tube_mesh_builder.cc index 2b99d6a9..9c5dbb2f 100644 --- a/dust3d/mesh/tube_mesh_builder.cc +++ b/dust3d/mesh/tube_mesh_builder.cc @@ -204,23 +204,27 @@ void TubeMeshBuilder::build() } // Build all vertex Uvs - std::vector> cutFaceVertexUvs(cutFaceVertexPositions.size()); - std::vector maxUs(cutFaceVertexPositions.size(), 0.0); - std::vector maxVs(cutFaceVertexPositions.front().size(), 0.0); - for (size_t n = 0; n < cutFaceVertexPositions.size(); ++n) { - const auto& cutFaceVertices = cutFaceVertexPositions[n]; + auto cutFaceVertexPositionsForUv = cutFaceVertexPositions; + for (size_t n = 0; n < cutFaceVertexPositionsForUv.size(); ++n) { + cutFaceVertexPositionsForUv[n].push_back(cutFaceVertexPositions[n].front()); + } + std::vector> cutFaceVertexUvs(cutFaceVertexPositionsForUv.size()); + std::vector maxUs(cutFaceVertexPositionsForUv.size(), 0.0); + std::vector maxVs(cutFaceVertexPositionsForUv.front().size(), 0.0); + for (size_t n = 0; n < cutFaceVertexPositionsForUv.size(); ++n) { + const auto& cutFaceVertices = cutFaceVertexPositionsForUv[n]; double offsetU = 0; if (n > 0) { size_t m = n - 1; for (size_t i = 0; i < cutFaceVertices.size(); ++i) { - maxVs[i] += (cutFaceVertexPositions[n][i] - cutFaceVertexPositions[m][i]).length(); + maxVs[i] += (cutFaceVertexPositionsForUv[n][i] - cutFaceVertexPositionsForUv[m][i]).length(); } } std::vector uvCoords = { Vector2 { offsetU, maxVs[0] } }; for (size_t j = 1; j < cutFaceVertices.size(); ++j) { size_t i = j - 1; - uvCoords.push_back({ offsetU, maxVs[j] }); offsetU += (cutFaceVertices[j] - cutFaceVertices[i]).length(); + uvCoords.push_back({ offsetU, maxVs[j] }); } cutFaceVertexUvs[n] = uvCoords; maxUs[n] = offsetU; @@ -239,7 +243,6 @@ void TubeMeshBuilder::build() cutFaceIndices[i].resize(cutFaceVertices.size()); for (size_t k = 0; k < cutFaceVertices.size(); ++k) { cutFaceIndices[i][k] = m_generatedVertices.size(); - m_generatedVertexUvs.emplace_back(cutFaceVertexUvs[i][k]); m_generatedVertices.emplace_back(cutFaceVertices[k]); } } @@ -259,10 +262,10 @@ void TubeMeshBuilder::build() m_generatedFaces.emplace_back(std::vector { cutFaceI[m], cutFaceI[n], cutFaceJ[n], cutFaceJ[m] }); m_generatedFaceUvs.emplace_back(std::vector { - m_generatedVertexUvs[cutFaceI[m]], - m_generatedVertexUvs[cutFaceI[n]], - m_generatedVertexUvs[cutFaceJ[n]], - m_generatedVertexUvs[cutFaceJ[m]] }); + cutFaceVertexUvs[i][m], + cutFaceVertexUvs[i][m + 1], + cutFaceVertexUvs[j][m + 1], + cutFaceVertexUvs[j][m] }); } for (size_t m = halfSize; m < cutFaceI.size(); ++m) { size_t n = (m + 1) % cutFaceI.size(); @@ -273,10 +276,10 @@ void TubeMeshBuilder::build() m_generatedFaces.emplace_back(std::vector { cutFaceJ[m], cutFaceI[m], cutFaceI[n], cutFaceJ[n] }); m_generatedFaceUvs.emplace_back(std::vector { - m_generatedVertexUvs[cutFaceJ[m]], - m_generatedVertexUvs[cutFaceI[m]], - m_generatedVertexUvs[cutFaceI[n]], - m_generatedVertexUvs[cutFaceJ[n]] }); + cutFaceVertexUvs[j][m], + cutFaceVertexUvs[i][m], + cutFaceVertexUvs[i][m + 1], + cutFaceVertexUvs[j][m + 1] }); } } if (!m_isCircle) { diff --git a/dust3d/mesh/tube_mesh_builder.h b/dust3d/mesh/tube_mesh_builder.h index 13901917..a9e7fc6a 100644 --- a/dust3d/mesh/tube_mesh_builder.h +++ b/dust3d/mesh/tube_mesh_builder.h @@ -58,7 +58,6 @@ private: std::vector m_nodeForwardDistances; std::vector m_generatedVertices; std::vector> m_generatedFaces; - std::vector m_generatedVertexUvs; std::vector> m_generatedFaceUvs; Vector3 m_generatedBaseNormal; bool m_isCircle = false;