extend texture mapping to meshes
This commit is contained in:
@@ -46,6 +46,77 @@ MeshTexture::MeshTexture(const Mesh::MeshObject& mesh, const MeshCore::Material
|
||||
}
|
||||
}
|
||||
|
||||
void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultColor, MeshCore::Material &material)
|
||||
{
|
||||
// copy the color values because the passed material could be the same instance as 'materialRefMesh'
|
||||
std::vector<App::Color> textureColor = materialRefMesh.diffuseColor;
|
||||
material.diffuseColor.clear();
|
||||
material.binding = MeshCore::MeshIO::OVERALL;
|
||||
|
||||
if (kdTree.get()) {
|
||||
// the points of the current mesh
|
||||
std::vector<App::Color> diffuseColor;
|
||||
const MeshCore::MeshPointArray& points = mesh.getKernel().GetPoints();
|
||||
const MeshCore::MeshFacetArray& facets = mesh.getKernel().GetFacets();
|
||||
|
||||
if (binding == MeshCore::MeshIO::PER_VERTEX) {
|
||||
diffuseColor.reserve(points.size());
|
||||
for (size_t index=0; index<points.size(); index++) {
|
||||
unsigned long pos = kdTree->FindExact(points[index]);
|
||||
if (pos < countPointsRefMesh) {
|
||||
diffuseColor.push_back(textureColor[pos]);
|
||||
}
|
||||
else {
|
||||
diffuseColor.push_back(defaultColor);
|
||||
}
|
||||
}
|
||||
|
||||
if (diffuseColor.size() == points.size()) {
|
||||
material.diffuseColor.swap(diffuseColor);
|
||||
material.binding = MeshCore::MeshIO::PER_VERTEX;
|
||||
}
|
||||
}
|
||||
else if (binding == MeshCore::MeshIO::PER_FACE) {
|
||||
// the values of the map give the point indices of the original mesh
|
||||
std::vector<unsigned long> pointMap;
|
||||
pointMap.reserve(points.size());
|
||||
for (size_t index=0; index<points.size(); index++) {
|
||||
unsigned long pos = kdTree->FindExact(points[index]);
|
||||
if (pos < countPointsRefMesh) {
|
||||
pointMap.push_back(pos);
|
||||
}
|
||||
else {
|
||||
pointMap.push_back(ULONG_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
// now determine the facet indices of the original mesh
|
||||
if (pointMap.size() == points.size()) {
|
||||
diffuseColor.reserve(facets.size());
|
||||
for (auto it : facets) {
|
||||
unsigned long index1 = pointMap[it._aulPoints[0]];
|
||||
unsigned long index2 = pointMap[it._aulPoints[1]];
|
||||
unsigned long index3 = pointMap[it._aulPoints[2]];
|
||||
if (index1 != ULONG_MAX && index2 != ULONG_MAX && index3 != ULONG_MAX) {
|
||||
std::vector<unsigned long> found = refPnt2Fac->GetIndices(index1, index2, index3);
|
||||
if (found.size() == 1) {
|
||||
diffuseColor.push_back(textureColor[found.front()]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
diffuseColor.push_back(defaultColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (diffuseColor.size() == facets.size()) {
|
||||
material.diffuseColor.swap(diffuseColor);
|
||||
material.binding = MeshCore::MeshIO::PER_FACE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MeshTexture::apply(const Mesh::MeshObject& mesh, MeshCore::Material &material)
|
||||
{
|
||||
// copy the color values because the passed material could be the same instance as 'materialRefMesh'
|
||||
|
||||
Reference in New Issue
Block a user