首页 > 解决方案 > 如何获取活动 MRTK 指针的位置?

问题描述

我正在尝试将预览对象放置在混合现实工具包中铰接式指针的末尾。如何获得指针撞击几何的位置?

我将 DefaultControllerPointer 设置为铰接手,但我需要获取对它的引用,然后获取尖端的变换位置。

标签: hololensmrtk

解决方案


这是一个如何迭代所有控制器的示例,找到作为手部光线的关节手,然后获取终点的位置(以及光线起点),最后确定光线是否击中几何体(一个对象),因为它有一个默认长度:

using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using UnityEngine;

public class HitPointTest : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        foreach(var source in MixedRealityToolkit.InputSystem.DetectedInputSources)
        {
            // Ignore anything that is not a hand because we want articulated hands
            if (source.SourceType == Microsoft.MixedReality.Toolkit.Input.InputSourceType.Hand)
            {
                foreach (var p in source.Pointers)
                {
                    if (p is IMixedRealityNearPointer)
                    {
                        // Ignore near pointers, we only want the rays
                        continue;
                    }
                    if (p.Result != null)
                    {
                        var startPoint = p.Position;
                        var endPoint = p.Result.Details.Point;
                        var hitObject = p.Result.Details.Object;
                        if (hitObject)
                        {
                            var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            sphere.transform.localScale = Vector3.one * 0.01f;
                            sphere.transform.position = endPoint;
                        }
                    }

                }
            }
        }
    }
}

请注意,这是最新的 mrtk_development 代码库,也应该从 RC1 开始工作。


推荐阅读