原文来自微软学院(https://docs.microsoft.com/zh-cn/windows/mixed-reality/holograms-101)
本教程将带领您完成一个完整的项目,使用Unity构建,演示全息窗口上的核心功能,包括凝视、手势、语音输入、空间声音和空间映射。
前提条件
已经配置好开发环境的win10电脑
一个开发使用的Hololens设备
项目文件
下载项目所需文件,系统要求Unity2017.2或者更新版本
如果你需要支持Unity5.6,请下载这个版本
如果你需要支持Unity5.5,请下载这个版本
如果你需要支持Unity5.4,请下载这个版本
将文件解压到你的桌面或者其他文件夹。文件命名为Origami
在本章中,我们将逐步构建和部署我们的第一个Unity项目
打开Unity
选择Open
选择Origami文件的地址
选择Origami文件夹
由于Origami项目不包含场景,使用File/Save Scene As将默认空场景保存为一个文件
在Hierarchy Panel选择 Main Camera
在Inspector中设置transform position为0,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 对象
用一个光标可视化你的视线
操作
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();
}
// 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解决方案并运行查看
为项目添加手势选择功能
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())
{
var rigidbody = this.gameObject.AddComponent();
rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
}
}
导出查看,并使用手势观察