【发布时间】:2022-06-28 03:31:54
【问题描述】:
我有一个 Unity 应用程序,它使用世界上的 qrcode 调用一些对象。二维码处理基于MixedReality-QRCode-Sample。作为 Hololens 上的原生应用,它非常好用。
但是当我尝试使用全息远程处理时,SpatialGraphCoordinateSystem 不起作用。微软文档Holographic Remoting troubleshooting 解释说,部分Windows.Perception.Spatial 不受支持。 我已经尝试重写脚本(如果是删除 WINDOWS_UWP)我得到它编译但在运行时得到比该错误消息:
InvalidCastException: Specified cast is not valid.
QRTracking.SpatialGraphCoordinateSystem.UpdateLocation () (at Assets/Scripts/SpatialGraphCoordinateSystem.cs:67)
QRTracking.SpatialGraphCoordinateSystem.Update () (at Assets/Scripts/SpatialGraphCoordinateSystem.cs:120)
谁有一个可行的解决方案来通过全息远程跟踪 qrcodes 的位置?
SpatialGraphCoordinateSystem.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if WINDOWS_UWP
using Windows.Perception.Spatial;
#endif
using Microsoft.MixedReality.Toolkit.Utilities;
namespace QRTracking
{
public class SpatialGraphCoordinateSystem : MonoBehaviour
{
#if WINDOWS_UWP
private SpatialCoordinateSystem CoordinateSystem = null;
#endif
private System.Guid id;
public System.Guid Id
{
get
{
return id;
}
set
{
id = value;
#if WINDOWS_UWP
CoordinateSystem = Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
if (CoordinateSystem == null)
{
Debug.Log("Id= " + id + " Failed to acquire coordinate system");
}
#endif
}
}
void Awake()
{
}
// Use this for initialization
void Start()
{
#if WINDOWS_UWP
if (CoordinateSystem == null)
{
CoordinateSystem = Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
if (CoordinateSystem == null)
{
Debug.Log("Id= " + id + " Failed to acquire coordinate system");
}
}
#endif
}
private void UpdateLocation()
{
{
#if WINDOWS_UWP
if (CoordinateSystem == null)
{
CoordinateSystem = Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
if (CoordinateSystem == null)
{
Debug.Log("Id= " + id + " Failed to acquire coordinate system");
}
}
if (CoordinateSystem != null)
{
Quaternion rotation = Quaternion.identity;
Vector3 translation = new Vector3(0.0f, 0.0f, 0.0f);
System.IntPtr rootCoordnateSystemPtr = UnityEngine.XR.WindowsMR.WindowsMREnvironment.OriginSpatialCoordinateSystem;
SpatialCoordinateSystem rootSpatialCoordinateSystem = (SpatialCoordinateSystem)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(rootCoordnateSystemPtr);
// Get the relative transform from the unity origin
System.Numerics.Matrix4x4? relativePose = CoordinateSystem.TryGetTransformTo(rootSpatialCoordinateSystem);
if (relativePose != null)
{
System.Numerics.Vector3 scale;
System.Numerics.Quaternion rotation1;
System.Numerics.Vector3 translation1;
System.Numerics.Matrix4x4 newMatrix = relativePose.Value;
// Platform coordinates are all right handed and unity uses left handed matrices. so we convert the matrix
// from rhs-rhs to lhs-lhs
// Convert from right to left coordinate system
newMatrix.M13 = -newMatrix.M13;
newMatrix.M23 = -newMatrix.M23;
newMatrix.M43 = -newMatrix.M43;
newMatrix.M31 = -newMatrix.M31;
newMatrix.M32 = -newMatrix.M32;
newMatrix.M34 = -newMatrix.M34;
System.Numerics.Matrix4x4.Decompose(newMatrix, out scale, out rotation1, out translation1);
translation = new Vector3(translation1.X, translation1.Y, translation1.Z);
rotation = new Quaternion(rotation1.X, rotation1.Y, rotation1.Z, rotation1.W);
Pose pose = new Pose(translation, rotation);
// If there is a parent to the camera that means we are using teleport and we should not apply the teleport
// to these objects so apply the inverse
if (CameraCache.Main.transform.parent != null)
{
pose = pose.GetTransformedBy(CameraCache.Main.transform.parent);
}
gameObject.transform.SetPositionAndRotation(pose.position, pose.rotation);
//Debug.Log("Id= " + id + " QRPose = " + pose.position.ToString("F7") + " QRRot = " + pose.rotation.ToString("F7"));
}
else
{
// Debug.Log("Id= " + id + " Unable to locate qrcode" );
}
}
else
{
gameObject.SetActive(false);
}
#endif
}
}
// Update is called once per frame
void Update()
{
UpdateLocation();
}
}
}
【问题讨论】:
-
正如您已经发现我们的文档说不支持
Windows.Perception.Spatial。您需要手动获取对象位置数据并设置gameobject.transform属性.. -
我的问题就是这样。没有
SpatialCoordinateSystem如何获取二维码的位置? -
例如,使用位置数据(平移和旋转)作为二维码的数据。当您扫描二维码时,您可以获得数据,然后使用它来设置您的游戏对象。