google-ar / arcore-unity-sdk

ARCore SDK for Unity
https://developers.google.com/ar
Other
1.4k stars 400 forks source link

Session Raycast with actual Ray casting. #99

Open AdiEddNorris opened 6 years ago

AdiEddNorris commented 6 years ago

ARCore Preview 1 allowed Session.Raycast with any given Ray and ARCore Preview 2 removed this functionality for a Unity Camera Space alternative.

Is this functionality available outside of the Session class?

I was planning to build a new Raycast that goes through my relevant planes and performs a standard raycast and wanted to make sure I wasn't wasting my time. (Couldn't find a standard raycast in the ARCoreSDK for Unity.).

Any information would be greatly appreciated...

chaosemer commented 6 years ago

I totally understand where you are coming from here. We'll be adding this in a future release.

pablisho commented 6 years ago

Hi, ARCore SDK for Unity v1.4.0 has been released and Raycast now allows to give it an arbitrary origin and direction.

haroldmthyng commented 5 years ago

Running into an issue with the new Raycast methods. Utilizing method:

bool Raycast(
  Vector3 origin,
  Vector3 direction,
  out TrackableHit hitResult,
  float maxDistance,
  TrackableHitFlags filter
)

outlined at: https://developers.google.com/ar/reference/unity/class/GoogleARCore/Frame#classGoogleARCore_1_1Frame_1ad1bdc454e62b3b7858c978f9b25c44a0

I'm getting an arbitrary error:

