Log in or register to post comments

Raycast in AR Camera

September 11, 2011 - 11:06pm #1

Hey guys, we are having a bit of trouble raycasting against an AR camera at the moment.

The problem stems from the fact you fudge the gl viewport in TrackerBehaviour.cs

    // Sets the viewport right before Unity renders 3D content.
    void OnPreRender()
    {
        // Don't set the viewport when running in the editor:
        if (!Application.isEditor)
        {
            GL.Viewport(mViewportRect);
        }
    }

Obviously this throws out all the camera based ray functions as they cannot take into account this viewport fudge.

        public Ray ScreenPointToRay(Vector3 position);
        public Vector3 ScreenToViewportPoint(Vector3 position);
        public Vector3 ScreenToWorldPoint(Vector3 position);
        public Ray ViewportPointToRay(Vector3 position);
        public Vector3 ViewportToScreenPoint(Vector3 position);
        public Vector3 ViewportToWorldPoint(Vector3 position);
        public Vector3 WorldToScreenPoint(Vector3 position);
        public Vector3 WorldToViewportPoint(Vector3 position);

Ideally the viewport fudge would be done with Camera.rect, but we haven't found a way of replicating it yet.

Any ideas how we do accurate ray casts against an AR Camera?

Thanks

Dan

Raycast in AR Camera

January 22, 2016 - 12:00am #11

Hello all,

 

I have attached the below code to the AR Camera as to detect the different augmentations and drag them using touch input.

This piece of code works fine with default camera but with the AR Camera and touch input, this doesn't seem to work.

Also I have a specific doubt. The AR Camera prefab that comes with Vuforia has a child camera tagged by default as Main Camera.

So being a newbie to Unity and Vuforia, I am not able to figure out to which of these should i attach my codes. I have tried changing the layer of AR Camera, its child and the objects on the scene to a separate layer(eg layer 10). but it doesnt seem to work too

Hoping to receive some inputs.

using UnityEngine;
using Vuforia;
using System.Collections;
using System.IO;
using UnityEngine.UI;

public class trial : MonoBehaviour {
 
 private GameObject focusObj = null;
 private float focusx;
 private float focusy;
 private float focusz;


 void Update()
 { 

   if(Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Began)
   {
    focusObj = null;
    Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    RaycastHit hit;
    
    if(Physics.Raycast(ray, out hit, Mathf.Infinity))
     
    {
     focusObj=hit.transform.gameObject;
    }
   }
   
   
   if(focusObj && Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Moved)
   {
    focusx= Input.GetTouch(0).deltaPosition.x/500;
    focusz= 0;
    focusy= Input.GetTouch(0).deltaPosition.y/500;
    
    Vector3 pos =  new Vector3(focusx, focusz, focusy);
    focusObj.transform.position += pos;
    
   
   }
   if(focusObj && Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Ended)
   {
    focusObj=null;
   }
 
 }
}

 

Re: Raycast in AR Camera

April 18, 2012 - 2:00pm #10

Nevermind, this was an amateur mistake. I just had the max distance for Raycast set too low. Thanks for the reply

Re: Raycast in AR Camera

April 18, 2012 - 1:10pm #9

Yes the raycasting issue has been addressed.

How are you calling Raycast? - and what is the threshold distance that you are able to successfully cast to?

Re: Raycast in AR Camera

April 18, 2012 - 12:28pm #8

Hi,

I think I might be coming across the same problem as this thread mentions. The raycast works fine at short distances, but when the camera moves a significant amount away from the object, the raycast never hits the object. I tried to implement these changes, but am unable to find the file TrackerBehaviour.cs:InitializeProjection

The closest thing I can find is QCARBehavior.cs:UpdateProjection, which looks like it deals with similar calculations. Have these changes already been incorporated into the newest version of the SDK? If so, would there be another reason that raycast is failing at farther distances?

Re: Raycast in AR Camera

September 15, 2011 - 11:33pm #7
ksiva wrote:

Hey Dan, let's give this a try:

Comment out the call to GL.Viewport(mViewportRect);

Add the following just before the projection matrix is set:

