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


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

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


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

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

一、概述

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.

凝視系統是HoloLens的第一輸入機制~(其實我覺得是借助了cardboard的交互模式),官文上有這么一句話 However, there is no explicit API or Component exposing Gaze in Unity. 凝視系統嘞~ 沒有統一的API,所以只需要自己去實現就好~ 不是太復雜,下面是凝視系統的原理。


二、凝視系統原理

原理很簡單:射線碰撞檢測 ,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.

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代碼~

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文檔~ 案例很簡單,下圖是模擬器的實際效果

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


三、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單例類~所以一定要把它先掛到場景里面~



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM