【问题标题】:Unity MRTK: Terrain InteractionUnity MRTK:地形交互
【发布时间】:2021-03-31 00:57:19
【问题描述】:

我正在向任何可能有幸与 Unity Terrain 和 MRTK 互动的人寻求指导。

我正在使用在线地图,并且正在尝试将应用程序移植到 Hololens 2 中。一切都准备就绪,除了我似乎无法触发对地形的点击——这是我的核心需要做的。

基本上我会渲染一个设定地理位置的地形,无论用户在哪里点击地形,我都会存储这些坐标,并会在该位置生成一个 3d 模型供其他用户查看。

在播放期间在编辑器中,如果我用鼠标单击地形,一切都很好。但是,如果我尝试使用凝视圈,我可以看到凝视与地形很好地碰撞,并且在鼠标单击期间我可以看到圈子缩小,但点击事件不会触发(实际鼠标不在此时的地形——这就是为什么什么都没有发生的原因)。使用空格键和手部替身,投射光线根本不会击中地形——这正是我在构建和部署到耳机时所看到的,地形本身就像没有对撞机一样,并且几乎被所有交互忽略。

我已经尝试了所有可交互状态的可能组合,但我只能生成一个非常基本的地形被点击状态,它忽略了地形被点击的“位置”,这是我需要实现的关键.

本质上,我需要弄清楚如何通过头显中的光线投射线或实际触摸地形来复制鼠标点击。

旁注,我注意到当我按住空格键并在编辑器中使用 3d 手时,我也无法与按钮交互。这些与 MRTK 示例中使用的按钮预制件相同,如果我使用凝视圈,它们可以在编辑器中进行交互。我不知道,也许如果我能弄清楚如何让它与按钮交互,它可能会让我朝着正确的方向前进,让它与地形交互。

【问题讨论】:

    标签: unity3d mrtk


    【解决方案1】:

    所以经过大量的挖掘,这是我想出的解决方案。

    首先是附加到空游戏对象的组件代码:

    using Microsoft.MixedReality.Toolkit;
    using Microsoft.MixedReality.Toolkit.Utilities;
    using UnityEngine;
    
    public class GetCoordHandler : MonoBehaviour
    {
        private Vector3 gazePoint, gazeNormal;
    
        private void Update()
        {
            // key z in editor, A button on xbox controller
            if (Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.Joystick1Button0))
            {
                // make sure we have hit something
                if (CoreServices.InputSystem.GazeProvider.GazeTarget)
                {
                    // grab where the gaze pointer is
                    gazePoint = CoreServices.InputSystem.GazeProvider.HitInfo.point;
                    // convert this position to screen coords and fire the method
                    AddMarker(CameraCache.Main.WorldToScreenPoint(gazePoint));
            }
        }
    }
    
    private void AddMarker(Vector3 point)
    {
        // Get the coordinates under the cursor.
        double lng, lat;
        // custom method that returns the coordinates
        OnlineMapsControlBase.instance.GetCoords_MRTK(point, out lng, out lat);
    
        Debug.Log("Lat: " + lat + "  Lng: " + lng);
        // Create a label for the marker.
        string label = "Marker: " + (OnlineMapsMarkerManager.CountItems + 1);
        // Create a new marker.
        OnlineMapsMarkerManager.CreateItem(lng, lat, label);
     }
    }
    

    最后是一个简单的 OnlineMaps 自定义方法,它使用从 Gaze 传入的 Vector3 而不是使用鼠标位置。

     public bool GetCoords_MRTK(Vector2 gazePosition, out double lng, out double lat)
     {
        return GetCoords(gazePosition, out lng, out lat);
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多