private void InitializeProjection()
{
    ....

    float viewportDistort = Screen.width / (mViewportRect.width / mViewportRect.height * Screen.height);
    projectionMatrix[5] *= viewportDistort;

    this.camera.projectionMatrix = projectionMatrix;

    ...
}

Let me know if that helps!

- Kim

Hi Kim,

I am working with Dan on this project. We actually ended up doing pretty much the same thing. We turned off the GL.Viewport call and altered the Y scale of the cameras projection matrix. This is what we did:

Matrix4x4 viewportMatrix = Matrix4x4.identity;
viewportMatrix.m11 = 1.0f + (Mathf.Abs((yOffset * 2.0f) / (Screen.height)));    
this.camera.projectionMatrix = viewportMatrix * projectionMatrix;

Though, we can probably drop the matrix mult. Thanks for the confirmation. I can also confirm that your code also fixes the issue, for those who might have the same problem.

Cheers,
Wes

Re: Raycast in AR Camera

September 15, 2011 - 11:32pm #6

Thanks Kim, we ended up with something very similar.

Now all our raycasts work! Woot.

Thanks again

Re: Raycast in AR Camera

September 14, 2011 - 9:12pm #5

Hey Dan, let's give this a try:

Comment out the call to GL.Viewport(mViewportRect);

Add the following just before the projection matrix is set:

private void InitializeProjection()
{
    ....

    float viewportDistort = Screen.width / (mViewportRect.width / mViewportRect.height * Screen.height);
    projectionMatrix[5] *= viewportDistort;

    this.camera.projectionMatrix = projectionMatrix;

    ...
}

Let me know if that helps!

- Kim

Re: Raycast in AR Camera

September 13, 2011 - 12:02am #4

Hey Kim, thanks for the response, can't believe you're still at work!

We tried and tried to match the camera rect, but we couldn't match it. Currently working on the xoom which has a huge offset.

We got close by trying to alter the projection matrix to account for it. We got it to match in a test case on pc, however going to device it doesn't work. Either GL.Viewport is slightly different on android or the projection matrix is :-(

Our code that works on pc but not on device is (in TrackerBehaviour.InitializeProjection)

        Matrix4x4 viewportMatrix = Matrix4x4.identity;

        viewportMatrix.m00 = 1.0f - (Mathf.Abs((mViewportRect.x) / (mViewportRect.width)));
        viewportMatrix.m11 = 1.0f - (Mathf.Abs((mViewportRect.y) / (mViewportRect.height)));

        viewportMatrix.m03 = ((mViewportRect.x) / (mViewportRect.width));
        viewportMatrix.m13 = ((mViewportRect.y) / (mViewportRect.height));

        this.camera.projectionMatrix = viewportMatrix * projectionMatrix;

Re: Raycast in AR Camera

September 12, 2011 - 5:44pm #3

Hi Dan,

Thanks for the report. We'll look into this, and try to fix the issue for our next release.

In the meanwhile, have you tried setting the Camera.rect to match the viewport?

GL.Viewport(mViewportRect);
Rect cameraRect = new Rect(
                            mViewportRect.x / Screen.width,
                            mViewportRect.y / Screen.height,
                            mViewportRect.width / Screen.width,
                            mViewportRect.height / Screen.height);
camera.rect = cameraRect;

- Kim

Re: Raycast in AR Camera

September 12, 2011 - 10:36am #2

Have you tried adding a collider to the ARCamera?

For example, the following will pick-up the ARCamera when you click or touch the screen. Try adding a box collider and then attach the following to the ARCamera.


var hit : RaycastHit;
var object : GameObject;// assign the ARCamera in the Inspector


function Update () {

        var cam : Transform = Camera.main.transform;
	var ray = new Ray(Vector3.zero, -cam.forward);
	
	if (Input.GetMouseButtonUp (0))  
	{
		
		if (Physics.Raycast (ray, hit, 1000) ) 
		{
			Debug.Log("Looking for "+object.name+" hitting "+hit.transform.name);
			
			if( hit.transform.name == object.name )
				Debug.Log("Found "+object.name +"  :  "+hit.transform.name+" @ "+hit.transform.position);
		}
	}

}

...

oops, I can see that I totally misinterpreted your question.

Log in or register to post comments