(四)Hololens Unity 開發之 凝視系統

學習源於官方文檔 Gaze in Unity git

筆記一部分是直接翻譯官方文檔,部分各人理解不一致的和一些比較淺顯的保留英文原文github


HoloLens 有三大輸入系統,凝視點、手勢和聲音 ~ 本文主要記錄凝視系統的學習筆記 ~windows

(四)Hololens Unity 開發之 凝視系統

1、概述

Gaze is the first input mechanism on HoloLens. It's the first "G" in Gaze, Gesture, and Voice input models on HoloLens. However, there is no explicit API or Component exposing Gaze in Unity.ide

凝視系統是HoloLens的第一輸入機制~(其實我以爲是藉助了cardboard的交互模式),官文上有這麼一句話 However, there is no explicit API or Component exposing Gaze in Unity. 凝視系統嘞~ 沒有統一的API,因此只須要本身去實現就好~ 不是太複雜,下面是凝視系統的原理。工具


2、凝視系統原理

原理很簡單:射線碰撞檢測 ,unity開發者都比較熟悉了~下面是官文中對射線碰撞的描述~再也不翻譯學習

Conceptually, Gaze is implemented by projecting a ray from the user's head where the HoloLens is, in the forward direction they are facing and determining what that ray collides with. In Unity, the user's head position and direction are exposed through the Unity Main Camera, specifically UnityEngine.Camera.main.transform.forward and UnityEngine.Camera.main.transform.position.this

Calling Physics.RayCast results in a RaycastHit structure which contains information about the collision including the 3D point where collision occurred and the other GameObject the gaze ray collided with.翻譯

上Demo代碼~3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GazeDemo : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        RaycastHit hitinfo;
        if (Physics.Raycast(
            Camera.main.transform.position,
            Camera.main.transform.forward,
            out hitinfo,
            20.0f,
            Physics.DefaultRaycastLayers)
            )
        {
            Debug.Log("檢測到了 物體 ~");

        }
    }
}

射線檢測碰撞 涉及到的一些具體的參數~請自行查閱unity文檔~ 案例很簡單,下圖是模擬器的實際效果code

上圖能夠看出,模擬器的凝視點爲屏幕中心點~ 而上面的官文提升過,設備上的凝視點爲用戶的眼睛 以及 透過全息透鏡 肯定的直線 來肯定的凝視點~ 兩點控制一條直線~ 眼睛 -- 全息透鏡中心點~


3、Visualizing Gaze 可視化的凝視點~

這個比較容易理解了,相似cardboard中的焦點,固然,也能夠本身實現,不過HoloLens在HoloToolkit-Unity的工具中已經提供了一個類 GazeManager.cs ,用於實現可視化焦點~ 並且用起來也挺方便的~

示例代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class VisualizingGazeDemo : MonoBehaviour {

    private GazeManager gazeManager;

    // Use this for initialization
    void Start () {

        gazeManager = GazeManager.Instance;
        gazeManager.FocusedObjectChanged += OnFocusedObjectChanged;

        Debug.Log("初始化完成~");

    }

    private void OnFocusedObjectChanged(GameObject previousObject, GameObject newObject)
    {
        Debug.Log("檢測到 物體 的 名字 ~  " + newObject.name);
    }

    // Update is called once per frame
    void Update () {

    }
}

注意了~ GazeManager 是個mono單例類~因此必定要把它先掛到場景裏面~

相關文章
相關標籤/搜索