Search Unity

Re-creating FocusSquare.cs using AR Foundation...(AR Raycast help)

Discussion in 'AR' started by Snaxz, Aug 31, 2018.

  1. Snaxz

    Snaxz

    Joined:
    May 13, 2013
    Posts:
    15
    So in basic I'd like to port the functionality from the FocusSquare.cs demo in the Unity ARkit plugin. At its base i'm just trying to get the raycasting from the camera into AR planes working. Here is where I'm at, i'm pretty sure I have something goofy in how to use the Pose stuff?

    (Code is attached to the AR Camera)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Experimental.XR;
    6. using UnityEngine.XR.ARFoundation;
    7.  
    8.  
    9. public class CameraRaycaster : MonoBehaviour {
    10.  
    11.     public Transform hitBox;
    12.  
    13.     ARSessionOrigin m_SessionOrigin;
    14.  
    15.     static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
    16.  
    17.     void Awake()
    18.     {
    19.         m_SessionOrigin = GetComponent<ARSessionOrigin>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         if (m_SessionOrigin.Raycast(transform.position, s_Hits, TrackableType.PlaneWithinPolygon))
    26.         {
    27.             Pose hitPose = s_Hits[0].pose;
    28.  
    29.             hitBox.transform.position = hitPose.position;
    30.             hitBox.transform.rotation = hitPose.rotation;
    31.  
    32.         }
    33.  
    34.     }
    35.  
    36.  
    37. }
    38.  
     
  2. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    This is my code.

    Code (CSharp):
    1.  
    2.  public void Excute()
    3.     {
    4.         Vector3 tmp_Center = new Vector3(Screen.width / 2, Screen.height / 2, m_FocusParamater.FindingSquareDist);
    5.         if (m_FocusParamater.ARSession.Raycast(tmp_Center, hits, TrackableType.PlaneWithinBounds))
    6.         {
    7.             GetSetState = FocusState.Found;
    8.             Pose tmp_HitPose = hits[0].pose;
    9.             m_FocusParamater.Focus.position = new Vector3(tmp_HitPose.position.x, tmp_HitPose.position.y + 0.01f, tmp_HitPose.position.z);
    10.             m_FocusParamater.Focus.rotation = tmp_HitPose.rotation;
    11.             if (m_FocusParamater.OnFoundCallback != null)
    12.             {
    13.                 m_FocusParamater.OnFoundCallback.Invoke(tmp_HitPose.position);
    14.                 m_Invoke = false;
    15.             }
    16.             Debug.Log("Hit on: " + tmp_HitPose.position);
    17.             return;
    18.         }
    19.  
    20.         GetSetState = FocusState.Finding;
    21.         if (Vector3.Dot(m_FocusParamater.MainCamera.transform.forward, Vector3.down) > 0)
    22.         {
    23.             m_FocusParamater.Focus.position = m_FocusParamater.MainCamera.ScreenToWorldPoint(tmp_Center);
    24.             Vector3 tmp_VecToCamera = m_FocusParamater.Focus.position - m_FocusParamater.MainCamera.transform.position;
    25.             Vector3 vecOrthoghonal = Vector3.Cross(tmp_VecToCamera, Vector3.up);
    26.             Vector3 vecForward = Vector3.Cross(vecOrthoghonal, Vector3.up);
    27.             m_FocusParamater.Focus.rotation = Quaternion.LookRotation(vecForward, Vector3.up);
    28.             if (m_FocusParamater.OnFindingCallback != null && !m_Invoke)
    29.             {
    30.                 m_FocusParamater.OnFindingCallback.Invoke();
    31.                 m_Invoke = true;
    32.             }
    33.         }
    34.     }
    35.  
    36.  
     
  3. Snaxz

    Snaxz

    Joined:
    May 13, 2013
    Posts:
    15
    Thank you! I'll give it a whirl.
     
  4. maurogarcia

    maurogarcia

    Joined:
    Nov 6, 2012
    Posts:
    12
    Hi,
    I would like to know if you could move forward with this topic. can you share an updated code?

    Thanks!!
     
  5. blanxdev

    blanxdev

    Joined:
    Apr 4, 2018
    Posts:
    4
    Would be really cool to find a solution for this as the "FocusSquare.cs" was really practical
     
  6. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    83
    Hi guys. I need to detect an 3D object on my scene using Raycast. In fact, I need to shoot to that object in my game. Did you solve the problem you post? I think that might help.
     
  7. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    You can raycast to the AR plane like this:

    Code (CSharp):
    1.            
    2. Vector3 center = new Vector3(Screen.width / 2, Screen.height / 2, 0f);
    3. Ray ray = Camera.main.ScreenPointToRay(center);
    4. if (m_SessionOrigin.Raycast(ray, s_Hits, TrackableType.PlaneWithinPolygon))
    5. {
    6.       Pose hitPose = s_Hits[0].pose;
    7.       Debug.Log(hitPose.position);
    8. }