private unsafe void apply( MDataBlock block, uint receptorSize, MDoubleArray magnitudeArray, MDoubleArray magnitudeOwnerArray, MVectorArray directionArray, MVectorArray directionOwnerArray, MVectorArray outputForce ) // // Compute output force for each particle. If there exists the // corresponding per particle attribute, use the data passed from // particle shape (stored in magnitudeArray and directionArray). // Otherwise, use the attribute value from the field. // { // get the default values MVector defaultDir = direction(block); double defaultMag = magnitude(block); uint magArraySize = magnitudeArray.length; uint dirArraySize = directionArray.length; uint magOwnerArraySize = magnitudeOwnerArray.length; uint dirOwnerArraySize = directionOwnerArray.length; uint numOfOwner = magOwnerArraySize; if (dirOwnerArraySize > numOfOwner) numOfOwner = dirOwnerArraySize; double m_magnitude = defaultMag; MVector m_direction = defaultDir; for (int ptIndex = 0; ptIndex < receptorSize; ptIndex++) { if (receptorSize == magArraySize) m_magnitude = magnitudeArray[ptIndex]; if (receptorSize == dirArraySize) m_direction = directionArray[ptIndex]; if (numOfOwner > 0) { for (int nthOwner = 0; nthOwner < numOfOwner; nthOwner++) { if (magOwnerArraySize == numOfOwner) m_magnitude = magnitudeOwnerArray[nthOwner]; if (dirOwnerArraySize == numOfOwner) m_direction = directionOwnerArray[nthOwner]; outputForce.append(m_direction * m_magnitude); } } else { outputForce.append(m_direction * m_magnitude); } } }
// // Description // // Returns offsets for the given components to be used my the // move tool in normal/u/v mode. // // Arguments // // component - components to calculate offsets for // direction - array of offsets to be filled // mode - the type of offset to be calculated // normalize - specifies whether the offsets should be normalized // // Returns // // true if the offsets could be calculated, false otherwise // // Support the move tools normal/u/v mode (components) // public override bool vertexOffsetDirection( MObject component, MVectorArray direction, MVertexOffsetMode mode, bool normalize) { bool offsetOkay = false ; MFnSingleIndexedComponent fnComp = new MFnSingleIndexedComponent( component ); if ( component.apiType != MFn.Type.kMeshVertComponent ) { return false; } offsetOkay = true ; apiMeshGeom geomPtr = meshGeom(); if ( null == geomPtr ) { return false; } // For each vertex add the appropriate offset // int count = fnComp.elementCount; for ( int idx=0; idx<count; idx++ ) { MVector normal = geomPtr.normals[ fnComp.element(idx) ]; if( mode == MVertexOffsetMode.kNormal ) { if( normalize ) normal.normalize() ; direction.append( normal ); } else { // Construct an orthonormal basis from the normal // uAxis, and vAxis are the new vectors. // MVector uAxis = new MVector(); MVector vAxis = new MVector(); uint i, j, k; double a; normal.normalize(); i = 0; a = Math.Abs( normal[0] ); if ( a < Math.Abs(normal[1]) ) { i = 1; a = Math.Abs(normal[1]); } if ( a < Math.Abs(normal[2]) ) { i = 2; } j = (i+1)%3; k = (j+1)%3; a = Math.Sqrt(normal[i]*normal[i] + normal[j]*normal[j]); uAxis[i] = -normal[j]/a; uAxis[j] = normal[i]/a; uAxis[k] = 0.0; vAxis = normal.crossProduct( uAxis ); if ( mode == MVertexOffsetMode.kUTangent || mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) uAxis.normalize() ; direction.append( uAxis ); } if ( mode == MVertexOffsetMode.kVTangent || mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) vAxis.normalize() ; direction.append( vAxis ); } if ( mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) normal.normalize() ; direction.append( normal ); } } } return offsetOkay; }
// Support the move tools normal/u/v mode (components) // public override bool vertexOffsetDirection( MObject component, MVectorArray direction, MVertexOffsetMode mode, bool normalize ) // // Description // // Returns offsets for the given components to be used my the // move tool in normal/u/v mode. // // Arguments // // component - components to calculate offsets for // direction - array of offsets to be filled // mode - the type of offset to be calculated // normalize - specifies whether the offsets should be normalized // // Returns // // true if the offsets could be calculated, false otherwise // { bool offsetOkay = false ; MFnSingleIndexedComponent fnComp = new MFnSingleIndexedComponent( component ); if ( component.apiType != MFn.Type.kMeshVertComponent ) { return false; } offsetOkay = true ; apiMeshGeom geomPtr = meshGeom(); if ( null == geomPtr ) { return false; } // For each vertex add the appropriate offset // int count = fnComp.elementCount; for ( int idx=0; idx<count; idx++ ) { MVector normal = geomPtr.normals[ fnComp.element(idx) ]; if( mode == MVertexOffsetMode.kNormal ) { if( normalize ) normal.normalize() ; direction.append( normal ); } else { // Construct an orthonormal basis from the normal // uAxis, and vAxis are the new vectors. // MVector uAxis = new MVector(); MVector vAxis = new MVector(); uint i, j, k; double a; normal.normalize(); i = 0; a = Math.Abs( normal[0] ); if ( a < Math.Abs(normal[1]) ) { i = 1; a = Math.Abs(normal[1]); } if ( a < Math.Abs(normal[2]) ) { i = 2; } j = (i+1)%3; k = (j+1)%3; a = Math.Sqrt(normal[i]*normal[i] + normal[j]*normal[j]); uAxis[i] = -normal[j]/a; uAxis[j] = normal[i]/a; uAxis[k] = 0.0; vAxis = normal.crossProduct( uAxis ); if ( mode == MVertexOffsetMode.kUTangent || mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) uAxis.normalize() ; direction.append( uAxis ); } if ( mode == MVertexOffsetMode.kVTangent || mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) vAxis.normalize() ; direction.append( vAxis ); } if ( mode == MVertexOffsetMode.kUVNTriad ) { if( normalize ) normal.normalize() ; direction.append( normal ); } } } return offsetOkay; }
// // Compute output force for each particle. If there exists the // corresponding per particle attribute, use the data passed from // particle shape (stored in magnitudeArray and directionArray). // Otherwise, use the attribute value from the field. // private unsafe void apply( MDataBlock block, uint receptorSize, MDoubleArray magnitudeArray, MDoubleArray magnitudeOwnerArray, MVectorArray directionArray, MVectorArray directionOwnerArray, MVectorArray outputForce ) { // get the default values MVector defaultDir = direction(block); double defaultMag = magnitude(block); uint magArraySize = magnitudeArray.length; uint dirArraySize = directionArray.length; uint magOwnerArraySize = magnitudeOwnerArray.length; uint dirOwnerArraySize = directionOwnerArray.length; uint numOfOwner = magOwnerArraySize; if (dirOwnerArraySize > numOfOwner) numOfOwner = dirOwnerArraySize; double m_magnitude = defaultMag; MVector m_direction = defaultDir; for (int ptIndex = 0; ptIndex < receptorSize; ptIndex++) { if (receptorSize == magArraySize) m_magnitude = magnitudeArray[ptIndex]; if (receptorSize == dirArraySize) m_direction = directionArray[ptIndex]; if (numOfOwner > 0) { for (int nthOwner = 0; nthOwner < numOfOwner; nthOwner++) { if (magOwnerArraySize == numOfOwner) m_magnitude = magnitudeOwnerArray[nthOwner]; if (dirOwnerArraySize == numOfOwner) m_direction = directionOwnerArray[nthOwner]; outputForce.append(m_direction * m_magnitude); } } else { outputForce.append(m_direction * m_magnitude); } } }
// // Description // // Constructs a cube // public void buildCube(double cube_size, MPointArray pa, MIntArray faceCounts, MIntArray faceConnects, MVectorArray normals, apiMeshGeomUV uvs) { const int num_faces = 6; const int num_face_connects = 24; const double normal_value = 0.5775; const int uv_count = 14; pa.clear(); faceCounts.clear(); faceConnects.clear(); uvs.reset(); pa.append( new MPoint( -cube_size, -cube_size, -cube_size ) ); pa.append( new MPoint( cube_size, -cube_size, -cube_size ) ); pa.append( new MPoint( cube_size, -cube_size, cube_size ) ); pa.append( new MPoint( -cube_size, -cube_size, cube_size ) ); pa.append( new MPoint( -cube_size, cube_size, -cube_size ) ); pa.append( new MPoint( -cube_size, cube_size, cube_size ) ); pa.append( new MPoint( cube_size, cube_size, cube_size ) ); pa.append( new MPoint( cube_size, cube_size, -cube_size ) ); normals.append( new MVector( -normal_value, -normal_value, -normal_value ) ); normals.append( new MVector( normal_value, -normal_value, -normal_value ) ); normals.append( new MVector( normal_value, -normal_value, normal_value ) ); normals.append( new MVector( -normal_value, -normal_value, normal_value ) ); normals.append( new MVector( -normal_value, normal_value, -normal_value ) ); normals.append( new MVector( -normal_value, normal_value, normal_value ) ); normals.append( new MVector( normal_value, normal_value, normal_value ) ); normals.append( new MVector( normal_value, normal_value, -normal_value ) ); // Define the UVs for the cube. // float[] uv_pts = new float[uv_count*2] { 0.375f, 0.0f, 0.625f, 0.0f, 0.625f, 0.25f, 0.375f, 0.25f, 0.625f, 0.5f, 0.375f, 0.5f, 0.625f, 0.75f, 0.375f, 0.75f, 0.625f, 1.0f, 0.375f, 1.0f, 0.875f, 0.0f, 0.875f, 0.25f, 0.125f, 0.0f, 0.125f, 0.25f }; // UV Face Vertex Id. // int[] uv_fvid = new int[num_face_connects]{ 0, 1, 2, 3, 3, 2, 4, 5, 5, 4, 6, 7, 7, 6, 8, 9, 1, 10, 11, 2, 12, 0, 3, 13 }; int i; for ( i = 0; i < uv_count; i ++ ) { uvs.append_uv( uv_pts[i*2], uv_pts[i*2 + 1] ); } for ( i = 0; i < num_face_connects; i ++ ) { uvs.faceVertexIndex.append( uv_fvid[i] ); } // Set up an array containing the number of vertices // for each of the 6 cube faces (4 vertices per face) // int[] face_counts = new int[num_faces]{ 4, 4, 4, 4, 4, 4 }; for ( i=0; i<num_faces; i++ ) { faceCounts.append( face_counts[i] ); } // Set up and array to assign vertices from pa to each face // int[] face_connects = new int[ num_face_connects ]{ 0, 1, 2, 3, 4, 5, 6, 7, 3, 2, 6, 5, 0, 3, 5, 4, 0, 4, 7, 1, 1, 7, 6, 2 }; for ( i=0; i<num_face_connects; i++ ) { faceConnects.append( face_connects[i] ); } }
// // Description // // Create circles of vertices starting with // the top pole ending with the bottom pole // public void buildSphere(double rad, int div, MPointArray vertices, MIntArray counts, MIntArray connects, MVectorArray normals, apiMeshGeomUV uvs) { double u = -Math.PI / 2.0; double v = -Math.PI; double u_delta = Math.PI / ((double)div); double v_delta = 2 * Math.PI / ((double)div); MPoint topPole = new MPoint( 0.0, rad, 0.0 ); MPoint botPole = new MPoint( 0.0, -rad, 0.0 ); // Build the vertex and normal table // vertices.append( botPole ); normals.append( botPole.minus(MPoint.origin) ); int i; for ( i=0; i<(div-1); i++ ) { u += u_delta; v = -Math.PI; for ( int j=0; j<div; j++ ) { double x = rad * Math.Cos(u) * Math.Cos(v); double y = rad * Math.Sin(u); double z = rad * Math.Cos(u) * Math.Sin(v) ; MPoint pnt = new MPoint( x, y, z ); vertices.append( pnt ); normals.append( pnt.minus(MPoint.origin) ); v += v_delta; } } vertices.append( topPole ); normals.append( topPole.minus(MPoint.origin) ); // Create the connectivity lists // int vid = 1; int numV = 0; for ( i=0; i<div; i++ ) { for ( int j=0; j<div; j++ ) { if ( i==0 ) { counts.append( 3 ); connects.append( 0 ); connects.append( j+vid ); connects.append( (j==(div-1)) ? vid : j+vid+1 ); } else if ( i==(div-1) ) { counts.append( 3 ); connects.append( j+vid+1-div ); connects.append( vid+1 ); connects.append( j==(div-1) ? vid+1-div : j+vid+2-div ); } else { counts.append( 4 ); connects.append( j + vid+1-div ); connects.append( j + vid+1 ); connects.append( j == (div-1) ? vid+1 : j+vid+2 ); connects.append( j == (div-1) ? vid+1-div : j+vid+2-div ); } numV++; } vid = numV; } // TODO: Define UVs for sphere ... // }
// // Description // // This function takes an input surface of type kMeshData and converts // the geometry into this nodes attributes. // Returns false if nothing is connected. // public bool computeInputMesh(MPlug plug, MDataBlock datablock, MPointArray vertices, MIntArray counts, MIntArray connects, MVectorArray normals, apiMeshGeomUV uvs) { // Get the input subdiv // MDataHandle inputData = datablock.inputValue( inputMesh ); MObject surf = inputData.asMesh; // Check if anything is connected // MObject thisObj = thisMObject(); MPlug surfPlug = new MPlug( thisObj, inputMesh ); if ( !surfPlug.isConnected ) { datablock.setClean( plug ); return false; } // Extract the mesh data // MFnMesh surfFn = new MFnMesh(surf); surfFn.getPoints( vertices, MSpace.Space.kObject ); // Check to see if we have UVs to copy. // bool hasUVs = surfFn.numUVsProperty > 0; surfFn.getUVs( uvs.ucoord, uvs.vcoord ); for ( int i=0; i<surfFn.numPolygons; i++ ) { MIntArray polyVerts = new MIntArray(); surfFn.getPolygonVertices( i, polyVerts ); int pvc = (int)polyVerts.length; counts.append( pvc ); int uvId; for ( int v=0; v<pvc; v++ ) { if ( hasUVs ) { surfFn.getPolygonUVid( i, v, out uvId ); uvs.faceVertexIndex.append( uvId ); } connects.append( polyVerts[v] ); } } for ( int n=0; n<(int)vertices.length; n++ ) { MVector normal = new MVector(); surfFn.getVertexNormal( n, normal ); normals.append( normal ); } return true; }
private unsafe void apply( MDataBlock block, uint receptorSize, MDoubleArray magnitudeArray, MDoubleArray magnitudeOwnerArray, MVectorArray directionArray, MVectorArray directionOwnerArray, MVectorArray outputForce ) // // Compute output force for each particle. If there exists the // corresponding per particle attribute, use the data passed from // particle shape (stored in magnitudeArray and directionArray). // Otherwise, use the attribute value from the field. // { // get the default values MVector defaultDir = direction(block); double defaultMag = magnitude(block); uint magArraySize = magnitudeArray.length; uint dirArraySize = directionArray.length; uint magOwnerArraySize = magnitudeOwnerArray.length; uint dirOwnerArraySize = directionOwnerArray.length; uint numOfOwner = magOwnerArraySize; if (dirOwnerArraySize > numOfOwner) { numOfOwner = dirOwnerArraySize; } double m_magnitude = defaultMag; MVector m_direction = defaultDir; for (int ptIndex = 0; ptIndex < receptorSize; ptIndex++) { if (receptorSize == magArraySize) { m_magnitude = magnitudeArray[ptIndex]; } if (receptorSize == dirArraySize) { m_direction = directionArray[ptIndex]; } if (numOfOwner > 0) { for (int nthOwner = 0; nthOwner < numOfOwner; nthOwner++) { if (magOwnerArraySize == numOfOwner) { m_magnitude = magnitudeOwnerArray[nthOwner]; } if (dirOwnerArraySize == numOfOwner) { m_direction = directionOwnerArray[nthOwner]; } outputForce.append(m_direction * m_magnitude); } } else { outputForce.append(m_direction * m_magnitude); } } }
// // Description // // Create circles of vertices starting with // the top pole ending with the bottom pole // public void buildSphere(double rad, int div, MPointArray vertices, MIntArray counts, MIntArray connects, MVectorArray normals, apiMeshGeomUV uvs) { double u = -Math.PI / 2.0; double v = -Math.PI; double u_delta = Math.PI / ((double)div); double v_delta = 2 * Math.PI / ((double)div); MPoint topPole = new MPoint(0.0, rad, 0.0); MPoint botPole = new MPoint(0.0, -rad, 0.0); // Build the vertex and normal table // vertices.append(botPole); normals.append(botPole.minus(MPoint.origin)); int i; for (i = 0; i < (div - 1); i++) { u += u_delta; v = -Math.PI; for (int j = 0; j < div; j++) { double x = rad * Math.Cos(u) * Math.Cos(v); double y = rad * Math.Sin(u); double z = rad * Math.Cos(u) * Math.Sin(v); MPoint pnt = new MPoint(x, y, z); vertices.append(pnt); normals.append(pnt.minus(MPoint.origin)); v += v_delta; } } vertices.append(topPole); normals.append(topPole.minus(MPoint.origin)); // Create the connectivity lists // int vid = 1; int numV = 0; for (i = 0; i < div; i++) { for (int j = 0; j < div; j++) { if (i == 0) { counts.append(3); connects.append(0); connects.append(j + vid); connects.append((j == (div - 1)) ? vid : j + vid + 1); } else if (i == (div - 1)) { counts.append(3); connects.append(j + vid + 1 - div); connects.append(vid + 1); connects.append(j == (div - 1) ? vid + 1 - div : j + vid + 2 - div); } else { counts.append(4); connects.append(j + vid + 1 - div); connects.append(j + vid + 1); connects.append(j == (div - 1) ? vid + 1 : j + vid + 2); connects.append(j == (div - 1) ? vid + 1 - div : j + vid + 2 - div); } numV++; } vid = numV; } // TODO: Define UVs for sphere ... // }
// // Description // // Constructs a cube // public void buildCube(double cube_size, MPointArray pa, MIntArray faceCounts, MIntArray faceConnects, MVectorArray normals, apiMeshGeomUV uvs) { const int num_faces = 6; const int num_face_connects = 24; const double normal_value = 0.5775; const int uv_count = 14; pa.clear(); faceCounts.clear(); faceConnects.clear(); uvs.reset(); pa.append(new MPoint(-cube_size, -cube_size, -cube_size)); pa.append(new MPoint(cube_size, -cube_size, -cube_size)); pa.append(new MPoint(cube_size, -cube_size, cube_size)); pa.append(new MPoint(-cube_size, -cube_size, cube_size)); pa.append(new MPoint(-cube_size, cube_size, -cube_size)); pa.append(new MPoint(-cube_size, cube_size, cube_size)); pa.append(new MPoint(cube_size, cube_size, cube_size)); pa.append(new MPoint(cube_size, cube_size, -cube_size)); normals.append(new MVector(-normal_value, -normal_value, -normal_value)); normals.append(new MVector(normal_value, -normal_value, -normal_value)); normals.append(new MVector(normal_value, -normal_value, normal_value)); normals.append(new MVector(-normal_value, -normal_value, normal_value)); normals.append(new MVector(-normal_value, normal_value, -normal_value)); normals.append(new MVector(-normal_value, normal_value, normal_value)); normals.append(new MVector(normal_value, normal_value, normal_value)); normals.append(new MVector(normal_value, normal_value, -normal_value)); // Define the UVs for the cube. // float[] uv_pts = new float[uv_count * 2] { 0.375f, 0.0f, 0.625f, 0.0f, 0.625f, 0.25f, 0.375f, 0.25f, 0.625f, 0.5f, 0.375f, 0.5f, 0.625f, 0.75f, 0.375f, 0.75f, 0.625f, 1.0f, 0.375f, 1.0f, 0.875f, 0.0f, 0.875f, 0.25f, 0.125f, 0.0f, 0.125f, 0.25f }; // UV Face Vertex Id. // int[] uv_fvid = new int[num_face_connects] { 0, 1, 2, 3, 3, 2, 4, 5, 5, 4, 6, 7, 7, 6, 8, 9, 1, 10, 11, 2, 12, 0, 3, 13 }; int i; for (i = 0; i < uv_count; i++) { uvs.append_uv(uv_pts[i * 2], uv_pts[i * 2 + 1]); } for (i = 0; i < num_face_connects; i++) { uvs.faceVertexIndex.append(uv_fvid[i]); } // Set up an array containing the number of vertices // for each of the 6 cube faces (4 vertices per face) // int[] face_counts = new int[num_faces] { 4, 4, 4, 4, 4, 4 }; for (i = 0; i < num_faces; i++) { faceCounts.append(face_counts[i]); } // Set up and array to assign vertices from pa to each face // int[] face_connects = new int[num_face_connects] { 0, 1, 2, 3, 4, 5, 6, 7, 3, 2, 6, 5, 0, 3, 5, 4, 0, 4, 7, 1, 1, 7, 6, 2 }; for (i = 0; i < num_face_connects; i++) { faceConnects.append(face_connects[i]); } }
// // Description // // This function takes an input surface of type kMeshData and converts // the geometry into this nodes attributes. // Returns false if nothing is connected. // public bool computeInputMesh(MPlug plug, MDataBlock datablock, MPointArray vertices, MIntArray counts, MIntArray connects, MVectorArray normals, apiMeshGeomUV uvs) { // Get the input subdiv // MDataHandle inputData = datablock.inputValue(inputMesh); MObject surf = inputData.asMesh; // Check if anything is connected // MObject thisObj = thisMObject(); MPlug surfPlug = new MPlug(thisObj, inputMesh); if (!surfPlug.isConnected) { datablock.setClean(plug); return(false); } // Extract the mesh data // MFnMesh surfFn = new MFnMesh(surf); surfFn.getPoints(vertices, MSpace.Space.kObject); // Check to see if we have UVs to copy. // bool hasUVs = surfFn.numUVsProperty > 0; surfFn.getUVs(uvs.ucoord, uvs.vcoord); for (int i = 0; i < surfFn.numPolygons; i++) { MIntArray polyVerts = new MIntArray(); surfFn.getPolygonVertices(i, polyVerts); int pvc = (int)polyVerts.length; counts.append(pvc); int uvId; for (int v = 0; v < pvc; v++) { if (hasUVs) { surfFn.getPolygonUVid(i, v, out uvId); uvs.faceVertexIndex.append(uvId); } connects.append(polyVerts[v]); } } for (int n = 0; n < (int)vertices.length; n++) { MVector normal = new MVector(); surfFn.getVertexNormal(n, normal); normals.append(normal); } return(true); }