Tilt Five™ Unity API  1.3.0
 
Loading...
Searching...
No Matches
TrackableCore.cs
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-2022 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 UnityEngine;
19
20namespace TiltFive
21{
22 public abstract class TrackableCore<TSettings, TState> where TSettings : TrackableSettings
23 {
24 #region Properties
25
29 public Pose Pose_GameboardSpace { get => pose_UGBD; }
30 protected Pose pose_UGBD;
31
35 public Pose Pose_UnityWorldSpace { get => pose_UWRLD; }
36 protected Pose pose_UWRLD;
37
41 public bool IsTracked { get => isTracked; }
42 protected bool isTracked = false;
43
47 public bool IsConnected { get => isConnected; }
48 protected bool isConnected = false;
49
54 protected Pose gameboardPos_UWRLD;
55
56 #endregion Properties
57
58
59 #region Protected Functions
60
61 protected void Reset(TSettings settings)
62 {
64 isTracked = false;
65 }
66
67 // Update is called once per frame
68 protected virtual void Update(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
69 {
70 if(settings == null)
71 {
72 Log.Error("TrackableSettings configuration required for tracking updates.");
73 return;
74 }
75
76 // Get the game board pose.
77 gameboardPos_UWRLD = new Pose(gameboardSettings.gameBoardCenter,
78 Quaternion.Inverse(gameboardSettings.currentGameBoard.rotation));
79
80 var successfullyConnected = TryCheckConnected(out isConnected) && isConnected;
81 var successfullyGotState = TryGetStateFromPlugin(out var state, out bool poseIsValid, gameboardSettings);
82
83 if (successfullyConnected && successfullyGotState && poseIsValid)
84 {
85 isTracked = true;
86 SetPoseGameboardSpace(state, settings, scaleSettings, gameboardSettings);
87 }
88 else
89 {
90 isTracked = false;
91 SetInvalidPoseGameboardSpace(state, settings, scaleSettings, gameboardSettings);
92 }
93
94 SetPoseUnityWorldSpace(scaleSettings, gameboardSettings);
95
96 SetDrivenObjectTransform(settings, scaleSettings, gameboardSettings);
97 }
98
99 protected static Vector3 ConvertPosGBDToUGBD(Vector3 pos_GBD)
100 {
101 // Swap Y and Z to change between GBD and UGBD
102 var pos_UGBD = new Vector3(pos_GBD.x, pos_GBD.z, pos_GBD.y);
103 return pos_UGBD;
104 }
105
106 protected static Pose GameboardToWorldSpace(Pose pose_GameboardSpace,
107 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
108 {
109 float scaleToUWRLD_UGBD = scaleSettings.GetScaleToUWRLD_UGBD(gameboardSettings.gameBoardScale);
110
111 Vector3 pos_UWRLD = gameboardSettings.currentGameBoard.rotation *
112 (scaleToUWRLD_UGBD * pose_GameboardSpace.position) + gameboardSettings.gameBoardCenter;
113
114 Quaternion rotToUWRLD_OBJ = GameboardToWorldSpace(pose_GameboardSpace.rotation, gameboardSettings);
115
116 return new Pose(pos_UWRLD, rotToUWRLD_OBJ);
117 }
118
119 protected static Vector3 GameboardToWorldSpace(Vector3 pos_UGBD,
120 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
121 {
122 float scaleToUWRLD_UGBD = scaleSettings.GetScaleToUWRLD_UGBD(gameboardSettings.gameBoardScale);
123
124 return gameboardSettings.currentGameBoard.rotation *
125 (scaleToUWRLD_UGBD * pos_UGBD) + gameboardSettings.gameBoardCenter;
126 }
127
128 protected static Vector3 WorldToGameboardSpace(Vector3 pos_UWRLD,
129 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
130 {
131 float scaleToUWRLD_UGBD = scaleSettings.GetScaleToUWRLD_UGBD(gameboardSettings.gameBoardScale);
132 var rotToUWRLD_UGBD = gameboardSettings.currentGameBoard.rotation;
133 var pos_UGBD = pos_UWRLD - gameboardSettings.gameBoardCenter;
134 pos_UGBD = Quaternion.Inverse(rotToUWRLD_UGBD) * pos_UGBD;
135 pos_UGBD /= scaleToUWRLD_UGBD;
136
137 return pos_UGBD;
138 }
139
140 protected static Quaternion GameboardToWorldSpace(Quaternion rotToUGBD_OBJ, GameBoardSettings gameboardSettings)
141 {
142 var rotToUWRLD_UGBD = gameboardSettings.currentGameBoard.rotation;
143 var rotToUWRLD_OBJ = rotToUWRLD_UGBD * rotToUGBD_OBJ;
144
145 return rotToUWRLD_OBJ;
146 }
147
148 protected static Quaternion WorldToGameboardSpace(Quaternion rotToUWRLD_OBJ, GameBoardSettings gameboardSettings)
149 {
150 var rotToUWRLD_UGBD = gameboardSettings.currentGameBoard.rotation;
151 var rotToUGBD_UWRLD = Quaternion.Inverse(rotToUWRLD_UGBD);
152 var rotToUGBD_OBJ = rotToUGBD_UWRLD * rotToUWRLD_OBJ;
153
154 return rotToUGBD_OBJ;
155
156 }
157
158 #endregion Protected Functions
159
160
161 #region Abstract Functions
162
168 protected abstract void SetDefaultPoseGameboardSpace(TSettings settings);
169
177 protected abstract void SetPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
178
186 protected abstract void SetInvalidPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
187
193 protected abstract void SetPoseUnityWorldSpace(ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
194
200 protected abstract bool TryCheckConnected(out bool connected);
201
209 protected abstract bool TryGetStateFromPlugin(out TState state, out bool poseIsValid, GameBoardSettings gameboardSettings);
210
217 protected abstract void SetDrivenObjectTransform(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
218
219 #endregion Abstract Functions
220 }
221}
Vector3 gameBoardCenter
The game board position or focal position offset.
GameBoard currentGameBoard
The game board is the window into the game world, as well as the origin about which the glasses/wand ...
float gameBoardScale
The game board's scale multiplies the perceived size of objects in the scene.
The Logger.
Definition: Log.cs:42
static void Error(string m, params object[] list)
ERROR logging function call.
Definition: Log.cs:127
ScaleSettings contains the scale data used to translate between Unity units and the user's physical s...
float GetScaleToUWRLD_UGBD(float gameboardScale)
abstract void SetDefaultPoseGameboardSpace(TSettings settings)
Gets the default pose of the tracked object.
bool IsConnected
Whether or not the trackable is connected.
abstract bool TryCheckConnected(out bool connected)
Determines whether the tracked object is still connected.
static Quaternion WorldToGameboardSpace(Quaternion rotToUWRLD_OBJ, GameBoardSettings gameboardSettings)
abstract void SetInvalidPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose values of the tracked object in Unity World Space when we already know the pose is inva...
virtual void Update(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
void Reset(TSettings settings)
Pose gameboardPos_UWRLD
The pose of the gameboard reference frame w.r.t. the Unity world-space reference frame.
abstract void SetDrivenObjectTransform(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose of the object(s) being driven by TrackableCore.
abstract bool TryGetStateFromPlugin(out TState state, out bool poseIsValid, GameBoardSettings gameboardSettings)
Gets the latest pose for the tracked object from the native plugin.
abstract void SetPoseUnityWorldSpace(ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose values of the tracked object in Unity World Space.
static Quaternion GameboardToWorldSpace(Quaternion rotToUGBD_OBJ, GameBoardSettings gameboardSettings)
static Pose GameboardToWorldSpace(Pose pose_GameboardSpace, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
static Vector3 WorldToGameboardSpace(Vector3 pos_UWRLD, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Pose Pose_GameboardSpace
The pose of the trackable w.r.t. the gameboard reference frame.
static Vector3 GameboardToWorldSpace(Vector3 pos_UGBD, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
bool IsTracked
Whether or not the trackable is being tracked.
Pose Pose_UnityWorldSpace
The Pose of the trackable in Unity world space.
abstract void SetPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose values of the tracked object in Unity World Space.
static Vector3 ConvertPosGBDToUGBD(Vector3 pos_GBD)
Quaternion rotation
The rotation vector for the associated transform.
Definition: Log.cs:21