MaxstARSDK  3.5.0
AbstractARManager.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System.Collections;
7 using UnityEngine.Rendering;
8 
9 namespace maxstAR
10 {
14  public abstract class AbstractARManager : MonoBehaviour
15  {
16  private static AbstractARManager instance = null;
17 
18  internal static AbstractARManager Instance
19  {
20  get
21  {
22  if (instance == null)
23  {
24  instance = FindObjectOfType<AbstractARManager>();
25  }
26 
27  return instance;
28  }
29  }
30 
31  private string licenseKey = null;
32  private AndroidEngine androidEngine = null;
33  private int screenWidth = 0;
34  private int screenHeight = 0;
35  private ScreenOrientation orientation = ScreenOrientation.Unknown;
36  private float nearClipPlane = 0.0f;
37  private float farClipPlane = 0.0f;
38  private Camera arCamera = null;
39  private int cameraWidth = 0;
40  private int cameraHeight = 0;
41  //private GameObject watermarkObject = null;
42 
46  protected virtual void Awake()
47  {
48  Init();
49 
50  if (Application.platform == RuntimePlatform.Android ||
51  Application.platform == RuntimePlatform.IPhonePlayer)
52  {
53  MaxstAR.SetScreenOrientation((int)Screen.orientation);
54  }
55  else
56  {
57  MaxstAR.SetScreenOrientation((int)ScreenOrientation.LandscapeLeft);
58  }
59 
60  MaxstAR.OnSurfaceChanged(Screen.width, Screen.height);
61  }
62 
66  protected void Init()
67  {
69  if (Application.platform == RuntimePlatform.Android)
70  {
71  androidEngine = new AndroidEngine(licenseKey);
72  }
73  else if (Application.platform == RuntimePlatform.IPhonePlayer)
74  {
75  NativeStaticAPI.init(licenseKey);
76  }
77 
78  // If CameraBackgroundBehaviour is not activated when start application, projection matrix
79  // can not be made because screen width and height isn't set properly.
80  if (screenWidth != Screen.width || screenHeight != Screen.height)
81  {
82  screenWidth = Screen.width;
83  screenHeight = Screen.height;
84  MaxstAR.OnSurfaceChanged(screenWidth, screenHeight);
85  }
86 
87  if (Application.platform == RuntimePlatform.Android ||
88  Application.platform == RuntimePlatform.IPhonePlayer)
89  {
90  MaxstAR.SetScreenOrientation((int)Screen.orientation);
91  }
92  else
93  {
94  MaxstAR.SetScreenOrientation((int)ScreenOrientation.LandscapeLeft);
95  }
96 
97  arCamera = GetComponent<Camera>();
98  }
99 
100  void Update()
101  {
102  // If world center is target then tracking pose should be set to main camera's transform
103  if (worldCenterMode == WorldCenterMode.TARGET)
104  {
106  TrackingResult trackingResult = trackingState.GetTrackingResult();
107  if (trackingResult.GetCount() > 0)
108  {
109  Trackable trackable = trackingResult.GetTrackable(0);
110  Matrix4x4 targetPose = trackable.GetTargetPose().inverse;
111 
112  if (targetPose == Matrix4x4.zero)
113  return;
114 
115  Quaternion rotation = Quaternion.Euler(90, 0, 0);
116  Matrix4x4 m = Matrix4x4.TRS(new Vector3(0, 0, 0), rotation, new Vector3(1, 1, 1));
117  targetPose = m * targetPose;
118 
119  Camera.main.transform.position = MatrixUtils.PositionFromMatrix(targetPose);
120  Camera.main.transform.rotation = MatrixUtils.QuaternionFromMatrix(targetPose);
121  Camera.main.transform.localScale = MatrixUtils.ScaleFromMatrix(targetPose);
122  }
123  }
124  }
125 
129  protected virtual void OnDestroy()
130  {
131  if (Application.platform == RuntimePlatform.Android)
132  {
133  androidEngine.Dispose();
134  }
135  }
136 
142  public enum WorldCenterMode
143  {
147  CAMERA = 0,
151  TARGET = 1
152  }
153 
154  [SerializeField]
155  private WorldCenterMode worldCenterMode;
156 
161  {
162  get
163  {
164  return worldCenterMode;
165  }
166  }
167 
172  public void SetWorldCenterMode(WorldCenterMode worldCenterMode)
173  {
174  this.worldCenterMode = worldCenterMode;
175  }
176 
177  void OnPreRender()
178  {
179  if (screenWidth != Screen.width || screenHeight != Screen.height)
180  {
181  screenWidth = Screen.width;
182  screenHeight = Screen.height;
183  MaxstAR.OnSurfaceChanged(screenWidth, screenHeight);
184  }
185 
186  if (orientation != Screen.orientation)
187  {
188  orientation = Screen.orientation;
189 
190  if (Application.platform == RuntimePlatform.Android ||
191  Application.platform == RuntimePlatform.IPhonePlayer)
192  {
193  MaxstAR.SetScreenOrientation((int)orientation);
194  }
195  }
196 
197  if (nearClipPlane != arCamera.nearClipPlane || farClipPlane != arCamera.farClipPlane)
198  {
199  nearClipPlane = arCamera.nearClipPlane;
200  farClipPlane = arCamera.farClipPlane;
201  }
202 
203  //if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan ||
204  // SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal)
205  //{
206  // Debug.LogError("Current graphics device type is " + SystemInfo.graphicsDeviceType + ". This is not supported");
207  // //return;
208  //}
209 
210  int tempCameraWidth = CameraDevice.GetInstance().GetWidth();
211  int tempCameraHeight = CameraDevice.GetInstance().GetHeight();
212 
213  if (tempCameraWidth == 0 || tempCameraHeight == 0)
214  {
215  return;
216  }
217 
218  if (cameraWidth != tempCameraWidth || cameraHeight != tempCameraHeight)
219  {
220  cameraWidth = tempCameraWidth;
221  cameraHeight = tempCameraHeight;
222  }
223 
224  TransformBackgroundPlane(arCamera, AbstractCameraBackgroundBehaviour.Instance.transform);
225 
226  arCamera.projectionMatrix = MatrixUtils.getProjectionMatrix();
227  }
228 
229  //private void DrawZombie()
230  //{
231  // if (MaxstARUtils.IsDirectXAPI())
232  // {
233  // GL.IssuePluginEvent(BackgroundRenderer.GetInstance().GetNativeRenderEventFunc(), MaxstARUtils.NATIVE_RENDER_EVENT_DRAW_ZOMBIE_DX);
234  // }
235  // else
236  // {
237  // GL.IssuePluginEvent(BackgroundRenderer.GetInstance().GetNativeRenderEventFunc(), MaxstARUtils.NATIVE_RENDER_EVENT_DRAW_ZOMBIE_GL);
238  // }
239  //}
240 
241  private void TransformBackgroundPlane(Camera camera, Transform planeTransform)
242  {
243  float widthRatio = (float)Screen.width / cameraWidth;
244  float heightRatio = (float)Screen.height / cameraHeight;
245  float farClipPlane = camera.farClipPlane * 0.90f;
246  float tanFovWidth = (1.0f / (float)Screen.width) * (float)Screen.height;
247  float frustumWidth = tanFovWidth * farClipPlane * camera.aspect;
248  float viewWidth = (float)frustumWidth / Screen.width;
249  float viewHeight = viewWidth * (widthRatio / heightRatio);
250  float flipHorizontal = 1.0f;
251  float flipVertical = 1.0f;
252 
253  // UnityEngine.VR.VRSettings.enabled = false;
254  // if (UnityEngine.XR.XRSettings.enabled)
255  //{
256  // viewWidth = viewWidth * 2.0f;
257  //}
258 
259  //Debug.Log("UnityEngine.VR.VRSettings.enabled : " + UnityEngine.VR.VRSettings.enabled);
260  //Debug.Log("Loaded device name: " + UnityEngine.VR.VRSettings.loadedDeviceName);
261 
262  if (CameraDevice.GetInstance().IsFlipHorizontal())
263  {
264  flipHorizontal = -1.0f;
265  }
266 
267  if (CameraDevice.GetInstance().IsFlipVertical())
268  {
269  flipVertical = -1.0f;
270  }
271 
272  if (MaxstARUtils.IsDirectXAPI())
273  {
274  flipHorizontal = -flipHorizontal;
275  }
276 
277  if (widthRatio > heightRatio)
278  {
279  planeTransform.localScale = new Vector3(widthRatio * cameraWidth * viewWidth * flipVertical,
280  widthRatio * cameraHeight * viewWidth * flipHorizontal, 0.1f);
281  planeTransform.localPosition = new Vector3(0.0f, 0.0f, farClipPlane);
282  }
283  else
284  {
285  planeTransform.localScale = new Vector3(heightRatio * cameraWidth * viewHeight * flipVertical,
286  heightRatio * cameraHeight * viewHeight * flipHorizontal, 0.1f);
287  planeTransform.localPosition = new Vector3(0.0f, 0.0f, farClipPlane);
288  }
289 
290  if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D9 ||
291  SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11 ||
292  SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12)
293  {
294  planeTransform.localScale = new Vector3(planeTransform.localScale.x, -planeTransform.localScale.y, planeTransform.localScale.z);
295  }
296  }
297 
298  //private void CreateWatermarkIfNeeded()
299  //{
300  // int tempCameraWidth = CameraDevice.GetInstance().GetWidth();
301  // int tempCameraHeight = CameraDevice.GetInstance().GetHeight();
302 
303  // if (tempCameraWidth == 0 || tempCameraHeight == 0)
304  // {
305  // return;
306  // }
307 
308  // if (MaxstARUtils.IsDirectXAPI())
309  // {
310  // int signatureType = NativeSharedAPI.Signature_getSignatureType();
311  // if (signatureType != 0 && signatureType != 2)
312  // {
313  // if (watermarkObject == null)
314  // {
315  // watermarkObject = WatermarkUtil.Instance.CreateWartermarkObject(signatureType);
316  // watermarkObject.transform.parent = transform;
317  // }
318 
319  // if (watermarkObject != null)
320  // {
321  // watermarkObject.transform.localScale = new Vector3(0.5f, 1.0f, -0.14f);
322  // watermarkObject.transform.localPosition = new Vector3(-0.8f, -0.6f, 2.5f);
323  // watermarkObject.transform.localRotation = Quaternion.Euler(new Vector3(-90.0f, 0.0f, 0.0f));
324 
325  // if (watermarkObject.GetComponent<MeshRenderer>() == null ||
326  // watermarkObject.GetComponent<MeshFilter>() == null ||
327  // watermarkObject.GetComponent<MeshRenderer>().enabled == false)
328  // {
329  // Destroy(watermarkObject);
330  // watermarkObject = null;
331  // }
332  // }
333  // }
334  // }
335  //}
336  }
337 }
WorldCenterMode
The world center mode defines what is the center in game view. If camera is world center then trackab...
Trackable GetTrackable(int index)
Get tracking target information
static AbstractConfigurationScriptableObject GetInstance()
Get configuration asset instance
virtual void OnDestroy()
Release sdk
static void SetScreenOrientation(int orientation)
Notify screen orientation chagned
Definition: MaxstAR.cs:33
WorldCenterMode WorldCenterModeSetting
Get world center mode value
void Init()
Set device orientation and surface size
TrackingResult GetTrackingResult()
Output the number of trackers that have been tracked successfully.
Container for individual tracking information
Initialize system environment with app key, screen size and orientation
int GetCount()
Get tracking target count. Current version ar engine could not track multi target. That feature will be implemented not so far future.
Set device environment
Definition: MaxstAR.cs:15
TrackingState GetTrackingState()
Get saved TrackingState value
void SetWorldCenterMode(WorldCenterMode worldCenterMode)
Set world center mode
virtual void Awake()
Intialize sdk
Contains tracked targets informations
static TrackerManager GetInstance()
Get TrackerManager instance
Control AR Engine (Singletone)
Container for individual tracking information
Definition: Trackable.cs:17