微软Hololens开发教程系列(2): Holograms 101-凝视、手势

原文来自微软学院(https://docs.microsoft.com/zh-cn/windows/mixed-reality/holograms-101

本教程将带领您完成一个完整的项目,使用Unity构建,演示全息窗口上的核心功能,包括凝视、手势、语音输入、空间声音和空间映射。

前提条件

已经配置好开发环境的win10电脑

一个开发使用的Hololens设备

项目文件

下载项目所需文件,系统要求Unity2017.2或者更新版本

如果你需要支持Unity5.6,请下载这个版本

如果你需要支持Unity5.5,请下载这个版本

如果你需要支持Unity5.4,请下载这个版本

将文件解压到你的桌面或者其他文件夹。文件命名为Origami

第一章 “Holo” world

在本章中,我们将逐步构建和部署我们的第一个Unity项目

目标

  • 为全息开发建立一个Unity项目
  • 制作一个全息影像
  • 看到你自己做的全息影像
操作

打开Unity

选择Open

选择Origami文件的地址

选择Origami文件夹

由于Origami项目不包含场景,使用File/Save Scene As将默认空场景保存为一个文件

设置主虚拟摄像机

Hierarchy Panel选择 Main Camera

Inspector中设置transform position0,0,0

找到Clear Flags 属性,将 Skybox改为Solid color

点击Background,打开颜色选择器

R,G,B,A值设置为0

开始这个场景

Hierarchy Panel中选择Create,点击Create Empty

选择这个GameObject,右键,重命名为OrigamiCollection

在项目面板中的Hologram文件中选择(点开Assets,选择Hologram并且双击Hologram文件):

拖动Stage到作为OrigamiCollection的一个孩子

拖动Sphere1到作为OrigamiCollection的一个孩子

拖动Sphere2到作为OrigamiCollection的一个孩子

Hierarchy Panel中选择 Directional Light对象,将其删除

将Holograms文件夹中的Lights拖动到Hierarchy Panel的跟目录下

Hierarchy Panel中选择OrigamiCollection

在Inspector页面将transform postion设置为0,-0.5,2.0

点击Unity的运行按钮,你将看到Origami 对象

将项目从Unity导出到VisualStudio(前一篇已经说了,不再赘述)

第二章 凝视

目标

用一个光标可视化你的视线

操作

1、在Unity项目中选择Holograms 文件

2、将Cursor 对象拖到Hierarchy panel 的根目录

3、在项目的Scripts 文件夹中新建一个C#脚本,命名为WorldCursor,注意:名称是区分大小写的。您不需要添加.cs扩展名

4、在Hierarchy panel中选择Cursor 对象

5、将WorldCursor脚本拖到Inspector panel

6、双击脚本,将下面代码拷贝到WorldCursor并保存

using UnityEngine;

public class WorldCursor : MonoBehaviour
{
    private MeshRenderer meshRenderer;

    // Use this for initialization
    void Start()
    {
        // Grab the mesh renderer that's on the same object as this script.
        meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;

        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram...
            // Display the cursor mesh.
            meshRenderer.enabled = true;

            // Move the cursor to the point where the raycast hit.
            this.transform.position = hitInfo.point;

            // Rotate the cursor to hug the surface of the hologram.
            this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
        }
        else
        {
            // If the raycast did not hit a hologram, hide the cursor mesh.
            meshRenderer.enabled = false;
        }
    }
}

最后生成Visual Studio解决方案并运行查看

第三章 手势

目标

为项目添加手势选择功能

操作
  • 在脚本文件夹中,创建一个名为GazeGestureManager的脚本。
  • 将GazeGestureManager脚本拖到OrigamiCollection对象上。
  • 在Visual Studio中打开GazeGestureManager脚本并添加以下代码:

using UnityEngine;
using UnityEngine.XR.WSA.Input;

public class GazeGestureManager : MonoBehaviour
{
    public static GazeGestureManager Instance { get; private set; }

    // Represents the hologram that is currently being gazed at.
    public GameObject FocusedObject { get; private set; }

    GestureRecognizer recognizer;

    // Use this for initialization
    void Awake()
    {
        Instance = this;

        // Set up a GestureRecognizer to detect Select gestures.
        recognizer = new GestureRecognizer();
        recognizer.Tapped += (args) =>
        {
            // Send an OnSelect message to the focused object and its ancestors.
            if (FocusedObject != null)
            {
                FocusedObject.SendMessageUpwards("OnSelect", SendMessageOptions.DontRequireReceiver);
            }
        };
        recognizer.StartCapturingGestures();
    }

    // Update is called once per frame
    void Update()
    {
        // Figure out which hologram is focused this frame.
        GameObject oldFocusObject = FocusedObject;

        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram, use that as the focused object.
            FocusedObject = hitInfo.collider.gameObject;
        }
        else
        {
            // If the raycast did not hit a hologram, clear the focused object.
            FocusedObject = null;
        }

        // If the focused object changed this frame,
        // start detecting fresh gestures again.
        if (FocusedObject != oldFocusObject)
        {
            recognizer.CancelGestures();
            recognizer.StartCapturingGestures();
        }
    }
}

新建一个名为SphereCommands的脚本

展开OrigamiCollection对象

SphereCommands脚本分别添加到Sphere1对象和Sphere2对象上

打开SphereCommands脚本,将复制下面代码并保存

using UnityEngine;

public class SphereCommands : MonoBehaviour
{
    // Called by GazeGestureManager when the user performs a Select gesture
    void OnSelect()
    {
        // If the sphere has no Rigidbody component, add one to enable physics.
        if (!this.GetComponent<Rigidbody>())
        {
            var rigidbody = this.gameObject.AddComponent<Rigidbody>();
            rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
        }
    }
}

导出查看,并使用手势观察



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值