移动VR添加视线交互

来源:互联网 发布:mac的cmd在哪里 编辑:程序博客网 时间:2024/05/18 01:45

使用MojingSDK+tales from the rift。


1.制作视线点

Crosshair为quad文件,参数如下图:

canvas 和image的参数分别如下:

image中加入的图片为一圈红线,当实现注视物体时,这一圈线会围绕crosshair进行旋转,代表选中物体的一个过程。

2.接下来是添加响应和控制脚本

给eventsystem添加如下脚本:

注意取消其自带的standalone module模块。


给image添加gaze fuze脚本。

3.给main camera 添加physics raycaster组件。

《附录》gaze fuze代码如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;public class GazeFuse : MonoBehaviour {    public GameObject gazeGameObject;    private Image image;    void Start()     {        image = GetComponent<Image>();    }        void Update()     {        if (gazeGameObject == null || GazeInputModule2.gazeGameObject == gazeGameObject)         {            FuseAmountChanged(GazeInputModule2.gazeFraction);        }    }    void FuseAmountChanged(float fuseAmount)    {        if (image != null)        {            image.fillAmount = fuseAmount;        }    }}


Gaze input module 2代码如下:

// Gaze Input Module by Peter Koch <peterept@gmail.com>using UnityEngine;using UnityEngine.EventSystems;using System.Collections.Generic;// To use:// 1. Drag onto your EventSystem game object.// 2. Disable any other Input Modules (eg: StandaloneInputModule & TouchInputModule) as they will fight over selections.// 3. Make sure your Canvas is in world space and has a GraphicRaycaster (should by default).// 4. If you have multiple cameras then make sure to drag your VR (center eye) camera into the canvas.public class GazeInputModule2 : PointerInputModule {    public enum Mode { Click = 0, Gaze };    public Mode mode;    [Header("Click Settings")]    public string ClickInputName = "Submit";    [Header("Gaze Settings")]    public float GazeTimeInSeconds = 2f;    // Current gazed at object and gaze time progress    public static float gazeFraction { get; private set; }     public static GameObject gazeGameObject { get; private set; }     public RaycastResult CurrentRaycast;    private PointerEventData pointerEventData;    private GameObject currentLookAtHandler;    private float currentLookAtHandlerClickTime;    public override void Process()    {         HandleLook();        HandleSelection();    }    void HandleLook()    {        if (pointerEventData == null)        {            pointerEventData = new PointerEventData(eventSystem);        }        // fake a pointer always being at the center of the screen        pointerEventData.position = new Vector2(Screen.width/2, Screen.height/2);        pointerEventData.delta = Vector2.zero;        List<RaycastResult> raycastResults = new List<RaycastResult>();        eventSystem.RaycastAll(pointerEventData, raycastResults);        CurrentRaycast = pointerEventData.pointerCurrentRaycast = FindFirstRaycast(raycastResults);        ProcessMove(pointerEventData);    }        void HandleSelection()    {        gazeFraction = 0;         if (pointerEventData.pointerEnter != null)        {            // if the ui receiver has changed, reset the gaze delay timer            GameObject handler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(pointerEventData.pointerEnter);            if (currentLookAtHandler != handler)            {                gazeGameObject = currentLookAtHandler = handler;                currentLookAtHandlerClickTime = Time.realtimeSinceStartup + GazeTimeInSeconds;            }            if (mode == Mode.Gaze && currentLookAtHandler != null)   // added for progressCursor                gazeFraction = Mathf.Clamp01 (1 - (currentLookAtHandlerClickTime - Time.realtimeSinceStartup) / GazeTimeInSeconds);   // added for progressCursor                        // if we have a handler and it's time to click, do it now            if (currentLookAtHandler != null &&                 (mode == Mode.Gaze && Time.realtimeSinceStartup > currentLookAtHandlerClickTime) ||                 (mode == Mode.Click && Input.GetButtonDown(ClickInputName)))            {                if (EventSystem.current.currentSelectedGameObject != null)                {        //            ExecuteEvents.ExecuteHierarchy(EventSystem.current.currentSelectedGameObject, pointerEventData, ExecuteEvents.deselectHandler);                }                EventSystem.current.SetSelectedGameObject(currentLookAtHandler);                gazeFraction = 0;   // added for progressCursor                ExecuteEvents.ExecuteHierarchy(currentLookAtHandler, pointerEventData, ExecuteEvents.pointerClickHandler);                currentLookAtHandlerClickTime = float.MaxValue;                            ExecuteEvents.ExecuteHierarchy(EventSystem.current.currentSelectedGameObject, pointerEventData, ExecuteEvents.deselectHandler);            }        }        else        {            gazeGameObject = currentLookAtHandler = null;        }    }}

0 0
原创粉丝点击