/Assets/UnityARInterface/ARRemote/Scripts/ARRemoteEditorInterface.cs

https://bitbucket.org/cerebralfix/experimental-arinterface · C# · 266 lines · 216 code · 44 blank · 6 comment · 39 complexity · 62f6ed5f5bbba7e36d8be85b911a4e32 MD5 · raw file

  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Networking.PlayerConnection;
  4. using Utils;
  5. using System.Collections;
  6. #if UNITY_EDITOR
  7. using UnityEditor.Networking.PlayerConnection;
  8. using UnityEngine.XR.iOS;
  9. // Runs on the Editor. Talks to the remote Player.
  10. namespace UnityARInterface
  11. {
  12. public class ARRemoteEditorInterface : ARInterface
  13. {
  14. private bool m_SendVideo;
  15. public bool sendVideo
  16. {
  17. get { return m_SendVideo; }
  18. set
  19. {
  20. m_SendVideo = value;
  21. if (editorConnection != null)
  22. {
  23. SendToPlayer(
  24. ARMessageIds.SubMessageIds.enableVideo,
  25. new SerializableEnableVideo(sendVideo));
  26. }
  27. }
  28. }
  29. public EditorConnection editorConnection = null;
  30. private int m_CurrentPlayerId = -1;
  31. private SerializableFrame m_Frame = null;
  32. private Settings m_CachedSettings;
  33. private Camera m_CachedCamera;
  34. private ARRemoteVideo m_ARVideo;
  35. private LightEstimate m_LightEstimate;
  36. public bool connected { get { return m_CurrentPlayerId != -1; } }
  37. public int playerId { get { return m_CurrentPlayerId; } }
  38. public bool IsRemoteServiceRunning { get; protected set; }
  39. Texture2D m_RemoteScreenYTexture;
  40. Texture2D m_RemoteScreenUVTexture;
  41. List<Vector3> m_PointCloud;
  42. private Matrix4x4 m_DisplayTransform;
  43. public void ScreenCaptureParamsMessageHandler(MessageEventArgs message)
  44. {
  45. var screenCaptureParams = message.data.Deserialize<SerializableScreenCaptureParams>();
  46. if (m_RemoteScreenYTexture == null ||
  47. m_RemoteScreenYTexture.width != screenCaptureParams.width ||
  48. m_RemoteScreenYTexture.height != screenCaptureParams.height)
  49. {
  50. if (m_RemoteScreenYTexture != null)
  51. GameObject.Destroy(m_RemoteScreenYTexture);
  52. m_RemoteScreenYTexture = new Texture2D(
  53. screenCaptureParams.width,
  54. screenCaptureParams.height,
  55. TextureFormat.R8, false, true);
  56. }
  57. if (m_RemoteScreenUVTexture == null ||
  58. m_RemoteScreenUVTexture.width != screenCaptureParams.width ||
  59. m_RemoteScreenUVTexture.height != screenCaptureParams.height)
  60. {
  61. if (m_RemoteScreenUVTexture != null)
  62. GameObject.Destroy(m_RemoteScreenUVTexture);
  63. m_RemoteScreenUVTexture = new Texture2D(
  64. screenCaptureParams.width / 2,
  65. screenCaptureParams.height / 2,
  66. TextureFormat.RG16, false, true);
  67. }
  68. m_ARVideo = m_CachedCamera.GetComponent<ARRemoteVideo>();
  69. if (m_ARVideo == null)
  70. {
  71. m_ARVideo = m_CachedCamera.gameObject.AddComponent<ARRemoteVideo>();
  72. m_ARVideo.clearMaterial = Resources.Load("YUVMaterial", typeof(Material)) as Material;
  73. m_CachedCamera.clearFlags = CameraClearFlags.Depth;
  74. }
  75. m_ARVideo.videoTextureY = m_RemoteScreenYTexture;
  76. m_ARVideo.videoTextureCbCr = m_RemoteScreenUVTexture;
  77. }
  78. public void ScreenCaptureYMessageHandler(MessageEventArgs message)
  79. {
  80. if (m_RemoteScreenYTexture == null)
  81. return;
  82. m_RemoteScreenYTexture.LoadRawTextureData(message.data);
  83. m_RemoteScreenYTexture.Apply();
  84. }
  85. public void ScreenCaptureUVMessageHandler(MessageEventArgs message)
  86. {
  87. //In case of ARCore sending grayscale image, UV data would be null.
  88. if (m_RemoteScreenUVTexture == null || message.data == null)
  89. return;
  90. m_RemoteScreenUVTexture.LoadRawTextureData(message.data);
  91. m_RemoteScreenUVTexture.Apply();
  92. }
  93. public void PlaneAddedMessageHandler(MessageEventArgs message)
  94. {
  95. OnPlaneAdded(message.data.Deserialize<SerializableBoundedPlane>());
  96. }
  97. public void PlaneUpdatedMessageHandler(MessageEventArgs message)
  98. {
  99. OnPlaneUpdated(message.data.Deserialize<SerializableBoundedPlane>());
  100. }
  101. public void PlaneRemovedMessageHandler(MessageEventArgs message)
  102. {
  103. OnPlaneRemoved(message.data.Deserialize<SerializableBoundedPlane>());
  104. }
  105. public void PointCloudMessageHandler(MessageEventArgs message)
  106. {
  107. // Copy to internal buffer
  108. var pointCloud = message.data.Deserialize<SerializablePointCloud>();
  109. if (m_PointCloud == null)
  110. m_PointCloud = new List<Vector3>();
  111. m_PointCloud.Clear();
  112. m_PointCloud.AddRange(pointCloud.asEnumerable);
  113. }
  114. public void LightEstimateMessageHandler(MessageEventArgs message)
  115. {
  116. m_LightEstimate = message.data.Deserialize<SerializableLightEstimate>();
  117. }
  118. public void FrameMessageHandler(MessageEventArgs message)
  119. {
  120. m_Frame = message.data.Deserialize<SerializableFrame>();
  121. }
  122. public void PlayerConnectedMessageHandler(EditorConnection editorConnection, int playerId)
  123. {
  124. this.editorConnection = editorConnection;
  125. m_CurrentPlayerId = playerId;
  126. }
  127. public void PlayerDisconnectedMessageHandler(int playerId)
  128. {
  129. if (m_CurrentPlayerId == playerId)
  130. {
  131. m_CurrentPlayerId = -1;
  132. m_Frame = null;
  133. editorConnection = null;
  134. }
  135. }
  136. void SendToPlayer(System.Guid msgId, object serializableObject)
  137. {
  138. var message = new SerializableSubMessage(msgId, serializableObject);
  139. var bytesToSend = message.SerializeToByteArray();
  140. editorConnection.Send(ARMessageIds.fromEditor, bytesToSend);
  141. }
  142. public void StartRemoteService(Settings settings)
  143. {
  144. sendVideo = m_SendVideo;
  145. var serializedSettings = (SerializableARSettings)settings;
  146. SendToPlayer(ARMessageIds.SubMessageIds.startService, serializedSettings);
  147. IsRemoteServiceRunning = true;
  148. }
  149. public void StopRemoteService()
  150. {
  151. SendToPlayer(ARMessageIds.SubMessageIds.stopService, null);
  152. IsRemoteServiceRunning = false;
  153. }
  154. //
  155. // From the ARInterface
  156. //
  157. public override IEnumerator StartService(Settings settings)
  158. {
  159. IsRunning = true;
  160. return null;
  161. }
  162. public override void StopService()
  163. {
  164. IsRunning = false;
  165. }
  166. public override void SetupCamera(Camera camera)
  167. {
  168. m_CachedCamera = camera;
  169. }
  170. public override bool TryGetUnscaledPose(ref Pose pose)
  171. {
  172. if (m_Frame != null)
  173. {
  174. pose.position = m_Frame.cameraPosition;
  175. pose.rotation = m_Frame.cameraRotation;
  176. return true;
  177. }
  178. return false;
  179. }
  180. public override bool TryGetCameraImage(ref CameraImage cameraImage)
  181. {
  182. return false;
  183. }
  184. public override bool TryGetPointCloud(ref PointCloud pointCloud)
  185. {
  186. if (m_PointCloud == null)
  187. return false;
  188. if (pointCloud.points == null)
  189. pointCloud.points = new List<Vector3>();
  190. pointCloud.points.Clear();
  191. pointCloud.points.AddRange(m_PointCloud);
  192. return true;
  193. }
  194. public override LightEstimate GetLightEstimate()
  195. {
  196. return m_LightEstimate;
  197. }
  198. public override Matrix4x4 GetDisplayTransform()
  199. {
  200. if (m_Frame != null) {
  201. return m_Frame.displayTransform;
  202. } else {
  203. return Matrix4x4.identity;
  204. }
  205. }
  206. public override void Update()
  207. {
  208. }
  209. public override void UpdateCamera(Camera camera)
  210. {
  211. if (m_Frame != null)
  212. {
  213. camera.projectionMatrix = m_Frame.projectionMatrix;
  214. if (m_ARVideo)
  215. {
  216. m_ARVideo.UpdateDisplayTransform(GetDisplayTransform());
  217. }
  218. }
  219. }
  220. }
  221. }
  222. #endif