-
Notifications
You must be signed in to change notification settings - Fork 58
/
ResearchModeVideoStream.cs
339 lines (299 loc) · 11.9 KB
/
ResearchModeVideoStream.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
#if ENABLE_WINMD_SUPPORT
using HL2UnityPlugin;
#endif
public class ResearchModeVideoStream : MonoBehaviour
{
#if ENABLE_WINMD_SUPPORT
HL2ResearchMode researchMode;
#endif
TCPClient tcpClient;
public GameObject depthPreviewPlane = null;
private Material depthMediaMaterial = null;
private Texture2D depthMediaTexture = null;
private byte[] depthFrameData = null;
public GameObject shortAbImagePreviewPlane = null;
private Material shortAbImageMediaMaterial = null;
private Texture2D shortAbImageMediaTexture = null;
private byte[] shortAbImageFrameData = null;
public GameObject longDepthPreviewPlane = null;
private Material longDepthMediaMaterial = null;
private Texture2D longDepthMediaTexture = null;
private byte[] longDepthFrameData = null;
public GameObject LFPreviewPlane = null;
private Material LFMediaMaterial = null;
private Texture2D LFMediaTexture = null;
private byte[] LFFrameData = null;
public GameObject RFPreviewPlane = null;
private Material RFMediaMaterial = null;
private Texture2D RFMediaTexture = null;
private byte[] RFFrameData = null;
public UnityEngine.UI.Text text;
public GameObject pointCloudRendererGo;
public Color pointColor = Color.white;
private PointCloudRenderer pointCloudRenderer;
#if ENABLE_WINMD_SUPPORT
Windows.Perception.Spatial.SpatialCoordinateSystem unityWorldOrigin;
#endif
private void Awake()
{
#if ENABLE_WINMD_SUPPORT
#if UNITY_2020_1_OR_NEWER
IntPtr WorldOriginPtr = UnityEngine.XR.WindowsMR.WindowsMREnvironment.OriginSpatialCoordinateSystem;
unityWorldOrigin = Marshal.GetObjectForIUnknown(WorldOriginPtr) as Windows.Perception.Spatial.SpatialCoordinateSystem;
//unityWorldOrigin = Windows.Perception.Spatial.SpatialLocator.GetDefault().CreateStationaryFrameOfReferenceAtCurrentLocation().CoordinateSystem;
#else
IntPtr WorldOriginPtr = UnityEngine.XR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr();
unityWorldOrigin = Marshal.GetObjectForIUnknown(WorldOriginPtr) as Windows.Perception.Spatial.SpatialCoordinateSystem;
#endif
#endif
}
void Start()
{
if (depthPreviewPlane != null)
{
depthMediaMaterial = depthPreviewPlane.GetComponent<MeshRenderer>().material;
depthMediaTexture = new Texture2D(512, 512, TextureFormat.Alpha8, false);
depthMediaMaterial.mainTexture = depthMediaTexture;
}
if (shortAbImagePreviewPlane != null)
{
shortAbImageMediaMaterial = shortAbImagePreviewPlane.GetComponent<MeshRenderer>().material;
shortAbImageMediaTexture = new Texture2D(512, 512, TextureFormat.Alpha8, false);
shortAbImageMediaMaterial.mainTexture = shortAbImageMediaTexture;
}
if (longDepthPreviewPlane != null)
{
longDepthMediaMaterial = longDepthPreviewPlane.GetComponent<MeshRenderer>().material;
longDepthMediaTexture = new Texture2D(320, 288, TextureFormat.Alpha8, false);
longDepthMediaMaterial.mainTexture = longDepthMediaTexture;
}
if (LFPreviewPlane != null)
{
LFMediaMaterial = LFPreviewPlane.GetComponent<MeshRenderer>().material;
LFMediaTexture = new Texture2D(640, 480, TextureFormat.Alpha8, false);
LFMediaMaterial.mainTexture = LFMediaTexture;
}
if (RFPreviewPlane != null)
{
RFMediaMaterial = RFPreviewPlane.GetComponent<MeshRenderer>().material;
RFMediaTexture = new Texture2D(640, 480, TextureFormat.Alpha8, false);
RFMediaMaterial.mainTexture = RFMediaTexture;
}
if (pointCloudRendererGo != null)
{
pointCloudRenderer = pointCloudRendererGo.GetComponent<PointCloudRenderer>();
}
tcpClient = GetComponent<TCPClient>();
#if ENABLE_WINMD_SUPPORT
researchMode = new HL2ResearchMode();
researchMode.InitializeDepthSensor();
//researchMode.InitializeLongDepthSensor();
researchMode.InitializeSpatialCamerasFront();
researchMode.SetReferenceCoordinateSystem(unityWorldOrigin);
researchMode.SetPointCloudDepthOffset(0);
// Depth sensor should be initialized in only one mode
researchMode.StartDepthSensorLoop();
//researchMode.StartLongDepthSensorLoop();
researchMode.StartSpatialCamerasFrontLoop();
#endif
}
bool startRealtimePreview = true;
void LateUpdate()
{
#if ENABLE_WINMD_SUPPORT
// update depth map texture
if (startRealtimePreview && depthPreviewPlane != null && researchMode.DepthMapTextureUpdated())
{
byte[] frameTexture = researchMode.GetDepthMapTextureBuffer();
if (frameTexture.Length > 0)
{
if (depthFrameData == null)
{
depthFrameData = frameTexture;
}
else
{
System.Buffer.BlockCopy(frameTexture, 0, depthFrameData, 0, depthFrameData.Length);
}
depthMediaTexture.LoadRawTextureData(depthFrameData);
depthMediaTexture.Apply();
}
}
// update short-throw AbImage texture
if (startRealtimePreview && shortAbImagePreviewPlane != null && researchMode.ShortAbImageTextureUpdated())
{
byte[] frameTexture = researchMode.GetShortAbImageTextureBuffer();
if (frameTexture.Length > 0)
{
if (shortAbImageFrameData == null)
{
shortAbImageFrameData = frameTexture;
}
else
{
System.Buffer.BlockCopy(frameTexture, 0, shortAbImageFrameData, 0, shortAbImageFrameData.Length);
}
shortAbImageMediaTexture.LoadRawTextureData(shortAbImageFrameData);
shortAbImageMediaTexture.Apply();
}
}
// update long depth map texture
//if (startRealtimePreview && longDepthPreviewPlane != null && researchMode.LongDepthMapTextureUpdated())
//{
// byte[] frameTexture = researchMode.GetLongDepthMapTextureBuffer();
// if (frameTexture.Length > 0)
// {
// if (longDepthFrameData == null)
// {
// longDepthFrameData = frameTexture;
// }
// else
// {
// System.Buffer.BlockCopy(frameTexture, 0, longDepthFrameData, 0, longDepthFrameData.Length);
// }
// longDepthMediaTexture.LoadRawTextureData(longDepthFrameData);
// longDepthMediaTexture.Apply();
// }
//}
// update LF camera texture
if (startRealtimePreview && LFPreviewPlane != null && researchMode.LFImageUpdated())
{
long ts;
byte[] frameTexture = researchMode.GetLFCameraBuffer(out ts);
if (frameTexture.Length > 0)
{
if (LFFrameData == null)
{
LFFrameData = frameTexture;
}
else
{
System.Buffer.BlockCopy(frameTexture, 0, LFFrameData, 0, LFFrameData.Length);
}
LFMediaTexture.LoadRawTextureData(LFFrameData);
LFMediaTexture.Apply();
}
}
// update RF camera texture
if (startRealtimePreview && RFPreviewPlane != null && researchMode.RFImageUpdated())
{
long ts;
byte[] frameTexture = researchMode.GetRFCameraBuffer(out ts);
if (frameTexture.Length > 0)
{
if (RFFrameData == null)
{
RFFrameData = frameTexture;
}
else
{
System.Buffer.BlockCopy(frameTexture, 0, RFFrameData, 0, RFFrameData.Length);
}
RFMediaTexture.LoadRawTextureData(RFFrameData);
RFMediaTexture.Apply();
}
}
// Update point cloud
if (renderPointCloud && pointCloudRendererGo != null)
{
float[] pointCloud = researchMode.GetPointCloudBuffer();
if (pointCloud.Length > 0)
{
int pointCloudLength = pointCloud.Length / 3;
Vector3[] pointCloudVector3 = new Vector3[pointCloudLength];
for (int i = 0; i < pointCloudLength; i++)
{
pointCloudVector3[i] = new Vector3(pointCloud[3 * i], pointCloud[3 * i + 1], pointCloud[3 * i + 2]);
}
//Debug.LogError("Point Cloud Size: " + pointCloudVector3.Length.ToString());
pointCloudRenderer.Render(pointCloudVector3, pointColor);
}
}
#endif
}
#region Button Event Functions
public void TogglePreviewEvent()
{
startRealtimePreview = !startRealtimePreview;
}
bool renderPointCloud = true;
public void TogglePointCloudEvent()
{
renderPointCloud = !renderPointCloud;
if (renderPointCloud)
{
pointCloudRendererGo.SetActive(true);
}
else
{
pointCloudRendererGo.SetActive(false);
}
}
public void StopSensorsEvent()
{
#if ENABLE_WINMD_SUPPORT
researchMode.StopAllSensorDevice();
#endif
startRealtimePreview = false;
}
public void SaveAHATSensorDataEvent()
{
#if ENABLE_WINMD_SUPPORT
var depthMap = researchMode.GetDepthMapBuffer();
var AbImage = researchMode.GetShortAbImageBuffer();
#if WINDOWS_UWP
if (tcpClient != null)
{
tcpClient.SendUINT16Async(depthMap, AbImage);
}
#endif
#endif
}
public void SaveSpatialImageEvent()
{
#if ENABLE_WINMD_SUPPORT
#if WINDOWS_UWP
long ts_ft_left, ts_ft_right;
var LRFImage = researchMode.GetLRFCameraBuffer(out ts_ft_left, out ts_ft_right);
Windows.Perception.PerceptionTimestamp ts_left = Windows.Perception.PerceptionTimestampHelper.FromHistoricalTargetTime(DateTime.FromFileTime(ts_ft_left));
Windows.Perception.PerceptionTimestamp ts_right = Windows.Perception.PerceptionTimestampHelper.FromHistoricalTargetTime(DateTime.FromFileTime(ts_ft_right));
long ts_unix_left = ts_left.TargetTime.ToUnixTimeMilliseconds();
long ts_unix_right = ts_right.TargetTime.ToUnixTimeMilliseconds();
long ts_unix_current = GetCurrentTimestampUnix();
text.text = "Left: " + ts_unix_left.ToString() + "\n" +
"Right: " + ts_unix_right.ToString() + "\n" +
"Current: " + ts_unix_current.ToString();
if (tcpClient != null)
{
tcpClient.SendSpatialImageAsync(LRFImage, ts_unix_left, ts_unix_right);
}
#endif
#endif
}
#endregion
private void OnApplicationFocus(bool focus)
{
if (!focus) StopSensorsEvent();
}
#if WINDOWS_UWP
private long GetCurrentTimestampUnix()
{
// Get the current time, in order to create a PerceptionTimestamp.
Windows.Globalization.Calendar c = new Windows.Globalization.Calendar();
Windows.Perception.PerceptionTimestamp ts = Windows.Perception.PerceptionTimestampHelper.FromHistoricalTargetTime(c.GetDateTime());
return ts.TargetTime.ToUnixTimeMilliseconds();
//return ts.SystemRelativeTargetTime.Ticks;
}
private Windows.Perception.PerceptionTimestamp GetCurrentTimestamp()
{
// Get the current time, in order to create a PerceptionTimestamp.
Windows.Globalization.Calendar c = new Windows.Globalization.Calendar();
return Windows.Perception.PerceptionTimestampHelper.FromHistoricalTargetTime(c.GetDateTime());
}
#endif
}