ARFocusSquare.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.iOS;
  5. namespace UnityARInterface
  6. {
  7. public class ARFocusSquare : MonoBehaviour {
  8. public enum FocusState {
  9. Initializing,
  10. Finding,
  11. Found
  12. }
  13. public GameObject findingSquare;
  14. public GameObject foundSquare;
  15. //for editor version
  16. public float maxRayDistance = 30.0f;
  17. public LayerMask collisionLayerMask;
  18. public float findingSquareDist = 0.5f;
  19. private FocusState squareState;
  20. public FocusState SquareState {
  21. get {
  22. return squareState;
  23. }
  24. set {
  25. squareState = value;
  26. foundSquare.SetActive (squareState == FocusState.Found);
  27. findingSquare.SetActive (squareState != FocusState.Found);
  28. }
  29. }
  30. bool trackingInitialized;
  31. // Use this for initialization
  32. void Start () {
  33. int layerIndex = LayerMask.NameToLayer("ARGameObject");
  34. collisionLayerMask = 1 << layerIndex;
  35. SquareState = FocusState.Initializing;
  36. trackingInitialized = true;
  37. }
  38. // Update is called once per frame
  39. void Update () {
  40. //use center of screen for focusing
  41. Vector3 center = new Vector3(Screen.width/2, Screen.height/2, findingSquareDist);
  42. Ray ray = Camera.main.ScreenPointToRay (center);
  43. RaycastHit hit;
  44. //we'll try to hit one of the plane collider gameobjects that were generated by the plugin
  45. //effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
  46. if (Physics.Raycast (ray, out hit, maxRayDistance, collisionLayerMask)) {
  47. //we're going to get the position from the contact point
  48. foundSquare.transform.position = hit.point;
  49. Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", foundSquare.transform.position.x, foundSquare.transform.position.y, foundSquare.transform.position.z));
  50. //and the rotation from the transform of the plane collider
  51. SquareState = FocusState.Found;
  52. foundSquare.transform.rotation = hit.transform.rotation;
  53. return;
  54. }
  55. //if you got here, we have not found a plane, so if camera is facing below horizon, display the focus "finding" square
  56. if (trackingInitialized) {
  57. SquareState = FocusState.Finding;
  58. //check camera forward is facing downward
  59. if (Vector3.Dot(Camera.main.transform.forward, Vector3.down) > 0)
  60. {
  61. //position the focus finding square a distance from camera and facing up
  62. findingSquare.transform.position = Camera.main.ScreenToWorldPoint(center);
  63. //vector from camera to focussquare
  64. Vector3 vecToCamera = findingSquare.transform.position - Camera.main.transform.position;
  65. //find vector that is orthogonal to camera vector and up vector
  66. Vector3 vecOrthogonal = Vector3.Cross(vecToCamera, Vector3.up);
  67. //find vector orthogonal to both above and up vector to find the forward vector in basis function
  68. Vector3 vecForward = Vector3.Cross(vecOrthogonal, Vector3.up);
  69. findingSquare.transform.rotation = Quaternion.LookRotation(vecForward,Vector3.up);
  70. }
  71. else
  72. {
  73. //we will not display finding square if camera is not facing below horizon
  74. findingSquare.SetActive(false);
  75. }
  76. }
  77. }
  78. }
  79. }