コード例 #1
0
        private void SetSessionEnabled(bool sessionEnabled)
        {
            if (sessionEnabled && SessionComponent == null)
            {
                return;
            }

            // If the session status is an error, do not fire the callback itself; but do
            // ArPresto_setEnabled to signal the intention to resume once the session status is
            // valid.
            if (!SessionStatus.IsError())
            {
                FireOnSessionSetEnabled(sessionEnabled);
            }

            ExternApi.ArPresto_setEnabled(sessionEnabled);
        }
コード例 #2
0
        private void _OnEarlyUpdate()
        {
            _SetCameraTextureName();

            // Update session activity before EarlyUpdate.
            if (m_HaveDisableToEnableTransition)
            {
                _SetSessionEnabled(false);
                _SetSessionEnabled(true);
                m_HaveDisableToEnableTransition = false;

                // Avoid firing session enable event twice.
                if (m_DesiredSessionState.HasValue && m_DesiredSessionState.Value)
                {
                    m_DesiredSessionState = null;
                }
            }

            if (m_DesiredSessionState.HasValue)
            {
                _SetSessionEnabled(m_DesiredSessionState.Value);
                m_DesiredSessionState = null;
            }

            // Perform updates before calling ArPresto_update.
            if (SessionComponent != null)
            {
                IntPtr previousSession = IntPtr.Zero;
                ExternApi.ArPresto_getSession(ref previousSession);

                if (UpdateSessionFeatures != null)
                {
                    UpdateSessionFeatures();
                }

                _SetCameraDirection(SessionComponent.DeviceCameraDirection);

                IntPtr currentSession = IntPtr.Zero;
                ExternApi.ArPresto_getSession(ref currentSession);

                // Fire the session enabled event when the underlying session has been changed
                // due to session feature update(camera direction etc).
                if (previousSession != currentSession)
                {
                    _FireOnSessionSetEnabled(false);
                    _FireOnSessionSetEnabled(true);
                }

                // Validate and convert the SessionConfig to a Instant Preview supported config by
                // logging and disabling limited supported features.
                if (InstantPreviewManager.IsProvidingPlatform &&
                    SessionComponent.SessionConfig != null &&
                    !InstantPreviewManager.ValidateSessionConfig(SessionComponent.SessionConfig))
                {
                    // A new SessionConfig object will be created based on the original
                    // SessionConfig with all limited support features disabled.
                    SessionComponent.SessionConfig =
                        InstantPreviewManager.GenerateInstantPreviewSupportedConfig(
                            SessionComponent.SessionConfig);
                }

                _UpdateConfiguration(SessionComponent.SessionConfig);
            }

            _UpdateDisplayGeometry();

            // Update ArPresto and potentially ArCore.
            ExternApi.ArPresto_update();
            if (SystemInfo.graphicsMultiThreaded && !InstantPreviewManager.IsProvidingPlatform)
            {
                // Synchronize render thread with update call.
                ExternApi.ARCoreRenderingUtils_CreatePostUpdateFence();
            }

            SessionStatus previousSessionStatus = SessionStatus;

            // Get state information from ARPresto.
            ApiPrestoStatus prestoStatus = ApiPrestoStatus.Uninitialized;

            ExternApi.ArPresto_getStatus(ref prestoStatus);
            SessionStatus = prestoStatus.ToSessionStatus();

            LostTrackingReason = LostTrackingReason.None;
            if (NativeSession != null && SessionStatus == SessionStatus.LostTracking)
            {
                var cameraHandle = NativeSession.FrameApi.AcquireCamera();
                LostTrackingReason = NativeSession.CameraApi.GetLostTrackingReason(cameraHandle);
                NativeSession.CameraApi.Release(cameraHandle);
            }

            // If the current status is an error, check if the SessionStatus error state changed.
            if (SessionStatus.IsError() &&
                previousSessionStatus.IsError() != SessionStatus.IsError())
            {
                // Disable internal session bits so we properly pause the session due to error.
                _FireOnSessionSetEnabled(false);
                m_DisabledSessionOnErrorState = true;
            }
            else if (SessionStatus.IsValid() && m_DisabledSessionOnErrorState)
            {
                if (SessionComponent.enabled)
                {
                    _FireOnSessionSetEnabled(true);
                }

                m_DisabledSessionOnErrorState = false;
            }

            // Get the current session from presto and note if it has changed.
            IntPtr sessionHandle = IntPtr.Zero;

            ExternApi.ArPresto_getSession(ref sessionHandle);
            IsSessionChangedThisFrame = m_CachedSessionHandle != sessionHandle;
            m_CachedSessionHandle     = sessionHandle;

            ExternApi.ArPresto_getFrame(ref m_CachedFrameHandle);

            // Update the native session with the newest frame.
            if (NativeSession != null)
            {
                NativeSession.OnUpdate(m_CachedFrameHandle);
            }

            _UpdateTextureIfNeeded();

            if (EarlyUpdate != null)
            {
                EarlyUpdate();
            }
        }
