Aller au contenu

Behaviors creation

InMind-VR GitHub Repository

InMind-VR provides a set of VRmaze compatible development examples in its GitHub space.

Loading behaviors

To enable loading of behaviors in VRmaze, they should be integrated via a compiled library (.dll file). VRmaze dynamically loads these plugins at launch.

Creating a behavior

VRmaze relies on Unity 3D for 3D visualization. It is therefore possible to create its own behaviors using the logic of Unity, based on the behavior system (MonoBehaviour). Generic behaviors do not require any additional dependencies and can be integrated into VRmaze without any problems.

Collision detection

In VRmaze, it is possible to add a collision to any object in the tree structure so that its entry into a zone can be detected via the Unity method void OnTriggerEnter(Collider other).

Interaction behavior

VRmaze has a specific interaction system that allows it to work with the mouse or the complex device in virtual reality. This system is based on a generic C# interface allowing to develop behaviors that will be executed in passing.

Here is the code of the IEventListener interface:

public interface IEventListener{
    void OnOver(); //Called when the pointer is on the object
    void OnExit(); //Called once when the pointer is no longer on the object
    void OnEnter(); //Called once when the pointer enters the object
    void OnAction(); //Called when the object is hovered and when the user uses the action button
    void OnAction(int buttonIndex);//Called when the object is hovered and when the user uses any button of the pointing device
}

Full example

See the full example here.

This interface is extended to be able to use more complex devices, like VR joysticks or even allow more mouse action :

public interface ISteamVREventListener : IEventListener {
    void OnTouchpadChange(Vector2 value);
    void OnTriggerChange(float value);
}

public interface IMouseEventListener : IEventListener {
    void OnRightClick();
    void OnMiddleClick();
}

Get the pointing information

It is quite possible to get the pointing information (class RayCastHit of Unity). The difficulty lies in finding the object used for pointing. This object has a specific behavior derived from the MouseEventManagerBehaviour class. You have to search for the object in the entire VRmaze object hierarchy:

 public class GetPointingDataBehaviourExample : MonoBehaviour
    {
        //2023 and above
        // LiveEventManagerDirectAccess.Singleton donne accès aux informations calculées par le gestionnaire l'évenements de vrmaze. Les informations sont mises à jour automatiquement a chaque frame.
        void Update()
        {
            LogRays();
            LogHits();
            LogObjects();
        }

        public void LogRays()
        {
            Debug.Log("Rays launched :" + LiveData.EventManager.Rays.Count);
            foreach (var item in LiveData.EventManager.Rays) 
                Debug.Log("Ray data:" + item.origin.ToString() + " " + item.direction);            
        }

        public void LogObjects()
        {
            Debug.Log("Objects hit :" + LiveData.EventManager.Objects.Count);
            foreach (var item in LiveData.EventManager.Objects)
                Debug.Log("Object info:" + item.name);
        }

        public void LogHits()
        {
            Debug.Log("Hits count :" + LiveData.EventManager.Hits.Count);
            foreach (var item in LiveData.EventManager.Hits)
                Debug.Log("Hit info:" + item.point.ToString());
        }

        //Méthode permettant d'afficher quelques informations.
        void OnGUI()
        {
            for(int i =0;i< LiveData.EventManager.Hits.Count;i++)
                GUI.Label(new Rect(0, i*50, 500, 50), "We hit something : " + LiveEventManagerDirectAccess.Singleton.Hits[i].point);
        }
    }

Interaction with joysticks and buttons (binary and analogue) is also possible:

public class RetreiveInteractionDeviceDataBehaviourExample : MonoBehaviour
    {
        void Update()
        {
            Console.WriteLine("Retreiving Interaction devices Data !");
            foreach (var item in LiveData.EventManager.InteractionData)
            {
                Console.WriteLine(item.Identifier);

                //We can get Vector3 values (containing verctor2 sometimes)
                foreach (var axe in item.Axes)
                {
                    Console.WriteLine("Axis value : " + axe.ToString());
                }

                //We can get float values
                foreach (var analog in item.Analogs)
                {
                    Console.WriteLine("Analog value : " + analog.ToString());
                }

                //We can get bool values
                foreach (var digital in item.Digitals)
                {
                    Console.WriteLine("digital value : " + digital.ToString());
                }
            }
        }
    }

Full example

See the full example here

Use subject and time information

It is possible to know the subject's position and orientation :


public class VRmazeSubjectDisplayer : MonoBehaviour
    {
        void OnGUI()
        {
            GUI.Label(new Rect(5, 20, 200, 20), "Current Subject position:"+ LiveData.Subject.HeadPosition.ToString());
            GUI.Label(new Rect(5, 40, 200, 20), "Current Subject rotation:"+ LiveData.Subject.HeadRotation.ToString());
        }
    }

Here is an example of using VRmaze's current time :


public class VRmazeTimerDisplayer : MonoBehaviour
    {
        void OnGUI()
        {
            GUI.Label(new Rect(5, 20, 200, 20), "Current Unity time:"+ Time.time.ToString());
            GUI.Label(new Rect(5, 40, 200, 20), "Current VRmaze time:" + LiveData.Timer.CurrentTimer.ToString());
            GUI.Label(new Rect(5, 60, 200, 20), "Current Experiment time:" + LiveData.Timer.CurrentExperimentTimer.ToString());
            GUI.Label(new Rect(5, 80, 200, 20), "Current Trial time:" + LiveData.Timer.CurrentTrialTimer.ToString());
        }
    }