public override void SaveAnimation(NUIHumanoidAnimation animation) { // Check if there is capture data if (animation == null) { UnityEngine.Debug.LogWarning("No capture data was found."); return; } // Map captured data to Collada data ColladaAnimationData data = GetColladaAnimation(animation); // Check filename string appendedFileName = string.Format("MoCapHumanoid@{0}", fileName); string newFileName = appendedFileName; if (BaseSystem.IO.File.Exists(string.Format(SAVE_DESTINATION_FORMAT, animSaveDestination, newFileName))) { newFileName = CinemaMocapHelper.GetNewFilename(animSaveDestination, appendedFileName, "dae"); UnityEngine.Debug.LogWarning(string.Format(NAME_DUPLICATE_ERROR_MSG, appendedFileName, newFileName)); } // Save if (transformationType == TransformationType.Matrix) { ColladaUtility.SaveAnimationData(data, SOURCE_FILE_MATRIX_PATH, string.Format(SAVE_DESTINATION_FORMAT, animSaveDestination, newFileName), true); } else { ColladaUtility.SaveAnimationData(data, SOURCE_FILE_PATH, string.Format(SAVE_DESTINATION_FORMAT, animSaveDestination, newFileName), false); } }
/// <summary> /// Once capturing is complete, write out the animation file. /// </summary> private void StopRecording() { // Change to stopped state stopwatch.Stop(); captureState = RecordingState.NotRecording; // Check if there is capture data if (captureData == null) { UnityEngine.Debug.LogWarning("No capture data was found."); return; } // Reload the rig data and mapper if necessary if (inputMapper == null) { rigData = ColladaUtility.ReadRigData(SOURCE_FILE); inputMapper = new NUIInputToRigMapper(rigData); } // Map captured data to Collada data ColladaAnimationData data = inputMapper.GetColladaAnimation(captureData); // Check filename string appendedFileName = string.Format("MoCapHumanoid@{0}", fileName); string newFileName = appendedFileName; if (System.IO.File.Exists(string.Format(FILE_DESTINATION, appendedFileName))) { newFileName = getNewFilename(appendedFileName); UnityEngine.Debug.LogWarning(string.Format(NAME_DUPLICATE_ERROR_MSG, appendedFileName, newFileName)); } // Save if (transformationType == TransformationType.Matrix) { ColladaUtility.SaveAnimationData(data, SOURCE_FILE_MATRIX, string.Format(FILE_DESTINATION, newFileName), true); } else { ColladaUtility.SaveAnimationData(data, SOURCE_FILE, string.Format(FILE_DESTINATION, newFileName), false); } AssetDatabase.Refresh(); }
public ColladaAnimationData GetColladaAnimation(NUIHumanoidAnimation animation) { NUISkeleton outputStructure = GetTargetStructure(); ColladaAnimationData colladaAnimation = new ColladaAnimationData(rigData); List <float> elapsedTimes = new List <float>(); Vector3 startingPosition = Vector3.zero; Vector3 rigStartingPosition = rigData.GetJoint("SpineBase").Translation; int frameRate = 0; switch (selectedFrameRateIndex) { case 0: frameRate = 30; break; case 1: frameRate = 60; break; } NUIHumanoidAnimation animationConstrained = animation.ConstrainFramerate(frameRate); for (int k = 0; k < animationConstrained.Keyframes.Count; k++) { NUIAnimationKeyframe keyframe = animationConstrained.Keyframes[k]; elapsedTimes.Add(keyframe.ElapsedTime); ColladaRigData currentRig = new ColladaRigData(); ColladaJointData parentJoint = rigData.GetJoint("SpineBase"); currentRig.Add(parentJoint.Id, parentJoint); foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData) { ColladaJointData colladaJointData = rigData.GetJoint(jointData.Key); if (colladaJointData.Id == "SpineBase") { Vector3 hipPosition = keyframe.Skeleton.Joints[NUIJointType.SpineBase].Position * 100; hipPosition.x *= -1; if (startingPosition == Vector3.zero) { startingPosition = hipPosition; } colladaJointData.Translation = (hipPosition - startingPosition) + rigStartingPosition; } colladaAnimation.jointTranslateX[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.x); colladaAnimation.jointTranslateY[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.y); colladaAnimation.jointTranslateZ[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.z); } foreach (KeyValuePair <NUIJointType, NUIJoint> kvp in keyframe.Skeleton.Joints) { // For parent joints Quaternion rotation = kvp.Value.Rotation; string id = NUIJointToColladaMapping(kvp.Key); ColladaJointData colladaJointData = rigData.GetJoint(id); if (!outputStructure.Structure.IsJointAnExtremity(kvp.Key)) { Vector3 revert = QuaternionHelper.ToEulerAnglesXYZ(rotation); Vector3 corrected = new Vector3(revert.x, -revert.y, -revert.z); colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", corrected.x); colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", corrected.y); colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", corrected.z); Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, QuaternionHelper.FromEulerAnglesXYZ(corrected), Vector3.one); colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString()); } else { // Extremeties colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.x); colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.y); colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.z); Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, colladaJointData.Rotation, Vector3.one); colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString()); } } } colladaAnimation.frameTimelapse = elapsedTimes; return(colladaAnimation); }