-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUnityARCameraManager.cs
125 lines (101 loc) · 4.08 KB
/
UnityARCameraManager.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
public class UnityARCameraManager : MonoBehaviour {
public Camera m_camera;
private UnityARSessionNativeInterface m_session;
private Material savedClearMaterial;
[Header("AR Config Options")]
public UnityARAlignment startAlignment = UnityARAlignment.UnityARAlignmentGravity;
public UnityARPlaneDetection planeDetection = UnityARPlaneDetection.Horizontal;
public bool getPointCloud = true;
public bool enableLightEstimation = true;
public bool enableAutoFocus = true;
public UnityAREnvironmentTexturing environmentTexturing = UnityAREnvironmentTexturing.UnityAREnvironmentTexturingNone;
[Header("Image Tracking")]
public ARReferenceImagesSet detectionImages = null;
public int maximumNumberOfTrackedImages = 0;
[Header("Object Tracking")]
public ARReferenceObjectsSetAsset detectionObjects = null;
private bool sessionStarted = false;
public ARKitWorldTrackingSessionConfiguration sessionConfiguration
{
get
{
ARKitWorldTrackingSessionConfiguration config = new ARKitWorldTrackingSessionConfiguration ();
config.planeDetection = planeDetection;
config.alignment = startAlignment;
config.getPointCloudData = getPointCloud;
config.enableLightEstimation = enableLightEstimation;
config.enableAutoFocus = enableAutoFocus;
config.maximumNumberOfTrackedImages = maximumNumberOfTrackedImages;
config.environmentTexturing = environmentTexturing;
if (detectionImages != null)
config.referenceImagesGroupName = detectionImages.resourceGroupName;
if (detectionObjects != null)
{
config.referenceObjectsGroupName = ""; //lets not read from XCode asset catalog right now
config.dynamicReferenceObjectsPtr = m_session.CreateNativeReferenceObjectsSet(detectionObjects.LoadReferenceObjectsInSet());
}
return config;
}
}
// Use this for initialization
void Start () {
m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
Application.targetFrameRate = 60;
var config = sessionConfiguration;
if (config.IsSupported) {
m_session.RunWithConfig (config);
UnityARSessionNativeInterface.ARFrameUpdatedEvent += FirstFrameUpdate;
}
if (m_camera == null) {
m_camera = Camera.main;
}
}
void OnDestroy()
{
m_session.Pause();
}
void FirstFrameUpdate(UnityARCamera cam)
{
sessionStarted = true;
UnityARSessionNativeInterface.ARFrameUpdatedEvent -= FirstFrameUpdate;
}
public void SetCamera(Camera newCamera)
{
if (m_camera != null) {
UnityARVideo oldARVideo = m_camera.gameObject.GetComponent<UnityARVideo> ();
if (oldARVideo != null) {
savedClearMaterial = oldARVideo.m_ClearMaterial;
Destroy (oldARVideo);
}
}
SetupNewCamera (newCamera);
}
private void SetupNewCamera(Camera newCamera)
{
m_camera = newCamera;
if (m_camera != null) {
UnityARVideo unityARVideo = m_camera.gameObject.GetComponent<UnityARVideo> ();
if (unityARVideo != null) {
savedClearMaterial = unityARVideo.m_ClearMaterial;
Destroy (unityARVideo);
}
unityARVideo = m_camera.gameObject.AddComponent<UnityARVideo> ();
unityARVideo.m_ClearMaterial = savedClearMaterial;
}
}
// Update is called once per frame
void Update () {
if (m_camera != null && sessionStarted)
{
// JUST WORKS!
Matrix4x4 matrix = m_session.GetCameraPose();
m_camera.transform.localPosition = UnityARMatrixOps.GetPosition(matrix);
m_camera.transform.localRotation = UnityARMatrixOps.GetRotation (matrix);
m_camera.projectionMatrix = m_session.GetCameraProjection ();
}
}
}