void IntializeRecording() { List <string> controlNames = character.GetControlList(); List <UnityEngine.Object> controls = LiveUnityInterface.GetControls(this.gameObject, controlNames); recordingTime = 0.0f; //Loop through controls and create anim curves for each for (int i = 0; i < controlNames.Count; i++) { FTIAnimCurve ftiCurve = new FTIAnimCurve(); UnityEngine.Object control = controls[i]; if (control is Transform) { Transform transfromControl = (Transform)control; ftiCurve.hierachyPath = LiveUnityInterface.GetRelativeGameObjectPath(transfromControl.gameObject, this.gameObject); ftiCurve.isBlendShape = false; int curveNum = 3; string attr = LiveHelpers.GetAttrString(controlNames [i]); if (attr == LiveCharacterSetup.rotationSuffix) { curveNum++; } for (int j = 0; j < curveNum; j++) { ftiCurve.animCurves.Add(new AnimationCurve()); //Intialize values to have at least 1 keyframe if (attr != LiveCharacterSetup.rotationSuffix) { ftiCurve.animCurves[j].AddKey(0f, transfromControl.localPosition[j]); } else { ftiCurve.animCurves[j].AddKey(0f, transfromControl.localRotation[j]); } } } else if (control is SkinnedMeshRenderer) { SkinnedMeshRenderer blendShapeControl = (SkinnedMeshRenderer)control; string attr = LiveHelpers.GetAttrString(controlNames[i]); int index = LiveUnityInterface.GetBlendShapeIndex(blendShapeControl, attr); ftiCurve.hierachyPath = LiveUnityInterface.GetRelativeGameObjectPath(blendShapeControl.gameObject, this.gameObject); ftiCurve.isBlendShape = true; ftiCurve.animCurves.Add(new AnimationCurve()); ftiCurve.animCurves[0].AddKey(0f, blendShapeControl.GetBlendShapeWeight(index)); } RecordedCurves.Add(controlNames[i], ftiCurve); } }
public void RecordFrame() { if (Recording && live.m_RecievedNewData) { live.m_RecievedNewData = false; //Grab values from rig Dictionary <string, Vector4> controlValues; if (cachedRigValues == null) { controlValues = LiveUnityInterface.GetControlValues(this.gameObject, character.GetControlList()); } else { controlValues = cachedRigValues; } //Write keyframes for each control, if keyframe on new data only check if there is new data foreach (KeyValuePair <string, Vector4> controlKvp in controlValues) { string controlName = controlKvp.Key; FTIAnimCurve curveData = RecordedCurves[controlName]; if (curveData.isBlendShape) { if (!KeyframeOnNewData || curveData.animCurves[0].keys[curveData.animCurves[0].keys.Length - 1].value != controlKvp.Value.x) { Keyframe newFrame = new Keyframe(recordingTime, controlKvp.Value.x); curveData.animCurves[0].AddKey(newFrame); } } else { if (!KeyframeOnNewData || curveData.animCurves[0].keys[curveData.animCurves[0].keys.Length - 1].value != controlKvp.Value.x) { Keyframe newFrame = new Keyframe(recordingTime, controlKvp.Value.x); curveData.animCurves[0].AddKey(newFrame); } if (!KeyframeOnNewData || curveData.animCurves[1].keys[curveData.animCurves[1].keys.Length - 1].value != controlKvp.Value.y) { Keyframe newFrame = new Keyframe(recordingTime, controlKvp.Value.y); curveData.animCurves[1].AddKey(newFrame); } if (!KeyframeOnNewData || curveData.animCurves[2].keys[curveData.animCurves[2].keys.Length - 1].value != controlKvp.Value.z) { Keyframe newFrame = new Keyframe(recordingTime, controlKvp.Value.z); curveData.animCurves[2].AddKey(newFrame); } string attr = LiveHelpers.GetAttrString(controlName); if (attr == LiveCharacterSetup.rotationSuffix) { if (!KeyframeOnNewData || curveData.animCurves[3].keys[curveData.animCurves[3].keys.Length - 1].value != controlKvp.Value.w) { Keyframe newFrame = new Keyframe(recordingTime, controlKvp.Value.w); curveData.animCurves[3].AddKey(newFrame); } } } } } }
public void OnToggleRecording() { if (RecordedCurves == null) { RecordedCurves = new Dictionary <string, FTIAnimCurve>(); } Recording = !Recording; if (!Recording) { //Write data to clip Debug.Log("[Faceware Live] Recording Stopped. Writing to animation clip. This may take a moment."); if (ClipToWriteTo != null) { foreach (KeyValuePair <string, FTIAnimCurve> curveKvp in RecordedCurves) { string controlName = curveKvp.Key; FTIAnimCurve curveData = curveKvp.Value; if (curveData.isBlendShape) { #if UNITY_EDITOR && (UNITY_5 || UNITY_2017 || UNITY_2018) #if UNITY_5_4_OR_NEWER //If in editor and a version that supports curve setting, set the curves to what is specified for (int i = 0; i < curveData.animCurves[0].keys.Length; i++) { UnityEditor.AnimationUtility.SetKeyLeftTangentMode(curveData.animCurves[0], i, keyframeTangentMode); UnityEditor.AnimationUtility.SetKeyRightTangentMode(curveData.animCurves[0], i, keyframeTangentMode); } #endif #endif string blendShapeAttr = LiveHelpers.GetAttrString(controlName); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(SkinnedMeshRenderer), "blendShape." + blendShapeAttr, curveData.animCurves[0]); } else { string controlAttr = LiveHelpers.GetAttrString(controlName); if (controlAttr == LiveCharacterSetup.rotationSuffix) { ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalRotation.x", curveData.animCurves[0]); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalRotation.y", curveData.animCurves[1]); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalRotation.z", curveData.animCurves[2]); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalRotation.w", curveData.animCurves[3]); } else if (controlAttr == LiveCharacterSetup.translationSuffix) { ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalPosition.x", curveData.animCurves[0]); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalPosition.y", curveData.animCurves[1]); ClipToWriteTo.SetCurve(curveData.hierachyPath, typeof(Transform), "m_LocalPosition.z", curveData.animCurves[2]); } } } Debug.Log("[Faceware Live] Animation successfully written to " + ClipToWriteTo.name + ".anim"); RecordedCurves.Clear(); } else { Debug.LogWarning("[Faceware Live] Animtion could not be recorded as Animation Clip to write to was null. Please set an animation clip to write the animation to"); } } else { RecordedCurves.Clear(); IntializeRecording(); Debug.Log("[Faceware Live] Recording Started!"); } }