Unity SDK Docs 1.5.0-beta.6
Loading...
Searching...
No Matches
TrackableCore.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 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_USTAGE; }
30 protected Pose pose_USTAGE;
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 gameboardPose_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 gameboardPose_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);
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 ConvertPosSTAGEToUSTAGE(Vector3 pos_STAGE)
100 {
101 // Swap Y and Z to change between STAGE and USTAGE
102 return new Vector3(pos_STAGE.x, pos_STAGE.z, pos_STAGE.y);
103 }
104
105 protected static Pose GameboardToWorldSpace(Pose pose_GameboardSpace,
106 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
107 {
108 float scaleToUWRLD_USTAGE = scaleSettings.GetScaleToWorldSpaceFromGameboardSpace(gameboardSettings.gameBoardScale);
109
110 Vector3 pos_UWRLD = gameboardSettings.currentGameBoard.rotation *
111 (scaleToUWRLD_USTAGE * pose_GameboardSpace.position) + gameboardSettings.gameBoardCenter;
112
113 Quaternion rotToUWRLD_OBJ = GameboardToWorldSpace(pose_GameboardSpace.rotation, gameboardSettings);
114
115 return new Pose(pos_UWRLD, rotToUWRLD_OBJ);
116 }
117
118 protected static Vector3 GameboardToWorldSpace(Vector3 pos_USTAGE,
119 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
120 {
121 float scaleToUWRLD_USTAGE = scaleSettings.GetScaleToWorldSpaceFromGameboardSpace(gameboardSettings.gameBoardScale);
122
123 return gameboardSettings.currentGameBoard.rotation *
124 (scaleToUWRLD_USTAGE * pos_USTAGE) + gameboardSettings.gameBoardCenter;
125 }
126
127 protected static Vector3 WorldToGameboardSpace(Vector3 pos_UWRLD,
128 ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
129 {
130 float scaleToUWRLD_USTAGE = scaleSettings.GetScaleToWorldSpaceFromGameboardSpace(gameboardSettings.gameBoardScale);
131 var rotToUWRLD_USTAGE = gameboardSettings.currentGameBoard.rotation;
132 var pos_USTAGE = pos_UWRLD - gameboardSettings.gameBoardCenter;
133 pos_USTAGE = Quaternion.Inverse(rotToUWRLD_USTAGE) * pos_USTAGE;
134 pos_USTAGE /= scaleToUWRLD_USTAGE;
135
136 return pos_USTAGE;
137 }
138
139 protected static Quaternion GameboardToWorldSpace(Quaternion rotToUSTAGE_OBJ, GameBoardSettings gameboardSettings)
140 {
141 var rotToUWRLD_USTAGE = gameboardSettings.currentGameBoard.rotation;
142 var rotToUWRLD_OBJ = rotToUWRLD_USTAGE * rotToUSTAGE_OBJ;
143
144 return rotToUWRLD_OBJ;
145 }
146
147 protected static Quaternion WorldToGameboardSpace(Quaternion rotToUWRLD_OBJ, GameBoardSettings gameboardSettings)
148 {
149 var rotToUWRLD_USTAGE = gameboardSettings.currentGameBoard.rotation;
150 var rotToUSTAGE_UWRLD = Quaternion.Inverse(rotToUWRLD_USTAGE);
151 var rotToUSTAGE_OBJ = rotToUSTAGE_UWRLD * rotToUWRLD_OBJ;
152
153 return rotToUSTAGE_OBJ;
154
155 }
156
157 #endregion Protected Functions
158
159
160 #region Abstract Functions
161
167 protected abstract void SetDefaultPoseGameboardSpace(TSettings settings);
168
176 protected abstract void SetPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
177
185 protected abstract void SetInvalidPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
186
192 protected abstract void SetPoseUnityWorldSpace(ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
193
199 protected abstract bool TryCheckConnected(out bool connected);
200
208 protected abstract bool TryGetStateFromPlugin(out TState state, out bool poseIsValid);
209
216 protected abstract void SetDrivenObjectTransform(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings);
217
218 #endregion Abstract Functions
219 }
220}
Vector3 gameBoardCenter
The gameboard position or focal position offset.
GameBoard currentGameBoard
The gameboard is the window into the game world, as well as the origin about which the glasses/wand a...
float gameBoardScale
The gameboard'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...
bool IsConnected
Whether or not the trackable is connected.
bool TryCheckConnected(out bool connected)
Determines whether the tracked object is still connected.
void SetDrivenObjectTransform(TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose of the object(s) being driven by TrackableCore.
bool TryGetStateFromPlugin(out TState state, out bool poseIsValid)
Gets the latest pose for the tracked object from the native plugin.
void SetPoseUnityWorldSpace(ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose values of the tracked object in Unity World Space.
void SetDefaultPoseGameboardSpace(TSettings settings)
Gets the default pose of the tracked object.
Pose Pose_GameboardSpace
The pose of the trackable w.r.t. the gameboard reference frame.
void SetPoseGameboardSpace(in TState state, TSettings settings, ScaleSettings scaleSettings, GameBoardSettings gameboardSettings)
Sets the pose values of the tracked object in Unity World Space.
Pose gameboardPose_UWRLD
The pose of the gameboard reference frame w.r.t. the Unity world-space reference frame.
bool IsTracked
Whether or not the trackable is being tracked.
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...
Pose Pose_UnityWorldSpace
The Pose of the trackable in Unity world space.
Quaternion rotation
The rotation vector for the associated transform.