コード例 #1
0
    /// <summary>
    /// Takes in a spatial node id and returns the transformation matrix that specifies the transformation from the spatial node to the Unity world.
    /// </summary>
    /// <param name="nodeId">Id of the spatial node.</param>
    /// <param name="runOnDevice">True if the application is running on a hololens device</param>
    /// <returns>Transformation matrix from the spatial node to the Unity world.</returns>
    public static System.Numerics.Matrix4x4?GetSceneToUnityTransform(Guid nodeId, bool runOnDevice)
    {
        System.Numerics.Matrix4x4?sceneToUnityTransform = System.Numerics.Matrix4x4.Identity;

#if WINDOWS_UWP
        // Only get the spatial coordinate if we are running on device
        if (runOnDevice)
        {
            Debug.Log("TransformUtils.GetSceneToUnityTransform: About to create a coordinate system for node id: " + nodeId);
            SpatialCoordinateSystem sceneSpatialCoordinateSystem = Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(nodeId);
            SpatialCoordinateSystem unitySpatialCoordinateSystem = (SpatialCoordinateSystem)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(
                UnityEngine.XR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr());

            sceneToUnityTransform = sceneSpatialCoordinateSystem.TryGetTransformTo(unitySpatialCoordinateSystem);

            if (sceneToUnityTransform != null)
            {
                sceneToUnityTransform = TransformUtils.ConvertRightHandedMatrix4x4ToLeftHanded(sceneToUnityTransform.Value);
            }
            else
            {
                Debug.LogWarning("TransformUtils.GetSceneToUnityTransform: Scene to Unity transform is null. Not good.");
            }
        }
#endif
        return(sceneToUnityTransform);
    }