ARRemoteEditorInterface.cs 10 KB

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