VRIKCalibrationController.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos
  5. {
  6. public class VRIKCalibrationController : MonoBehaviour
  7. {
  8. [Tooltip("Reference to the VRIK component on the avatar.")] public VRIK ik;
  9. [Tooltip("The settings for VRIK calibration.")] public VRIKCalibrator.Settings settings;
  10. [Tooltip("The HMD.")] public Transform headTracker;
  11. [Tooltip("(Optional) A tracker placed anywhere on the body of the player, preferrably close to the pelvis, on the belt area.")] public Transform bodyTracker;
  12. [Tooltip("(Optional) A tracker or hand controller device placed anywhere on or in the player's left hand.")] public Transform leftHandTracker;
  13. [Tooltip("(Optional) A tracker or hand controller device placed anywhere on or in the player's right hand.")] public Transform rightHandTracker;
  14. [Tooltip("(Optional) A tracker placed anywhere on the ankle or toes of the player's left leg.")] public Transform leftFootTracker;
  15. [Tooltip("(Optional) A tracker placed anywhere on the ankle or toes of the player's right leg.")] public Transform rightFootTracker;
  16. [Header("Data stored by Calibration")]
  17. public VRIKCalibrator.CalibrationData data = new VRIKCalibrator.CalibrationData();
  18. void LateUpdate()
  19. {
  20. if (Input.GetKeyDown(KeyCode.C))
  21. {
  22. // Calibrate the character, store data of the calibration
  23. data = VRIKCalibrator.Calibrate(ik, settings, headTracker, bodyTracker, leftHandTracker, rightHandTracker, leftFootTracker, rightFootTracker);
  24. }
  25. /*
  26. * calling Calibrate with settings will return a VRIKCalibrator.CalibrationData, which can be used to calibrate that same character again exactly the same in another scene (just pass data instead of settings),
  27. * without being dependent on the pose of the player at calibration time.
  28. * Calibration data still depends on bone orientations though, so the data is valid only for the character that it was calibrated to or characters with identical bone structures.
  29. * If you wish to use more than one character, it would be best to calibrate them all at once and store the CalibrationData for each one.
  30. * */
  31. if (Input.GetKeyDown(KeyCode.D))
  32. {
  33. if (data.scale == 0f)
  34. {
  35. Debug.LogError("No Calibration Data to calibrate to, please calibrate with settings first.");
  36. }
  37. else
  38. {
  39. // Use data from a previous calibration to calibrate that same character again.
  40. VRIKCalibrator.Calibrate(ik, data, headTracker, bodyTracker, leftHandTracker, rightHandTracker, leftFootTracker, rightFootTracker);
  41. }
  42. }
  43. // Recalibrates avatar scale only. Can be called only if the avatar has been calibrated already.
  44. if (Input.GetKeyDown(KeyCode.S))
  45. {
  46. if (data.scale == 0f)
  47. {
  48. Debug.LogError("Avatar needs to be calibrated before RecalibrateScale is called.");
  49. }
  50. VRIKCalibrator.RecalibrateScale(ik, data, settings);
  51. }
  52. }
  53. }
  54. }