Unity3D LeapMotion Demo
Leap Motion Controller
Leap Motion Controller 是一款小巧却功能强大的 USB 设备,它能让你以全新的方式控制 Mac 或 PC。该控制器可以感知双手在空气中的自然移动,精确跟踪手和手指的运动,让你在屏幕之外的数字世界中尽情玩乐、创作和浏览。
桥接代码实现
以下是用于在 Unity 场景中使用 Leap 输入的桥接代码:
using UnityEngine;
using System.Collections;
using Leap;
/// <summary>
/// 将此类挂载到场景中的某个对象上,即可使用 Leap 输入。
/// 它会负责调用 LeapInput 的更新方法,并使用 LeapUnityHandController 在场景中创建手部对象来表示手部数据。
/// 该类有多个公共字段,你可以在 Unity 编辑器的检查器中轻松设置这些值。
/// </summary>
public class LeapUnityBridge : MonoBehaviour
{
/// <summary>
/// 这些值从检查器中设置,用于设置 LeapUnityExtension 中用于向量转换的相应字段。
/// </summary>
public Vector3 m_LeapScaling = new Vector3(0.02f, 0.02f, 0.02f);
public Vector3 m_LeapOffset = new Vector3(0, 0, 0);
public bool EnableGesture = false;
public bool EnableTransform = false;
public bool EnableScale = false;
public bool EnableRotate = false;
public bool EnableVirtualKeyBoard = false;
public bool EnableMouseControl = false;
void Awake()
{
Leap.UnityVectorExtension.InputScale = m_LeapScaling;
Leap.UnityVectorExtension.InputOffset = m_LeapOffset;
Leap4Unity.EnableGesture = EnableGesture;
VirtualMouseKeyboardContral.MouseControl.enableMouseControl = EnableMouseControl;
VirtualMouseKeyboardContral.KeyBoardControl.enableKeyBoardControl = EnableVirtualKeyBoard;
}
void Update()
{
Leap.UnityVectorExtension.InputOffset = m_LeapOffset;
Leap4Unity.Update();
LeapScreenManager.ScreenUpdate();
}
}
LEAP MOTION + 虚拟现实
Leap Motion 与虚拟现实的结合开启了真正沉浸式 VR 的未来。VR 开发员装置专门设计用于优化开发环境的稳定性和可靠性,能简单方便地将 Leap Motion Controller 与虚拟现实头盔相连接。你可以立即购买,实现对未来的憧憬。
手指传递参数代码实现
以下是用于处理手指碰撞并传递参数的代码:
using UnityEngine;
using System.Collections;
// 该相对简单的类由 LeapUnityBridge 添加到指尖对象上,
// 当手指与任何标记为 'Touchable' 的对象发生碰撞时,它能让 LeapSelectionController 得到通知。
public class LeapFingerCollisionDispatcher : MonoBehaviour
{
const float kHitDistance = 20.0f;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Touchable")
{
LeapUnitySelectionController.Get().OnTouched(gameObject, other);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Touchable")
{
LeapUnitySelectionController.Get().OnStoppedTouching(gameObject, other);
}
}
void FixedUpdate()
{
if (gameObject.GetComponent<Collider>().enabled)
{
Debug.DrawRay(transform.position, transform.forward, Color.green);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 20.0f))
{
LeapUnitySelectionController.Get().OnRayHit(hit);
}
}
}
}
以上代码展示了如何在 Unity3D 中使用 Leap Motion 控制器,包括桥接代码和手指碰撞处理代码,帮助你实现更丰富的交互体验。