Ejemplo n.º 1
0
        private System.Numerics.Matrix4x4 GetSceneToWorldTransform()
        {
            var result = System.Numerics.Matrix4x4.Identity;

            if (Application.isEditor && !isRemoting)
            {
                return(result);
            }

            SpatialCoordinateSystem sceneOrigin = SpatialGraphInteropPreview.CreateCoordinateSystemForNode(sceneOriginId);

            var nativePtr = UnityEngine.XR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr();
            SpatialCoordinateSystem worldOrigin = SpatialCoordinateSystem.FromNativePtr(nativePtr);

            var sceneToWorld = sceneOrigin.TryGetTransformTo(worldOrigin);

            if (sceneToWorld.HasValue)
            {
                result = sceneToWorld.Value; // numerics
            }
            else
            {
                Debug.LogError("Getting coordinate system failed!");
            }

            return(result);
        }
        private void ProcessMarkerUpdates()
        {
            bool locatedAllMarkers = true;
            var  markerDictionary  = new Dictionary <int, Marker>();

            lock (_contentLock)
            {
                foreach (var markerPair in _markerIds)
                {
                    if (!_markerCoordinateSystems.ContainsKey(markerPair.Key))
                    {
                        var coordinateSystem = SpatialGraphInteropPreview.CreateCoordinateSystemForNode(markerPair.Key.SpatialGraphNodeId);
                        if (coordinateSystem != null)
                        {
                            _markerCoordinateSystems[markerPair.Key] = coordinateSystem;
                        }
                    }
                }

                foreach (var coordinatePair in _markerCoordinateSystems)
                {
                    if (!_markerIds.TryGetValue(coordinatePair.Key, out var markerId))
                    {
                        DebugLog($"Failed to locate marker:{coordinatePair.Key}, {markerId}");
                        locatedAllMarkers = false;
                        continue;
                    }

                    if (_qrCodesManager.TryGetLocationForQRCode(coordinatePair.Value, out var location))
                    {
                        var translation = location.GetColumn(3);
                        // The obtained QRCode orientation will reflect a positive y axis down the QRCode.
                        // Spectator view marker detectors should return a positive y axis up the marker,
                        // so, we rotate the marker orientation 180 degrees around its z axis.
                        var rotation = Quaternion.LookRotation(location.GetColumn(2), location.GetColumn(1)) * Quaternion.Euler(0, 0, 180);

                        if (_markerSizes.TryGetValue(markerId, out var size))
                        {
                            var transform    = Matrix4x4.TRS(translation, rotation, Vector3.one);
                            var offset       = -1.0f * size / 2.0f;
                            var markerCenter = transform.MultiplyPoint(new Vector3(offset, offset, 0));
                            var marker       = new Marker(markerId, markerCenter, rotation);
                            markerDictionary[markerId] = marker;
                        }
                    }
                }
            }

            if (markerDictionary.Count > 0 || locatedAllMarkers)
            {
                MarkersUpdated?.Invoke(markerDictionary);
            }

            // Stop processing markers once all markers have been located
            _processMarkers = !locatedAllMarkers;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tries to obtain the QRCode location in Unity Space.
        /// The position component of the location matrix will be at the top left of the QRCode
        /// The orientation of the location matrix will reflect the following axii:
        /// x axis: horizontal with the QRCode.
        /// y axis: positive direction down the QRCode.
        /// z axis: positive direction outward from the QRCode.
        /// Note: This function should be called from the main thread
        /// </summary>
        /// <param name="id">QRCode guid id</param>
        /// <param name="location">Output location for the QRCode in Unity Space</param>
        /// <returns>returns true if the QRCode was located</returns>
        public bool TryGetLocationForQRCode(Guid id, out Matrix4x4 location)
        {
            location = Matrix4x4.identity;

#if WINDOWS_UWP
            try
            {
                var coordinateSystem = SpatialGraphInteropPreview.CreateCoordinateSystemForNode(id);
                return(TryGetLocationForQRCode(coordinateSystem, out location));
            }
            catch (Exception e)
            {
                Debug.LogError($"Exception thrown creating coordinate system for qr code id: {id.ToString()}, {e.ToString()}");
                return(false);
            }
#else
            Debug.LogError($"Failed to create coordinate system for qr code id: {id.ToString()}");
            return(false);
#endif
        }