Exemple #1
0
        private void _Initialize()
        {
            if (m_NativeSessions != null)
            {
                foreach (var nativeSession in m_NativeSessions.Values)
                {
                    nativeSession.MarkDestroyed();
                }
            }

            m_NativeSessions          = new Dictionary <IntPtr, NativeSession>(new IntPtrEqualityComparer());
            m_CachedSessionHandle     = IntPtr.Zero;
            m_CachedFrameHandle       = IntPtr.Zero;
            m_CachedConfig            = null;
            m_CachedSessionEnabled    = null;
            BackgroundTexture         = null;
            SessionComponent          = null;
            IsSessionChangedThisFrame = true;
            SessionStatus             = SessionStatus.None;
            LostTrackingReason        = LostTrackingReason.None;
        }
Exemple #2
0
        private void _UpdateConfiguration(ARCoreSessionConfig config)
        {
            // There is no configuration to set.
            if (config == null)
            {
                return;
            }

            // The configuration has not been updated.
            if (m_CachedConfig != null && config.Equals(m_CachedConfig) &&
                (config.AugmentedImageDatabase == null ||
                 !config.AugmentedImageDatabase.IsDirty) &&
                !ExperimentManager.Instance.IsConfigurationDirty)
            {
                return;
            }

            m_CachedConfig = ScriptableObject.CreateInstance <ARCoreSessionConfig>();
            m_CachedConfig.CopyFrom(config);
            ExternApi.ArPresto_setConfigurationDirty();
        }
Exemple #3
0
        private void Initialize()
        {
            if (_nativeSessions != null)
            {
                foreach (var nativeSession in _nativeSessions.Values)
                {
                    nativeSession.MarkDestroyed();
                }
            }

            _nativeSessions                = new Dictionary <IntPtr, NativeSession>(new IntPtrEqualityComparer());
            _cachedSessionHandle           = IntPtr.Zero;
            _cachedFrameHandle             = IntPtr.Zero;
            _cachedConfig                  = null;
            _desiredSessionState           = null;
            _haveDisableToEnableTransition = false;
            BackgroundTexture              = null;
            SessionComponent               = null;
            IsSessionChangedThisFrame      = true;
            SessionStatus                  = SessionStatus.None;
            LostTrackingReason             = LostTrackingReason.None;
        }
