Jump to content

How to detect object that is being focused ?


Recommended Posts

Hi,

I am trying to use SRanipal SDK. I would like to detect which object that is being looked at. By just returning the name of objects that are being looked at should be fine with me. Could anyone give me a hand please ? I am trying to look at the sample script but could not really get it.

Here is the script that I call from Update function in UNITY

using Photon.Pun;
using System;
using System.IO;
using UnityEngine;
using ViveSR.anipal.Eye;


void Update()
{
        CheckFocus();
}

public void CheckFocus()
{
        Ray GazeRay;

        bool eyeFocus;
        if (SRanipal_Eye.Focus(GazeIndex.COMBINE, out GazeRay, out focusInfo)) { }
        else if (SRanipal_Eye.Focus(GazeIndex.RIGHT, out GazeRay, out focusInfo)) { }
        else if (SRanipal_Eye.Focus(GazeIndex.LEFT, out GazeRay, out focusInfo)) { }
        else return;

        var currentFocusObject = focusInfo.collider.gameObject.name;

}

 

Kindly see the error that I have got.

NullReferenceException: Object reference not set to an instance of an object
ViveSR.anipal.Eye.SRanipal_Eye.Focus (ViveSR.anipal.Eye.GazeIndex index, UnityEngine.Ray& ray, ViveSR.anipal.Eye.FocusInfo& focusInfo, System.Single radius, System.Single maxDistance, System.Int32 focusableLayer, ViveSR.anipal.Eye.EyeData eye_data) (at Assets/ViveSR/Scripts/Eye/SRanipal_Eye.cs:357)
ViveSR.anipal.Eye.SRanipal_Eye.Focus (ViveSR.anipal.Eye.GazeIndex index, UnityEngine.Ray& ray, ViveSR.anipal.Eye.FocusInfo& focusInfo, ViveSR.anipal.Eye.EyeData eye_data) (at Assets/ViveSR/Scripts/Eye/SRanipal_Eye.cs:462)
ViveSR.anipal.Eye.SRanipal_Eye.Focus (ViveSR.anipal.Eye.GazeIndex index, UnityEngine.Ray& ray, ViveSR.anipal.Eye.FocusInfo& focusInfo) (at Assets/ViveSR/Scripts/Eye/SRanipal_Eye.cs:475)
EyeInfo.CheckFocus () (at Assets/Scripts/EyeInfo.cs:180)
EyeInfo.Update () (at Assets/Scripts/EyeInfo.cs:63)

 

Thanks

 

Link to post
Share on other sites

Hi,

I just took a quick look at the script and the Focus method in the SRanipal_Eye script. I think I'm on a different version of the SDK (line 357 is a comment for me), but I suspect the issue is caused by Camera.main.

In Unity, does your camera have the MainCamera tag?

Link to post
Share on other sites

@Ihshan Gumilar  Here is a sample for using the Focus api in Unity to get the current gameobject and it's name with FocusName(). Also, make sure you have the SRanipal_Eye_Framework script attached to a gameobject in the scene with "Enable Eye" checked. This method will only report gameobjects with colliders attached. And as @C3D mentioned you will need the MainCamera tag to prevent the NullReferenceException errors.

 

    private static Ray testRay;
    private static FocusInfo focusInfo;

    /// <summary>
    /// Checks for current object in focus.
    /// </summary>
    public static GameObject Focus() 
    {
        if (EyeTool.GetVersion() == 1)
        {
            if (SRanipal_Eye.Focus(GazeIndex.COMBINE, out testRay, out focusInfo)) { }
            else if (SRanipal_Eye.Focus(GazeIndex.LEFT, out testRay, out focusInfo)) { }
            else if (SRanipal_Eye.Focus(GazeIndex.RIGHT, out testRay, out focusInfo)) { }
            else return null;
        }
        else if (EyeTool.GetVersion() == 2)
        {
            if (SRanipal_Eye_v2.Focus(GazeIndex.COMBINE, out testRay, out focusInfo)) { }
            else if (SRanipal_Eye_v2.Focus(GazeIndex.LEFT, out testRay, out focusInfo)) { }
            else if (SRanipal_Eye_v2.Focus(GazeIndex.RIGHT, out testRay, out focusInfo)) { }
            else return null;
        }

        return focusInfo.collider.gameObject;
    }

    /// <summary>
    /// Checks name for current object in focus.
    /// </summary>
    public static string FocusName() 
    {
        if (Focus() is null)
            return "";
        else
            return Focus().name;
    }

 

Link to post
Share on other sites

Please sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...