Пример #1
0
        /// <summary>
        /// Gather trackable objects in the simulated environment and do any other initialization that depends on the environment
        /// </summary>
        protected virtual void SetupTrackingForEnvironment()
        {
            // Once the environment is set up we can gather its synthesized trackables. To simulate discovery of these
            // trackables we can check how visible they are to the camera - this includes frustum checking, occlusion checking,
            // and other heuristics.
            m_EnvironmentScene = EditorOnlyDelegates.GetSimulatedEnvironmentScene();
            if (!m_EnvironmentScene.IsValid())
            {
                return;
            }

            // Get the environment physics scene to check if trackable objects are occluded by other objects in the environment
            m_EnvironmentPhysicsScene = m_EnvironmentScene.GetPhysicsScene();

            m_SimulatedTrackableObjects.Clear();
            k_SimulatedObjects.Clear();
            GameObjectUtils.GetComponentsInScene(m_EnvironmentScene, k_SimulatedObjects);
            foreach (var simulatedObject in k_SimulatedObjects)
            {
                var synthTrackable = simulatedObject.GetComponent <TSynthTrackable>();
                if (synthTrackable == null)
                {
                    continue;
                }

                synthTrackable.Initialize();
                m_SimulatedTrackableObjects[synthTrackable] = simulatedObject;
                this.InjectFunctionalitySingle(simulatedObject);
                simulatedObject.StartRunInEditMode();
            }

            k_SimulatedObjects.Clear();
        }
Пример #2
0
        void SetupTrackingForEnvironment()
        {
            m_EnvironmentScene = EditorOnlyDelegates.GetSimulatedEnvironmentScene();
            if (!m_EnvironmentScene.IsValid())
            {
                return;
            }

            m_EnvironmentPhysicsScene = m_EnvironmentScene.GetPhysicsScene();

            m_SynthMarkers.Clear();
            k_SimulatedObjects.Clear();
            GameObjectUtils.GetComponentsInScene(m_EnvironmentScene, k_SimulatedObjects);
            foreach (var simulatedObject in k_SimulatedObjects)
            {
                var synthesizedMarker = simulatedObject.GetComponent <SynthesizedMarker>();
                if (synthesizedMarker == null || !synthesizedMarker.isActiveAndEnabled)
                {
                    continue;
                }

                synthesizedMarker.Initialize();
                m_SynthMarkers.Add(synthesizedMarker);
                this.InjectFunctionalitySingle(simulatedObject);
                simulatedObject.StartRunInEditMode();
            }

            k_SimulatedObjects.Clear();
        }
Пример #3
0
        // combine all marker tracking heuristics into one result
        static SimulatedMarkerTrackingResult GetMarsCameraTrackingQuality(SynthesizedMarker marker, Camera cam)
        {
            var markerTrans = marker.transform;
            var markerPos   = markerTrans.position;
            var markerUp    = markerTrans.up;

            // tolerance > 1 allows tracking loss to happen when most of the marker goes out, not just the center
            const float relaxedTolerance = 1.1f;

            // if the marker is outside the camera frustum, the device can't see/track it anymore
            if (!SimulatedTrackingUtils.PointInFrustum(cam, markerPos, relaxedTolerance))
            {
                return(new SimulatedMarkerTrackingResult(0f, TrackingFailureCause.OutOfFrustum));
            }

            var camTrans   = cam.transform;
            var camPos     = camTrans.position;
            var camForward = camTrans.forward;

            var distance        = Vector3.Distance(camPos, markerPos);
            var distanceQuality = GetDistanceQuality(marker.Extents, distance);

            // markers that are too far away can't be tracked
            if (distanceQuality <= 0f)
            {
                return(new SimulatedMarkerTrackingResult(0f, TrackingFailureCause.OutOfRange));
            }

            // assume marker isn't occluded by environment in the case where the physics scene is not available
            var occluded = false;
            var hitCount = 0;

#if UNITY_EDITOR
            var simScene = EditorOnlyDelegates.GetSimulatedEnvironmentScene();
            if (simScene.IsValid())
            {
                var physicScene = simScene.GetPhysicsScene();
                if (physicScene.IsValid())
                {
                    occluded = TestPointOcclusion(physicScene, camPos, markerPos, out hitCount);
                }
            }
#endif
            if (occluded)
            {
                return(new SimulatedMarkerTrackingResult(0f, TrackingFailureCause.Occluded, k_RayHits[0].point, hitCount));
            }

            var alignmentQuality = GetImageAlignmentQuality(markerUp, camForward);
            // this is important to consider in combination with the surface viewing angle test,
            // because the camera's forward can be aligned with the marker's normal without actually looking at the marker
            var lookDirQuality = GetLookDirectionQuality(markerPos, camPos, camForward);

            return(new SimulatedMarkerTrackingResult(alignmentQuality * lookDirQuality * distanceQuality));
        }
        internal void SimulationRaycastHits()
        {
            var cameraTransform = m_Camera.transform;
            var origin          = cameraTransform.position;

            var fov      = m_Camera.fieldOfView * 0.5f;
            var rotation = cameraTransform.rotation;
            var mask     = SimulationConstants.SimulatedEnvironmentLayerMask;

            var horizontalFOV        = m_Camera.GetHorizontalFOV();
            var scaledMaxHitDistance = m_MaxHitDistance * m_DistanceScale;

            if (m_RaycastHits.Length != m_RaysPerCast)
            {
                m_RaycastHits = new RaycastHit[m_RaysPerCast];
            }

            if (m_Raycasts.Length != m_RaysPerCast)
            {
                m_Raycasts = new Ray[m_RaysPerCast];
            }

            var simScene = EditorOnlyDelegates.GetSimulatedEnvironmentScene();

            if (!simScene.IsValid())
            {
                return;
            }

            var physicScene = simScene.GetPhysicsScene();

            if (!physicScene.IsValid())
            {
                return;
            }

            for (var i = 0; i < m_RaysPerCast; i++)
            {
                var direction = FrustumRay(fov, horizontalFOV, rotation);

                RaycastHit hit;
                m_Raycasts[i] = new Ray(origin, direction);
                physicScene.Raycast(m_Raycasts[i].origin, m_Raycasts[i].direction, out hit, scaledMaxHitDistance, mask);

                m_RaycastHits[i] = hit;
            }

            m_LastCastTime = MarsTime.Time;
        }