-
Notifications
You must be signed in to change notification settings - Fork 71
/
LocatableCamera.cs
196 lines (161 loc) · 6.51 KB
/
LocatableCamera.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.WebCam;
namespace Microsoft.MixedReality.OpenXR.BasicSample
{
public class LocatableCamera : MonoBehaviour
{
[SerializeField]
private Shader textureShader = null;
[SerializeField]
private TextMesh text = null;
private PhotoCapture photoCaptureObject = null;
private Resolution cameraResolution = default(Resolution);
private bool isCapturingPhoto, isReadyToCapturePhoto = false;
private uint numPhotos = 0;
private void Start()
{
var resolutions = PhotoCapture.SupportedResolutions;
if (resolutions == null || resolutions.Count() == 0)
{
if (text != null)
{
text.text = "Resolutions not available. Did you provide web cam access?";
}
return;
}
cameraResolution = resolutions.OrderByDescending((res) => res.width * res.height).First();
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
if (text != null)
{
text.text = "Starting camera...";
}
}
private void OnDestroy()
{
isReadyToCapturePhoto = false;
if (photoCaptureObject != null)
{
photoCaptureObject.StopPhotoModeAsync(OnPhotoCaptureStopped);
if (text != null)
{
text.text = "Stopping camera...";
}
}
}
private void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
if (text != null)
{
text.text += "\nPhotoCapture created...";
}
photoCaptureObject = captureObject;
CameraParameters cameraParameters = new CameraParameters(WebCamMode.PhotoMode)
{
hologramOpacity = 0.0f,
cameraResolutionWidth = cameraResolution.width,
cameraResolutionHeight = cameraResolution.height,
pixelFormat = CapturePixelFormat.BGRA32
};
captureObject.StartPhotoModeAsync(cameraParameters, OnPhotoModeStarted);
}
private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
isReadyToCapturePhoto = true;
if (text != null)
{
text.text = "Ready!\nPress the above button to take a picture.";
}
}
else
{
isReadyToCapturePhoto = false;
if (text != null)
{
text.text = "Unable to start photo mode!";
}
}
}
/// <summary>
/// Takes a photo and attempts to load it into the scene using its location data.
/// </summary>
public void TakePhoto()
{
if (!isReadyToCapturePhoto || isCapturingPhoto)
{
return;
}
isCapturingPhoto = true;
if (text != null)
{
text.text = "Taking picture...";
}
photoCaptureObject.TakePhotoAsync(OnPhotoCaptured);
}
private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
if (result.success)
{
if (text != null)
{
text.text += "\nTook picture!";
}
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.name = $"Photo{numPhotos++}";
quad.transform.parent = transform;
float ratio = cameraResolution.height / (float)cameraResolution.width;
quad.transform.localScale = new Vector3(quad.transform.localScale.x, quad.transform.localScale.x * ratio, quad.transform.localScale.z);
Renderer quadRenderer = quad.GetComponent<Renderer>();
quadRenderer.material = new Material(textureShader);
Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
photoCaptureFrame.UploadImageDataToTexture(targetTexture);
quadRenderer.sharedMaterial.SetTexture("_MainTex", targetTexture);
if (photoCaptureFrame.hasLocationData)
{
photoCaptureFrame.TryGetCameraToWorldMatrix(out Matrix4x4 cameraToWorldMatrix);
Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2);
Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));
photoCaptureFrame.TryGetProjectionMatrix(Camera.main.nearClipPlane, Camera.main.farClipPlane, out Matrix4x4 projectionMatrix);
targetTexture.wrapMode = TextureWrapMode.Clamp;
quadRenderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", cameraToWorldMatrix.inverse);
quadRenderer.sharedMaterial.SetMatrix("_CameraProjectionMatrix", projectionMatrix);
quad.transform.position = position;
quad.transform.rotation = rotation;
if (text != null)
{
text.text += $"\nPosition: ({position.x}, {position.y}, {position.z})";
text.text += $"\nRotation: ({rotation.x}, {rotation.y}, {rotation.z}, {rotation.w})";
}
}
else
{
if (text != null)
{
text.text += "\nNo location data :(";
}
}
}
else
{
if (text != null)
{
text.text += "\nPicture taking failed: " + result.hResult;
}
}
isCapturingPhoto = false;
}
private void OnPhotoCaptureStopped(PhotoCapture.PhotoCaptureResult result)
{
if (text != null)
{
text.text = result.success ? "Photo mode stopped." : "Unable to stop photo mode.";
}
photoCaptureObject.Dispose();
photoCaptureObject = null;
}
}
}