SteamVR_Utils.cs
17.5 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: Utilities for working with SteamVR
//
//=============================================================================
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using Valve.VR;
using System.IO;
public static class SteamVR_Utils
{
public class Event
{
public delegate void Handler(params object[] args);
public static void Listen(string message, Handler action)
{
var actions = listeners[message] as Handler;
if (actions != null)
{
listeners[message] = actions + action;
}
else
{
listeners[message] = action;
}
}
public static void Remove(string message, Handler action)
{
var actions = listeners[message] as Handler;
if (actions != null)
{
listeners[message] = actions - action;
}
}
public static void Send(string message, params object[] args)
{
var actions = listeners[message] as Handler;
if (actions != null)
{
actions(args);
}
}
private static Hashtable listeners = new Hashtable();
}
public static bool IsValid(Vector3 vector)
{
return (float.IsNaN(vector.x) == false && float.IsNaN(vector.y) == false && float.IsNaN(vector.z) == false);
}
public static bool IsValid(Quaternion rotation)
{
return (float.IsNaN(rotation.x) == false && float.IsNaN(rotation.y) == false && float.IsNaN(rotation.z) == false && float.IsNaN(rotation.w) == false) &&
(rotation.x != 0 || rotation.y != 0 || rotation.z != 0 || rotation.w != 0);
}
// this version does not clamp [0..1]
public static Quaternion Slerp(Quaternion A, Quaternion B, float t)
{
var cosom = Mathf.Clamp(A.x * B.x + A.y * B.y + A.z * B.z + A.w * B.w, -1.0f, 1.0f);
if (cosom < 0.0f)
{
B = new Quaternion(-B.x, -B.y, -B.z, -B.w);
cosom = -cosom;
}
float sclp, sclq;
if ((1.0f - cosom) > 0.0001f)
{
var omega = Mathf.Acos(cosom);
var sinom = Mathf.Sin(omega);
sclp = Mathf.Sin((1.0f - t) * omega) / sinom;
sclq = Mathf.Sin(t * omega) / sinom;
}
else
{
// "from" and "to" very close, so do linear interp
sclp = 1.0f - t;
sclq = t;
}
return new Quaternion(
sclp * A.x + sclq * B.x,
sclp * A.y + sclq * B.y,
sclp * A.z + sclq * B.z,
sclp * A.w + sclq * B.w);
}
public static Vector3 Lerp(Vector3 A, Vector3 B, float t)
{
return new Vector3(
Lerp(A.x, B.x, t),
Lerp(A.y, B.y, t),
Lerp(A.z, B.z, t));
}
public static float Lerp(float A, float B, float t)
{
return A + (B - A) * t;
}
public static double Lerp(double A, double B, double t)
{
return A + (B - A) * t;
}
public static float InverseLerp(Vector3 A, Vector3 B, Vector3 result)
{
return Vector3.Dot(result - A, B - A);
}
public static float InverseLerp(float A, float B, float result)
{
return (result - A) / (B - A);
}
public static double InverseLerp(double A, double B, double result)
{
return (result - A) / (B - A);
}
public static float Saturate(float A)
{
return (A < 0) ? 0 : (A > 1) ? 1 : A;
}
public static Vector2 Saturate(Vector2 A)
{
return new Vector2(Saturate(A.x), Saturate(A.y));
}
public static float Abs(float A)
{
return (A < 0) ? -A : A;
}
public static Vector2 Abs(Vector2 A)
{
return new Vector2(Abs(A.x), Abs(A.y));
}
private static float _copysign(float sizeval, float signval)
{
return Mathf.Sign(signval) == 1 ? Mathf.Abs(sizeval) : -Mathf.Abs(sizeval);
}
public static Quaternion GetRotation(this Matrix4x4 matrix)
{
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt(Mathf.Max(0, 1 + matrix.m00 + matrix.m11 + matrix.m22)) / 2;
q.x = Mathf.Sqrt(Mathf.Max(0, 1 + matrix.m00 - matrix.m11 - matrix.m22)) / 2;
q.y = Mathf.Sqrt(Mathf.Max(0, 1 - matrix.m00 + matrix.m11 - matrix.m22)) / 2;
q.z = Mathf.Sqrt(Mathf.Max(0, 1 - matrix.m00 - matrix.m11 + matrix.m22)) / 2;
q.x = _copysign(q.x, matrix.m21 - matrix.m12);
q.y = _copysign(q.y, matrix.m02 - matrix.m20);
q.z = _copysign(q.z, matrix.m10 - matrix.m01);
return q;
}
public static Vector3 GetPosition(this Matrix4x4 matrix)
{
var x = matrix.m03;
var y = matrix.m13;
var z = matrix.m23;
return new Vector3(x, y, z);
}
public static Vector3 GetScale(this Matrix4x4 m)
{
var x = Mathf.Sqrt(m.m00 * m.m00 + m.m01 * m.m01 + m.m02 * m.m02);
var y = Mathf.Sqrt(m.m10 * m.m10 + m.m11 * m.m11 + m.m12 * m.m12);
var z = Mathf.Sqrt(m.m20 * m.m20 + m.m21 * m.m21 + m.m22 * m.m22);
return new Vector3(x, y, z);
}
public static float GetLossyScale(Transform t)
{
return t.lossyScale.x;
}
private const string secretKey = "foobar";
public static string GetBadMD5Hash(string usedString)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(usedString + secretKey);
return GetBadMD5Hash(bytes);
}
public static string GetBadMD5Hash(byte[] bytes)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(bytes);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
public static string GetBadMD5HashFromFile(string filePath)
{
if (File.Exists(filePath) == false)
return null;
string data = File.ReadAllText(filePath);
return GetBadMD5Hash(data + secretKey);
}
public static string SanitizePath(string path, bool allowLeadingSlash = true)
{
if (path.Contains("\\\\"))
path = path.Replace("\\\\", "\\");
if (path.Contains("//"))
path = path.Replace("//", "/");
if (allowLeadingSlash == false)
{
if (path[0] == '/' || path[0] == '\\')
path = path.Substring(1);
}
return path;
}
public static System.Type FindType(string typeName)
{
var type = System.Type.GetType(typeName);
if (type != null) return type;
foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
{
type = a.GetType(typeName);
if (type != null)
return type;
}
return null;
}
[System.Serializable]
public struct RigidTransform
{
public Vector3 pos;
public Quaternion rot;
public static RigidTransform identity
{
get { return new RigidTransform(Vector3.zero, Quaternion.identity); }
}
public static RigidTransform FromLocal(Transform t)
{
return new RigidTransform(t.localPosition, t.localRotation);
}
public RigidTransform(Vector3 pos, Quaternion rot)
{
this.pos = pos;
this.rot = rot;
}
public RigidTransform(Transform t)
{
this.pos = t.position;
this.rot = t.rotation;
}
public RigidTransform(Transform from, Transform to)
{
var inv = Quaternion.Inverse(from.rotation);
rot = inv * to.rotation;
pos = inv * (to.position - from.position);
}
public RigidTransform(HmdMatrix34_t pose)
{
var m = Matrix4x4.identity;
m[0, 0] = pose.m0;
m[0, 1] = pose.m1;
m[0, 2] = -pose.m2;
m[0, 3] = pose.m3;
m[1, 0] = pose.m4;
m[1, 1] = pose.m5;
m[1, 2] = -pose.m6;
m[1, 3] = pose.m7;
m[2, 0] = -pose.m8;
m[2, 1] = -pose.m9;
m[2, 2] = pose.m10;
m[2, 3] = -pose.m11;
this.pos = m.GetPosition();
this.rot = m.GetRotation();
}
public RigidTransform(HmdMatrix44_t pose)
{
var m = Matrix4x4.identity;
m[0, 0] = pose.m0;
m[0, 1] = pose.m1;
m[0, 2] = -pose.m2;
m[0, 3] = pose.m3;
m[1, 0] = pose.m4;
m[1, 1] = pose.m5;
m[1, 2] = -pose.m6;
m[1, 3] = pose.m7;
m[2, 0] = -pose.m8;
m[2, 1] = -pose.m9;
m[2, 2] = pose.m10;
m[2, 3] = -pose.m11;
m[3, 0] = pose.m12;
m[3, 1] = pose.m13;
m[3, 2] = -pose.m14;
m[3, 3] = pose.m15;
this.pos = m.GetPosition();
this.rot = m.GetRotation();
}
public HmdMatrix44_t ToHmdMatrix44()
{
var m = Matrix4x4.TRS(pos, rot, Vector3.one);
var pose = new HmdMatrix44_t();
pose.m0 = m[0, 0];
pose.m1 = m[0, 1];
pose.m2 = -m[0, 2];
pose.m3 = m[0, 3];
pose.m4 = m[1, 0];
pose.m5 = m[1, 1];
pose.m6 = -m[1, 2];
pose.m7 = m[1, 3];
pose.m8 = -m[2, 0];
pose.m9 = -m[2, 1];
pose.m10 = m[2, 2];
pose.m11 = -m[2, 3];
pose.m12 = m[3, 0];
pose.m13 = m[3, 1];
pose.m14 = -m[3, 2];
pose.m15 = m[3, 3];
return pose;
}
public HmdMatrix34_t ToHmdMatrix34()
{
var m = Matrix4x4.TRS(pos, rot, Vector3.one);
var pose = new HmdMatrix34_t();
pose.m0 = m[0, 0];
pose.m1 = m[0, 1];
pose.m2 = -m[0, 2];
pose.m3 = m[0, 3];
pose.m4 = m[1, 0];
pose.m5 = m[1, 1];
pose.m6 = -m[1, 2];
pose.m7 = m[1, 3];
pose.m8 = -m[2, 0];
pose.m9 = -m[2, 1];
pose.m10 = m[2, 2];
pose.m11 = -m[2, 3];
return pose;
}
public override bool Equals(object o)
{
if (o is RigidTransform)
{
RigidTransform t = (RigidTransform)o;
return pos == t.pos && rot == t.rot;
}
return false;
}
public override int GetHashCode()
{
return pos.GetHashCode() ^ rot.GetHashCode();
}
public static bool operator ==(RigidTransform a, RigidTransform b)
{
return a.pos == b.pos && a.rot == b.rot;
}
public static bool operator !=(RigidTransform a, RigidTransform b)
{
return a.pos != b.pos || a.rot != b.rot;
}
public static RigidTransform operator *(RigidTransform a, RigidTransform b)
{
return new RigidTransform
{
rot = a.rot * b.rot,
pos = a.pos + a.rot * b.pos
};
}
public void Inverse()
{
rot = Quaternion.Inverse(rot);
pos = -(rot * pos);
}
public RigidTransform GetInverse()
{
var t = new RigidTransform(pos, rot);
t.Inverse();
return t;
}
public void Multiply(RigidTransform a, RigidTransform b)
{
rot = a.rot * b.rot;
pos = a.pos + a.rot * b.pos;
}
public Vector3 InverseTransformPoint(Vector3 point)
{
return Quaternion.Inverse(rot) * (point - pos);
}
public Vector3 TransformPoint(Vector3 point)
{
return pos + (rot * point);
}
public static Vector3 operator *(RigidTransform t, Vector3 v)
{
return t.TransformPoint(v);
}
public static RigidTransform Interpolate(RigidTransform a, RigidTransform b, float t)
{
return new RigidTransform(Vector3.Lerp(a.pos, b.pos, t), Quaternion.Slerp(a.rot, b.rot, t));
}
public void Interpolate(RigidTransform to, float t)
{
pos = SteamVR_Utils.Lerp(pos, to.pos, t);
rot = SteamVR_Utils.Slerp(rot, to.rot, t);
}
}
public delegate object SystemFn(CVRSystem system, params object[] args);
public static object CallSystemFn(SystemFn fn, params object[] args)
{
var initOpenVR = (!SteamVR.active && !SteamVR.usingNativeSupport);
if (initOpenVR)
{
var error = EVRInitError.None;
OpenVR.Init(ref error, EVRApplicationType.VRApplication_Utility);
}
var system = OpenVR.System;
var result = (system != null) ? fn(system, args) : null;
if (initOpenVR)
OpenVR.Shutdown();
return result;
}
public static void TakeStereoScreenshot(uint screenshotHandle, GameObject target, int cellSize, float ipd, ref string previewFilename, ref string VRFilename)
{
const int width = 4096;
const int height = width / 2;
const int halfHeight = height / 2;
var texture = new Texture2D(width, height * 2, TextureFormat.ARGB32, false);
var timer = new System.Diagnostics.Stopwatch();
Camera tempCamera = null;
timer.Start();
var camera = target.GetComponent<Camera>();
if (camera == null)
{
if (tempCamera == null)
tempCamera = new GameObject().AddComponent<Camera>();
camera = tempCamera;
}
// Render preview texture
const int previewWidth = 2048;
const int previewHeight = 2048;
var previewTexture = new Texture2D(previewWidth, previewHeight, TextureFormat.ARGB32, false);
var targetPreviewTexture = new RenderTexture(previewWidth, previewHeight, 24);
var oldTargetTexture = camera.targetTexture;
var oldOrthographic = camera.orthographic;
var oldFieldOfView = camera.fieldOfView;
var oldAspect = camera.aspect;
var oldstereoTargetEye = camera.stereoTargetEye;
camera.stereoTargetEye = StereoTargetEyeMask.None;
camera.fieldOfView = 60.0f;
camera.orthographic = false;
camera.targetTexture = targetPreviewTexture;
camera.aspect = 1.0f;
camera.Render();
// copy preview texture
RenderTexture.active = targetPreviewTexture;
previewTexture.ReadPixels(new Rect(0, 0, targetPreviewTexture.width, targetPreviewTexture.height), 0, 0);
RenderTexture.active = null;
camera.targetTexture = null;
Object.DestroyImmediate(targetPreviewTexture);
var fx = camera.gameObject.AddComponent<SteamVR_SphericalProjection>();
var oldPosition = target.transform.localPosition;
var oldRotation = target.transform.localRotation;
var basePosition = target.transform.position;
var baseRotation = Quaternion.Euler(0, target.transform.rotation.eulerAngles.y, 0);
var transform = camera.transform;
int vTotal = halfHeight / cellSize;
float dv = 90.0f / vTotal; // vertical degrees per segment
float dvHalf = dv / 2.0f;
var targetTexture = new RenderTexture(cellSize, cellSize, 24);
targetTexture.wrapMode = TextureWrapMode.Clamp;
targetTexture.antiAliasing = 8;
camera.fieldOfView = dv;
camera.orthographic = false;
camera.targetTexture = targetTexture;
camera.aspect = oldAspect;
camera.stereoTargetEye = StereoTargetEyeMask.None;
// Render sections of a sphere using a rectilinear projection
// and resample using a sphereical projection into a single panorama
// texture per eye. We break into sections in order to keep the eye
// separation similar around the sphere. Rendering alternates between
// top and bottom sections, sweeping horizontally around the sphere,
// alternating left and right eyes.
for (int v = 0; v < vTotal; v++)
{
var pitch = 90.0f - (v * dv) - dvHalf;
var uTotal = width / targetTexture.width;
var du = 360.0f / uTotal; // horizontal degrees per segment
var duHalf = du / 2.0f;
var vTarget = v * halfHeight / vTotal;
for (int i = 0; i < 2; i++) // top, bottom
{
if (i == 1)
{
pitch = -pitch;
vTarget = height - vTarget - cellSize;
}
for (int u = 0; u < uTotal; u++)
{
var yaw = -180.0f + (u * du) + duHalf;
var uTarget = u * width / uTotal;
var vTargetOffset = 0;
var xOffset = -ipd / 2 * Mathf.Cos(pitch * Mathf.Deg2Rad);
for (int j = 0; j < 2; j++) // left, right
{
if (j == 1)
{
vTargetOffset = height;
xOffset = -xOffset;
}
var offset = baseRotation * Quaternion.Euler(0, yaw, 0) * new Vector3(xOffset, 0, 0);
transform.position = basePosition + offset;
var direction = Quaternion.Euler(pitch, yaw, 0.0f);
transform.rotation = baseRotation * direction;
// vector pointing to center of this section
var N = direction * Vector3.forward;
// horizontal span of this section in degrees
var phi0 = yaw - (du / 2);
var phi1 = phi0 + du;
// vertical span of this section in degrees
var theta0 = pitch + (dv / 2);
var theta1 = theta0 - dv;
var midPhi = (phi0 + phi1) / 2;
var baseTheta = Mathf.Abs(theta0) < Mathf.Abs(theta1) ? theta0 : theta1;
// vectors pointing to corners of image closes to the equator
var V00 = Quaternion.Euler(baseTheta, phi0, 0.0f) * Vector3.forward;
var V01 = Quaternion.Euler(baseTheta, phi1, 0.0f) * Vector3.forward;
// vectors pointing to top and bottom midsection of image
var V0M = Quaternion.Euler(theta0, midPhi, 0.0f) * Vector3.forward;
var V1M = Quaternion.Euler(theta1, midPhi, 0.0f) * Vector3.forward;
// intersection points for each of the above
var P00 = V00 / Vector3.Dot(V00, N);
var P01 = V01 / Vector3.Dot(V01, N);
var P0M = V0M / Vector3.Dot(V0M, N);
var P1M = V1M / Vector3.Dot(V1M, N);
// calculate basis vectors for plane
var P00_P01 = P01 - P00;
var P0M_P1M = P1M - P0M;
var uMag = P00_P01.magnitude;
var vMag = P0M_P1M.magnitude;
var uScale = 1.0f / uMag;
var vScale = 1.0f / vMag;
var uAxis = P00_P01 * uScale;
var vAxis = P0M_P1M * vScale;
// update material constant buffer
fx.Set(N, phi0, phi1, theta0, theta1,
uAxis, P00, uScale,
vAxis, P0M, vScale);
camera.aspect = uMag / vMag;
camera.Render();
RenderTexture.active = targetTexture;
texture.ReadPixels(new Rect(0, 0, targetTexture.width, targetTexture.height), uTarget, vTarget + vTargetOffset);
RenderTexture.active = null;
}
// Update progress
var progress = (float)(v * (uTotal * 2.0f) + u + i * uTotal) / (float)(vTotal * (uTotal * 2.0f));
OpenVR.Screenshots.UpdateScreenshotProgress(screenshotHandle, progress);
}
}
}
// 100% flush
OpenVR.Screenshots.UpdateScreenshotProgress(screenshotHandle, 1.0f);
// Save textures to disk.
// Add extensions
previewFilename += ".png";
VRFilename += ".png";
// Preview
previewTexture.Apply();
System.IO.File.WriteAllBytes(previewFilename, previewTexture.EncodeToPNG());
// VR
texture.Apply();
System.IO.File.WriteAllBytes(VRFilename, texture.EncodeToPNG());
// Cleanup.
if (camera != tempCamera)
{
camera.targetTexture = oldTargetTexture;
camera.orthographic = oldOrthographic;
camera.fieldOfView = oldFieldOfView;
camera.aspect = oldAspect;
camera.stereoTargetEye = oldstereoTargetEye;
target.transform.localPosition = oldPosition;
target.transform.localRotation = oldRotation;
}
else
{
tempCamera.targetTexture = null;
}
Object.DestroyImmediate(targetTexture);
Object.DestroyImmediate(fx);
timer.Stop();
Debug.Log(string.Format("Screenshot took {0} seconds.", timer.Elapsed));
if (tempCamera != null)
{
Object.DestroyImmediate(tempCamera.gameObject);
}
Object.DestroyImmediate(previewTexture);
Object.DestroyImmediate(texture);
}
}