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