GithubHelp home page GithubHelp logo

Comments (1)

liufei2008 avatar liufei2008 commented on July 23, 2024 1

After a lot of test I success to make it work! Just copy these dlls to same folder of exe file:
image

And, I also install "Microsoft.Windows.MixedReality.DotNetWinRT" package from nuget. Then change some code of SpatialGraphCoordinateSystem.cs so that the tracking can work in editor and standalone build:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Windows.Perception.Spatial;
using Microsoft.MixedReality.Toolkit.Utilities;

namespace QRTracking
{
    public class SpatialGraphCoordinateSystem : MonoBehaviour
    {
        private SpatialCoordinateSystem CoordinateSystem = null;
        private System.Guid id;
        public System.Guid Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
                CoordinateSystem = Microsoft.Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
                if (CoordinateSystem == null)
                {
                    Debug.Log("Id= " + id + " Failed to acquire coordinate system");
                }
            }
        }

        void Awake()
        {
        }

        // Use this for initialization
        void Start()
        {
            if (CoordinateSystem == null)
            {
                CoordinateSystem = Microsoft.Windows.Perception.Spatial.Preview.SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
                if (CoordinateSystem == null)
                {
                    Debug.Log("Id= " + id + " Failed to acquire coordinate system");
                }
            }
        }

        private void UpdateLocation()
        {
            {
                if (CoordinateSystem == null)
                {
                    CoordinateSystem = Microsoft.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;
                    if (rootCoordnateSystemPtr != System.IntPtr.Zero)
                    {
                        var rootSpatialCoordinateSystem = SpatialCoordinateSystem.FromNativePtr(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);
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            UpdateLocation();
        }
    }
}

from mixedreality-qrcode-sample.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.