Exemple #4
0
        private void _SetConfiguration(ARCoreSessionConfig config)
        {
            if (config == null)
            {
                return;
            }

            if (m_CachedConfig == null || !config.Equals(m_CachedConfig) ||
                ExperimentManager.Instance.IsConfigurationDirty)
            {
                GCHandle handle;
                var      prestoConfig = new ApiPrestoConfig(config, out handle);
                ExternApi.ArPresto_setConfiguration(ref prestoConfig);
                m_CachedConfig = ScriptableObject.CreateInstance <ARCoreSessionConfig>();
                m_CachedConfig.CopyFrom(config);

                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
        }
        public void UpdateApiConfigWithArCoreSessionConfig(IntPtr configHandle, ARCoreSessionConfig arCoreSessionConfig)
        {
            var lightingMode = ApiLightEstimationMode.Disabled;

            if (arCoreSessionConfig.EnableLightEstimation)
            {
                lightingMode = ApiLightEstimationMode.AmbientIntensity;
            }
            ExternApi.ArConfig_setLightEstimationMode(m_NativeSession.SessionHandle, configHandle, lightingMode);
            var planeFindingMode = ApiPlaneFindingMode.Disabled;

            switch (arCoreSessionConfig.PlaneFindingMode)
            {
            case DetectedPlaneFindingMode.Horizontal:
                planeFindingMode = ApiPlaneFindingMode.Horizontal;
                break;

            case DetectedPlaneFindingMode.Vertical:
                planeFindingMode = ApiPlaneFindingMode.Vertical;
                break;

            case DetectedPlaneFindingMode.HorizontalAndVertical:
                planeFindingMode = ApiPlaneFindingMode.HorizontalAndVertical;
                break;

            default:
                break;
            }
            ExternApi.ArConfig_setPlaneFindingMode(m_NativeSession.SessionHandle, configHandle, planeFindingMode);
            var updateMode = ApiUpdateMode.LatestCameraImage;

            if (arCoreSessionConfig.MatchCameraFramerate)
            {
                updateMode = ApiUpdateMode.Blocking;
                // Set vSyncCount to 0 so frame in rendered only when we have a new background texture.
                QualitySettings.vSyncCount = 0;
            }
            ExternApi.ArConfig_setUpdateMode(m_NativeSession.SessionHandle, configHandle, updateMode);
        }
        /// <summary>
        /// Validates whether the SessionConfig enables limited supported features and also logs
        /// those features.
        /// </summary>
        /// <param name="config">The SessionConfig needs to be validated.</param>
        /// <returns><c>true</c>, if the SessionConfig isn't enable any limited support features.
        /// Otherwise, returns <c>false</c>.</returns>
        public static bool ValidateSessionConfig(ARCoreSessionConfig config)
        {
            bool isValid = true;

            if (config == null)
            {
                Debug.LogWarning("Attempted to check empty configuration.");
                return(false);
            }

            if (config.LightEstimationMode != LightEstimationMode.Disabled)
            {
                LogLimitedSupportMessage("enable 'Light Estimation'", true);
                isValid = false;
            }

            if (config.AugmentedImageDatabase != null)
            {
                LogLimitedSupportMessage("enable 'Augmented Images'", true);
                isValid = false;
            }

            if (config.AugmentedFaceMode == AugmentedFaceMode.Mesh)
            {
                LogLimitedSupportMessage("enable 'Augmented Faces'", true);
                isValid = false;
            }

            if (config.InstantPlacementMode != InstantPlacementMode.Disabled)
            {
                LogLimitedSupportMessage("enable 'Instant Placement'", true);
                isValid = false;
            }

            return(isValid);
        }
Exemple #7
0
        /// <summary>
        /// Wrap an ARCoreSessionConfig in an API config.
        /// </summary>
        /// <param name="config">Config to wrap.</param>
        public ApiPrestoConfig(ARCoreSessionConfig config)
        {
            UpdateMode = config.MatchCameraFramerate ?
                         ApiUpdateMode.Blocking : ApiUpdateMode.LatestCameraImage;
            var planeFindingMode = ApiPlaneFindingMode.Disabled;

            switch (config.PlaneFindingMode)
            {
            case DetectedPlaneFindingMode.Horizontal:
                planeFindingMode = ApiPlaneFindingMode.Horizontal;
                break;

            case DetectedPlaneFindingMode.Vertical:
                planeFindingMode = ApiPlaneFindingMode.Vertical;
                break;

            case DetectedPlaneFindingMode.HorizontalAndVertical:
                planeFindingMode = ApiPlaneFindingMode.HorizontalAndVertical;
                break;

            default:
                break;
            }

            PlaneFindingMode    = planeFindingMode;
            LightEstimationMode = config.LightEstimationMode.ToApiLightEstimationMode();
            CloudAnchorMode     = config.EnableCloudAnchor ?
                                  ApiCloudAnchorMode.Enabled : ApiCloudAnchorMode.Disabled;

            if (config.AugmentedImageDatabase != null)
            {
                ArPrestoAugmentedImageDatabase =
                    config.AugmentedImageDatabase.m_ArPrestoDatabaseHandle;
            }
            else
            {
                ArPrestoAugmentedImageDatabase = IntPtr.Zero;
            }

            switch (config.CameraFocusMode)
            {
            case GoogleARCore.CameraFocusMode.Fixed:
                CameraFocusMode = ApiCameraFocusMode.Fixed;
                break;

            case GoogleARCore.CameraFocusMode.Auto:
                CameraFocusMode = ApiCameraFocusMode.Auto;
                break;

            default:
                CameraFocusMode = ApiCameraFocusMode.Fixed;
                break;
            }

            switch (config.AugmentedFaceMode)
            {
            case AugmentedFaceMode.Disabled:
                FaceMode = ApiAugmentedFaceMode.Disabled;
                break;

            case AugmentedFaceMode.Mesh:
                FaceMode = ApiAugmentedFaceMode.Mesh3D;
                break;

            default:
                FaceMode = ApiAugmentedFaceMode.Disabled;
                break;
            }
        }
Exemple #8
0
        public override IEnumerator StartService(Settings settings)
        {
            if (m_ARCoreSessionConfig == null)
            {
                m_ARCoreSessionConfig = ScriptableObject.CreateInstance <ARCoreSessionConfig>();
            }

            m_ARCoreSessionConfig.EnableLightEstimation = settings.enableLightEstimation;
            m_ARCoreSessionConfig.PlaneFindingMode      = DetectedPlaneFindingMode.HorizontalAndVertical;
            //Do we want to match framerate to the camera?
            m_ARCoreSessionConfig.MatchCameraFramerate = true;

            // Create a GameObject on which the session component will live.
            if (m_ARCoreSession == null)
            {
                var go = new GameObject("ARCore Session");
                go.SetActive(false);
                m_ARCoreSession = go.AddComponent <ARCoreSession>();
                m_ARCoreSession.SessionConfig = m_ARCoreSessionConfig;
                go.SetActive(true);
            }

            // Enabling the session triggers the connection
            m_ARCoreSession.SessionConfig = m_ARCoreSessionConfig;
            m_ARCoreSession.enabled       = true;

            if (!IsSupported)
            {
                switch (Session.Status)
                {
                case SessionStatus.ErrorApkNotAvailable:
                    Debug.LogError("ARCore APK is not installed");
                    yield break;

                case SessionStatus.ErrorPermissionNotGranted:
                    Debug.LogError("A needed permission (likely the camera) has not been granted");
                    yield break;

                case SessionStatus.ErrorSessionConfigurationNotSupported:
                    Debug.LogError("The given ARCore session configuration is not supported on this device");
                    yield break;

                case SessionStatus.FatalError:
                    Debug.LogError("A fatal error was encountered trying to start the ARCore session");
                    yield break;
                }
            }

            while (!Session.Status.IsValid())
            {
                IsRunning = false;

                if (Session.Status.IsError())
                {
                    switch (Session.Status)
                    {
                    case SessionStatus.ErrorPermissionNotGranted:
                        Debug.LogError("A needed permission (likely the camera) has not been granted");
                        yield break;

                    case SessionStatus.FatalError:
                        Debug.LogError("A fatal error was encountered trying to start the ARCore session");
                        yield break;
                    }
                }

                yield return(null);
            }

            // If we make it out of the while loop, then the session is initialized and valid
            IsRunning = true;

            if (IsRunning)
            {
                TextureReader_create((int)k_ImageFormatType, k_ARCoreTextureWidth, k_ARCoreTextureHeight, true);
            }
        }
        public static void UpdateApiConfigWithARCoreSessionConfig(IntPtr sessionHandle,
                                                                  IntPtr configHandle, ARCoreSessionConfig sessionConfig)
        {
            ApiLightEstimationMode lightingMode =
                sessionConfig.LightEstimationMode.ToApiLightEstimationMode();

            ExternApi.ArConfig_setLightEstimationMode(sessionHandle, configHandle, lightingMode);

            ApiPlaneFindingMode planeFindingMode =
                sessionConfig.PlaneFindingMode.ToApiPlaneFindingMode();

            ExternApi.ArConfig_setPlaneFindingMode(sessionHandle, configHandle, planeFindingMode);

            ApiUpdateMode updateMode = sessionConfig.MatchCameraFramerate ?
                                       ApiUpdateMode.Blocking : ApiUpdateMode.LatestCameraImage;

            ExternApi.ArConfig_setUpdateMode(sessionHandle, configHandle, updateMode);

            ApiCloudAnchorMode cloudAnchorMode = sessionConfig.EnableCloudAnchor ?
                                                 ApiCloudAnchorMode.Enabled : ApiCloudAnchorMode.Disabled;

            ExternApi.ArConfig_setCloudAnchorMode(sessionHandle, configHandle, cloudAnchorMode);

            IntPtr augmentedImageDatabaseHandle = IntPtr.Zero;

            if (sessionConfig.AugmentedImageDatabase != null)
            {
                augmentedImageDatabaseHandle = sessionConfig.AugmentedImageDatabase.NativeHandle;
                ExternApi.ArConfig_setAugmentedImageDatabase(sessionHandle, configHandle,
                                                             augmentedImageDatabaseHandle);
            }

            ApiAugmentedFaceMode augmentedFaceMode =
                sessionConfig.AugmentedFaceMode.ToApiAugmentedFaceMode();

            ExternApi.ArConfig_setAugmentedFaceMode(sessionHandle, configHandle, augmentedFaceMode);

            ApiCameraFocusMode focusMode = sessionConfig.CameraFocusMode.ToApiCameraFocusMode();

            ExternApi.ArConfig_setFocusMode(sessionHandle, configHandle, focusMode);
        }
 public bool SetConfiguration(ARCoreSessionConfig config)
 {
     return(m_NativeApi.Session.SetConfiguration(config));
 }
 public bool CheckSupported(ARCoreSessionConfig config)
 {
     return(m_NativeApi.Session.CheckSupported(config) == ApiArStatus.Success);
 }
Exemple #12
0
        /// <summary>
        /// The Unity Update() method.
        /// </summary>
        public void Update()
        {
            _UpdateApplicationLifecycle();

            // If the player has not touched the screen, we are done with this update.
            Touch touch;

            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
            {
                return;
            }

            // Should not handle input if the player is pointing on UI.
            if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                return;
            }

            // Raycast against the location the player touched to search for planes.
            TrackableHit      hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                                              TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit) && !isSpawned)
            {
                // Use hit pose and camera pose to check if hittest is from the
                // back of the plane, if it is, no need to create the anchor.
                if ((hit.Trackable is DetectedPlane) &&
                    Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                                hit.Pose.rotation * Vector3.up) < 0)
                {
                    Debug.Log("Hit at back of the current DetectedPlane");
                }
                else
                {
                    // Choose the prefab based on the Trackable that got hit.
                    GameObject prefab;
                    if (hit.Trackable is FeaturePoint)
                    {
                        prefab = GameObjectPointPrefab;
                    }
                    else if (hit.Trackable is DetectedPlane)
                    {
                        DetectedPlane detectedPlane = hit.Trackable as DetectedPlane;
                        if (detectedPlane.PlaneType == DetectedPlaneType.Vertical)
                        {
                            prefab = GameObjectVerticalPlanePrefab;
                        }
                        else
                        {
                            prefab    = GameObjectHorizontalPlanePrefab;
                            isSpawned = true;
                        }
                    }
                    else
                    {
                        prefab    = GameObjectHorizontalPlanePrefab;
                        isSpawned = true;
                    }

                    // Instantiate prefab at the hit pose.
                    var gameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation);

                    // Compensate for the hitPose rotation facing away from the raycast (i.e.
                    // camera).
                    gameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self);

                    // Create an anchor to allow ARCore to track the hitpoint as understanding of
                    // the physical world evolves.
                    var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                    // Make game object a child of the anchor.
                    gameObject.transform.parent = anchor.transform;
                }
            }

            if (isSpawned)
            {
                ARCoreSession       session  = goARCoreDevice.GetComponent <ARCoreSession>();
                ARCoreSessionConfig myConfig = session.SessionConfig;
                //myConfig.PlaneFindingMode = DetectedPlaneFindingMode.Disabled;
                myConfig.EnablePlaneFinding = false;
            }
        }
