MaxstARSDK  3.5.0
AbstractWearableManager.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System.Collections;
7 using System.Runtime.InteropServices;
8 using System;
9 
10 namespace maxstAR
11 {
15  public abstract class AbstractWearableManager : MonoBehaviour
16  {
17  private GameObject eyeLeft = null;
18  private GameObject eyeRight = null;
19  private WearableDeviceController wearableDeviceController;
20  private WearableCalibration wearableCalibration;
21 
22  void Start()
23  {
24  if (Application.platform == RuntimePlatform.Android)
25  {
27  WearableCalibration.WearableType.OpticalSeeThrough)
28  {
29  wearableDeviceController = new WearableDeviceController();
30  wearableCalibration = WearableCalibration.GetInstance();
31 
32  bool res = wearableDeviceController.IsSupportedWearableDevice();
33  Debug.Log("wearableDeviceController.IsSupportedWearableDevice()" + res.ToString());
34  if (wearableDeviceController.IsSupportedWearableDevice())
35  {
36  string modelName = wearableDeviceController.GetModelName();
37 
38  Debug.Log(modelName.ToString());
39  if (wearableCalibration.Init(modelName, Screen.width, Screen.height))
40  {
41  Debug.Log("wearableCalibration.Init suc");
42  wearableDeviceController.SetStereoMode(true);
43 
44  AbstractCameraBackgroundBehaviour cameraBackground = GetComponentInChildren<AbstractCameraBackgroundBehaviour>();
45  cameraBackground.gameObject.SetActive(false);
46 
47  CreateWearableEye();
48  }
49  }
50  }
51  }
52  }
53 
54  void OnDestroy()
55  {
56  if (Application.platform == RuntimePlatform.Android)
57  {
59  WearableCalibration.WearableType.OpticalSeeThrough)
60  {
61  //if (wearableCalibration.IsActivated())
62  {
63  wearableDeviceController.SetStereoMode(false);
64  wearableCalibration.Deinit();
65  DestroyWearableEye();
66  }
67  }
68  }
69  }
70 
71  private void CreateWearableEye()
72  {
73  if (eyeLeft == null)
74  {
75  eyeLeft = new GameObject("EyeLeft");
76  eyeLeft.AddComponent<Camera>();
77  }
78 
79  if (eyeRight == null)
80  {
81  eyeRight = new GameObject("EyeRight");
82  eyeRight.AddComponent<Camera>();
83  }
84 
85  eyeLeft.transform.parent = transform;
86  eyeRight.transform.parent = transform;
87 
88  eyeLeft.transform.localPosition = Vector3.zero;
89  eyeLeft.transform.localRotation = Quaternion.identity;
90  eyeLeft.transform.localScale = Vector3.one;
91 
92  eyeRight.transform.localPosition = Vector3.zero;
93  eyeRight.transform.localRotation = Quaternion.identity;
94  eyeRight.transform.localScale = Vector3.one;
95 
96  Camera mainCamera = transform.GetComponent<Camera>();
97  Camera leftCamera = eyeLeft.GetComponent<Camera>();
98  Camera rightCamera = eyeRight.GetComponent<Camera>();
99 
100  CameraClearFlags clearFlags = mainCamera.clearFlags;
101  Color backgroundColor = mainCamera.backgroundColor;
102  float depth = mainCamera.depth + 1;
103  float nearClipPlane = mainCamera.nearClipPlane;
104  float farClipPlane = mainCamera.farClipPlane;
105 
106  float[] projectionMatrixEyeLeftPtr = wearableCalibration.GetProjectionMatrix(WearableCalibration.EyeType.EYE_LEFT);
107  float[] projectionMatrixEyeRightPtr = wearableCalibration.GetProjectionMatrix(WearableCalibration.EyeType.EYE_RIGHT);
108 
109  Matrix4x4 projectionEyeLeft = MatrixUtils.ConvertGLProjectionToUnityProjection(projectionMatrixEyeLeftPtr);
110  Matrix4x4 projectionEyeRight = MatrixUtils.ConvertGLProjectionToUnityProjection(projectionMatrixEyeRightPtr);
111 
112  leftCamera.clearFlags = clearFlags;
113  leftCamera.backgroundColor = backgroundColor;
114  leftCamera.depth = depth;
115  leftCamera.nearClipPlane = nearClipPlane;
116  leftCamera.farClipPlane = farClipPlane;
117  leftCamera.rect = new Rect(0.0f, 0.0f, 0.5f, 1.0f);
118  leftCamera.projectionMatrix = projectionEyeLeft;
119 
120  rightCamera.clearFlags = clearFlags;
121  rightCamera.backgroundColor = backgroundColor;
122  rightCamera.depth = depth;
123  rightCamera.nearClipPlane = nearClipPlane;
124  rightCamera.farClipPlane = farClipPlane;
125  rightCamera.rect = new Rect(0.5f, 0.0f, 0.5f, 1.0f);
126  rightCamera.projectionMatrix = projectionEyeRight;
127  }
128 
129  private void DestroyWearableEye()
130  {
131  if (eyeLeft != null)
132  {
133  DestroyImmediate(eyeLeft);
134  }
135 
136  if (eyeRight != null)
137  {
138  DestroyImmediate(eyeRight);
139  }
140  }
141  }
142 }
bool Init(string modelName, int width, int height)
Initialize the HMD device.
static AbstractConfigurationScriptableObject GetInstance()
Get configuration asset instance
WearableType
Wearable Device Type
float [] GetProjectionMatrix(EyeType eyeType)
Get calibrated HMD projection matrix.
API for wearable calibration.
static WearableCalibration GetInstance()
Get a WearableCalibration instance.
void Deinit()
Deinitialize the HMD device.
Handles native background rendering. Background rendering includes camera image, feature point...
WearableCalibration.WearableType WearableType
Select wearable device type
Create stereo camera for optical see-through HMD