Skip to content

Commit

Permalink
Merge pull request #11 from yamahigashi/develop
Browse files Browse the repository at this point in the history
Add support for Maya 2025 and Smooth Mesh
  • Loading branch information
yamahigashi authored Sep 25, 2024
2 parents c6e8f65 + 245a1dc commit 135391a
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 78 deletions.
1 change: 1 addition & 0 deletions buildAll.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rem SET COMPILER=Visual Studio 15 2017 Win64
SET COMPILER=Visual Studio 16 2019
rem SET COMPILER=Visual Studio 17 2022

call :build_for_maya 2025
call :build_for_maya 2024
call :build_for_maya 2023
call :build_for_maya 2022
Expand Down
33 changes: 22 additions & 11 deletions src/intersectionMarkerCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,47 @@ MStatus IntersectionMarkerCommand::doIt(const MArgList& argList)
MFnMesh meshFnA(meshA);
MFnMesh meshFnB(meshB);
MPlug meshAPlug = MFnDependencyNode(meshFnA.object()).findPlug("outMesh", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
MPlug meshBPlug = MFnDependencyNode(meshFnB.object()).findPlug("outMesh", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);

MFnDependencyNode markerNodeFn(this->markerNode);
MPlug markerInMeshAPlug = markerNodeFn.findPlug("inMeshA", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
MPlug markerInMeshBPlug = markerNodeFn.findPlug("inMeshB", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);

dgMod.connect(meshAPlug, markerInMeshAPlug);
status = dgMod.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
dgMod.connect(meshBPlug, markerInMeshBPlug);

// smooth meshes Subdivision
MPlug smoothMeshAPlug = MFnDependencyNode(meshFnA.object()).findPlug("outSmoothMesh", false, &status);
MPlug smoothMeshBPlug = MFnDependencyNode(meshFnB.object()).findPlug("outSmoothMesh", false, &status);
MPlug smoothModeAPlug = MFnDependencyNode(meshFnA.object()).findPlug("displaySmoothMesh", false, &status);
MPlug smoothModeBPlug = MFnDependencyNode(meshFnB.object()).findPlug("displaySmoothMesh", false, &status);
MPlug smoothLevelAPlug = MFnDependencyNode(meshFnA.object()).findPlug("smoothLevel", false, &status);
MPlug smoothLevelBPlug = MFnDependencyNode(meshFnB.object()).findPlug("smoothLevel", false, &status);

MPlug markerInSmoothMeshAPlug = markerNodeFn.findPlug("inSmoothMeshA", false, &status);
MPlug markerInSmoothMeshBPlug = markerNodeFn.findPlug("inSmoothMeshB", false, &status);
MPlug markerSmoothModeAPlug = markerNodeFn.findPlug("smoothModeA", false, &status);
MPlug markerSmoothModeBPlug = markerNodeFn.findPlug("smoothModeB", false, &status);
MPlug markerSmoothLevelAPlug = markerNodeFn.findPlug("smoothLevelA", false, &status);
MPlug markerSmoothLevelBPlug = markerNodeFn.findPlug("smoothLevelB", false, &status);

dgMod.connect(smoothMeshAPlug, markerInSmoothMeshAPlug);
dgMod.connect(smoothMeshBPlug, markerInSmoothMeshBPlug);
dgMod.connect(smoothModeAPlug, markerSmoothModeAPlug);
dgMod.connect(smoothModeBPlug, markerSmoothModeBPlug);
dgMod.connect(smoothLevelAPlug, markerSmoothLevelAPlug);
dgMod.connect(smoothLevelBPlug, markerSmoothLevelBPlug);
status = dgMod.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
// MGlobal::displayInfo("plug connected");

// offset matrix
MPlug meshAMatrixPlug = MFnDependencyNode(meshFnA.parent(0)).findPlug("matrix", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);

MPlug meshBMatrixPlug = MFnDependencyNode(meshFnB.parent(0)).findPlug("matrix", false, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);

MPlug markerOffsetAPlug = markerNodeFn.findPlug("offsetMatrixA", false, &status);
MPlug markerOffsetBPlug = markerNodeFn.findPlug("offsetMatrixB", false, &status);

dgMod.connect(meshAMatrixPlug, markerOffsetAPlug);
status = dgMod.doIt();
dgMod.connect(meshBMatrixPlug, markerOffsetBPlug);
status = dgMod.doIt();

Expand Down
97 changes: 75 additions & 22 deletions src/intersectionMarkerDrawOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,23 @@ MUserData* IntersectionMarkerDrawOverride::prepareForDraw(
prevChecksum = newChecksum;

MFnMesh meshAFn;
status = node->getInputDagMesh(node->meshA, meshAFn);
int smoothModeA;
status = node->getSmoothMode(node->smoothModeA, smoothModeA);
if( smoothModeA == 0 ) {
status = node->getInputDagMesh(node->meshA, meshAFn);
} else {
status = node->getInputDagMesh(node->smoothMeshA, meshAFn);
}
CHECK_MSTATUS_AND_RETURN_DATA("prepareForDraw: meshAFn is null");

MFnMesh meshBFn;
status = node->getInputDagMesh(node->meshB, meshBFn);
int smoothModeB;
status = node->getSmoothMode(node->smoothModeB, smoothModeB);
if( smoothModeB == 0 ) {
status = node->getInputDagMesh(node->meshB, meshBFn);
} else {
status = node->getInputDagMesh(node->smoothMeshB, meshBFn);
}
CHECK_MSTATUS_AND_RETURN_DATA("prepareForDraw: meshBFn is null");

// Get the offset matrix
Expand All @@ -129,10 +141,12 @@ MUserData* IntersectionMarkerDrawOverride::prepareForDraw(

if (showMeshA) {
addIntersectedVertices(meshAFn, data, node->intersectedFaceIdsA, outMatrixA);
// addIntersectedVertices(meshAFn, data, node->intersectedFacesA, outMatrixA);
}

if (showMeshB) {
addIntersectedVertices(meshBFn, data, node->intersectedFaceIdsB, outMatrixB);
// addIntersectedVertices(meshBFn, data, node->intersectedFacesB, outMatrixB);
}

return data;
Expand All @@ -147,43 +161,82 @@ MStatus IntersectionMarkerDrawOverride::addIntersectedVertices(
) {
MStatus status;

MItMeshPolygon itPoly(meshFn.object(), &status);
CHECK_MSTATUS_AND_RETURN_IT(status);

MIntArray vertexIndices;
MPointArray vertices;
int prevIndex;


MIntArray triangleCounts; // number of triangles in each face
MIntArray triangleVertices; // The triangle vertex Ids for each triangle
MIntArray triangleIndices; // The index array for each triangle in face vertex space
MPointArray vertexPositions;
MVector normal;

int numPolygons = meshFn.numPolygons();
meshFn.getTriangles(triangleCounts, triangleVertices);
meshFn.getTriangleOffsets(triangleCounts, triangleIndices);
meshFn.getPoints(vertexPositions, MSpace::kObject); // not having DAG path, so use object space

// Calculate the offset into the triangleVertices array for each polygon
MIntArray polygonTriangleOffsets(numPolygons, 0);
MIntArray polygonIndexOffsets(numPolygons, 0);
for (int polygonIndex = 1; polygonIndex < numPolygons; polygonIndex++) {
int previousTriangles = triangleCounts[polygonIndex - 1];
polygonTriangleOffsets[polygonIndex] = polygonTriangleOffsets[polygonIndex - 1] + previousTriangles * 3;
polygonIndexOffsets[polygonIndex] = polygonIndexOffsets[polygonIndex - 1] + previousTriangles;
}

for (const auto &faceId : intersectedFaceIds) {
status = itPoly.setIndex(faceId, prevIndex);
if( faceId < 0 || faceId >= numPolygons ) {
MString message = "Face ID out of bounds: " + MString(std::to_string(faceId).c_str());
MGlobal::displayInfo(message);
continue;
}
TriangleData triangle;
int numTrianglesInPolygon = triangleCounts[faceId];
int triangleVerticesOffset = polygonTriangleOffsets[faceId];
int triangleIndicesOffset = polygonIndexOffsets[faceId];

CHECK_MSTATUS_AND_RETURN_IT(status);
IntersectionMarkerData::FaceData faceData;

MVector normal;
meshFn.getPolygonNormal(faceId, normal);
int numTriangles;
itPoly.numTriangles(numTriangles);
for (int j = 0; j < numTriangles; ++j) {
itPoly.getTriangle(j, vertices, vertexIndices, MSpace::kObject);
for (unsigned int k = 0; k < vertices.length(); ++k) {
// To avoid z-fighting, move the vertex a little bit along the normal
vertices[k] += normal * 0.001;
vertices[k] *= offsetMatrix;
faceData.vertices.append(vertices[k]);
faceData.edges.append(vertices[k]);
faceData.normals.append(normal);
}
faceData.edges.append(vertices[0]); // close the loop of the triangle
for (int triangleIndex = 0; triangleIndex < numTrianglesInPolygon; triangleIndex++) {
// Get the vertex positions of each triangle
int vertexId0 = triangleVertices[triangleVerticesOffset + triangleIndex * 3 + 0];
int vertexId1 = triangleVertices[triangleVerticesOffset + triangleIndex * 3 + 1];
int vertexId2 = triangleVertices[triangleVerticesOffset + triangleIndex * 3 + 2];
MPoint p0 = vertexPositions[vertexId0] * offsetMatrix;
MPoint p1 = vertexPositions[vertexId1] * offsetMatrix;
MPoint p2 = vertexPositions[vertexId2] * offsetMatrix;
TriangleData triangle(faceId, triangleIndex, p0, p1, p2);

faceData.vertices.append(p0 + normal * 0.001);
faceData.vertices.append(p1 + normal * 0.001);
faceData.vertices.append(p2 + normal * 0.001);
}

// Get normal
data->faces.push_back(faceData);
}

return status;
}


// MStatus IntersectionMarkerDrawOverride::addIntersectedVertices(
// const MFnMesh& meshFn,
// IntersectionMarkerData* data,
// const std::vector<IntersectionMarkerData::FaceData> &intersectedFaces,
// const MMatrix &offsetMatrix
// ) {
// MStatus status;
//
// for (const auto &faceData : intersectedFaces) {
// data->faces.push_back(faceData);
// }
//
// return status;
// }

void IntersectionMarkerDrawOverride::addUIDrawables(
const MDagPath& objPath,
MHWRender::MUIDrawManager& drawManager,
Expand Down
7 changes: 7 additions & 0 deletions src/intersectionMarkerDrawOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,12 @@ class IntersectionMarkerDrawOverride : public MHWRender::MPxDrawOverride
const MMatrix& offsetMatrix
);

MStatus addIntersectedVertices(
const MFnMesh& meshFn,
IntersectionMarkerData* data,
const std::vector<IntersectionMarkerData::FaceData> &intersectedFaces,
const MMatrix& offsetMatrix
);

int prevChecksum = -1;
};
Loading

0 comments on commit 135391a

Please sign in to comment.