MRTK-辅助类

作者:追风剑情 发布于:2023-10-25 10:25 分类:Unity3d

using UnityEngine;
using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
/// <summary>
/// MRTK辅助类
/// </summary>
public sealed class MRTKHelper
{
    // 凝视数据提供者
    public static IMixedRealityEyeGazeProvider EyeGazeProvider
    {
        get
        {
            return CoreServices.InputSystem?.EyeGazeProvider;
        }
    }

    // 性能分析器是否开启
    public static bool ShowProfiler
    {
        get
        {
            bool showProfiler = (CoreServices.DiagnosticsSystem?.ShowProfiler).GetValueOrDefault(false);
            return showProfiler;
        }
        set
        {
            if (CoreServices.DiagnosticsSystem != null)
                CoreServices.DiagnosticsSystem.ShowProfiler = value;
        }
    }

    public static MixedRealityHandTrackingProfile HandTrackingProfile
    {
        get
        {
            MixedRealityHandTrackingProfile handProfile = null;
            if (CoreServices.InputSystem?.InputSystemProfile != null)
            {
                handProfile = CoreServices.InputSystem.InputSystemProfile.HandTrackingProfile;
            }
            return handProfile;
        }
    }

    // 手部网格可视化是否开启
    public static bool EnableHandMeshVisualization
    {
        get
        {
            MixedRealityHandTrackingProfile handProfile = HandTrackingProfile;
            return handProfile != null && handProfile.EnableHandMeshVisualization;
        }
        set
        {
            MixedRealityHandTrackingProfile handProfile = HandTrackingProfile;
            if (handProfile != null)
                handProfile.EnableHandMeshVisualization = value;
        }
    }

    // 手部关节可视化是否开启
    public static bool EnableHandJointVisualization
    {
        get
        {
            MixedRealityHandTrackingProfile handProfile = HandTrackingProfile;
            return handProfile != null && handProfile.EnableHandJointVisualization;
        }
        set
        {
            MixedRealityHandTrackingProfile handProfile = HandTrackingProfile;
            if (handProfile != null)
                handProfile.EnableHandJointVisualization = value;
        }
    }

    // 开启物理Mesh层射线遮挡
    public static bool PhysicalSpaceLayerMaskEnabled
    {
        set
        {
            LayerMask layerMask = new LayerMask();
            if (value)
                layerMask.value = LayerMask.GetMask("Default", "TransparentFX", "Water", "UI", "Spatial Awareness");
            else
                layerMask.value = LayerMask.GetMask("Default", "TransparentFX", "Water", "UI");
            LayerMask[] layerMasks = new LayerMask[] { layerMask };

            IEnumerable<IMixedRealityPointer> enumerator = CoreServices.FocusProvider.GetPointers<IMixedRealityPointer>();
            foreach (IMixedRealityPointer pointer in enumerator)
            {
                //覆盖配置文件中的碰撞层
                pointer.PrioritizedLayerMasksOverride = layerMasks;
            }
        }
    }
    
    // 将UI重定位到眼前1米处,并面向Camera
    public static void ReorientUI(Transform ui, float distance = 1.0f)
    {
        Vector3 position = CameraCache.Main.transform.position;
        Quaternion rotation = CameraCache.Main.transform.rotation;
        ui.position = position + CameraCache.Main.transform.forward * distance;
        ui.rotation = rotation;
    }

    // 将3D对象重定位到眼前1米处,并面向Camera
    public static void Reorient3D(Transform tran, float distance = 1.0f)
    {
        Vector3 forward = CameraCache.Main.transform.forward;
        Vector3 position = CameraCache.Main.transform.position;
        tran.position = position + forward * distance;
        tran.rotation = Quaternion.LookRotation(-forward);
    }

    // 重置旋转
    public static void ResetRotation(Transform tran)
    {
        tran.rotation = Quaternion.identity;
    }

    // 克隆对象
    public static T Clone<T>(T template) where T : MonoBehaviour
    {
        if (template == null)
            return null;
        T o = GameObject.Instantiate<T>(template);
        o.transform.SetParent(template.transform.parent);
        o.transform.localScale = template.transform.localScale;
        o.transform.localRotation = template.transform.localRotation;
        o.transform.localPosition = template.transform.localPosition;
        return o;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号