SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9 KiB

4 years ago
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.XR.ARFoundation;
  4. using UnityEngine.XR.ARSubsystems;
  5. using UnityEngine.Experimental;
  6. // This script is inspired by:
  7. // https://www.dropbox.com/s/n1i5v200npx1mf1/AutoPlaceItem.cs from video 7 of udemy course:
  8. // https://www.udemy.com/create-ar-placement-app-and-full-template-for-photo-app/
  9. // same video available on youtube:
  10. // https://www.youtube.com/watch?v=x08UU-I8eZ8&list=PLw3UgsOGHn4loDyxHG75eJxSnxxVgB-Yb&index=5&t=0s
  11. // arfoundation-examples PlaceOnPlace.cs with GetMouseButtonDown instead of GetMouseButton (was spawning 10 cubes with one click)
  12. // video Getting Started With ARFoundation in Unity (ARKit, ARCore): https://www.youtube.com/watch?v=Ml2UakwRxjk&list=WL&index=4
  13. [RequireComponent(typeof(ARRaycastManager))]
  14. public class TapTest : MonoBehaviour
  15. {
  16. public GameObject objectToPlace;
  17. public GameObject placementIndicator;
  18. private ARRaycastManager m_RaycastManager;
  19. private Pose m_PlacementPose;
  20. private bool m_PlacementPoseIsValid = false;
  21. static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
  22. public LayerMask layerMask;
  23. public GameObject[] TestingGround;
  24. void Awake()
  25. {
  26. #if UNITY_EDITOR
  27. for (int i = 0; i < TestingGround.Length; i++)
  28. {
  29. TestingGround[i].SetActive(true);
  30. }
  31. #else
  32. for (int i = 0; i < TestingGround.Length; i++)
  33. {
  34. TestingGround[i].SetActive(false);
  35. }
  36. #endif
  37. m_RaycastManager = GetComponent<ARRaycastManager>();
  38. }
  39. bool TryGetTouchPosition(out Vector2 touchPosition)
  40. {
  41. #if UNITY_EDITOR
  42. if (Input.GetMouseButtonDown(0))
  43. {
  44. var mousePosition = Input.mousePosition;
  45. touchPosition = new Vector2(mousePosition.x, mousePosition.y);
  46. return true;
  47. }
  48. #else
  49. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  50. {
  51. touchPosition = Input.GetTouch(0).position;
  52. return true;
  53. }
  54. #endif
  55. touchPosition = default;
  56. return false;
  57. }
  58. void Update()
  59. {
  60. TryUpdatePlacementPose();
  61. UpdatePlacementIndicator();
  62. if (!TryGetTouchPosition(out Vector2 touchPosition))
  63. return;
  64. // we actually ignore touchPosition, and always use screenCenter for the ray
  65. if (m_PlacementPoseIsValid)
  66. {
  67. PlaceObject();
  68. }
  69. }
  70. private void PlaceObject()
  71. {
  72. Instantiate(objectToPlace, m_PlacementPose.position, m_PlacementPose.rotation);
  73. }
  74. private void UpdatePlacementIndicator()
  75. {
  76. if (m_PlacementPoseIsValid)
  77. {
  78. placementIndicator.SetActive(true);
  79. placementIndicator.transform.SetPositionAndRotation(m_PlacementPose.position, m_PlacementPose.rotation);
  80. }
  81. else
  82. {
  83. placementIndicator.SetActive(false);
  84. }
  85. }
  86. private void UpdatePlacementPose(Vector3 hitPoint)
  87. {
  88. m_PlacementPose.position = hitPoint;
  89. var cameraForward = Camera.main.transform.forward;
  90. var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
  91. m_PlacementPose.rotation = Quaternion.LookRotation(cameraBearing);
  92. }
  93. private void TryUpdatePlacementPose()
  94. {
  95. #if UNITY_EDITOR
  96. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Camera.main.pixelWidth * 0.5f, Camera.main.pixelHeight * 0.5f, 0f));
  97. RaycastHit hit;
  98. m_PlacementPoseIsValid = Physics.Raycast(ray, out hit, 500f, layerMask);
  99. if (m_PlacementPoseIsValid)
  100. {
  101. UpdatePlacementPose(hit.point);
  102. }
  103. #else
  104. var screenCenter = Camera.main.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
  105. m_RaycastManager.Raycast(screenCenter, s_Hits, TrackableType.Planes);
  106. m_PlacementPoseIsValid = s_Hits.Count > 0;
  107. if (m_PlacementPoseIsValid)
  108. {
  109. UpdatePlacementPose(s_Hits[0].pose.position);
  110. }
  111. #endif
  112. }
  113. }