Unity SDK Docs 1.5.0-beta.6
Loading...
Searching...
No Matches
GameBoard.cs
1/*
2 * Copyright (C) 2020-2023 Tilt Five, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17using System;
18using System.Collections.Generic;
19using UnityEngine;
21
22namespace TiltFive
23{
27 [ExecuteInEditMode]
29 {
30 #region Public Fields
31
35 [Tooltip("Show/Hide the Board Gizmo in the Editor.")]
36 public bool ShowGizmo;
37
38 [Tooltip("Show/Hide the Unit Grid on the Board Gizmo in the Editor.")]
39 public bool ShowGrid;
40
41 public float GridHeightOffset = 0f;
42 public bool StickyHeightOffset = true;
43
44
48 [Tooltip("Sets the Alpha transparency of the Board Gizmo in the Editor.")]
49 [Range(0f, 1f)]
50 public float GizmoOpacity = 0.75f;
51
55 [HideInInspector]
56 [Obsolete("Please use Gameboard.TryGetGameboardType() instead.", true)]
58
59 #endregion Public Fields
60
61
62 #region Private Fields
63
64#if UNITY_EDITOR
65
69 private TableTopGizmo boardGizmo = new TableTopGizmo();
70
74 private float gridOffsetY => StickyHeightOffset ? Mathf.RoundToInt(GridHeightOffset) : GridHeightOffset;
75
79 private LengthUnit currentContentScaleUnit;
80
84 private float currentContentScaleRatio;
85
89 private Vector3 currentScale;
90
91 private const float MIN_SCALE = 0.00001f;
92
93#endif // UNITY_EDITOR
94
95 private static Dictionary<PlayerIndex, GameboardType> playerGameboards = new Dictionary<PlayerIndex, GameboardType>();
96 private static Dictionary<GlassesHandle, PrimaryGameboardCore> gameboardCores
97 = new Dictionary<GlassesHandle, PrimaryGameboardCore>();
98
99 private static HashSet<PrimaryGameboardCore> lostGameboards = new HashSet<PrimaryGameboardCore>();
100 private static HashSet<GlassesHandle> incomingGlassesHandles = new HashSet<GlassesHandle>();
101 private static HashSet<GlassesHandle> lostGlassesHandles = new HashSet<GlassesHandle>();
102
103 private const UInt16 MAX_SUPPORTED_GAMEBOARD_POSES = 1;
104 #endregion Private Fields
105
106
107 #region Public Enums
108
113 public enum Edge
114 {
137 }
138
142 public enum Corner
143 {
144 FarLeft,
145 FarRight,
146 NearLeft,
147 NearRight
148 }
149
150 #endregion Public Enums
151
152
153 #region Public Structs
154
155 [Obsolete("GameboardDimensions is obsolete, please use GameboardExtents instead.", true)]
156 public struct GameboardDimensions
157 {
158 public Length playableSpaceX;
159 public Length playableSpaceY;
160 public Length borderWidth;
161 public Length totalSpaceX => playableSpaceX + (borderWidth * 2);
162 public Length totalSpaceY => playableSpaceY + (borderWidth * 2);
163
164 public GameboardDimensions(T5_GameboardSize gameboardSize)
165 {
166 var gameboardExtents = new GameboardExtents(gameboardSize);
167 playableSpaceX = gameboardExtents.ViewableSpanX;
168 playableSpaceY = gameboardExtents.ViewableSpanZ;
169 borderWidth = new Length(GameboardExtents.BORDER_WIDTH_IN_METERS, LengthUnit.Meters);
170 }
171 }
172
176 public struct GameboardExtents
177 {
182
187
192
197
202
215
223 public Length OriginOffsetZ => new Length(ViewableSpanZ.ToMeters / 2f - ViewableExtentNegativeZ.ToMeters, LengthUnit.Meters);
224
225 internal const float BORDER_WIDTH_IN_METERS = 0.05f;
226 internal static readonly T5_GameboardSize GAMEBOARD_SIZE_LE;
227 internal static readonly T5_GameboardSize GAMEBOARD_SIZE_XE;
228 internal static readonly T5_GameboardSize GAMEBOARD_SIZE_XE_RAISED;
229
230 static GameboardExtents()
231 {
232 GAMEBOARD_SIZE_LE = new T5_GameboardSize(0.35f, 0.35f, 0.35f, 0.35f, 0f);
233 GAMEBOARD_SIZE_XE = new T5_GameboardSize(0.35f, 0.35f, 0.61667f, 0.35f, 0.0f);
234 GAMEBOARD_SIZE_XE_RAISED = new T5_GameboardSize(0.35f, 0.35f, 0.55944f, 0.35f, 0.20944f);
235 }
236
237 public GameboardExtents(T5_GameboardSize gameboardSize)
238 {
239 ViewableExtentPositiveX = new Length(gameboardSize.ViewableExtentPositiveX, LengthUnit.Meters);
240 ViewableExtentNegativeX = new Length(gameboardSize.ViewableExtentNegativeX, LengthUnit.Meters);
241 ViewableExtentPositiveZ = new Length(gameboardSize.ViewableExtentPositiveZ, LengthUnit.Meters);
242 ViewableExtentNegativeZ = new Length(gameboardSize.ViewableExtentNegativeZ, LengthUnit.Meters);
243 ViewableExtentPositiveY = new Length(gameboardSize.ViewableExtentPositiveY, LengthUnit.Meters);
244 }
245
253 {
254 switch (corner)
255 {
256 case Corner.FarLeft:
257 return Vector3.forward * ViewableExtentPositiveZ.ToMeters
258 + Vector3.left * ViewableExtentNegativeX.ToMeters;
259 case Corner.FarRight:
260 return Vector3.forward * ViewableExtentPositiveZ.ToMeters
261 + Vector3.right * ViewableExtentPositiveX.ToMeters;
262 case Corner.NearLeft:
263 return Vector3.back * ViewableExtentNegativeZ.ToMeters
264 + Vector3.left * ViewableExtentNegativeX.ToMeters;
265 case Corner.NearRight:
266 return Vector3.back * ViewableExtentNegativeZ.ToMeters
267 + Vector3.right * ViewableExtentPositiveX.ToMeters;
268 default:
269 throw new System.ArgumentException("Cannot get corner positions for invalid Corner enum values.");
270 }
271 }
272
280 {
281 switch (edge)
282 {
283 case Edge.Left:
284 return Vector3.left * ViewableExtentNegativeX.ToMeters
285 + Vector3.forward * OriginOffsetZ.ToMeters;
286 case Edge.Right:
287 return Vector3.right * ViewableExtentPositiveX.ToMeters
288 + Vector3.forward * OriginOffsetZ.ToMeters;
289 case Edge.Near:
290 return Vector3.back * ViewableExtentNegativeZ.ToMeters;
291 case Edge.Far:
292 return Vector3.forward * ViewableExtentPositiveZ.ToMeters;
293 default:
294 throw new System.ArgumentException("Cannot get edge center positions for invalid Edge enum values.");
295 }
296 }
297
305 [Obsolete("GameboardExtents.GetPhysicalCenterPositionInGameboardSpace is deprecated. Please use GameboardExtents.GetGeometricCenterPositionInGameboardSpace instead.")]
307
316 {
317 // If we ever have some funky gameboard in the future with a double-eccentric tracking origin,
318 // we'd just need to add another vector along the X axis (e.g. Vector3.right * OriginOffsetX.ToMeters)
319 return Vector3.forward * OriginOffsetZ.ToMeters;
320 }
321 }
322
323 #endregion Private Structs
324
325
326 #region Public Functions
327
335 public static bool TryGetGameboardType(out GameboardType gameboardType)
336 {
337 return TryGetGameboardType(PlayerIndex.One, out gameboardType);
338 }
339
340 public static bool TryGetGameboardType(PlayerIndex playerIndex, out GameboardType gameboardType)
341 {
342 // Return false and a default gameboard if the player is nonexistent or disconnected.
343 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
344 || !playerGameboards.TryGetValue(playerIndex, out var currentGameboardType))
345 {
346 gameboardType = GameboardType.GameboardType_None;
347 return false;
348 }
349
350 gameboardType = currentGameboardType;
351 return true;
352 }
353
360 [Obsolete("TryGetGameboardDimensions is obsolete. Please use TryGetGameboardExtents instead.", true)]
361 public static bool TryGetGameboardDimensions(GameboardType gameboardType, out GameboardDimensions gameboardDimensions)
362 {
363 if(gameboardType == GameboardType.GameboardType_None)
364 {
365 gameboardDimensions = new GameboardDimensions();
366 return false;
367 }
368
369 // Default to the LE gameboard dimensions in meters.
370 T5_GameboardSize gameboardSize = GameboardExtents.GAMEBOARD_SIZE_LE;
371 int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
372
373 try
374 {
375 result = NativePlugin.GetGameboardDimensions(gameboardType, ref gameboardSize);
376 }
377 catch (Exception e)
378 {
379 Log.Error(e.Message);
380 }
381
382 gameboardDimensions = new GameboardDimensions(gameboardSize);
383
384 return result == NativePlugin.T5_RESULT_SUCCESS;
385 }
386
394 public static bool TryGetGameboardExtents(GameboardType gameboardType, out GameboardExtents gameboardExtents)
395 {
396 if(gameboardType == GameboardType.GameboardType_None)
397 {
398 gameboardExtents = new GameboardExtents();
399 return false;
400 }
401
402 T5_GameboardSize gameboardSize = new T5_GameboardSize();
403 int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
404
405 try
406 {
407 result = NativePlugin.GetGameboardDimensions(gameboardType, ref gameboardSize);
408 }
409 catch (Exception e)
410 {
411 Log.Error(e.Message);
412 }
413
414 gameboardExtents = new GameboardExtents(gameboardSize);
415
416 return result == NativePlugin.T5_RESULT_SUCCESS;
417 }
418
431 public bool TransformPose(Vector3 positionInGameboardSpace, Quaternion rotationToGameboardSpace, PlayerIndex playerIndex,
432 out Vector3 positionInWorldSpace, out Quaternion rotationToWorldSpace)
433 {
434 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
435 {
436 positionInWorldSpace = Vector3.zero;
437 rotationToWorldSpace = Quaternion.identity;
438 return false;
439 }
440
441 TransformPose(positionInGameboardSpace, rotationToGameboardSpace, playerSettings.scaleSettings,
442 out positionInWorldSpace, out rotationToWorldSpace);
443 return true;
444 }
445
456 public bool TransformPose(Pose poseInGameboardSpace, PlayerIndex playerIndex, out Pose poseInWorldSpace)
457 {
458 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
459 {
460 poseInWorldSpace = new Pose();
461 return false;
462 }
463
464 poseInWorldSpace = TransformPose(poseInGameboardSpace, playerSettings.scaleSettings);
465 return true;
466 }
467
478 public bool TransformPoint(Vector3 pointInGameboardSpace, PlayerIndex playerIndex, out Vector3 pointInWorldSpace)
479 {
480 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
481 {
482 pointInWorldSpace = Vector3.zero;
483 return false;
484 }
485
486 pointInWorldSpace = TransformPoint(pointInGameboardSpace, playerSettings.scaleSettings);
487 return true;
488 }
489
502 public bool InverseTransformPose(Vector3 positionInWorldSpace, Quaternion rotationToWorldSpace, PlayerIndex playerIndex,
503 out Vector3 positionInGameboardSpace, out Quaternion rotationToGameboardSpace)
504 {
505 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
506 {
507 positionInGameboardSpace = Vector3.zero;
508 rotationToGameboardSpace = Quaternion.identity;
509 return false;
510 }
511
512 InverseTransformPose(positionInWorldSpace, rotationToWorldSpace, playerSettings.scaleSettings,
513 out positionInGameboardSpace, out rotationToGameboardSpace);
514 return true;
515 }
516
527 public bool InverseTransformPose(Pose poseInWorldSpace, PlayerIndex playerIndex, out Pose poseInGameboardSpace)
528 {
529 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
530 {
531 poseInGameboardSpace = new Pose();
532 return false;
533 }
534
535 poseInGameboardSpace = InverseTransformPose(poseInWorldSpace, playerSettings.scaleSettings);
536 return true;
537 }
538
549 public bool InverseTransformPoint(Vector3 pointInWorldSpace, PlayerIndex playerIndex, out Vector3 pointInGameboardSpace)
550 {
551 if (!Player.TryGetSettings(playerIndex, out var playerSettings))
552 {
553 pointInGameboardSpace = Vector3.zero;
554 return false;
555 }
556
557 pointInGameboardSpace = InverseTransformPoint(pointInWorldSpace, playerSettings.scaleSettings);
558 return true;
559 }
560
561#if UNITY_EDITOR
562
563 new public void Awake()
564 {
565 base.Awake();
566 currentScale = transform.localScale;
567 }
568
572 public void DrawGizmo(ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings)
573 {
574 UnifyScale();
575
576 if (ShowGizmo)
577 {
578 boardGizmo.Draw(scaleSettings, gameBoardSettings, GizmoOpacity, ShowGrid, gridOffsetY);
579 }
580
581 var sceneViewRepaintNecessary = ScaleCompensate(scaleSettings);
582 sceneViewRepaintNecessary |= ContentScaleCompensate(scaleSettings);
583
584 if(sceneViewRepaintNecessary)
585 {
586 boardGizmo.ResetGrid(scaleSettings, gameBoardSettings); // This may need to change once separate game board configs are in.
587 UnityEditor.SceneView.lastActiveSceneView.Repaint();
588 }
589 }
590
596 public GameboardType GetDisplayedGameboardType(GameBoardSettings gameBoardSettings)
597 {
598 return GetDisplayedGameboardType(PlayerIndex.One, gameBoardSettings);
599 }
600
601 public static GameboardType GetDisplayedGameboardType(PlayerIndex playerIndex, GameBoardSettings gameBoardSettings)
602 {
603 if(playerIndex == PlayerIndex.None)
604 {
605 return GameboardType.GameboardType_None;
606 }
607
608 var displayedGameboardType = playerGameboards.TryGetValue(playerIndex, out var gameboardType)
609 ? gameboardType
610 : GameboardType.GameboardType_LE;
611
612 displayedGameboardType = gameBoardSettings.gameboardTypeOverride != GameboardType.GameboardType_None
613 ? gameBoardSettings.gameboardTypeOverride
614 : displayedGameboardType;
615
616 return displayedGameboardType;
617 }
618
619#endif // UNITY_EDITOR
620
621 #endregion Public Functions
622
623
624 #region Internal Functions
625
626 internal static void Scan()
627 {
628 incomingGlassesHandles.Clear();
629 lostGlassesHandles.Clear();
630 lostGameboards.Clear();
631 var glassesHandles = Glasses.GetAllConnectedGlassesHandles();
632
633 // Incoming gameboard cores
634 for(int i = 0; i < glassesHandles.Length; i++)
635 {
636 var glassesHandle = glassesHandles[i];
637 incomingGlassesHandles.Add(glassesHandle);
638
639 if (Glasses.IsTracked(glassesHandle))
640 {
641 if(!gameboardCores.ContainsKey(glassesHandle)
642 && Player.TryGetPlayerIndex(glassesHandle, out var playerIndex)
643 && Player.TryGetSettings(playerIndex, out var playerSettings)
644 && playerSettings.gameboardSettings.physicalGameboardOrigin != null)
645 {
646 gameboardCores.Add(glassesHandle, new PrimaryGameboardCore(
647 glassesHandle, playerSettings.gameboardSettings.physicalGameboardOrigin.transform));
648 }
649 }
650 }
651
652 // Outgoing gameboard cores
653 foreach (var glassesHandle in gameboardCores.Keys)
654 {
655 if(!incomingGlassesHandles.Contains(glassesHandle))
656 {
657 lostGlassesHandles.Add(glassesHandle);
658 }
659 // Ensure the gameboard is still tracked
660 else if(!Glasses.IsTracked(glassesHandle)
661 || !Player.TryGetPlayerIndex(glassesHandle, out var playerIndex)
662 || !Player.TryGetSettings(playerIndex, out var playerSettings)
663 || playerSettings.gameboardSettings.physicalGameboardOrigin == null)
664 {
665 lostGameboards.Add(gameboardCores[glassesHandle]);
666 }
667 }
668
669 // Bookkeeping for outgoing gameboard cores
670 foreach(var lostGlassesHandle in lostGlassesHandles)
671 {
672 lostGameboards.Add(gameboardCores[lostGlassesHandle]);
673 gameboardCores.Remove(lostGlassesHandle);
674 }
675
676 foreach(var lostGameboard in lostGameboards)
677 {
678 lostGameboard.Dispose();
679 }
680 }
681
682 internal static void Update(GlassesHandle glassesHandle, GameBoardSettings gameboardSettings, ScaleSettings scaleSettings)
683 {
684 if(gameboardCores.TryGetValue(glassesHandle, out var gameboardCore))
685 {
686 gameboardCore.Update(gameboardSettings, scaleSettings);
687 }
688 }
689
690 internal static void SetGameboardType(PlayerIndex playerIndex, GameboardType gameboardType)
691 {
692 if(playerIndex == PlayerIndex.None)
693 {
694 return;
695 }
696
697 playerGameboards[playerIndex] = gameboardType;
698 }
699
700 internal static void SetGameboardType(GlassesHandle glassesHandle, GameboardType gameboardType)
701 {
702 if (Player.TryGetPlayerIndex(glassesHandle, out var playerIndex))
703 {
704 SetGameboardType(playerIndex, gameboardType);
705 }
706 }
707
715 internal Pose TransformPose(Pose poseInGameboardSpace, ScaleSettings scaleSettings)
716 {
717 TransformPose(poseInGameboardSpace.position, poseInGameboardSpace.rotation, scaleSettings,
718 out var posUWRLD, out var rotToUWRLD_OBJ);
719 return new Pose(posUWRLD, rotToUWRLD_OBJ);
720 }
721
731 internal void TransformPose(Vector3 positionInGameboardSpace, Quaternion rotationToGameboardSpace, ScaleSettings scaleSettings,
732 out Vector3 positionInWorldSpace, out Quaternion rotationToWorldSpace)
733 {
734 positionInWorldSpace = TransformPoint(positionInGameboardSpace, scaleSettings);
735
736 // The rotation from gameboard space to world space
737 var rotToUWRLD_USTAGE = transform.rotation;
738
739 // The rotation from the object's local space to gameboard space
740 var rotToUSTAGE_OBJ = rotationToGameboardSpace;
741
742 // The rotation from the object's local space to world space
743 var rotToUWRLD_OBJ = rotToUWRLD_USTAGE * rotToUSTAGE_OBJ;
744
745 rotationToWorldSpace = rotToUWRLD_OBJ;
746 }
747
755 internal Vector3 TransformPoint(Vector3 pointInGameboardSpace, ScaleSettings scaleSettings)
756 {
757 var pos_USTAGE = pointInGameboardSpace;
758
759 var scaleToUWRLD_USTAGE = scaleSettings.GetScaleToWorldSpaceFromGameboardSpace(localScale);
760 var rotToUWRLD_USTAGE = transform.rotation;
761 var pos_UWRLD = rotToUWRLD_USTAGE * (scaleToUWRLD_USTAGE * pos_USTAGE) + transform.position;
762
763 return pos_UWRLD;
764 }
765
773 internal Pose InverseTransformPose(Pose poseInWorldSpace, ScaleSettings scaleSettings)
774 {
775 InverseTransformPose(poseInWorldSpace.position, poseInWorldSpace.rotation, scaleSettings,
776 out var posUSTAGE, out var rotToUSTAGE_OBJ);
777 return new Pose(posUSTAGE, rotToUSTAGE_OBJ);
778 }
779
789 internal void InverseTransformPose(Vector3 positionInWorldSpace, Quaternion rotationToWorldSpace, ScaleSettings scaleSettings,
790 out Vector3 positionInGameboardSpace, out Quaternion rotationToGameboardSpace)
791 {
792 positionInGameboardSpace = InverseTransformPoint(positionInWorldSpace, scaleSettings);
793
794 // The rotation from gameboard space to world space
795 var rotToUWRLD_USTAGE = transform.rotation;
796
797 // The rotation from world space to gameboard space
798 var rotToUSTAGE_UWRLD = Quaternion.Inverse(rotToUWRLD_USTAGE);
799
800 // The rotation from the object's local space to world space
801 var rotToUWRLD_OBJ = rotationToWorldSpace;
802
803 // The rotation from the object's local space to gameboard space
804 var rotToUSTAGE_OBJ = rotToUSTAGE_UWRLD * rotToUWRLD_OBJ;
805
806 rotationToGameboardSpace = rotToUSTAGE_OBJ;
807 }
808
816 internal Vector3 InverseTransformPoint(Vector3 pointInWorldSpace, ScaleSettings scaleSettings)
817 {
818 var pos_UWRLD = pointInWorldSpace;
819
820 var scaleToUSTAGE_UWRLD = 1f / scaleSettings.GetScaleToWorldSpaceFromGameboardSpace(localScale);
821 var rotToUSTAGE_UWRLD = Quaternion.Inverse(transform.rotation);
822 var pos_USTAGE = pos_UWRLD - transform.position;
823 pos_USTAGE = rotToUSTAGE_UWRLD * pos_USTAGE;
824 pos_USTAGE *= scaleToUSTAGE_UWRLD;
825
826 return pos_USTAGE;
827 }
828
829 internal static void OnDisable()
830 {
831 foreach (var gameboardCore in gameboardCores.Values)
832 {
833 gameboardCore.Dispose();
834 }
835 gameboardCores.Clear();
836 }
837
838 #endregion
839
840
841 #region Private Functions
842
843#if UNITY_EDITOR
844
851 private bool ScaleCompensate(ScaleSettings scaleSettings)
852 {
853 if(currentScale == transform.localScale) { return false; }
854
855 // Prevent negative scale values for the game board.
856 if( transform.localScale.x < MIN_SCALE)
857 {
858 transform.localScale = Vector3.one * MIN_SCALE;
859 }
860
861 //var sceneView = UnityEditor.SceneView.lastActiveSceneView;
862
863 //sceneView.Frame(new Bounds(transform.position, (1/5f) * Vector3.one * scaleSettings.worldSpaceUnitsPerPhysicalMeter / localScale ), true);
864
865 currentScale = transform.localScale;
866 return true;
867 }
868
872 private bool ContentScaleCompensate(ScaleSettings scaleSettings)
873 {
874 if(currentContentScaleRatio == scaleSettings.contentScaleRatio
875 && currentContentScaleUnit == scaleSettings.contentScaleUnit) { return false; }
876
877 //var sceneView = UnityEditor.SceneView.lastActiveSceneView;
878
879 currentContentScaleUnit = scaleSettings.contentScaleUnit;
880 currentContentScaleRatio = scaleSettings.contentScaleRatio;
881
882 //sceneView.Frame(new Bounds(transform.position, (1/5f) * Vector3.one * scaleSettings.worldSpaceUnitsPerPhysicalMeter / localScale ), true);
883
884 return true;
885 }
886
887
888#endif // UNITY_EDITOR
889
890 #endregion Private Functions
891
892
893 #region Private Classes
894
895 private abstract class BaseGameboardCore : TrackableCore<GameBoardSettings, T5_GameboardPose>, IDisposable
896 {
897 #region Protected Fields
898
899 protected GlassesHandle glassesHandle;
900 protected Transform gameboardTransform;
901
902 protected Pose DEFAULT_GAMEBOARD_POSE_USTAGE = new Pose(Vector3.zero, Quaternion.identity);
903
904 #endregion Protected Fields
905
906
907 #region Public Functions
908
909 public BaseGameboardCore(GlassesHandle glassesHandle, Transform gameboardTransform)
910 {
911 this.glassesHandle = glassesHandle;
912 this.gameboardTransform = gameboardTransform;
913 }
914
915 public void Dispose()
916 {
917 // Perform any necessary cleanup here.
918 }
919
920 #endregion Public Functions
921
922
923 #region Protected Functions
924
925 protected override void Update(GameBoardSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings)
926 {
927 // Obtain the latest gameboard pose.
928 base.Update(settings, scaleSettings, settings);
929 }
930
931 #endregion Protected Functions
932
933
934 #region TrackableCore Overrides
935
936 protected override void SetDefaultPoseGameboardSpace(GameBoardSettings settings)
937 {
938 pose_USTAGE = DEFAULT_GAMEBOARD_POSE_USTAGE;
939 }
940
941 protected override void SetDrivenObjectTransform(GameBoardSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
942 {
943 gameboardTransform?.SetPositionAndRotation(pose_UWRLD.position, pose_UWRLD.rotation);
944 }
945
946 protected override void SetInvalidPoseGameboardSpace(in T5_GameboardPose state, GameBoardSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
947 {
948 SetPoseGameboardSpace(state, settings, scaleSettings, settings);
949 }
950
951 protected override void SetPoseGameboardSpace(in T5_GameboardPose state, GameBoardSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
952 {
953 var gameboardPos_USTAGE = ConvertPosSTAGEToUSTAGE(state.PosOfBOARD_STAGE);
954 Quaternion rotToSTAGE_BOARD = Quaternion.Inverse(state.RotationToBOARD_STAGE);
955 Quaternion rotToUSTAGE_UBOARD = ChangeHandednessRightToLeft(rotToSTAGE_BOARD);
956
957 pose_USTAGE = new Pose(gameboardPos_USTAGE, rotToUSTAGE_UBOARD);
958 }
959
960 protected override void SetPoseUnityWorldSpace(ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
961 {
962 pose_UWRLD = gameboardSettings.currentGameBoard.TransformPose(pose_USTAGE, scaleSettings);
963 }
964
965 protected override bool TryCheckConnected(out bool connected)
966 {
967 // Trackable gameboards depend on glasses that are connected and tracking.
968 connected = Glasses.IsTracked(glassesHandle);
969 return true;
970 }
971
972 #endregion TrackableCore Overrides
973
974
975 #region Private Helper Functions
976
977 private Quaternion ChangeHandednessRightToLeft(Quaternion rotToB_A)
978 {
979 return new Quaternion(-rotToB_A.x, -rotToB_A.z, -rotToB_A.y, rotToB_A.w);
980 }
981
982 #endregion Private Helper Functions
983 }
984
985
986 private class PrimaryGameboardCore : BaseGameboardCore
987 {
988 public PrimaryGameboardCore(GlassesHandle glassesHandle, Transform gameboardTransform)
989 : base(glassesHandle, gameboardTransform) { }
990
991 public void Update(GameBoardSettings gameboardSettings, ScaleSettings scaleSettings)
992 {
993 base.Update(gameboardSettings, scaleSettings, gameboardSettings);
994 // If the gameboard settings' physical gameboard was reassigned, update this gameboard core.
995 gameboardTransform = gameboardSettings.physicalGameboardOrigin.transform;
996 }
997
998 protected override bool TryGetStateFromPlugin(out T5_GameboardPose state, out bool poseIsValid)
999 {
1000 var succeeded = false;
1001 T5_GameboardPose[] resultingPoses = new T5_GameboardPose[MAX_SUPPORTED_GAMEBOARD_POSES];
1002 UInt16 count = MAX_SUPPORTED_GAMEBOARD_POSES;
1003
1004 try
1005 {
1006 succeeded = NativePlugin.GetGameboardPoses(glassesHandle, resultingPoses, ref count) == NativePlugin.T5_RESULT_SUCCESS;
1007 }
1008 catch (Exception e)
1009 {
1010 Log.Error(e.Message);
1011 }
1012
1013 if (!succeeded || count < 1 || resultingPoses.Length < 1)
1014 {
1015 state = new T5_GameboardPose(); // Default values
1016 poseIsValid = false;
1017 GameBoard.SetGameboardType(glassesHandle, GameboardType.GameboardType_None);
1018 return succeeded;
1019 }
1020
1021 state = resultingPoses[0];
1022 poseIsValid = true;
1023 GameBoard.SetGameboardType(glassesHandle, state.GameboardType);
1024 return true;
1025 }
1026 }
1027
1028 #endregion Private Classes
1029 }
1030}
Represents the game board.
Definition GameBoard.cs:29
bool TransformPoint(Vector3 pointInGameboardSpace, PlayerIndex playerIndex, out Vector3 pointInWorldSpace)
Transforms the provided position from gameboard space to world space.
Definition GameBoard.cs:478
static bool TryGetGameboardExtents(GameboardType gameboardType, out GameboardExtents gameboardExtents)
Attempts to obtain the physical dimensions for a particular gameboard type.
Definition GameBoard.cs:394
static bool TryGetGameboardDimensions(GameboardType gameboardType, out GameboardDimensions gameboardDimensions)
Attempts to obtain the physical dimensions for a particular gameboard type.
Definition GameBoard.cs:361
Edge
Represents cardinal positions around the gameboard (i.e. the centers of the edges of the gameboard),...
Definition GameBoard.cs:114
@ Right
The right edge of the gameboard.
Definition GameBoard.cs:136
@ Left
The left edge of the gameboard.
Definition GameBoard.cs:130
@ Near
The near edge of the gameboard.
Definition GameBoard.cs:119
@ Far
The far edge of the gameboard.
Definition GameBoard.cs:124
bool ShowGizmo
Shows the game board gizmo in the editor.
Definition GameBoard.cs:36
static bool TryGetGameboardType(out GameboardType gameboardType)
Attempts to check the latest glasses pose for the current gameboard type, such as LE,...
Definition GameBoard.cs:335
bool InverseTransformPoint(Vector3 pointInWorldSpace, PlayerIndex playerIndex, out Vector3 pointInGameboardSpace)
Transforms the provided position from world space to gameboard space.
Definition GameBoard.cs:549
bool InverseTransformPose(Pose poseInWorldSpace, PlayerIndex playerIndex, out Pose poseInGameboardSpace)
Transforms the provided pose from world space to gameboard space.
Definition GameBoard.cs:527
float GizmoOpacity
Sets the opacity of the game board gizmo in the editor.
Definition GameBoard.cs:50
bool TransformPose(Pose poseInGameboardSpace, PlayerIndex playerIndex, out Pose poseInWorldSpace)
Transforms the provided pose from gameboard space to world space.
Definition GameBoard.cs:456
Corner
Represents ordinal positions around the gameboard, relative to a user positioned at the near (default...
Definition GameBoard.cs:143
bool TransformPose(Vector3 positionInGameboardSpace, Quaternion rotationToGameboardSpace, PlayerIndex playerIndex, out Vector3 positionInWorldSpace, out Quaternion rotationToWorldSpace)
Transforms the provided pose from gameboard space to world space.
Definition GameBoard.cs:431
bool InverseTransformPose(Vector3 positionInWorldSpace, Quaternion rotationToWorldSpace, PlayerIndex playerIndex, out Vector3 positionInGameboardSpace, out Quaternion rotationToGameboardSpace)
Transforms the provided pose from world space to gameboard space.
Definition GameBoard.cs:502
GameboardType GameboardType
The gameboard configuration, such as LE, XE, or folded XE.
Definition GameBoard.cs:57
GameBoard currentGameBoard
The gameboard is the window into the game world, as well as the origin about which the glasses/wand a...
GameObject physicalGameboardOrigin
A GameObject whose pose is driven by the gameboard's orientation in physical space,...
The Logger.
Definition Log.cs:42
static void Error(string m, params object[] list)
ERROR logging function call.
Definition Log.cs:127
Provides access to player settings and functionality.
Definition Player.cs:31
static bool TryGetSettings(PlayerIndex playerIndex, out PlayerSettings playerSettings)
Obtains the PlayerSettings corresponding to the specified player.
Definition Player.cs:79
ScaleSettings contains the scale data used to translate between Unity units and the user's physical s...
LengthUnit contentScaleUnit
The real-world unit to be compared against when using .
float contentScaleRatio
The scaling ratio relates physical distances to world-space units.
Enforces uniform scaling by setting the x, y, and z scale components of the associated transform to b...
float localScale
The size of the object as a single float value, rather than a scale vector.
void UnifyScale()
Synchronizes the component values of the game object's local scale vector (e.g. [1,...
GameboardType
The type of Gameboard being tracked by the glasses.
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)
Represents the distances from the gameboard tracking origin to the borders of the gameboard's viewabl...
Definition GameBoard.cs:177
Length ViewableSpanX
The distance in meters representing the side-to-side width of the viewable area of the gameboard;.
Definition GameBoard.cs:206
Length ViewableExtentNegativeZ
The distance in meters from the gameboard origin to the edge of the viewable area in the negative Z d...
Definition GameBoard.cs:196
Vector3 GetCornerPositionInGameboardSpace(Corner corner)
Gets the coordinates of the specified corner in gameboard space.
Definition GameBoard.cs:252
Length ViewableExtentPositiveZ
The distance in meters from the gameboard origin to the edge of the viewable area in the positive Z d...
Definition GameBoard.cs:191
Length ViewableExtentPositiveY
The distance in meters above the gameboard origin that the viewable area extends in the positive Y di...
Definition GameBoard.cs:201
Vector3 GetGeometricCenterPositionInGameboardSpace()
Gets the coordinates of the geometric center (or centroid) of the gameboard in gameboard space.
Definition GameBoard.cs:315
Length ViewableExtentNegativeX
The distance in meters from the gameboard origin to the edge of the viewable area in the negative X d...
Definition GameBoard.cs:186
Length ViewableSpanZ
The distance in meters representing the height of the viewable area of the gameboard;.
Definition GameBoard.cs:214
Vector3 GetEdgeCenterPositionInGameboardSpace(Edge edge)
Gets the coordinates of the center of the specified edge in gameboard space.
Definition GameBoard.cs:279
Length ViewableSpanY
The distance in meters representing the front-to-back length of the viewable area of the gameboard;.
Definition GameBoard.cs:210
Length OriginOffsetZ
The distance from the tracking origin to the geometric center of the gameboard.
Definition GameBoard.cs:223
Length ViewableExtentPositiveX
The distance in meters from the gameboard origin to the edge of the viewable area in the positive X d...
Definition GameBoard.cs:181
Vector3 GetPhysicalCenterPositionInGameboardSpace()
Gets the coordinates of the geometric center (or centroid) of the gameboard in gameboard space.
Definition GameBoard.cs:306
Physical dimensions of a gameboard, in meters.
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...