(四)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单例类所以一定要把它先挂到场景里面


posted @ 2017-01-25 10:56  Erma_Jack  阅读(1976)  评论(1编辑  收藏  举报