Exemple #13
0
 void Awake()
 {
     mySession = transform.parent.GetComponent <ARCoreSessionConfig>();
 }
Exemple #14
0
 public bool Resume(ARCoreSessionConfig sessionConfig)
 {
     return(m_NativeSession.Resume(sessionConfig));
 }
Exemple #15
0
 public void SetReferenceImageDatabase(ARCoreSessionConfig config, ReferenceImageDatabase database)
 {
     m_NativeSession.SessionConfigApi.SetReferenceImageDatabase(IntPtr.Zero, database.m_ReferenceImageDatabaseNativeHandle);
 }
Exemple #16
0
 public void SetConfiguration(ARCoreSessionConfig config)
 {
 }
Exemple #17
0
        public static void UpdateApiConfigWithARCoreSessionConfig(IntPtr sessionHandle,
                                                                  IntPtr configHandle, ARCoreSessionConfig sessionConfig)
        {
            ApiLightEstimationMode lightingMode =
                sessionConfig.LightEstimationMode.ToApiLightEstimationMode();

            ExternApi.ArConfig_setLightEstimationMode(sessionHandle, configHandle, lightingMode);

            ApiPlaneFindingMode planeFindingMode =
                sessionConfig.PlaneFindingMode.ToApiPlaneFindingMode();

            ExternApi.ArConfig_setPlaneFindingMode(sessionHandle, configHandle, planeFindingMode);

            ApiUpdateMode updateMode = sessionConfig.MatchCameraFramerate ?
                                       ApiUpdateMode.Blocking : ApiUpdateMode.LatestCameraImage;

            ExternApi.ArConfig_setUpdateMode(sessionHandle, configHandle, updateMode);

            ApiCloudAnchorMode cloudAnchorMode =
                sessionConfig.CloudAnchorMode.ToApiCloudAnchorMode();

            ExternApi.ArConfig_setCloudAnchorMode(sessionHandle, configHandle, cloudAnchorMode);

            IntPtr augmentedImageDatabaseHandle = IntPtr.Zero;

            if (sessionConfig.AugmentedImageDatabase != null)
            {
                augmentedImageDatabaseHandle = sessionConfig.AugmentedImageDatabase.NativeHandle;
                ExternApi.ArConfig_setAugmentedImageDatabase(sessionHandle, configHandle,
                                                             augmentedImageDatabaseHandle);
            }
            else
            {
                ExternApi.ArConfig_setAugmentedImageDatabase(sessionHandle, configHandle,
                                                             IntPtr.Zero);
            }

            ApiAugmentedFaceMode augmentedFaceMode =
                sessionConfig.AugmentedFaceMode.ToApiAugmentedFaceMode();

            ExternApi.ArConfig_setAugmentedFaceMode(sessionHandle, configHandle, augmentedFaceMode);

            ApiCameraFocusMode focusMode = sessionConfig.CameraFocusMode.ToApiCameraFocusMode();

            ExternApi.ArConfig_setFocusMode(sessionHandle, configHandle, focusMode);

            if (!InstantPreviewManager.IsProvidingPlatform)
            {
                ApiDepthMode depthMode = sessionConfig.DepthMode.ToApiDepthMode();
                ExternApi.ArConfig_setDepthMode(sessionHandle, configHandle, depthMode);
            }
        }
Exemple #18
0
        /// <summary>
        /// Wrap an ARCoreSessionConfig in an API config.
        /// </summary>
        /// <param name="config">Config to wrap.</param>
        public ApiPrestoConfig(ARCoreSessionConfig config)
        {
            UpdateMode = config.MatchCameraFramerate ?
                         ApiUpdateMode.Blocking : ApiUpdateMode.LatestCameraImage;
            var planeFindingMode = ApiPlaneFindingMode.Disabled;

            switch (config.PlaneFindingMode)
            {
            case DetectedPlaneFindingMode.Horizontal:
                planeFindingMode = ApiPlaneFindingMode.Horizontal;
                break;

            case DetectedPlaneFindingMode.Vertical:
                planeFindingMode = ApiPlaneFindingMode.Vertical;
                break;

            case DetectedPlaneFindingMode.HorizontalAndVertical:
                planeFindingMode = ApiPlaneFindingMode.HorizontalAndVertical;
                break;

            default:
                break;
            }

            PlaneFindingMode    = planeFindingMode;
            LightEstimationMode = config.EnableLightEstimation ?
                                  ApiLightEstimationMode.AmbientIntensity : ApiLightEstimationMode.Disabled;
            CloudAnchorMode = config.EnableCloudAnchor ?
                              ApiCloudAnchorMode.Enabled : ApiCloudAnchorMode.Disabled;

            if (config.AugmentedImageDatabase != null)
            {
                ArPrestoAugmentedImageDatabase = config.AugmentedImageDatabase.m_ArPrestoDatabaseHandle;
            }
            else
            {
                ArPrestoAugmentedImageDatabase = IntPtr.Zero;
            }

            switch (config.CameraFocusMode)
            {
            case GoogleARCore.CameraFocusMode.Fixed:
                CameraFocusMode = ApiCameraFocusMode.Fixed;
                break;

            case GoogleARCore.CameraFocusMode.Auto:
#if UNITY_EDITOR
                CameraFocusMode = ApiCameraFocusMode.Fixed;
                UnityEngine.Debug.LogWarningFormat(
                    "Camera focus mode '{0}' not supported in editor.\n" +
                    "Using camera focus mode '{1}' instead.", config.CameraFocusMode, CameraFocusMode);
#else
                CameraFocusMode = ApiCameraFocusMode.Auto;
#endif
                break;

            default:
                CameraFocusMode = ApiCameraFocusMode.Fixed;
                break;
            }
        }