VRIKCalibrationBasic.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using RootMotion.FinalIK;
  5. namespace RootMotion.Demos
  6. {
  7. public class VRIKCalibrationBasic : MonoBehaviour
  8. {
  9. [Tooltip("The VRIK component.")] public VRIK ik;
  10. [Header("Head")]
  11. [Tooltip("HMD.")] public Transform centerEyeAnchor;
  12. [Tooltip("Position offset of the camera from the head bone (root space).")] public Vector3 headAnchorPositionOffset;
  13. [Tooltip("Rotation offset of the camera from the head bone (root space).")] public Vector3 headAnchorRotationOffset;
  14. [Header("Hands")]
  15. [Tooltip("Left Hand Controller")] public Transform leftHandAnchor;
  16. [Tooltip("Right Hand Controller")] public Transform rightHandAnchor;
  17. [Tooltip("Position offset of the hand controller from the hand bone (controller space).")] public Vector3 handAnchorPositionOffset;
  18. [Tooltip("Rotation offset of the hand controller from the hand bone (controller space).")] public Vector3 handAnchorRotationOffset;
  19. [Header("Scale")]
  20. [Tooltip("Multiplies the scale of the root.")] public float scaleMlp = 1f;
  21. [Header("Data stored by Calibration")]
  22. public VRIKCalibrator.CalibrationData data = new VRIKCalibrator.CalibrationData();
  23. private void LateUpdate()
  24. {
  25. if (Input.GetKeyDown(KeyCode.C))
  26. {
  27. // Calibrate the character, store data of the calibration
  28. data = VRIKCalibrator.Calibrate(ik, centerEyeAnchor, leftHandAnchor, rightHandAnchor, headAnchorPositionOffset, headAnchorRotationOffset, handAnchorPositionOffset, handAnchorRotationOffset, scaleMlp);
  29. }
  30. /*
  31. * 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),
  32. * without being dependent on the pose of the player at calibration time.
  33. * 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.
  34. * 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.
  35. * */
  36. if (Input.GetKeyDown(KeyCode.D))
  37. {
  38. if (data.scale == 0f)
  39. {
  40. Debug.LogError("No Calibration Data to calibrate to, please calibrate with 'C' first.");
  41. }
  42. else
  43. {
  44. VRIKCalibrator.Calibrate(ik, data, centerEyeAnchor, null, leftHandAnchor, rightHandAnchor);
  45. }
  46. }
  47. // Recalibrates avatar scale only. Can be called only if the avatar has been calibrated already.
  48. if (Input.GetKeyDown(KeyCode.S))
  49. {
  50. if (data.scale == 0f)
  51. {
  52. Debug.LogError("Avatar needs to be calibrated before RecalibrateScale is called.");
  53. }
  54. VRIKCalibrator.RecalibrateScale(ik, data, scaleMlp);
  55. }
  56. }
  57. }
  58. }