EditorDevice.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  15. using UnityEngine;
  16. using System.Collections.Generic;
  17. using NibiruAxis;
  18. /// @cond
  19. namespace Nxr.Internal
  20. {
  21. // Sends simulated values for use when testing within the Unity Editor.
  22. public class EditorDevice : BaseARDevice
  23. {
  24. // Simulated neck model. Vector from the neck pivot point to the point between the eyes.
  25. private static readonly Vector3 neckOffset = new Vector3(0, 0.075f, 0.08f);
  26. // Use mouse to emulate head in the editor.
  27. private float mouseX = 0;
  28. private float mouseY = 0;
  29. private float mouseZ = 0;
  30. private bool loadConfigData = false;
  31. private float[] deviceConfigData;
  32. Quaternion remoteQaut;
  33. public override void Init()
  34. {
  35. Input.gyro.enabled = true;
  36. // Debug.Log("RemoteDebug_" + NxrViewer.Instance.RemoteDebug);
  37. if (NxrViewer.Instance.RemoteDebug)
  38. {
  39. NxrViewer.Instance.InitialRecenter = false;
  40. NibiruEmulatorManager nibiruEmulatorManager = NibiruEmulatorManager.Instance;
  41. nibiruEmulatorManager.OnConfigDataEvent += ConfigDataLoaded;
  42. nibiruEmulatorManager.OnHmdPoseDataEvent += HmdPoseDataEvent;
  43. nibiruEmulatorManager.OnHmdStatusEvent += HmdStatusEvent;
  44. nibiruEmulatorManager.OnControllerPoseDataEvent += ControllerPoseDataEvent;
  45. }
  46. }
  47. private void ControllerPoseDataEvent(NibiruEmulatorClientSocket.ControllerPoseData data)
  48. {
  49. Loom.QueueOnMainThread((param) => {
  50. NibiruEmulatorClientSocket.ControllerPoseData controllerPoseData = (NibiruEmulatorClientSocket.ControllerPoseData) param;
  51. NibiruEmulatorClientSocket.TrackingQuat quat = controllerPoseData.right_controller_Pose_Orientation;
  52. NxrPlayerCtrl.Instance.EditorRemoteQuat = new Quaternion(quat.x, quat.y, quat.z, quat.w);
  53. }, data);
  54. }
  55. private void HmdStatusEvent(NibiruEmulatorClientSocket.HmdStatusData data)
  56. {
  57. bool IsControllerConnect = data.controllerStatus == 1;
  58. Loom.QueueOnMainThread((param) => {
  59. NxrPlayerCtrl.Instance.debugInEditor = (bool) param;
  60. }, IsControllerConnect);
  61. }
  62. private void HmdPoseDataEvent(NibiruEmulatorClientSocket.HmdPoseData data)
  63. {
  64. Loom.QueueOnMainThread((param) => {
  65. NibiruEmulatorClientSocket.TrackingQuat quat = ((NibiruEmulatorClientSocket.HmdPoseData) param).HeadPose_Pose_Orientation;
  66. NibiruEmulatorClientSocket.TrackingVector3 pos = ((NibiruEmulatorClientSocket.HmdPoseData)param).HeadPose_Pose_Position;
  67. remoteQaut = new Quaternion(quat.x, quat.y, quat.z, quat.w);
  68. }, data);
  69. }
  70. private void ConfigDataLoaded(NibiruEmulatorClientSocket.OpticalConfigData data)
  71. {
  72. if (!loadConfigData)
  73. {
  74. loadConfigData = true;
  75. deviceConfigData = new float[14];
  76. }
  77. }
  78. public override bool SupportsNativeDistortionCorrection(List<string> diagnostics)
  79. {
  80. return false; // No need for diagnostic message.
  81. }
  82. // Since we can check all these settings by asking Nvr.Instance, no need
  83. // to keep a separate copy here.
  84. public override void SetSplitScreenModeEnabled(bool enabled) { }
  85. private Quaternion initialRotation = Quaternion.identity;
  86. public override void UpdateState()
  87. {
  88. Quaternion rot = Quaternion.identity;
  89. if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
  90. {
  91. mouseX += Input.GetAxis("Mouse X") * 5;
  92. if (mouseX <= -180)
  93. {
  94. mouseX += 360;
  95. }
  96. else if (mouseX > 180)
  97. {
  98. mouseX -= 360;
  99. }
  100. mouseY -= Input.GetAxis("Mouse Y") * 2.4f;
  101. mouseY = Mathf.Clamp(mouseY, -85, 85);
  102. }
  103. else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
  104. {
  105. mouseZ += Input.GetAxis("Mouse X") * 5;
  106. mouseZ = Mathf.Clamp(mouseZ, -85, 85);
  107. }
  108. bool IsNeedUpdatePose = false;
  109. if (Application.isEditor && NxrViewer.Instance.RemoteDebug)
  110. {
  111. if (remoteQaut.w != 0)
  112. {
  113. rot = remoteQaut;
  114. IsNeedUpdatePose = true;
  115. }
  116. }
  117. if (mouseX != 0 || mouseY != 0 || mouseZ != 0)
  118. {
  119. rot = Quaternion.Euler(mouseY, mouseX, mouseZ);
  120. IsNeedUpdatePose = true;
  121. }
  122. if (IsNeedUpdatePose)
  123. {
  124. headPose.Set(Vector3.zero, rot);
  125. }
  126. #if UNITY_STANDALONE_WIN || ANDROID_REMOTE_NRR
  127. // ÊÖ±ú¼üֵ״̬
  128. if (NxrInstantNativeApi.Inited)
  129. {
  130. NxrInstantNativeApi.Nibiru_Pose pose = NxrInstantNativeApi.GetPoseByDeviceType(NxrInstantNativeApi.NibiruDeviceType.Hmd);
  131. if (pose.rotation.w == 0)
  132. {
  133. pose.rotation.w = 1;
  134. }
  135. this.headPose.Set(pose.position, new Quaternion(pose.rotation.x, pose.rotation.y, -pose.rotation.z, -pose.rotation.w));
  136. }
  137. #endif
  138. isHeadPoseUpdated = true;
  139. }
  140. public override void PostRender(RenderTexture stereoScreen)
  141. {
  142. // Do nothing.
  143. }
  144. public override void UpdateScreenData()
  145. {
  146. // configData is 0.062000,0.037250,0.039000,40.000000,40.000000,43.299999,43.299999,0.127560,0.084400,2560.000000,1440.000000,0.003000,0.120960,0.068040
  147. string deviceConfigInfo = "ar device config parameter : ";
  148. if (deviceConfigData != null)
  149. {
  150. for (int i = 0; i < deviceConfigData.Length; i++)
  151. {
  152. deviceConfigInfo += deviceConfigData[i];
  153. if (i < deviceConfigData.Length - 1)
  154. {
  155. deviceConfigInfo += ",";
  156. }
  157. }
  158. Debug.Log(deviceConfigInfo);
  159. }
  160. Profile = NxrProfile.GetKnownProfile(NxrViewer.Instance.ScreenSize, NxrViewer.Instance.ViewerType);
  161. if (loadConfigData && deviceConfigData != null)
  162. {
  163. Profile.screen.width = deviceConfigData[12];
  164. Profile.screen.height = deviceConfigData[13];
  165. Profile.viewer = new NxrProfile.Viewer
  166. {
  167. lenses = {
  168. separation = deviceConfigData[0],
  169. offset = deviceConfigData[1],
  170. screenDistance = deviceConfigData[2],
  171. alignment = NxrProfile.Lenses.AlignBottom,
  172. },
  173. maxFOV = {
  174. outer = deviceConfigData[3],
  175. inner = deviceConfigData[4],
  176. upper = deviceConfigData[5],
  177. lower = deviceConfigData[6]
  178. },
  179. distortion = {
  180. Coef = new [] { deviceConfigData[7], deviceConfigData[8] },
  181. },
  182. inverse = NxrProfile.ApproximateInverse(new[] { deviceConfigData[7], deviceConfigData[8] })
  183. };
  184. }
  185. if(userIpd > 0)
  186. {
  187. Profile.viewer.lenses.separation = userIpd;
  188. }
  189. ComputeEyesFromProfile(1, 2000);
  190. profileChanged = true;
  191. Debug.Log("UpdateScreenData=" + Profile.viewer.lenses.separation);
  192. }
  193. public override void Recenter()
  194. {
  195. mouseX = mouseZ = 0; // Do not reset pitch, which is how it works on the phone.
  196. }
  197. public override bool GazeApi(GazeTag tag, string param)
  198. {
  199. return true;
  200. }
  201. public override void SetCameraNearFar(float near, float far)
  202. {
  203. Debug.Log("EditorDevice.SetCameraNearFar : " + near + "," + far);
  204. }
  205. private bool isHeadPoseUpdated = false;
  206. private float userIpd = -1;
  207. public override void SetIpd(float ipd)
  208. {
  209. userIpd = ipd;
  210. }
  211. public override bool IsHeadPoseUpdated()
  212. {
  213. return isHeadPoseUpdated;
  214. }
  215. }
  216. }
  217. /// @endcond
  218. #endif