Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.72 KB | None | 0 0
  1. namespace GoogleARCore.Examples.HelloAR
  2. {
  3. //
  4. #if UNITY_EDITOR
  5. // NOTE:
  6. // - InstantPreviewInput does not support `deltaPosition`.
  7. // - InstantPreviewInput does not support input from
  8. // multiple simultaneous screen touches.
  9. // - InstantPreviewInput might miss frames. A steady stream
  10. // of touch events across frames while holding your finger
  11. // on the screen is not guaranteed.
  12. // - InstantPreviewInput does not generate Unity UI event system
  13. // events from device touches. Use mouse/keyboard in the editor
  14. // instead.
  15. using Input = InstantPreviewInput;
  16. #endif
  17.  
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using UnityEngine;
  21. using GoogleARCore;
  22. //
  23. public enum ControllerState
  24. {
  25. Placing,
  26. Scaling,
  27. Rotating,
  28. Targeting,
  29. Playing
  30. }
  31.  
  32.  
  33.  
  34. public class ARController : MonoBehaviour
  35. {
  36.  
  37. //
  38. [SerializeField]
  39. private float ScaleSpeed = 0.05f;
  40. [SerializeField]
  41. private float RotateSpeed = 0.5f;
  42. //
  43. [SerializeField]
  44. private Camera FirstpersonCam;
  45. private static GameObject Model;
  46. public static ControllerState controllerstate;
  47. [SerializeField]
  48. private GameObject CurrentplacedObject;
  49. [SerializeField]
  50. GameObject GridViewer;
  51. //
  52. Vector2 FirstPos;
  53. Vector2 LastPos;
  54. //
  55. [SerializeField]
  56. GameObject BuildCanvas;
  57. [SerializeField]
  58. GameObject SelectBuildCanvas;
  59. [SerializeField]
  60. GameObject ObjectEditCanvas;
  61. [SerializeField]
  62. GameObject ScalingFeedback;
  63. [SerializeField]
  64. GameObject RotateFeedback;
  65.  
  66. public bool canPlace = false;
  67.  
  68. private void Awake()
  69. {
  70. SceneHandler.SwitchToPlayMode += TurnOffBuildGrid;
  71. }
  72.  
  73. private void Start()
  74. {
  75. canPlace = false;
  76. }
  77. void Update()
  78. {
  79. if (SelectBuildCanvas.activeSelf || ObjectEditCanvas.activeSelf)
  80. {
  81. canPlace = false;
  82. }
  83. else
  84. {
  85. StartCoroutine(SetBuildMode());
  86. }
  87.  
  88. SessionHandling();
  89. }
  90.  
  91. private void SessionHandling()
  92. {
  93. if (Session.Status != SessionStatus.Tracking)
  94. {
  95. return;
  96. }
  97. //
  98. if (controllerstate == ControllerState.Placing && canPlace)
  99. {
  100. Debug.Log("Allowed");
  101. //BuildCanvas.SetActive(true);
  102. ObjectEditCanvas.SetActive(false);
  103. //
  104. ScreenInputPlacing();
  105. GridViewer.SetActive(true);
  106. }
  107. //
  108. if (controllerstate != ControllerState.Placing && controllerstate != ControllerState.Targeting && controllerstate != ControllerState.Playing)
  109. {
  110. BuildCanvas.SetActive(false);
  111. ObjectEditCanvas.SetActive(true);
  112. if (Input.touchCount == 1)
  113. {
  114. controllerstate = ControllerState.Rotating;
  115. ScreenInputRotating();
  116. GridViewer.SetActive(false);
  117. ScalingFeedback.SetActive(false);
  118. RotateFeedback.SetActive(true);
  119.  
  120. }
  121. if(Input.touchCount == 2)
  122. {
  123. controllerstate = ControllerState.Scaling;
  124. //
  125. ScreenInputScaling();
  126. GridViewer.SetActive(false);
  127. ScalingFeedback.SetActive(true);
  128. RotateFeedback.SetActive(false);
  129. }
  130. if(Input.touchCount == 0)
  131. {
  132. ScalingFeedback.SetActive(true);
  133. RotateFeedback.SetActive(true);
  134. }
  135. }
  136. }
  137. public IEnumerator SetBuildMode()
  138. {
  139. yield return new WaitForSeconds(1f);
  140. canPlace = true;
  141. }
  142. #region Input Voids
  143. void ScreenInputPlacing()
  144. {
  145. Touch touch;
  146. if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
  147. {
  148. return;
  149. }
  150.  
  151. TrackableHit hit;
  152. TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
  153. TrackableHitFlags.FeaturePointWithSurfaceNormal;
  154.  
  155. if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
  156. {
  157. if ((hit.Trackable is DetectedPlane) &&
  158. Vector3.Dot(FirstpersonCam.transform.position - hit.Pose.position,
  159. hit.Pose.rotation * Vector3.up) < 0)
  160. {
  161. Debug.Log("Hit at back of the current DetectedPlane");
  162. }
  163. else
  164. {
  165.  
  166. GameObject SpawnedObject = Instantiate(Model, hit.Pose.position, hit.Pose.rotation);
  167. SpawnedObject.transform.Rotate(0, 180, 0, Space.Self);
  168. CurrentplacedObject = SpawnedObject;
  169. ConsoleScript.Instance.SetFeedback(MessageType.BuildMessage, CurrentplacedObject.name);
  170. //
  171. Anchor anchor = hit.Trackable.CreateAnchor(hit.Pose);
  172. SpawnedObject.transform.parent = anchor.transform;
  173. //Switch Editor State to scaling
  174. controllerstate = ControllerState.Rotating;
  175. //
  176.  
  177. }
  178. }
  179. }
  180.  
  181. void ScreenInputScaling()
  182. {
  183. if (Input.touchCount == 2)
  184. {
  185. // Store both touches.
  186. Touch touchZero = Input.GetTouch(0);
  187. Touch touchOne = Input.GetTouch(1);
  188.  
  189. // Find the position in the previous frame of each touch.
  190. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
  191. Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
  192.  
  193. // Find the magnitude of the vector (the distance) between the touches in each frame.
  194. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
  195. float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
  196.  
  197. // Find the difference in the distances between each frame.
  198. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
  199. //
  200.  
  201. CurrentplacedObject.transform.localScale += new Vector3(CurrentplacedObject.transform.localScale.x * deltaMagnitudeDiff * ScaleSpeed, CurrentplacedObject.transform.localScale.y * deltaMagnitudeDiff * ScaleSpeed, CurrentplacedObject.transform.localScale.z * deltaMagnitudeDiff * ScaleSpeed);
  202.  
  203. // Clamp the field of view to make sure it's between 0 and 180.
  204. FirstpersonCam.fieldOfView = Mathf.Clamp(FirstpersonCam.fieldOfView, 0.1f, 179.9f);
  205. }
  206.  
  207. }
  208.  
  209. void ScreenInputRotating()
  210. {
  211. if (Input.touchCount == 1)
  212. {
  213. Touch touch = Input.GetTouch(0);
  214. if (touch.phase == TouchPhase.Began)
  215. {
  216. Debug.Log("began");
  217. FirstPos = touch.position;
  218. LastPos = touch.position;
  219. }
  220. if (touch.phase == TouchPhase.Moved)
  221. {
  222. LastPos = touch.position;
  223. if (FirstPos.x < LastPos.x)
  224. {
  225. Debug.Log("Swiping right");
  226. CurrentplacedObject.transform.Rotate(new Vector3(0, RotateSpeed, 0));
  227.  
  228. }
  229. else if (LastPos.x < FirstPos.x)
  230. {
  231. Debug.Log("Swiping Left");
  232. CurrentplacedObject.transform.Rotate(new Vector3(0, -RotateSpeed, 0));
  233.  
  234. }
  235. }
  236. }
  237. }
  238. #endregion
  239.  
  240. //
  241. public void TurnOffBuildGrid()
  242. {
  243. GridViewer.SetActive(false);
  244. controllerstate = ControllerState.Playing;
  245. }
  246.  
  247. //Static void to change model from another script
  248. public static void ChangeModel(GameObject modelpar)
  249. {
  250. Model = modelpar;
  251. }
  252.  
  253. public static void ChangeToBuild()
  254. {
  255. controllerstate = ControllerState.Placing;
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement