Ejemplo n.º 1
0
        public void CreateSession(ARCoreSession sessionComponent)
        {
            sessionComponent.StartCoroutine(InstantPreviewManager.InitializeIfNeeded());

            if (SessionComponent != null)
            {
                Debug.LogError("Multiple ARCore session components cannot exist in the scene. " +
                               "Destroying the newest.");
                GameObject.Destroy(sessionComponent);
                return;
            }

            SessionComponent = sessionComponent;
        }
Ejemplo n.º 2
0
        public void GetImageDimensions(IntPtr cameraConfigHandle, out int width, out int height)
        {
            width  = 0;
            height = 0;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access ARCamera image dimensions");
                return;
            }

            ExternApi.ArCameraConfig_getImageDimensions(
                m_NativeSession.SessionHandle, cameraConfigHandle, ref width, ref height);
        }
Ejemplo n.º 3
0
        public CameraConfigDepthSensorUsages GetDepthSensorUsage(IntPtr cameraConfigHandle)
        {
            int depthSensorUsage = (int)CameraConfigDepthSensorUsages.DoNotUse;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access ARCamera DepthSensorUsage");
                return((CameraConfigDepthSensorUsages)depthSensorUsage);
            }

            ExternApi.ArCameraConfig_getDepthSensorUsage(
                m_NativeSession.SessionHandle, cameraConfigHandle, ref depthSensorUsage);
            return((CameraConfigDepthSensorUsages)depthSensorUsage);
        }
Ejemplo n.º 4
0
        public void GetFpsRange(IntPtr cameraConfigHandle, out int minFps, out int maxFps)
        {
            minFps = 0;
            maxFps = 0;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access ARCamera FpsRange");
                return;
            }

            ExternApi.ArCameraConfig_getFpsRange(
                m_NativeSession.SessionHandle, cameraConfigHandle, ref minFps, ref maxFps);
        }
Ejemplo n.º 5
0
        public LostTrackingReason GetLostTrackingReason(IntPtr cameraHandle)
        {
            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("determine tracking failure " +
                                                               "reasons");
                return(LostTrackingReason.None);
            }

            ApiTrackingFailureReason apiTrackingFailureReason = ApiTrackingFailureReason.None;

            ExternApi.ArCamera_getTrackingFailureReason(m_NativeSession.SessionHandle,
                                                        cameraHandle, ref apiTrackingFailureReason);
            return(apiTrackingFailureReason.ToLostTrackingReason());
        }
Ejemplo n.º 6
0
        public ApiCameraConfigFacingDirection GetFacingDirection(IntPtr cameraConfigHandle)
        {
            ApiCameraConfigFacingDirection direction = ApiCameraConfigFacingDirection.Back;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access ARCamera facing direction");
                return(direction);
            }

            ExternApi.ArCameraConfig_getFacingDirection(
                m_NativeSession.SessionHandle, cameraConfigHandle, ref direction);

            return(direction);
        }
Ejemplo n.º 7
0
        public CameraConfig GetCameraConfig()
        {
            IntPtr cameraConfigHandle = m_NativeSession.CameraConfigApi.Create();

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access camera config");
                return(new CameraConfig());
            }

            ExternApi.ArSession_getCameraConfig(m_NativeSession.SessionHandle, cameraConfigHandle);
            CameraConfig currentCameraConfig = _CreateCameraConfig(cameraConfigHandle);

            m_NativeSession.CameraConfigApi.Destroy(cameraConfigHandle);
            return(currentCameraConfig);
        }
Ejemplo n.º 8
0
        private void _SetConfiguration(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.m_IsDirty) &&
                !ExperimentManager.Instance.IsConfigurationDirty)
            {
                return;
            }

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                if (config.LightEstimationMode != LightEstimationMode.Disabled)
                {
                    InstantPreviewManager.LogLimitedSupportMessage("enable 'Light Estimation'");
                    config.LightEstimationMode = LightEstimationMode.Disabled;
                }

                if (config.AugmentedImageDatabase != null)
                {
                    InstantPreviewManager.LogLimitedSupportMessage("enable 'Augmented Images'");
                    config.AugmentedImageDatabase = null;
                }

                if (config.AugmentedFaceMode == AugmentedFaceMode.Mesh)
                {
                    InstantPreviewManager.LogLimitedSupportMessage("enable 'Augmented Faces'");
                    config.AugmentedFaceMode = AugmentedFaceMode.Disabled;
                }
            }

            var prestoConfig = new ApiPrestoConfig(config);

            ExternApi.ArPresto_setConfiguration(ref prestoConfig);
            m_CachedConfig = ScriptableObject.CreateInstance <ARCoreSessionConfig>();
            m_CachedConfig.CopyFrom(config);
        }
Ejemplo n.º 9
0
        private void _UpdateTextureIfNeeded()
        {
            // If running in editor, updates background texture from Instant Preview only.
            Texture2D previewBackgroundTexture = BackgroundTexture;

            if (InstantPreviewManager.UpdateBackgroundTextureIfNeeded(ref previewBackgroundTexture))
            {
                BackgroundTexture = previewBackgroundTexture;
                return;
            }

            IntPtr frameHandle = IntPtr.Zero;

            ExternApi.ArPresto_getFrame(ref frameHandle);

            int backgroundTextureId = ExternApi.ArCoreUnity_getBackgroundTextureId();

            if (frameHandle == IntPtr.Zero)
            {
                // This prevents using a texture that has not been filled out by ARCore.
                return;
            }
            else if (backgroundTextureId == -1)
            {
                return;
            }
            else if (BackgroundTexture != null &&
                     BackgroundTexture.GetNativeTexturePtr().ToInt32() == backgroundTextureId)
            {
                return;
            }
            else if (BackgroundTexture == null)
            {
                // The Unity-cached size and format of the texture (0x0, ARGB) is not the
                // actual format of the texture. This is okay because the texture is not
                // accessed by pixels, it is accessed with UV coordinates.
                BackgroundTexture = Texture2D.CreateExternalTexture(
                    0, 0, TextureFormat.ARGB32, false, false, new IntPtr(backgroundTextureId));
                return;
            }

            BackgroundTexture.UpdateExternalTexture(new IntPtr(backgroundTextureId));
        }
Ejemplo n.º 10
0
        public AsyncTask <ApkAvailabilityStatus> CheckApkAvailability()
        {
            Action <ApkAvailabilityStatus>    onComplete;
            AsyncTask <ApkAvailabilityStatus> task =
                new AsyncTask <ApkAvailabilityStatus>(out onComplete);

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("determine ARCore APK " +
                                                               "availability");
                return(task);
            }

            ExternApi.ArPresto_checkApkAvailability(m_CheckApkAvailabilityResultCallback,
                                                    IntPtr.Zero);

            m_PendingAvailabilityCheckCallbacks.Add(onComplete);

            return(task);
        }
Ejemplo n.º 11
0
        public AsyncTask <ApkInstallationStatus> RequestApkInstallation(bool userRequested)
        {
            Action <ApkInstallationStatus>    onComplete;
            AsyncTask <ApkInstallationStatus> task =
                new AsyncTask <ApkInstallationStatus>(out onComplete);

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("request installation of ARCore " +
                                                               "APK");
                return(task);
            }

            ExternApi.ArPresto_requestApkInstallation(userRequested,
                                                      m_RequestApkInstallationResultCallback, IntPtr.Zero);

            m_PendingInstallationRequestCallbacks.Add(onComplete);

            return(task);
        }
