void Awake() { try { // ensure the fbx wrapper is available bool bNeedRestart = false; if (MocapFbxWrapper.EnsureFbxWrapperAvailability(ref bNeedRestart)) { bFbxAvailable = true; if (bNeedRestart) { //KinectInterop.RestartLevel(gameObject, "MF"); return; } } else { throw new Exception("Fbx-Unity-Wrapper is not available!"); } } catch (Exception ex) { Debug.LogError(ex.Message); Debug.LogException(ex); if (infoText != null) { infoText.GetComponent <GUIText>().text = ex.Message; } } }
// saves the specified fbx file private bool SaveFile() { if (saveFilePath == string.Empty) { saveFilePath = loadFilePath; } // delete the old csv file if (saveFilePath != string.Empty && File.Exists(saveFilePath)) { File.Delete(saveFilePath); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); //.ImportAsset(saveFilePath); #endif } if (saveFilePath != string.Empty) { if (MocapFbxWrapper.SaveFbxFile(saveFilePath, outputFileFormat)) { #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); //.ImportAsset(saveFilePath); #endif bFbxDirty = false; // play the animation AnimationPlayer animPlayer = AnimationPlayer.Instance; if (animPlayer) { animPlayer.PlayAnimationClip(saveFilePath, animationName); } return(true); } else { Debug.LogError("Could not save mo-cap to: " + saveFilePath); } } return(false); }
// returns post-rotation of a joint (fbx node) private Quaternion GetJointPostRot(Transform jointTransform) { if (jointTransform != null) { string sJointName = jointTransform.name; Vector3 vPostRot = Vector3.zero; MocapFbxWrapper.GetNodePostRot(sJointName, ref vPostRot); Quaternion qPostRot = Quaternion.identity; MocapFbxWrapper.Rot2Quat(ref vPostRot, ref qPostRot); qPostRot.y = -qPostRot.y; qPostRot.z = -qPostRot.z; return(qPostRot); } return(Quaternion.identity); }
void OnDestroy() { // check if fbx-wrapper is available if (!bFbxAvailable) { return; } // finish recording, if needed isRecording = false; // save the fbx file, if needed if (!SaveFileIfNeeded()) { //Debug.LogError("Could not save the modified fbx to: " + saveFilePath); } // terminate the fbx wrapper if (bFbxInited) { MocapFbxWrapper.TermFbxFrapper(); bFbxInited = false; } }
void Start() { // check if fbx-wrapper is available if (!bFbxAvailable) { return; } try { // instantiate the avatar object from the fbx file if (LoadAvatarObject() && avatarObject) { avatarCtrl = avatarObject.gameObject.GetComponent <AvatarController>(); } else { if (loadFilePath == string.Empty) { throw new Exception("Please specify fbx-file to load."); } else { throw new Exception("Cannot instantiate avatar from file: " + loadFilePath); } } // check the KinectManager availability if (!manager) { manager = KinectManager.Instance; } if (!manager) { throw new Exception("KinectManager not found, probably not initialized. See the log for details"); } // initialize fbx wrapper if (!bFbxInited) { bFbxInited = MocapFbxWrapper.InitFbxWrapper(); if (!bFbxInited) { throw new Exception("Fbx wrapper could not be initialized."); } } if (infoText != null) { infoText.GetComponent <GUIText>().text = "Say: 'Record' to start the recorder."; } // play the animation AnimationPlayer animPlayer = AnimationPlayer.Instance; if (animPlayer) { animPlayer.PlayAnimationClip(saveFilePath, animationName); } if (recordAtStart) { StartRecording(); } } catch (Exception ex) { Debug.LogError(ex.Message); Debug.LogException(ex); if (infoText != null) { infoText.GetComponent <GUIText>().text = ex.Message; } } }
// saves needed animation frames at the current save-time private bool SaveAnimFrame(int jointIndex, Transform jointTransform, StringBuilder sbDebug) { if (jointTransform == null) { return(false); } string sJointName = jointTransform.name; bool bSuccess = true; float fSaveTime = (float)iSavedFrame / fGlobalFps; if (jointIndex == 0 && !dontSaveRootPos) { // save avatar position Vector3 currentPos = jointTransform.position - initialPos; currentPos.x = -currentPos.x; currentPos.y += initialPos.y; float distToLast = (currentPos - lastSavedPos[jointIndex]).magnitude; if (distToLast >= jointDistanceThreshold) { lastSavedPos[jointIndex] = currentPos; if (sbDebug != null) { sbDebug.AppendFormat("{0}:{1:F2} ", jointIndex, distToLast); } Vector3 vJointPos = currentPos * fUnityScale * fFbxScale; bSuccess &= MocapFbxWrapper.SetAnimCurveTrans(sJointName, fSaveTime, ref vJointPos); } } // save joint orientation Quaternion qJointRot = jointTransform.localRotation; float angleToLast = Quaternion.Angle(lastSavedRot[jointIndex], qJointRot); if (angleToLast >= jointAngleThreshold) { lastSavedRot[jointIndex] = qJointRot; if (sbDebug != null) { sbDebug.AppendFormat("{0}:{1:F0} ", jointIndex, angleToLast); } if (alJointPreRots != null && alJointPostRots != null) { Quaternion qJointPreRot = alJointPreRots[jointIndex]; Quaternion qJointPostRot = alJointPostRots[jointIndex]; qJointRot = Quaternion.Inverse(qJointPreRot) * qJointRot * Quaternion.Inverse(qJointPostRot); } qJointRot.x = -qJointRot.x; qJointRot.w = -qJointRot.w; Vector3 vJointRot = Vector3.zero; MocapFbxWrapper.Quat2Rot(ref qJointRot, ref vJointRot); bSuccess &= MocapFbxWrapper.SetAnimCurveRot(sJointName, fSaveTime, ref vJointRot); } if (iSavedFrame == 0) { // save the node scale in the first frame only Vector3 vJointScale = jointTransform.localScale; MocapFbxWrapper.SetAnimCurveScale(sJointName, 0f, ref vJointScale); } return(bSuccess); }
// ----- end of public functions ----- // loads the specified fbx file private bool LoadFile() { if (!bFbxInited) { return(false); } if (!SaveFileIfNeeded()) { throw new Exception("Could not save the modified file to: " + saveFilePath); } string sLoadFbxFile = loadFilePath; if (loadSaveFileIfExists && saveFilePath != string.Empty && File.Exists(saveFilePath)) { sLoadFbxFile = saveFilePath; } if (sLoadFbxFile != string.Empty) { bFbxLoaded = MocapFbxWrapper.LoadFbxFile(sLoadFbxFile, false); } else { bFbxLoaded = false; } if (avatarCtrl && manager && manager.IsInitialized()) { // get the pre- and post-rotations int jointCount = manager.GetJointCount(); lastSavedPos = new Vector3[jointCount + 2]; lastSavedRot = new Quaternion[jointCount + 2]; for (int j = 0; j < (jointCount + 2); j++) { lastSavedPos [j] = Vector3.zero; lastSavedRot [j] = Quaternion.identity; } alJointPreRots = new Quaternion[jointCount + 2]; alJointPostRots = new Quaternion[jointCount + 2]; for (int j = 0; j < jointCount; j++) { int boneIndex = avatarCtrl.GetBoneIndexByJoint((KinectInterop.JointType)j, false); Transform jointTransform = avatarCtrl.GetBoneTransform(boneIndex); alJointPreRots [j] = GetJointPreRot(jointTransform); alJointPostRots [j] = GetJointPostRot(jointTransform); } // get special transforms int specIndex = avatarCtrl.GetSpecIndexByJoint(KinectInterop.JointType.ShoulderLeft, KinectInterop.JointType.SpineShoulder, false); Transform specTransform = avatarCtrl.GetBoneTransform(specIndex); alJointPreRots [jointCount] = GetJointPreRot(specTransform); alJointPostRots [jointCount] = GetJointPostRot(specTransform); specIndex = avatarCtrl.GetSpecIndexByJoint(KinectInterop.JointType.ShoulderRight, KinectInterop.JointType.SpineShoulder, false); specTransform = avatarCtrl.GetBoneTransform(specIndex); alJointPreRots [jointCount + 1] = GetJointPreRot(specTransform); alJointPostRots [jointCount + 1] = GetJointPostRot(specTransform); } else { bFbxLoaded = false; } bFbxDirty = false; return(bFbxLoaded); }
// starts recording public bool StartRecording() { if (avatarObject == null) { return(false); } if (isRecording) { return(false); } isRecording = true; // load the fbx file if (!LoadFile()) { isRecording = false; Debug.LogError("File could not be loaded: " + loadFilePath); if (infoText != null) { infoText.GetComponent <GUIText>().text = "File could not be loaded: " + loadFilePath; } } // check for save file name if (saveFilePath == string.Empty) { isRecording = false; Debug.LogError("No file to save."); if (infoText != null) { infoText.GetComponent <GUIText>().text = "No file to save."; } } // create the animation stack, if animation name is specified if (animationName != string.Empty && !MocapFbxWrapper.CreateAnimStack(animationName)) { isRecording = false; //throw new Exception("Could not create animation: " + animationName); Debug.LogError("Could not create animation: " + animationName); if (infoText != null) { infoText.GetComponent <GUIText>().text = "Could not create animation: " + animationName; } } if (isRecording && animationName != string.Empty) { MocapFbxWrapper.SetCurrentAnimStack(animationName); } if (isRecording) { Debug.Log("Recording started."); if (infoText != null) { infoText.GetComponent <GUIText>().text = "Recording... Say 'Stop' to stop the recorder."; } // get initial avatar position if (avatarCtrl != null) { int hipsIndex = avatarCtrl.GetBoneIndexByJoint(KinectInterop.JointType.SpineBase, false); Transform hipsTransform = avatarCtrl.GetBoneTransform(hipsIndex); initialPos = hipsTransform != null ? hipsTransform.position : Vector3.zero; } // initialize times fStartTime = fCurrentTime = Time.time; fMinFrameTime = maxFramesPerSecond > 0f ? 1f / maxFramesPerSecond : 0f; iSavedFrame = 0; // get global time mode as fps fGlobalFps = MocapFbxWrapper.GetGlobalFps(); // get global scale factor fFbxScale = 100f / MocapFbxWrapper.GetGlobalScaleFactor(); // Unity scale factor fUnityScale = importScaleFactor != 0f ? 1f / importScaleFactor : 1f; } return(isRecording); }