コード例 #3
0
    void Update()
    {
        if (!isInitialized)
        {
            return;
        }

        // check for errors
        _QuitOnConnectionErrors();

        // check for input (touch)
        CheckForInputAction();

        // estimate the tracking state
        SessionStatus status = Session.Status;

        if (status.IsError() || status.IsNotInitialized())
        {
            cameraTrackingState = TrackingState.Stopped;
            return;
        }
        else if (status == SessionStatus.Tracking)
        {
            cameraTrackingState = TrackingState.Tracking;
        }
        else
        {
            cameraTrackingState = TrackingState.Paused;
        }

        // get frame timestamp and light intensity
        lastFrameTimestamp = GetCurrentTimestamp();

        if (Frame.LightEstimate.State == LightEstimateState.Valid)
        {
            // Normalize pixel intensity by middle gray in gamma space.
            const float middleGray = 0.466f;
            currentLightIntensity = Frame.LightEstimate.PixelIntensity / middleGray;
        }

        // get point cloud, if needed
        MultiARInterop.MultiARData arData = arManager.GetARData();

        if (arManager.usePointCloudData)
        {
            if (Frame.PointCloud.PointCount > 0 && Frame.PointCloud.IsUpdatedThisFrame)
            {
                // Copy the point cloud points
                for (int i = 0; i < Frame.PointCloud.PointCount; i++)
                {
                    PointCloudPoint point = Frame.PointCloud.GetPointAsStruct(i);
                    arData.pointCloudData[i] = new Vector3(point.Position.x, point.Position.y, point.Position.z);
                }

                arData.pointCloudLength    = Frame.PointCloud.PointCount;
                arData.pointCloudTimestamp = lastFrameTimestamp;
            }
        }

//		// display the tracked planes if needed
//		if(arManager.displayTrackedSurfaces && trackedPlanePrefab)
//		{
//			// get the new planes
//			Frame.GetNewPlanes(ref newTrackedPlanes);
//
//			// Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
//			for (int i = 0; i < newTrackedPlanes.Count; i++)
//			{
//				// Instantiate a plane visualization prefab and set it to track the new plane.
//				GameObject planeObject = Instantiate(trackedPlanePrefab, Vector3.zero, Quaternion.identity);
//				planeObject.GetComponent<GoogleARCore.HelloAR.TrackedPlaneVisualizer>().SetTrackedPlane(newTrackedPlanes[i]);
//
//				// Apply a random color and grid rotation.
//				planeObject.GetComponent<Renderer>().material.SetColor("_GridColor", planeColors[Random.Range(0, planeColors.Length - 1)]);
//				planeObject.GetComponent<Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f));
//			}
//		}

        // get all tracked planes
        Session.GetTrackables <DetectedPlane>(allTrackedPlanes, TrackableQueryFilter.All);

        // create overlay surfaces as needed
        if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
        {
            alSurfacesToDelete.Clear();
            alSurfacesToDelete.AddRange(arData.dictOverlaySurfaces.Keys);

            // estimate the material
            Material surfaceMat   = arManager.GetSurfaceMaterial();
            int      surfaceLayer = MultiARInterop.GetSurfaceLayer();

            for (int i = 0; i < allTrackedPlanes.Count; i++)
            {
                string surfId = allTrackedPlanes[i].m_TrackableNativeHandle.ToString();

                if (!arData.dictOverlaySurfaces.ContainsKey(surfId))
                {
                    GameObject overlaySurfaceObj = new GameObject();
                    overlaySurfaceObj.name = "surface-" + surfId;

                    overlaySurfaceObj.layer = surfaceLayer;
                    overlaySurfaceObj.transform.SetParent(arData.surfaceRendererRoot ? arData.surfaceRendererRoot.transform : null);

//					GameObject overlayCubeObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
//					overlayCubeObj.name = "surface-cube-" + surfId;
//					overlayCubeObj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
//					overlayCubeObj.transform.SetParent(overlaySurfaceObj.transform);

                    OverlaySurfaceUpdater overlaySurface = overlaySurfaceObj.AddComponent <OverlaySurfaceUpdater>();
                    overlaySurface.SetSurfaceMaterial(surfaceMat);
                    overlaySurface.SetSurfaceCollider(arManager.surfaceCollider, arManager.colliderMaterial);

                    arData.dictOverlaySurfaces.Add(surfId, overlaySurface);
                }

                // update the surface mesh
                bool bValidSurface = UpdateOverlaySurface(arData.dictOverlaySurfaces[surfId], allTrackedPlanes[i]);

                if (bValidSurface && alSurfacesToDelete.Contains(surfId))
                {
                    alSurfacesToDelete.Remove(surfId);
                }
            }

            // delete not tracked surfaces
            foreach (string surfId in alSurfacesToDelete)
            {
                OverlaySurfaceUpdater overlaySurface = arData.dictOverlaySurfaces[surfId];
                arData.dictOverlaySurfaces.Remove(surfId);

                Destroy(overlaySurface.gameObject);
            }
        }

        // check status of the anchors
        List <string> alAnchorsToRemove = new List <string>();

        foreach (string anchorId in arData.allAnchorsDict.Keys)
        {
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchorId];

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                Transform parentTrans = anchoredObj.transform.parent;

                if (parentTrans == null)
                {
                    if (!alAnchorsToRemove.Contains(anchorId))
                    {
                        alAnchorsToRemove.Add(anchorId);
                    }
                    anchoredObj.SetActive(false);
                }
                else
                {
                    Anchor anchor = parentTrans.GetComponent <Anchor>();

                    if (anchor == null || anchor.TrackingState == TrackingState.Stopped)
                    {
                        if (!alAnchorsToRemove.Contains(anchorId))
                        {
                            alAnchorsToRemove.Add(anchorId);
                        }

                        anchoredObj.transform.parent = null;
                        anchoredObj.SetActive(false);
                    }
                }
            }
        }

        // remove the stopped anchors from our list
        foreach (string anchorId in alAnchorsToRemove)
        {
            arData.allAnchorsDict.Remove(anchorId);
        }

        // clean up
        alAnchorsToRemove.Clear();

        // look for image anchors, if enabled
        if (arData.imageAnchorsEnabled)
        {
            // Get updated augmented images for this frame.
            Session.GetTrackables <AugmentedImage>(alTrackedAugmentedImages, TrackableQueryFilter.Updated);

            foreach (var image in alTrackedAugmentedImages)
            {
                string sImageName      = image.Name;
                bool   wasImageTracked = dictImageAnchors.ContainsKey(sImageName);

                if (!wasImageTracked && image.TrackingState == TrackingState.Tracking)
                {
                    // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                    Anchor anchor = image.CreateAnchor(image.CenterPose);
                    anchor.gameObject.name = "ImageAnchor-" + sImageName;
                    DontDestroyOnLoad(anchor.gameObject);

                    alImageAnchorNames.Add(sImageName);
                    dictImageAnchors.Add(sImageName, anchor.gameObject);
                }
                else if (wasImageTracked && image.TrackingState == TrackingState.Stopped)
                {
                    // remove the anchor
                    GameObject anchorObj = dictImageAnchors[sImageName];

                    alImageAnchorNames.Remove(sImageName);
                    dictImageAnchors.Remove(sImageName);

                    GameObject.Destroy(anchorObj);
                }
            }
        }
    }