Ejemplo n.º 12
0
        public CameraIntrinsics GetTextureIntrinsics(IntPtr cameraHandle)
        {
            IntPtr cameraIntrinsicsHandle = IntPtr.Zero;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access GPU texture intrinsics");
                return(new CameraIntrinsics());
            }

            ExternApi.ArCameraIntrinsics_create(
                m_NativeSession.SessionHandle, ref cameraIntrinsicsHandle);

            ExternApi.ArCamera_getTextureIntrinsics(
                m_NativeSession.SessionHandle, cameraHandle, cameraIntrinsicsHandle);

            CameraIntrinsics textureIntrinsics =
                _GetCameraIntrinsicsFromHandle(cameraIntrinsicsHandle);

            ExternApi.ArCameraIntrinsics_destroy(cameraIntrinsicsHandle);

            return(textureIntrinsics);
        }
Ejemplo n.º 13
0
        private bool _SetCameraDirection(DeviceCameraDirection cameraDirection)
        {
            // The camera direction has not changed.
            if (m_CachedCameraDirection.HasValue &&
                m_CachedCameraDirection.Value == cameraDirection)
            {
                return(false);
            }

            if (InstantPreviewManager.IsProvidingPlatform &&
                cameraDirection == DeviceCameraDirection.BackFacing)
            {
                return(false);
            }
            else if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage(
                    "enable front-facing (selfie) camera");
                m_CachedCameraDirection = DeviceCameraDirection.BackFacing;
                if (SessionComponent != null)
                {
                    SessionComponent.DeviceCameraDirection = DeviceCameraDirection.BackFacing;
                }

                return(false);
            }

            m_CachedCameraDirection = cameraDirection;
            var apiCameraDirection =
                cameraDirection == DeviceCameraDirection.BackFacing ?
                ApiPrestoDeviceCameraDirection.BackFacing :
                ApiPrestoDeviceCameraDirection.FrontFacing;

            ExternApi.ArPresto_setDeviceCameraDirection(apiCameraDirection);

            return(true);
        }
Ejemplo n.º 14
0
        public Int32 AddImageAtRuntime(
            IntPtr databaseHandle, string name, Texture2D image, float width)
        {
            Int32 outIndex = -1;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("add images to Augmented Image " +
                                                               "database");
                return(outIndex);
            }

            GCHandle grayscaleBytesHandle = _ConvertTextureToGrayscaleBytes(image);

            if (grayscaleBytesHandle.AddrOfPinnedObject() == IntPtr.Zero)
            {
                return(-1);
            }

            ApiArStatus status = ExternApi.ArPrestoAugmentedImageDatabase_addImageAtRuntime(
                databaseHandle, name, grayscaleBytesHandle.AddrOfPinnedObject(), image.width,
                image.height, image.width, width, ref outIndex);

            if (grayscaleBytesHandle.IsAllocated)
            {
                grayscaleBytesHandle.Free();
            }

            if (status != ApiArStatus.Success)
            {
                Debug.LogWarningFormat(
                    "Failed to add aumented image at runtime with status {0}", status);
                return(-1);
            }

            return(outIndex);
        }
Ejemplo n.º 15
0
        public bool GetAllCameraMetadataTags(
            IntPtr cameraMetadataHandle, List <CameraMetadataTag> resultList)
        {
            IntPtr ndkMetadataHandle = IntPtr.Zero;

            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access camera metadata tags");
                return(false);
            }

            ExternApi.ArImageMetadata_getNdkCameraMetadata(m_NativeSession.SessionHandle,
                                                           cameraMetadataHandle, ref ndkMetadataHandle);

            IntPtr          tagsHandle = IntPtr.Zero;
            int             tagsCount  = 0;
            NdkCameraStatus status     = ExternApi.ACameraMetadata_getAllTags(
                ndkMetadataHandle, ref tagsCount, ref tagsHandle);

            if (status != NdkCameraStatus.Ok)
            {
                ARDebug.LogErrorFormat(
                    "ACameraMetadata_getAllTags error with native camera error code: {0}",
                    status);
                return(false);
            }

            for (int i = 0; i < tagsCount; i++)
            {
                resultList.Add((CameraMetadataTag)Marshal.PtrToStructure(
                                   MarshalingHelper.GetPtrToUnmanagedArrayElement <int>(tagsHandle, i),
                                   typeof(int)));
            }

            return(true);
        }