Hair Curve Deformer

I was sick for a couple days this week and decided to create a instancer/deformer plugin for maya. I’ve based this on the maya hair system, at first I got it functioning with one hair and one instance object, which was not too difficult. I drive the deformation by building a new transformation matrix for each point of the instance object, based on the relative height on the hair curve and the tangents of where it was birthed on the mesh. This keeps it from twisting relative to the mesh during complex motions. After I proved this could work I put together a polygon duplicator, I was expecting there to be a button for this, there isn’t. Instead I had to have the plugin record all the data maya uses to create a mesh from scratch, re-append the data as I built each instance and then create the mesh after all my calculations were done. Although this sucked, I had to do something similar last month to rebuild meshes in mental ray so it wasn’t so bad. Here’s a sample video:





			MArrayDataHandle curve_array_handle = data.inputArrayValue(aInCurve);
			int numCurves = curve_array_handle.elementCount ();

			num_polys = pre_num_polys*numCurves;
			num_verts = pre_num_verts*numCurves;
			curve_array_handle.jumpToElement(0);

			for (int curveNum = 0; curveNum < numCurves; curveNum++)
			{

				MDataHandle id_handle= curve_array_handle.inputValue();
				MObject current_curve_obj =id_handle.asNurbsCurve();
				MFnNurbsCurve curveFn( current_curve_obj);
				MPoint inst_pos;
				curveFn.getCV(0,inst_pos,MSpace::kWorld);
				MVector inst_normal;
				MPointOnMesh meshPt;
				MFloatVector inst_tangent;
				intersector.getClosestPoint(inst_pos, meshPt);
				int id = meshPt.faceIndex();

				MFloatVectorArray inst_tangent_array;
				transMeshFn.getFaceVertexTangents(id,inst_tangent_array,MSpace::kWorld);
				int len_inst_tangent_array=inst_tangent_array.length();
				for( int a = 0; a < len_inst_tangent_array; a++ )
					{
							inst_tangent += inst_tangent_array[a];
					}
				inst_tangent/= len_inst_tangent_array;
				featherIt.reset();
				while (featherIt.isDone() == false)
				{
						int index = featherIt.index();
						MPoint current_point = pre_feath_points[index];
						MVector curve_deriv;
						MFloatVector inst_binorm;
						MPoint curve_pos;
						double arc_len = curveFn.length();
						double hi_low_ratio = (current_point.y-lowest_point)/(highest_point - lowest_point);
						double curve_param = curveFn.findParamFromLength(hi_low_ratio*arc_len);
						curveFn.getDerivativesAtParm(curve_param,curve_pos,curve_deriv,MSpace::kObject);
						inst_tangent.normalize();
						curve_deriv.normalize();
						inst_binorm = inst_tangent^curve_deriv;
						MVector cross_tangent =  inst_binorm^curve_deriv;
						cross_tangent.normalize();
						double matrix[4][4] = {{inst_binorm.x,inst_binorm.y,inst_binorm.z,0}, {curve_deriv.x,curve_deriv.y,curve_deriv.z,0}, {cross_tangent.x,cross_tangent.y,cross_tangent.z,0},{curve_pos.x,curve_pos.y,curve_pos.z,0}};
						MMatrix inst_matrix(matrix);
						inst_matrix.homogenize();
						MPoint flattened_point(current_point.x,0.0,current_point.z);
						feath_points.append(flattened_point* inst_matrix);
						featherIt.next();
				}
				for (int i = 0; i < pre_connected_verts.length();i++)
				{
					int con_vert_print = pre_connected_verts[i]+(pre_num_verts*curveNum);
					connected_verts.append(con_vert_print);
				}
				for (int i = 0; i < pre_poly_counts.length();i++)
				{
					poly_counts.append(pre_poly_counts[i]);
				}
				for (int i = 0; i < pre_u_array.length();i++)
				{
					u_array.append(pre_u_array[i]);
					v_array.append(pre_v_array[i]);
				}

				curve_array_handle.next();
			}

			MFnMeshData meshDataFn;
			MObject newMeshObj = meshDataFn.create();
			MFnMesh newMeshFn;

			newMeshFn.create(num_verts,num_polys,feath_points,poly_counts,connected_verts,u_array,v_array,newMeshObj);
			outMeshDataHandle.setMObject(newMeshObj );

			data.setClean(plug);
		//				MGlobal::displ

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: