17 using System.Runtime.InteropServices;
25 #if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
31 internal const int T5_RESULT_SUCCESS = 0;
32 internal const int T5_RESULT_UNKNOWN_ERROR = 1;
34 #region Native Functions
38 internal static extern int SetApplicationInfo(
39 T5_StringUTF8 appName,
41 T5_StringUTF8 appVersion);
44 internal static extern int SetPlatformContext(
45 IntPtr platformContext);
48 [
return: MarshalAs(UnmanagedType.I4)]
52 internal static extern int IsTiltFiveUIRequestingAttention(ref T5_Bool attentionRequested);
56 internal static extern int RefreshGlassesAvailable();
65 internal static extern void SetMaxDesiredGlasses(
byte maxCount);
69 internal static extern int GetGlassesHandles(
71 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] UInt64[] glassesHandle);
75 internal static extern int GetGlassesFriendlyName(UInt64 glassesHandle, ref T5_StringUTF8 glassesFriendlyName);
79 internal static extern int GetGlassesPose(
81 ref T5_GlassesPose glassesPose,
82 [MarshalAs(UnmanagedType.I4)] T5_GlassesPoseUsage glassesPoseUsage);
85 internal static extern int ConfigureCameraStream(UInt64 glassesHandle,
T5_CameraStreamConfig cameraConfig);
88 internal static extern int GetFilledCamImageBuffer(UInt64 glassesHandle, ref
T5_CamImage camImageBuffer);
91 internal static extern int SubmitEmptyCamImageBuffer(UInt64 glassesHandle, IntPtr camImageBuffer, UInt32 bufferSize);
94 internal static extern int CancelCamImageBuffer(UInt64 glassesHandle, IntPtr buffer);
96 #if TILT_FIVE_ENABLE_PROJECTOR_EXTRINSICS_ADJUSTMENTS
98 internal static extern int SetProjectorExtrinsicsAdjustment(UInt64 glassesHandle,
99 [MarshalAs(UnmanagedType.LPArray, SizeConst = 14)]
float[] args);
104 internal static extern int GetGameboardDimensions(
110 internal static extern int GetWandAvailability(
111 UInt64 glassesHandle,
112 ref T5_Bool wandAvailable,
117 internal static extern int ScanForWands();
121 internal static extern int GetControllerState(
122 UInt64 glassesHandle,
124 ref T5_ControllerState controllerState);
127 internal static extern int SendImpulse(UInt64 glassesHandle,
128 [MarshalAs(UnmanagedType.I4)]
ControllerIndex controllerIndex,
float amplitude, ushort duration);
132 internal static extern int QueueStereoImages(UInt64 glassesHandle, T5_FrameInfo frameInfo);
135 internal static extern IntPtr GetSendFrameCallback();
138 internal static extern int GetMaxDisplayDimensions(
139 [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)]
int[] displayDimensions);
142 internal static extern int GetGlassesIPD(UInt64 glassesHandle, ref
float glassesIPD);
145 internal static extern void UnloadWorkaround();
147 #if UNITY_WEBGL && !UNITY_EDITOR
148 [DllImport (
"__Internal")]
149 internal static extern void RegisterPlugin();
152 #endregion Native Functions
160 internal struct T5_Bool
162 private readonly
byte booleanByte;
164 public T5_Bool(
bool boolean)
166 booleanByte = Convert.ToByte(
boolean);
169 public static implicit
operator bool(T5_Bool t5_boolean)
170 => Convert.ToBoolean(t5_boolean.booleanByte);
171 public static implicit
operator T5_Bool(
bool boolean) =>
new T5_Bool(
boolean);
179 [StructLayout(LayoutKind.Sequential)]
180 internal struct T5_Position
182 public float X, Y, Z;
184 public T5_Position(Vector3 position)
191 public static implicit
operator Vector3(T5_Position t5_position)
192 =>
new Vector3(t5_position.X, t5_position.Y, t5_position.Z);
193 public static implicit
operator T5_Position(Vector3 position) =>
new T5_Position(position);
201 [StructLayout(LayoutKind.Sequential)]
202 internal struct T5_Rotation
204 public float W, X, Y, Z;
206 public T5_Rotation(Quaternion rotation)
214 public static implicit
operator Quaternion(T5_Rotation t5_rotation)
215 =>
new Quaternion(t5_rotation.X, t5_rotation.Y, t5_rotation.Z, t5_rotation.W);
216 public static implicit
operator T5_Rotation(Quaternion rotation) =>
new T5_Rotation(rotation);
219 internal struct GlassesHandle : IEquatable<GlassesHandle>
221 private UInt64 glassesHandle;
222 public static implicit
operator UInt64(GlassesHandle handle) => handle.glassesHandle;
223 public static implicit
operator GlassesHandle(UInt64 handle) =>
new GlassesHandle() { glassesHandle = handle };
224 public override int GetHashCode() => glassesHandle.GetHashCode();
225 public bool Equals(GlassesHandle other) => glassesHandle == other.glassesHandle;
226 public override string ToString()
228 return glassesHandle.ToString();
235 [StructLayout(LayoutKind.Sequential)]
236 internal struct T5_GlassesPose
238 public UInt64 TimestampNanos;
240 private T5_Position posOfGLS_GBD;
241 private T5_Rotation rotationToGLS_GBD;
245 public Vector3 PosOfGLS_GBD {
get => posOfGLS_GBD;
set => posOfGLS_GBD = value; }
246 public Quaternion RotationToGLS_GBD {
get => rotationToGLS_GBD;
set => rotationToGLS_GBD = value; }
256 [StructLayout(LayoutKind.Sequential)]
269 public T5_CamImage(UInt16 bufferWidth, UInt16 bufferHeight, UInt16 bufferStride, UInt32 bufferSize)
284 [StructLayout(LayoutKind.Sequential)]
295 [StructLayout(LayoutKind.Sequential)]
296 internal struct T5_ControllerState
298 [StructLayout(LayoutKind.Sequential)]
303 public static implicit
operator Vector2(
Joystick joystick) =>
new Vector2(joystick.X, joystick.Y);
306 [StructLayout(LayoutKind.Sequential)]
318 public bool T5 {
get =>
t5;
set =>
t5 = value; }
322 public bool A {
get =>
a;
set =>
a = value; }
323 public bool B {
get =>
b;
set =>
b = value; }
324 public bool X {
get =>
x;
set =>
x = value; }
325 public bool Y {
get =>
y;
set =>
y = value; }
326 [Obsolete(
"Buttons.System is deprecated, please use Wandbutton.T5 instead.")]
328 [Obsolete(
"Buttons.Z is deprecated, please use Wandbutton.Three instead.")]
332 public UInt64 TimestampNanos;
334 private T5_Bool analogValid;
335 private T5_Bool batteryValid;
336 private T5_Bool buttonsValid;
337 private T5_Bool poseValid;
339 public float Trigger;
344 private T5_Rotation rotToWND_GBD;
345 private T5_Position aimPos_GBD;
346 private T5_Position fingertipsPos_GBD;
347 private T5_Position gripPos_GBD;
351 public bool AnalogValid {
get => analogValid;
set => analogValid = value; }
352 public bool BatteryValid {
get => batteryValid;
set => batteryValid = value; }
353 public bool ButtonsValid {
get => buttonsValid;
set => buttonsValid = value; }
354 public bool PoseValid {
get => poseValid;
set => poseValid = value; }
356 public Quaternion RotToWND_GBD {
get => rotToWND_GBD;
set => rotToWND_GBD = value; }
357 public Vector3 AimPos_GBD {
get => aimPos_GBD;
set => aimPos_GBD = value; }
358 public Vector3 FingertipsPos_GBD {
get => fingertipsPos_GBD;
set => fingertipsPos_GBD = value; }
359 public Vector3 GripPos_GBD {
get => gripPos_GBD;
set => gripPos_GBD = value; }
367 [StructLayout(LayoutKind.Sequential)]
368 internal struct T5_VCI
370 public float StartX_VCI;
371 public float StartY_VCI;
372 public float Width_VCI;
373 public float Height_VCI;
375 public T5_VCI(Rect rect)
379 Width_VCI = rect.width;
380 Height_VCI = rect.height;
383 public static implicit
operator Rect(T5_VCI vci)
384 =>
new Rect(vci.StartX_VCI, vci.StartY_VCI, vci.Width_VCI, vci.Height_VCI);
385 public static implicit
operator T5_VCI(Rect rect) =>
new T5_VCI(rect);
391 [StructLayout(LayoutKind.Sequential)]
392 internal struct T5_FrameInfo
394 public IntPtr LeftTexHandle;
395 public IntPtr RightTexHandle;
396 public UInt16 TexWidth_PIX;
397 public UInt16 TexHeight_PIX;
399 private T5_Bool isSrgb;
400 private T5_Bool isUpsideDown;
404 private T5_Rotation rotToLVC_GBD;
405 private T5_Position posOfLVC_GBD;
407 private T5_Rotation rotToRVC_GBD;
408 private T5_Position posOfRVC_GBD;
410 public bool IsSrgb {
get => isSrgb;
set => isSrgb = value; }
411 public bool IsUpsideDown {
get => isUpsideDown;
set => isUpsideDown = value; }
412 public Rect VCI {
get => vci;
set => vci = value; }
413 public Quaternion RotToLVC_GBD {
get => rotToLVC_GBD;
set => rotToLVC_GBD = value; }
414 public Vector3 PosOfLVC_GBD {
get => posOfLVC_GBD;
set => posOfLVC_GBD = value; }
415 public Quaternion RotToRVC_GBD {
get => rotToRVC_GBD;
set => rotToRVC_GBD = value; }
416 public Vector3 PosOfRVC_GBD {
get => posOfRVC_GBD;
set => posOfRVC_GBD = value; }
426 [StructLayout(LayoutKind.Explicit, Pack = 4)]
427 internal struct T5_StringUTF8 : IDisposable
429 [FieldOffset(0)]
private UInt32 maxBufferSize;
430 [FieldOffset(4)]
private UInt32 length;
431 [FieldOffset(8)]
private IntPtr pStringBytesUTF8;
433 public T5_StringUTF8(
string text)
435 pStringBytesUTF8 = IntPtr.Zero;
437 maxBufferSize = 16 * 1024;
442 byte[] textBytesUTF8 = System.Text.Encoding.UTF8.GetBytes(text);
445 length = (UInt32)Math.Min(textBytesUTF8.Length, maxBufferSize);
446 pStringBytesUTF8 = Marshal.AllocHGlobal((
int)maxBufferSize);
449 Marshal.Copy(textBytesUTF8, 0, pStringBytesUTF8, (
int)length);
453 public static implicit
operator string(T5_StringUTF8 t5_StringUTF8)
454 => ToString(t5_StringUTF8);
456 public static implicit
operator T5_StringUTF8(
string text) =>
new T5_StringUTF8(text);
458 private static string ToString(T5_StringUTF8 t5_StringUTF8)
460 if (t5_StringUTF8.pStringBytesUTF8 == IntPtr.Zero)
465 var managedBytes =
new byte[t5_StringUTF8.length];
468 Marshal.Copy(t5_StringUTF8.pStringBytesUTF8, managedBytes, 0, (
int)t5_StringUTF8.length);
469 return System.Text.Encoding.UTF8.GetString(managedBytes);
473 Debug.LogError($
"Failed to copy string from unmanaged memory: {e}");
481 public void Dispose()
485 Marshal.FreeHGlobal(pStringBytesUTF8);
486 pStringBytesUTF8 = IntPtr.Zero;
540 [Obsolete(
"ControllerIndex.Primary is obsolete, please update to use left/right based on user preference instead.",
true)]
543 [Obsolete(
"ControllerIndex.Secondary is obsolete, please update to use left/right based on user preference instead.",
true)]
579 [StructLayout(LayoutKind.Sequential)]
608 float viewableExtentPositiveZ,
float viewableExtentNegativeZ,
609 float viewableExtentPositiveY)
618 [Obsolete(
"This version of the T5_GameboardSize constructor is obsolete. " +
619 "Please use T5_GameboardSize(float viewableExtentPositiveX, float viewableExtentNegativeX, " +
620 "float viewableExtentPositiveZ, float viewableExtentNegativeZ, float viewableExtentPositiveY) instead.")]
676 internal enum T5_GlassesPoseUsage : Int32
688 GlassesPresentation = 1,
698 SpectatorPresentation = 2,
const string PLUGIN_LIBRARY
ServiceCompatibility
Whether the running service is compatible.
ControllerPosition
Points of interest along the wand controller, such as the handle position or wand tip.
@ Fingertips
The typical resting position of the player's fingertips, near the wand joystick and trigger.
@ Aim
The tip of the wand.
@ Grip
The center of the wand handle.
ControllerIndex
Since wands are all physically identical (they have no "handedness"), it doesn't make sense to addres...
@ Right
The wand held in the player's right hand.
@ Left
The wand held in the player's left hand.
GameboardType
The type of Gameboard being tracked by the glasses
@ GameboardType_None
No Gameboard at all.
@ GameboardType_LE
The LE Gameboard.
@ GameboardType_XE
The XE Gameboard, laid out flat.
@ GameboardType_XE_Raised
The XE Gameboard, folded upward using its kickstand.
T5_Hand
Reported wand hand
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)
Represents a wrapper on a buffer comtaining a camera image.
UInt32 ImageBufferSize_PIX
UInt16 ImageBufferStride_PIX
UInt16 ImageBufferWidth_PIX
UInt16 ImageBufferHeight_PIX
T5_CamImage(UInt16 bufferWidth, UInt16 bufferHeight, UInt16 bufferStride, UInt32 bufferSize)
Camera Stream Configuration.
Physical dimensions of a gameboard, in meters.
T5_GameboardSize(float playableSpaceX, float playableSpaceZ, float borderWidth)
T5_GameboardSize(float viewableExtentPositiveX, float viewableExtentNegativeX, float viewableExtentPositiveZ, float viewableExtentNegativeZ, float viewableExtentPositiveY)
float ViewableExtentNegativeX
The distance in meters from the gameboard origin to the edge of the viewable area in the negative X d...
float ViewableExtentPositiveY
The distance in meters above the gameboard origin that the viewable area extends in the positive Y di...
float ViewableExtentPositiveX
The distance in meters from the gameboard origin to the edge of the viewable area in the positive X d...
float ViewableExtentPositiveZ
The distance in meters from the gameboard origin to the edge of the viewable area in the positive Z d...
float ViewableExtentNegativeZ
The distance in meters from the gameboard origin to the edge of the viewable area in the negative Z d...