コード例 #4
0
        private void _OnEarlyUpdate()
        {
            // Update session activity before EarlyUpdate.
            if (m_HaveDisableToEnableTransition)
            {
                _SetSessionEnabled(false);
                _SetSessionEnabled(true);
                m_HaveDisableToEnableTransition = false;

                // Avoid firing session enable event twice.
                if (m_DesiredSessionState.HasValue && m_DesiredSessionState.Value)
                {
                    m_DesiredSessionState = null;
                }
            }

            if (m_DesiredSessionState.HasValue)
            {
                _SetSessionEnabled(m_DesiredSessionState.Value);
                m_DesiredSessionState = null;
            }

            // Perform updates before calling ArPresto_update.
            if (SessionComponent != null)
            {
                IntPtr previousSession = IntPtr.Zero;
                ExternApi.ArPresto_getSession(ref previousSession);

                if (UpdateSessionFeatures != null)
                {
                    UpdateSessionFeatures();
                }

                _SetCameraDirection(SessionComponent.DeviceCameraDirection);

                IntPtr currentSession = IntPtr.Zero;
                ExternApi.ArPresto_getSession(ref currentSession);

                // Fire the session enabled event when the underlying session has been changed
                // due to session feature update(camera direction etc).
                if (previousSession != currentSession)
                {
                    _FireOnSessionSetEnabled(false);
                    _FireOnSessionSetEnabled(true);
                }

                _SetConfiguration(SessionComponent.SessionConfig);
            }

            _UpdateDisplayGeometry();

            // Update ArPresto and potentially ArCore.
            ExternApi.ArPresto_update();

            SessionStatus previousSessionStatus = SessionStatus;

            // Get state information from ARPresto.
            ApiPrestoStatus prestoStatus = ApiPrestoStatus.Uninitialized;

            ExternApi.ArPresto_getStatus(ref prestoStatus);
            SessionStatus = prestoStatus.ToSessionStatus();

            LostTrackingReason = LostTrackingReason.None;
            if (NativeSession != null && SessionStatus == SessionStatus.LostTracking)
            {
                var cameraHandle = NativeSession.FrameApi.AcquireCamera();
                LostTrackingReason = NativeSession.CameraApi.GetLostTrackingReason(cameraHandle);
                NativeSession.CameraApi.Release(cameraHandle);
            }

            // If the current status is an error, check if the SessionStatus error state changed.
            if (SessionStatus.IsError() &&
                previousSessionStatus.IsError() != SessionStatus.IsError())
            {
                // Disable internal session bits so we properly pause the session due to error.
                _FireOnSessionSetEnabled(false);
                m_DisabledSessionOnErrorState = true;
            }
            else if (SessionStatus.IsValid() && m_DisabledSessionOnErrorState)
            {
                if (SessionComponent.enabled)
                {
                    _FireOnSessionSetEnabled(true);
                }

                m_DisabledSessionOnErrorState = false;
            }

            // Get the current session from presto and note if it has changed.
            IntPtr sessionHandle = IntPtr.Zero;

            ExternApi.ArPresto_getSession(ref sessionHandle);
            IsSessionChangedThisFrame = m_CachedSessionHandle != sessionHandle;
            m_CachedSessionHandle     = sessionHandle;

            ExternApi.ArPresto_getFrame(ref m_CachedFrameHandle);

            // Update the native session with the newest frame.
            if (NativeSession != null)
            {
                NativeSession.OnUpdate(m_CachedFrameHandle);
            }

            _UpdateTextureIfNeeded();

            if (EarlyUpdate != null)
            {
                EarlyUpdate();
            }
        }