EntryPointNotFoundException: ArFrame_hitTestRay
GoogleARCoreInternal.HitTestApi.Raycast (IntPtr frameHandle, Vector3 origin, Vector3 direction, Single maxDistance, TrackableHitFlags filter, System.Collections.Generic.List`1 outHitList) (at Assets/GoogleARCore/SDK/Scripts/Api/Wrappers/HitTestApi.cs:69)
GoogleARCore.Frame.Raycast (Vector3 origin, Vector3 direction, GoogleARCore.TrackableHit& hitResult, Single maxDistance, TrackableHitFlags filter) (at Assets/GoogleARCore/SDK/Scripts/Frame.cs:136)
FocusSquareController.Update () (at Assets/Scripts/FocusSquareController.cs:25)

The method is pretty straightforward. I'm only supplying the camera transform position, compute a forward direction vector, with a 200 meter distance. Doing so fails every frame.

My code is below:

TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
            TrackableHitFlags.FeaturePointWithSurfaceNormal;
Vector3 fwd =camera.transform.TransformDirection(Vector3.forward);

if (Frame.Raycast(camera.transform.position, fwd, out hit, 200f, raycastFilter))
{
    // never reaches here as Raycast always throws an error in this instance.
}

Any insight you may have would be a big help. Camera's transform is updated accordingly. The other Frame.Raycast() method that accepts screen points doesn't throw this error. I'm using arcore-unity-sdk-v1.6.0.

Please let me know.

haroldmthyng commented 5 years ago

Was checking the resolution in #407 as I was running in InstantPreview. But still fails when built to the device.

haroldmthyng commented 5 years ago

Unfortunately I couldn't continue to spend time on this. In my case I was able to just compute the screen point I needed and use the simpler Frame.Raycast method for now just so I could keep going.

McCulloughKN commented 5 years ago

Is this issue still being looked into? I have near identical code as haroldmthyng and the same issue persists for me. Building my app does not fix this either. I have re-cloned the repository and imported it to Unity (current version 2017.4.19f1) as well as used the downloaded package in projects connected to Unity Hub and standalone editors.

chaosemer commented 5 years ago

Could you please share which of these issues you're having?

Thanks!

McCulloughKN commented 5 years ago

Hey Jared,

I modified the HelloARController as a test, I put the offending code between asterisks; I have it hooked up to begin raycasting when a plane is found. This error is getting thrown in editor.

public void Update() {
            _UpdateApplicationLifecycle();

            // Hide snackbar when currently tracking at least one plane.
            Session.GetTrackables<DetectedPlane>(m_AllPlanes);
            bool showSearchingUI = true;
            for (int i = 0; i < m_AllPlanes.Count; i++) {
                if (m_AllPlanes[i].TrackingState == TrackingState.Tracking) {
                    showSearchingUI = false;
                    break;
                }
            }

            SearchingForPlaneUI.SetActive(showSearchingUI);

            //*****************************************************************************************************************************************
            if (searchWithRaycast) {
                TrackableHit hits;
                TrackableHitFlags rayFilter = TrackableHitFlags.PlaneWithinPolygon |
                    TrackableHitFlags.FeaturePointWithSurfaceNormal;
                if (Frame.Raycast(FirstPersonCamera.transform.position, FirstPersonCamera.transform.forward, out hits, 50f, rayFilter)) {
                    canSpawnAndy = true;
                }
            }
            //*******************************************************************************************************************************************

            // If the player has not touched the screen, we are done with this update.
            Touch touch;
            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) {
                return;
            }

            // Raycast against the location the player touched to search for planes.
            TrackableHit hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) {
                // Use hit pose and camera pose to check if hittest is from the
                // back of the plane, if it is, no need to create the anchor.
                if ((hit.Trackable is DetectedPlane) &&
                    Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                        hit.Pose.rotation * Vector3.up) < 0) {
                    Debug.Log("Hit at back of the current DetectedPlane");
                }
                else {
                    if (canSpawnAndy) {
                        // Choose the Andy model for the Trackable that got hit.
                        GameObject prefab;
                        if (hit.Trackable is FeaturePoint) {
                            prefab = AndyPointPrefab;
                        }
                        else {
                            prefab = AndyPlanePrefab;
                        }

                        // Instantiate Andy model at the hit pose.
                        var andyObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation);

                        // Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
                        andyObject.transform.Rotate(0, k_ModelRotation, 0, Space.Self);

                        // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                        // world evolves.
                        var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                        // Make Andy model a child of the anchor.
                        andyObject.transform.parent = anchor.transform;
                    }
                }
            }
        }

As soon as the raycast beings Unity throws this error:

EntryPointNotFoundException: ArFrame_hitTestRay GoogleARCoreInternal.HitTestApi.Raycast (IntPtr frameHandle, Vector3 origin, Vector3 direction, Single maxDistance, TrackableHitFlags filter, System.Collections.Generic.List`1 outHitList) (at Assets/GoogleARCore/SDK/Scripts/Api/Wrappers/HitTestApi.cs:69) GoogleARCore.Frame.Raycast (Vector3 origin, Vector3 direction, GoogleARCore.TrackableHit& hitResult, Single maxDistance, TrackableHitFlags filter) (at Assets/GoogleARCore/SDK/Scripts/Frame.cs:136) GoogleARCore.Examples.HelloAR.HelloARController.Update () (at Assets/GoogleARCore/Examples/HelloAR/Scripts/HelloARController.cs:103)

Thank you for the response! Krista

chaosemer commented 5 years ago

Thanks, that is unexpected. Not all functionality is expected to work in-editor, but no exceptions should be thrown. I will add this to our internal bug log.

KAbakumov commented 4 years ago

Any updates on this issue? I am having the same problem, same exception when trying to use Frame.Raycast function

chaosemer commented 4 years ago

Sorry for not updating here.

I believe we fixed this in June of 2019. However, you still are encountering the issue. Please capture a bug report via Help > Capture ARCore Bug Report and attach it to this thread, that will help us understand how to repro the issue.

Edit: A teammate of mine looked internally and found that while we had properly hooked up the internal function ArFrame_hitTest in June, we had not yet hooked up ArFrame_hitTestRay. We're hooking that up now, the fix should come in an upcoming release.

KAbakumov commented 4 years ago

@chaosemer Thank you for the update! Also, I need to note that the issue is reproducible in Unity Editor, however it works on my Android device.

chaosemer commented 4 years ago

@KAbakumov That's aligned with the bug we fixed internally. The fix was submitted earlier today; it'll come out in an upcoming release.