-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathARAnchorManager.cs
66 lines (53 loc) · 2.51 KB
/
ARAnchorManager.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
/* Gets the anchorData's transform value (representing the center of the face), and places an anchor there.*/
public class ARAnchorManager : MonoBehaviour {
[SerializeField]
private GameObject anchorPrefab;
private GameObject anchorGo;
private UnityARSessionNativeInterface m_session;
// Use this for initialization
void Start() {
m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
Application.targetFrameRate = 60;
ARKitFaceTrackingConfiguration config = new ARKitFaceTrackingConfiguration();
config.alignment = UnityARAlignment.UnityARAlignmentGravity;
config.enableLightEstimation = true;
if (config.IsSupported) {
m_session.RunWithConfig(config);
/* This code subscribes our methods FaceAdded, FaceUpdated, & FaceRemoved to
* ARKit's Events, meaning our functions will be called when these events occur. */
UnityARSessionNativeInterface.ARFaceAnchorAddedEvent += FaceAdded;
UnityARSessionNativeInterface.ARFaceAnchorUpdatedEvent += FaceUpdated;
UnityARSessionNativeInterface.ARFaceAnchorRemovedEvent += FaceRemoved;
}
anchorGo = GameObject.Instantiate(anchorPrefab);
}
void FaceAdded(ARFaceAnchor anchorData) {
/* anchorData contains the transform position for the center of the face. We set anchorGo's transform to be the same.
* Then, we activate the anchor gameobject so it is visible. */
anchorGo.transform.position = UnityARMatrixOps.GetPosition(anchorData.transform);
anchorGo.transform.rotation = UnityARMatrixOps.GetRotation(anchorData.transform);
anchorGo.SetActive(true);
}
void FaceUpdated(ARFaceAnchor anchorData) {
if (anchorGo.activeSelf != anchorData.isTracked) {
anchorGo.SetActive(anchorData.isTracked);
}
if (anchorData.isTracked) {
/* anchorData contains the transform position for the center of the face. We set anchorGo's transform to be the same. */
anchorGo.transform.position = UnityARMatrixOps.GetPosition(anchorData.transform);
anchorGo.transform.rotation = UnityARMatrixOps.GetRotation(anchorData.transform);
}
}
void FaceRemoved(ARFaceAnchor anchorData) {
anchorGo.SetActive(false);
}
// Update is called once per frame
void Update() {
}
void OnDestroy() {
}
}