AREditorInterface.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityARInterface
  6. {
  7. public class AREditorInterface : ARInterface
  8. {
  9. float m_LastTime;
  10. enum State
  11. {
  12. Uninitialized,
  13. Initialized,
  14. WaitingToAddPlane1,
  15. WaitingToAddPlane2,
  16. Finished
  17. }
  18. State m_State;
  19. BoundedPlane[] m_FakePlanes;
  20. Pose m_CameraPose;
  21. bool m_WasMouseDownLastFrame;
  22. Vector3 m_LastMousePosition;
  23. Vector3 m_EulerAngles;
  24. Vector3[] m_PointCloud;
  25. public override IEnumerator StartService(Settings settings)
  26. {
  27. m_CameraPose = Pose.identity;
  28. m_CameraPose.position.Set(0, 0, 0);
  29. m_LastTime = Time.time;
  30. m_State = State.Initialized;
  31. m_FakePlanes = new BoundedPlane[2];
  32. m_FakePlanes[0] = new BoundedPlane()
  33. {
  34. id = "0x1",
  35. extents = new Vector2(2.2f, 2f),
  36. //extents = new Vector2(1f, 1f),
  37. rotation = Quaternion.AngleAxis(60f, Vector3.up),
  38. center = new Vector3(2f, -1f, 3f)
  39. //center = new Vector3(1f, 1f, 1f)
  40. };
  41. m_FakePlanes[1] = new BoundedPlane()
  42. {
  43. id = "0x2",
  44. extents = new Vector2(2f, 2f),
  45. rotation = Quaternion.AngleAxis(200f, Vector3.up),
  46. center = new Vector3(3f, 1f, 3f)
  47. };
  48. m_PointCloud = new Vector3[20];
  49. for (int i = 0; i < 20; ++i)
  50. {
  51. m_PointCloud[i] = new Vector3(
  52. UnityEngine.Random.Range(-2f, 2f),
  53. UnityEngine.Random.Range(-.5f, .5f),
  54. UnityEngine.Random.Range(-2f, 2f));
  55. }
  56. IsRunning = true;
  57. return null;
  58. }
  59. public override void StopService()
  60. {
  61. IsRunning = false;
  62. }
  63. public override bool TryGetUnscaledPose(ref Pose pose)
  64. {
  65. pose = m_CameraPose;
  66. return true;
  67. }
  68. public override bool TryGetCameraImage(ref CameraImage cameraImage)
  69. {
  70. return false;
  71. }
  72. public override void SetupCamera(Camera camera)
  73. {
  74. }
  75. public override bool TryGetPointCloud(ref PointCloud pointCloud)
  76. {
  77. if (pointCloud.points == null)
  78. pointCloud.points = new List<Vector3>();
  79. pointCloud.points.Clear();
  80. pointCloud.points.AddRange(m_PointCloud);
  81. return true;
  82. }
  83. public override LightEstimate GetLightEstimate()
  84. {
  85. return new LightEstimate()
  86. {
  87. capabilities = LightEstimateCapabilities.None
  88. };
  89. }
  90. public override Matrix4x4 GetDisplayTransform()
  91. {
  92. return Matrix4x4.identity;
  93. }
  94. public override void UpdateCamera(Camera camera)
  95. {
  96. float speed = camera.transform.parent.localScale.x / 10f;
  97. float turnSpeed = 10f;
  98. var forward = m_CameraPose.rotation * Vector3.forward;
  99. var right = m_CameraPose.rotation * Vector3.right;
  100. var up = m_CameraPose.rotation * Vector3.up;
  101. if (Input.GetKey(KeyCode.W))
  102. m_CameraPose.position += forward * Time.deltaTime * speed;
  103. if (Input.GetKey(KeyCode.S))
  104. m_CameraPose.position -= forward * Time.deltaTime * speed;
  105. if (Input.GetKey(KeyCode.A))
  106. m_CameraPose.position -= right * Time.deltaTime * speed;
  107. if (Input.GetKey(KeyCode.D))
  108. m_CameraPose.position += right * Time.deltaTime * speed;
  109. if (Input.GetKey(KeyCode.Q))
  110. m_CameraPose.position += up * Time.deltaTime * speed;
  111. if (Input.GetKey(KeyCode.Z))
  112. m_CameraPose.position -= up * Time.deltaTime * speed;
  113. if (Input.GetMouseButton(1))
  114. {
  115. if (!m_WasMouseDownLastFrame)
  116. m_LastMousePosition = Input.mousePosition;
  117. var deltaPosition = Input.mousePosition - m_LastMousePosition;
  118. m_EulerAngles.y += Time.deltaTime * turnSpeed * deltaPosition.x;
  119. m_EulerAngles.x -= Time.deltaTime * turnSpeed * deltaPosition.y;
  120. m_CameraPose.rotation = Quaternion.Euler(m_EulerAngles);
  121. m_LastMousePosition = Input.mousePosition;
  122. m_WasMouseDownLastFrame = true;
  123. }
  124. else
  125. {
  126. m_WasMouseDownLastFrame = false;
  127. }
  128. }
  129. public override void Update()
  130. {
  131. switch (m_State)
  132. {
  133. case State.Initialized:
  134. m_State = State.WaitingToAddPlane1;
  135. m_LastTime = Time.time;
  136. break;
  137. case State.WaitingToAddPlane1:
  138. if (Time.time - m_LastTime > 1f)
  139. {
  140. OnPlaneAdded(m_FakePlanes[0]);
  141. m_LastTime = Time.time;
  142. m_State = State.WaitingToAddPlane2;
  143. }
  144. break;
  145. case State.WaitingToAddPlane2:
  146. if (Time.time - m_LastTime > 1f)
  147. {
  148. OnPlaneAdded(m_FakePlanes[1]);
  149. m_LastTime = Time.time;
  150. m_State = State.Finished;
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. }