Importing custom mesh without material.
This commit is contained in:
@@ -76,8 +76,8 @@ std::map<int, MeshClass::WeightInfo> MeshClass::GetWeightData()
|
||||
Mesh MeshClass::GetMeshData(MObject object)
|
||||
{
|
||||
Mesh newMesh;
|
||||
std::vector<VertexLayout>& vertexList = newMesh.Vertices;
|
||||
std::vector<int>& indexList = newMesh.Indices;
|
||||
vector<VertexLayout>& vertexList = newMesh.Vertices;
|
||||
map<string, vector<int>>& indexLists = newMesh.Indices;
|
||||
// In here, we retrieve triangulated polygons from the mesh
|
||||
MFnMesh mesh(object);
|
||||
|
||||
@@ -91,11 +91,28 @@ Mesh MeshClass::GetMeshData(MObject object)
|
||||
float2 UV;
|
||||
double biTangent[3];
|
||||
double biNormal[3];
|
||||
VertexLayout thisVertex;
|
||||
MFloatVectorArray Tangents;
|
||||
MFloatVectorArray biNormals;
|
||||
|
||||
std::map<int, WeightInfo> vertexWeights = GetWeightData();
|
||||
MObjectArray shaderList;
|
||||
MIntArray shaderIndexList;
|
||||
mesh.getConnectedShaders(0, shaderList, shaderIndexList);
|
||||
|
||||
map<string, vector<int>> materialFaceIDs;
|
||||
MGlobal::displayInfo(MString() + "shaderIndexList: " + shaderIndexList.length());
|
||||
MGlobal::displayInfo(MString() + "shaderList: " + shaderList.length());
|
||||
MPlugArray plugArray;
|
||||
for (int i = 0; i < shaderIndexList.length(); i++)
|
||||
{
|
||||
MFnDependencyNode shader(shaderList[shaderIndexList[i]]);
|
||||
MPlug p_Plug = shader.findPlug("surfaceShader");
|
||||
if (p_Plug.connectedTo(plugArray, true, false)) {
|
||||
MFnDependencyNode node = plugArray[0].node();
|
||||
materialFaceIDs[node.name().asChar()].push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
map<int, WeightInfo> vertexWeights = GetWeightData();
|
||||
|
||||
mesh.getTangents(Tangents, MSpace::kObject, NULL);
|
||||
mesh.getBinormals(biNormals, MSpace::kObject, NULL);
|
||||
@@ -103,82 +120,139 @@ Mesh MeshClass::GetMeshData(MObject object)
|
||||
MItMeshFaceVertex faceVert(object);
|
||||
|
||||
int intDummy = 0;
|
||||
|
||||
MItMeshPolygon meshPolyIter(object);
|
||||
|
||||
for (MItMeshPolygon meshPolyIter(object); !meshPolyIter.isDone(); meshPolyIter.next()) {
|
||||
for (auto aMaterial : materialFaceIDs) {
|
||||
for (auto faceID : aMaterial.second) {
|
||||
|
||||
vector<unsigned int> localVertexToGlobalIndex;
|
||||
unsigned int indexOffset = vertexList.size();
|
||||
vector<array<unsigned int, 2>> localVertexToGlobalIndex;
|
||||
meshPolyIter.setIndex(faceID, intDummy);
|
||||
|
||||
meshPolyIter.getVertices(vertices);
|
||||
meshPolyIter.getTriangles(dummy, triangleList);
|
||||
meshPolyIter.getVertices(vertices);
|
||||
meshPolyIter.getTriangles(dummy, triangleList);
|
||||
//MGlobal::displayInfo("Befor Second Loop");
|
||||
for (unsigned int i = 0; i < vertices.length(); i++) {
|
||||
VertexLayout thisVertex;
|
||||
vertexIndex = meshPolyIter.vertexIndex(i);
|
||||
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
|
||||
//MGlobal::displayInfo("In Second Loop");
|
||||
pos = faceVert.position();
|
||||
if (abs(pos.x) > 0.0001)
|
||||
thisVertex.Pos[0] = pos.x;
|
||||
if (abs(pos.y) > 0.0001)
|
||||
thisVertex.Pos[1] = pos.y;
|
||||
if (abs(pos.z) > 0.0001)
|
||||
thisVertex.Pos[2] = pos.z;
|
||||
|
||||
//MGlobal::displayInfo("Befor Second Loop");
|
||||
for (unsigned int i = 0; i < vertices.length(); i++) {
|
||||
vertexIndex = meshPolyIter.vertexIndex(i);
|
||||
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
|
||||
//MGlobal::displayInfo("In Second Loop");
|
||||
faceVert.position().get(thisVertex.Pos);
|
||||
faceVert.getNormal(normal);
|
||||
thisVertex.Normal[0] = normal[0];
|
||||
thisVertex.Normal[1] = normal[1];
|
||||
thisVertex.Normal[2] = normal[2];
|
||||
faceVert.getNormal(normal);
|
||||
if (abs(normal[0]) > 0.0001)
|
||||
thisVertex.Normal[0] = normal[0];
|
||||
if (abs(normal[1]) > 0.0001)
|
||||
thisVertex.Normal[1] = normal[1];
|
||||
if (abs(normal[2]) > 0.0001)
|
||||
thisVertex.Normal[2] = normal[2];
|
||||
|
||||
MFloatVector Tangent = Tangents[faceVert.tangentId()];
|
||||
//MVector tmp = faceVert.getTangent(MSpace::kObject, NULL);
|
||||
//tmp.get(biTangent);
|
||||
thisVertex.Tangent[0] = Tangent[0];
|
||||
thisVertex.Tangent[1] = Tangent[1];
|
||||
thisVertex.Tangent[2] = Tangent[2];
|
||||
MFloatVector Tangent = Tangents[faceVert.tangentId()];
|
||||
//MVector tmp = faceVert.getTangent(MSpace::kObject, NULL);
|
||||
//tmp.get(biTangent);
|
||||
if (abs(Tangent[0]) > 0.0001)
|
||||
thisVertex.Tangent[0] = Tangent[0];
|
||||
if (abs(Tangent[1]) > 0.0001)
|
||||
thisVertex.Tangent[1] = Tangent[1];
|
||||
if (abs(Tangent[2]) > 0.0001)
|
||||
thisVertex.Tangent[2] = Tangent[2];
|
||||
|
||||
MFloatVector biNormal = biNormals[faceVert.tangentId()];
|
||||
//faceVert.getBinormal().get(biNormal);
|
||||
thisVertex.BiNormal[0] = biNormal[0];
|
||||
thisVertex.BiNormal[1] = biNormal[1];
|
||||
thisVertex.BiNormal[2] = biNormal[2];
|
||||
MFloatVector biNormal = biNormals[faceVert.tangentId()];
|
||||
//faceVert.getBinormal().get(biNormal);
|
||||
if (abs(biNormal[0]) > 0.0001)
|
||||
thisVertex.BiNormal[0] = biNormal[0];
|
||||
if (abs(biNormal[1]) > 0.0001)
|
||||
thisVertex.BiNormal[1] = biNormal[1];
|
||||
if (abs(biNormal[2]) > 0.0001)
|
||||
thisVertex.BiNormal[2] = biNormal[2];
|
||||
|
||||
faceVert.getUV(UV);
|
||||
thisVertex.Uv[0] = UV[0];
|
||||
thisVertex.Uv[1] = UV[1];
|
||||
faceVert.getUV(UV);
|
||||
thisVertex.Uv[0] = UV[0];
|
||||
thisVertex.Uv[1] = UV[1];
|
||||
|
||||
thisVertex.BoneIndices[0] = vertexWeights[faceVert.vertId()].BoneIndices[0];
|
||||
thisVertex.BoneIndices[1] = vertexWeights[faceVert.vertId()].BoneIndices[1];
|
||||
thisVertex.BoneIndices[2] = vertexWeights[faceVert.vertId()].BoneIndices[2];
|
||||
thisVertex.BoneIndices[3] = vertexWeights[faceVert.vertId()].BoneIndices[3];
|
||||
thisVertex.BoneIndices[0] = vertexWeights[faceVert.vertId()].BoneIndices[0];
|
||||
thisVertex.BoneIndices[1] = vertexWeights[faceVert.vertId()].BoneIndices[1];
|
||||
thisVertex.BoneIndices[2] = vertexWeights[faceVert.vertId()].BoneIndices[2];
|
||||
thisVertex.BoneIndices[3] = vertexWeights[faceVert.vertId()].BoneIndices[3];
|
||||
|
||||
thisVertex.BoneWeights[0] = vertexWeights[faceVert.vertId()].BoneWeights[0];
|
||||
thisVertex.BoneWeights[1] = vertexWeights[faceVert.vertId()].BoneWeights[1];
|
||||
thisVertex.BoneWeights[2] = vertexWeights[faceVert.vertId()].BoneWeights[2];
|
||||
thisVertex.BoneWeights[3] = vertexWeights[faceVert.vertId()].BoneWeights[3];
|
||||
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[0]) > 0.0001)
|
||||
thisVertex.BoneWeights[0] = vertexWeights[faceVert.vertId()].BoneWeights[0];
|
||||
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[1]) > 0.0001)
|
||||
thisVertex.BoneWeights[1] = vertexWeights[faceVert.vertId()].BoneWeights[1];
|
||||
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[2]) > 0.0001)
|
||||
thisVertex.BoneWeights[2] = vertexWeights[faceVert.vertId()].BoneWeights[2];
|
||||
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[3]) > 0.0001)
|
||||
thisVertex.BoneWeights[3] = vertexWeights[faceVert.vertId()].BoneWeights[3];
|
||||
|
||||
std::vector<VertexLayout>::iterator it = std::find(vertexList.begin(), vertexList.end(), thisVertex);
|
||||
|
||||
if (it != vertexList.end()) {
|
||||
localVertexToGlobalIndex.push_back(vertexIndex);
|
||||
} else {
|
||||
localVertexToGlobalIndex.push_back(vertexIndex);
|
||||
vertexList.push_back(thisVertex);
|
||||
}
|
||||
|
||||
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl;
|
||||
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl;
|
||||
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl;
|
||||
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
|
||||
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
|
||||
}
|
||||
|
||||
|
||||
for (unsigned int i = 0; i < triangleList.length(); i++) {
|
||||
unsigned int k = 0;
|
||||
if (localVertexToGlobalIndex.size() > 0) {
|
||||
while (localVertexToGlobalIndex[k] != triangleList[i] && k < localVertexToGlobalIndex.size()) {
|
||||
k++;
|
||||
float totalWeight = thisVertex.BoneWeights[0] + thisVertex.BoneWeights[1] + thisVertex.BoneWeights[2] + thisVertex.BoneWeights[3];
|
||||
if (totalWeight < 1.00f && totalWeight > 0.01f) {
|
||||
thisVertex.BoneWeights[0] /= totalWeight;
|
||||
thisVertex.BoneWeights[1] /= totalWeight;
|
||||
thisVertex.BoneWeights[2] /= totalWeight;
|
||||
thisVertex.BoneWeights[3] /= totalWeight;
|
||||
}
|
||||
indexList.push_back(indexOffset + k);
|
||||
}
|
||||
|
||||
std::vector<VertexLayout>::iterator it = std::find(vertexList.begin(), vertexList.end(), thisVertex);
|
||||
array<unsigned int, 2> tmp;
|
||||
if (it != vertexList.end()) {
|
||||
tmp[0] = vertexIndex;
|
||||
tmp[1] = it - vertexList.begin();
|
||||
localVertexToGlobalIndex.push_back(tmp);
|
||||
} else {
|
||||
tmp[0] = vertexIndex;
|
||||
tmp[1] = vertexList.size();
|
||||
localVertexToGlobalIndex.push_back(tmp);
|
||||
vertexList.push_back(thisVertex);
|
||||
}
|
||||
//MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1]: " + localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1][0] + " " + localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1][1]);
|
||||
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl;
|
||||
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl;
|
||||
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl;
|
||||
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
|
||||
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
|
||||
}
|
||||
for (unsigned int i = 0; i < triangleList.length(); i++) {
|
||||
unsigned int k = 0;
|
||||
if (localVertexToGlobalIndex.size() > 0) {
|
||||
//MGlobal::displayInfo(MString() + "triangleList[i] : " + triangleList[i]);
|
||||
while (localVertexToGlobalIndex[k][0] != triangleList[i] && k < localVertexToGlobalIndex.size()) {
|
||||
k++;
|
||||
}
|
||||
//MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[k] : " + localVertexToGlobalIndex[k][0] + " " + localVertexToGlobalIndex[k][1]);
|
||||
indexLists[aMaterial.first.c_str()].push_back(localVertexToGlobalIndex[k][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// MGlobal::displayInfo( MString() + "localVertexToGlobalIndex.size(): " + localVertexToGlobalIndex.size());
|
||||
// if (localVertexToGlobalIndex.size() > 0) {
|
||||
// MGlobal::displayInfo(MString() + "triangleList.length(): " + triangleList.length());
|
||||
// for (unsigned int i = triangleList.length() - 1; i >= 0; i--) {
|
||||
// MGlobal::displayInfo(MString() + "i: " + i);
|
||||
// unsigned int k = localVertexToGlobalIndex.size() - 1;
|
||||
// MGlobal::displayInfo(MString() + "triangleList[i] : " + triangleList[i]);
|
||||
// while (localVertexToGlobalIndex[k] != triangleList[i] && k >= 0) {
|
||||
// MGlobal::displayInfo(MString() + "k: " + k);
|
||||
// k--;
|
||||
// }
|
||||
// MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[k] : " + localVertexToGlobalIndex[k]);
|
||||
// indexList.push_back(indexOffset + k);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
newMesh.NumIndices = newMesh.Indices.size();
|
||||
|
||||
int totalIndecies = 0;
|
||||
for (auto aList : newMesh.Indices) {
|
||||
totalIndecies += aList.second.size();
|
||||
}
|
||||
|
||||
newMesh.NumIndices = totalIndecies;
|
||||
newMesh.NumVertices = newMesh.Vertices.size();
|
||||
|
||||
return newMesh;
|
||||
|
||||
Reference in New Issue
Block a user