ARKitRemoteConnection.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using UnityEngine;
  2. using UnityEngine.Networking.PlayerConnection;
  3. using System.Text;
  4. using Utils;
  5. #if UNITY_EDITOR
  6. using UnityEditor.Networking.PlayerConnection;
  7. namespace UnityEngine.XR.iOS
  8. {
  9. public class ARKitRemoteConnection : MonoBehaviour
  10. {
  11. [Header("AR Config Options")]
  12. public UnityARAlignment startAlignment = UnityARAlignment.UnityARAlignmentGravity;
  13. public UnityARPlaneDetection planeDetection = UnityARPlaneDetection.Horizontal;
  14. public bool getPointCloud = true;
  15. public bool enableLightEstimation = true;
  16. [Header("Run Options")]
  17. public bool resetTracking = true;
  18. public bool removeExistingAnchors = true;
  19. EditorConnection editorConnection ;
  20. int currentPlayerID = -1;
  21. string guimessage = "none";
  22. Texture2D remoteScreenYTex;
  23. Texture2D remoteScreenUVTex;
  24. bool bTexturesInitialized;
  25. // Use this for initialization
  26. void Start () {
  27. bTexturesInitialized = false;
  28. editorConnection = EditorConnection.instance;
  29. editorConnection.Initialize ();
  30. editorConnection.RegisterConnection (PlayerConnected);
  31. editorConnection.RegisterDisconnection (PlayerDisconnected);
  32. editorConnection.Register (ConnectionMessageIds.updateCameraFrameMsgId, UpdateCameraFrame);
  33. editorConnection.Register (ConnectionMessageIds.addPlaneAnchorMsgeId, AddPlaneAnchor);
  34. editorConnection.Register (ConnectionMessageIds.updatePlaneAnchorMsgeId, UpdatePlaneAnchor);
  35. editorConnection.Register (ConnectionMessageIds.removePlaneAnchorMsgeId, RemovePlaneAnchor);
  36. editorConnection.Register (ConnectionMessageIds.screenCaptureYMsgId, ReceiveRemoteScreenYTex);
  37. editorConnection.Register (ConnectionMessageIds.screenCaptureUVMsgId, ReceiveRemoteScreenUVTex);
  38. }
  39. void PlayerConnected(int playerID)
  40. {
  41. currentPlayerID = playerID;
  42. }
  43. void OnGUI()
  44. {
  45. if (!bTexturesInitialized)
  46. {
  47. if (currentPlayerID != -1) {
  48. guimessage = "Connected to ARKit Remote device : " + currentPlayerID.ToString ();
  49. if (GUI.Button (new Rect ((Screen.width / 2) - 200, (Screen.height / 2) - 200, 400, 100), "Start Remote ARKit Session"))
  50. {
  51. SendInitToPlayer ();
  52. }
  53. }
  54. else
  55. {
  56. guimessage = "Please connect to player in the console menu";
  57. }
  58. GUI.Box (new Rect ((Screen.width / 2) - 200, (Screen.height / 2) + 100, 400, 50), guimessage);
  59. }
  60. }
  61. void PlayerDisconnected(int playerID)
  62. {
  63. if (currentPlayerID == playerID) {
  64. currentPlayerID = -1;
  65. }
  66. }
  67. void OnDestroy()
  68. {
  69. #if UNITY_2017_1_OR_NEWER
  70. if(editorConnection != null) {
  71. editorConnection.DisconnectAll ();
  72. }
  73. #endif
  74. }
  75. void InitializeTextures(UnityARCamera camera)
  76. {
  77. int yWidth = camera.videoParams.yWidth;
  78. int yHeight = camera.videoParams.yHeight;
  79. int uvWidth = yWidth / 2;
  80. int uvHeight = yHeight / 2;
  81. if (remoteScreenYTex == null || remoteScreenYTex.width != yWidth || remoteScreenYTex.height != yHeight) {
  82. if (remoteScreenYTex) {
  83. Destroy (remoteScreenYTex);
  84. }
  85. remoteScreenYTex = new Texture2D (yWidth, yHeight, TextureFormat.R8, false, true);
  86. }
  87. if (remoteScreenUVTex == null || remoteScreenUVTex.width != uvWidth || remoteScreenUVTex.height != uvHeight) {
  88. if (remoteScreenUVTex) {
  89. Destroy (remoteScreenUVTex);
  90. }
  91. remoteScreenUVTex = new Texture2D (uvWidth, uvHeight, TextureFormat.RG16, false, true);
  92. }
  93. bTexturesInitialized = true;
  94. }
  95. void UpdateCameraFrame(MessageEventArgs mea)
  96. {
  97. serializableUnityARCamera serCamera = mea.data.Deserialize<serializableUnityARCamera> ();
  98. UnityARCamera scamera = new UnityARCamera ();
  99. scamera = serCamera;
  100. InitializeTextures (scamera);
  101. UnityARSessionNativeInterface.SetStaticCamera (scamera);
  102. UnityARSessionNativeInterface.RunFrameUpdateCallbacks ();
  103. }
  104. void AddPlaneAnchor(MessageEventArgs mea)
  105. {
  106. serializableUnityARPlaneAnchor serPlaneAnchor = mea.data.Deserialize<serializableUnityARPlaneAnchor> ();
  107. ARPlaneAnchor arPlaneAnchor = serPlaneAnchor;
  108. UnityARSessionNativeInterface.RunAddAnchorCallbacks (arPlaneAnchor);
  109. }
  110. void UpdatePlaneAnchor(MessageEventArgs mea)
  111. {
  112. serializableUnityARPlaneAnchor serPlaneAnchor = mea.data.Deserialize<serializableUnityARPlaneAnchor> ();
  113. ARPlaneAnchor arPlaneAnchor = serPlaneAnchor;
  114. UnityARSessionNativeInterface.RunUpdateAnchorCallbacks (arPlaneAnchor);
  115. }
  116. void RemovePlaneAnchor(MessageEventArgs mea)
  117. {
  118. serializableUnityARPlaneAnchor serPlaneAnchor = mea.data.Deserialize<serializableUnityARPlaneAnchor> ();
  119. ARPlaneAnchor arPlaneAnchor = serPlaneAnchor;
  120. UnityARSessionNativeInterface.RunRemoveAnchorCallbacks (arPlaneAnchor);
  121. }
  122. void ReceiveRemoteScreenYTex(MessageEventArgs mea)
  123. {
  124. if (!bTexturesInitialized)
  125. return;
  126. remoteScreenYTex.LoadRawTextureData(mea.data);
  127. remoteScreenYTex.Apply ();
  128. UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
  129. if (arVideo) {
  130. arVideo.SetYTexure(remoteScreenYTex);
  131. }
  132. }
  133. void ReceiveRemoteScreenUVTex(MessageEventArgs mea)
  134. {
  135. if (!bTexturesInitialized)
  136. return;
  137. remoteScreenUVTex.LoadRawTextureData(mea.data);
  138. remoteScreenUVTex.Apply ();
  139. UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
  140. if (arVideo) {
  141. arVideo.SetUVTexure(remoteScreenUVTex);
  142. }
  143. }
  144. void SendInitToPlayer()
  145. {
  146. serializableFromEditorMessage sfem = new serializableFromEditorMessage ();
  147. sfem.subMessageId = SubMessageIds.editorInitARKit;
  148. serializableARSessionConfiguration ssc = new serializableARSessionConfiguration (startAlignment, planeDetection, getPointCloud, enableLightEstimation);
  149. UnityARSessionRunOption roTracking = resetTracking ? UnityARSessionRunOption.ARSessionRunOptionResetTracking : 0;
  150. UnityARSessionRunOption roAnchors = removeExistingAnchors ? UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors : 0;
  151. sfem.arkitConfigMsg = new serializableARKitInit (ssc, roTracking | roAnchors);
  152. SendToPlayer (ConnectionMessageIds.fromEditorARKitSessionMsgId, sfem);
  153. }
  154. void SendToPlayer(System.Guid msgId, byte[] data)
  155. {
  156. editorConnection.Send (msgId, data);
  157. }
  158. public void SendToPlayer(System.Guid msgId, object serializableObject)
  159. {
  160. byte[] arrayToSend = serializableObject.SerializeToByteArray ();
  161. SendToPlayer (msgId, arrayToSend);
  162. }
  163. // Update is called once per frame
  164. void Update () {
  165. }
  166. }
  167. }
  168. #endif