SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3 KiB

4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. using UnityEngine.XR;
  7. namespace UnityEngine.XR.ARFoundation
  8. {
  9. /// <summary>
  10. /// The ARPoseDriver component applies the current Pose value of an AR device to the transform of the GameObject.
  11. /// </summary>
  12. public class ARPoseDriver : MonoBehaviour
  13. {
  14. internal struct NullablePose
  15. {
  16. internal Vector3? position;
  17. internal Quaternion? rotation;
  18. }
  19. protected void OnEnable()
  20. {
  21. Application.onBeforeRender += OnBeforeRender;
  22. }
  23. protected void OnDisable()
  24. {
  25. Application.onBeforeRender -= OnBeforeRender;
  26. }
  27. protected void Update()
  28. {
  29. PerformUpdate();
  30. }
  31. protected void OnBeforeRender()
  32. {
  33. PerformUpdate();
  34. }
  35. protected void PerformUpdate()
  36. {
  37. if (!enabled)
  38. return;
  39. var updatedPose = GetNodePoseData(XR.XRNode.CenterEye);
  40. if (updatedPose.position.HasValue)
  41. transform.localPosition = updatedPose.position.Value;
  42. if (updatedPose.rotation.HasValue)
  43. transform.localRotation = updatedPose.rotation.Value;
  44. }
  45. static internal List<XR.XRNodeState> nodeStates = new List<XR.XRNodeState>();
  46. static internal NullablePose GetNodePoseData(XR.XRNode currentNode)
  47. {
  48. NullablePose resultPose = new NullablePose();
  49. XR.InputTracking.GetNodeStates(nodeStates);
  50. foreach (var nodeState in nodeStates)
  51. {
  52. if (nodeState.nodeType == currentNode)
  53. {
  54. var pose = Pose.identity;
  55. var positionSuccess = nodeState.TryGetPosition(out pose.position);
  56. var rotationSuccess = nodeState.TryGetRotation(out pose.rotation);
  57. if (positionSuccess)
  58. resultPose.position = pose.position;
  59. if (rotationSuccess)
  60. resultPose.rotation = pose.rotation;
  61. return resultPose;
  62. }
  63. }
  64. return resultPose;
  65. }
  66. }
  67. }