private void _Connect()
        {
            ARDebug.LogInfo("_connect begin");
            const string ANDROID_CAMERA_PERMISSION_NAME = "android.permission.CAMERA";

            if (AndroidPermissionsRequest.IsPermissionGranted(ANDROID_CAMERA_PERMISSION_NAME))
            {
                _ConnectToService();
                return;
            }
            var permissionsArray = new string[] { ANDROID_CAMERA_PERMISSION_NAME };

            AndroidPermissionsRequest.RequestPermission(permissionsArray).ThenAction((requestResult) =>
            {
                if (requestResult.IsAllGranted)
                {
                    _ConnectToService();
                }
                else
                {
                    ARDebug.LogError("connection failed because a needed permission was rejected.");
                    errorMessage = "This app require camera permission";
                    Log();
                    Invoke("_DoQuit", 0.5f);
                    return;
                }
            });
        }
Esempio n. 2
0
    private void _ResumeSession(ARCoreSessionConfig sessionConfig, Action <SessionConnectionState> onComplete)
    {
        if (!m_SessionManager.CheckSupported(sessionConfig))
        {
            ARDebug.LogError("The requested ARCore session configuration is not supported.");
            onComplete(SessionConnectionState.InvalidConfiguration);
            return;
        }

        if (!m_SessionManager.SetConfiguration(sessionConfig))
        {
            ARDebug.LogError("ARCore connection failed because the current configuration is not supported.");
            onComplete(SessionConnectionState.InvalidConfiguration);
            return;
        }

        Frame.Initialize(m_SessionManager.FrameManager);

        if (!m_SessionManager.Resume(sessionConfig))
        {
            onComplete(SessionConnectionState.ConnectToServiceFailed);
        }
        else
        {
            onComplete(SessionConnectionState.Connected);
        }
    }
 public void OnApplicationPause(bool isPaused)
 {
     if (isPaused)
     {
         ARSession.Pause();
     }
     else
     {
         if (!isSessionCreated)
         {
             Init();
         }
         if (isErrorHappendWhenInit)
         {
             return;
         }
         try
         {
             ARSession.Resume();
         }
         catch (ARCameraPermissionDeniedException e)
         {
             ARDebug.LogError("camera permission is denied");
             errorMessage = "This app require camera permission";
             Log();
             Invoke("_DoQuit", 0.5f);
         }
     }
 }
Esempio n. 4
0
        public void Connect_NoPermission(ARCoreSessionConfig sessionConfig)
        {
            if (m_SessionManager == null)
            {
                ARDebug.LogError("Cannot connect because ARCoreSession failed to initialize.");
            }

            if (sessionConfig == null)
            {
                ARDebug.LogError("Unable to connect ARSession session due to missing ARSessionConfig.");
                m_SessionManager.ConnectionState = SessionConnectionState.MissingConfiguration;
            }

            // We have already connected at least once.
            if (Session.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Multiple attempts to connect to the ARSession.  Note that the ARSession connection " +
                                 "spans the lifetime of the application and cannot be reconfigured.  This will change in future " +
                                 "versions of ARCore.");
            }

            // Create an asynchronous task for the potential permissions flow and service connection.
            Action <SessionConnectionState> onTaskComplete;
            var returnTask = new AsyncTask <SessionConnectionState>(out onTaskComplete);

            returnTask.ThenAction((connectionState) =>
            {
                m_SessionManager.ConnectionState = connectionState;
            });

            _ResumeSession(sessionConfig, onTaskComplete);
        }
Esempio n. 5
0
        /// <summary>
        /// Connects to the ARCore service.
        /// </summary>
        /// <param name="sessionConfig">The session configuration to connect with.</param>
        /// <param name="onComplete">A callback for when the result of the connection attempt is known.</param>
        private void _ResumeSession(ARCoreSessionConfig sessionConfig, Action <SessionConnectionState> onComplete)
        {
            if (!m_SessionManager.CheckSupported(sessionConfig))
            {
                ARDebug.LogError("The requested ARCore session configuration is not supported.");
                onComplete(SessionConnectionState.InvalidConfiguration);
                return;
            }

            if (!m_SessionManager.SetConfiguration(sessionConfig))
            {
                ARDebug.LogError("ARCore connection failed because the current configuration is not supported.");
                onComplete(SessionConnectionState.InvalidConfiguration);
                return;
            }

            Frame.Initialize(m_SessionManager.FrameManager);

            // ArSession_resume needs to be called in the UI thread due to b/69682628.
            AsyncTask.PerformActionInUIThread(() =>
            {
                if (!m_SessionManager.Resume())
                {
                    onComplete(SessionConnectionState.ConnectToServiceFailed);
                }
                else
                {
                    onComplete(SessionConnectionState.Connected);
                }
            });
        }
Esempio n. 6
0
        /// <summary>
        /// Unity Awake.
        /// </summary>
        public void Awake()
        {
            if (Application.isEditor)
            {
                enabled = false;
                return;
            }

            if (FindObjectsOfType <ARCoreSession>().Length > 1)
            {
                ARDebug.LogError("Multiple SessionComponents present in the game scene.  Destroying the gameobject " +
                                 "of the newest one.");
                Destroy(gameObject);
                return;
            }

            m_SessionManager = SessionManager.CreateSession();
            Session.Initialize(m_SessionManager);

            if (Session.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Could not create an ARCore session.  The current Unity Editor may not support this " +
                                 "version of ARCore.");
                return;
            }

            if (ConnectOnAwake)
            {
                Connect();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Connects an ARSession.  Note that if user permissions are needed they will be requested and thus this is an
        /// asynchronous method.
        /// </summary>
        /// <param name="sessionConfig">The session configuration.</param>
        /// <returns>An {@link AsyncTask<T>} that completes when the connection has been made or failed. </returns>
        public AsyncTask <SessionConnectionState> Connect(ARCoreSessionConfig sessionConfig)
        {
            const string androidCameraPermissionName = "android.permission.CAMERA";

            if (m_SessionManager == null)
            {
                ARDebug.LogError("Cannot connect because ARCoreSession failed to initialize.");
                return(new AsyncTask <SessionConnectionState>(SessionConnectionState.Uninitialized));
            }

            if (sessionConfig == null)
            {
                ARDebug.LogError("Unable to connect ARSession session due to missing ARSessionConfig.");
                m_SessionManager.ConnectionState = SessionConnectionState.MissingConfiguration;
                return(new AsyncTask <SessionConnectionState>(Session.ConnectionState));
            }

            // We have already connected at least once.
            if (Session.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Multiple attempts to connect to the ARSession.  Note that the ARSession connection " +
                                 "spans the lifetime of the application and cannot be reconfigured.  This will change in future " +
                                 "versions of ARCore.");
                return(new AsyncTask <SessionConnectionState>(Session.ConnectionState));
            }

            // Create an asynchronous task for the potential permissions flow and service connection.
            Action <SessionConnectionState> onTaskComplete;
            var returnTask = new AsyncTask <SessionConnectionState>(out onTaskComplete);

            returnTask.ThenAction((connectionState) =>
            {
                m_SessionManager.ConnectionState = connectionState;
            });

            // Attempt service connection immediately if permissions are granted.
            if (AndroidPermissionsManager.IsPermissionGranted(androidCameraPermissionName))
            {
                _ResumeSession(sessionConfig, onTaskComplete);
                return(returnTask);
            }

            // Request needed permissions and attempt service connection if granted.
            AndroidPermissionsManager.RequestPermission(androidCameraPermissionName).ThenAction((requestResult) =>
            {
                if (requestResult.IsAllGranted)
                {
                    _ResumeSession(sessionConfig, onTaskComplete);
                }
                else
                {
                    ARDebug.LogError("ARCore connection failed because a needed permission was rejected.");
                    onTaskComplete(SessionConnectionState.UserRejectedNeededPermission);
                }
            });

            return(returnTask);
        }
Esempio n. 8
0
        /// <summary>
        /// Update the plane's data with APIPlaneData
        /// Note that this will convert plane's pose from Tango space to Unity world space.
        /// </summary>
        /// <param name="apiPlaneData">ApiPlaneData source.</param>
        /// <param name="subsumedBy">The plane subsuming this plane or null.</param>
        /// <param name="forceUpdate">Force to update.</param>
        private void _UpdatePlaneIfNeeded(ApiPlaneData apiPlaneData, TrackedPlane subsumedBy, bool forceUpdate)
        {
            if (m_initialized && apiPlaneData.id != m_apiPlaneData.id)
            {
                ARDebug.LogError("Cannot update plane with mismatched id.");
                return;
            }
            else if (m_initialized && !forceUpdate && apiPlaneData.timestamp == m_apiPlaneData.timestamp)
            {
                return;
            }

            if (subsumedBy != null)
            {
                SubsumedBy = subsumedBy;
            }

            m_apiPlaneData     = apiPlaneData;
            m_initialized      = true;
            m_lastUpdatedFrame = Time.frameCount;

            Matrix4x4 startServiceTplane = Matrix4x4.TRS(apiPlaneData.pose.translation.ToVector3(),
                                                         apiPlaneData.pose.orientation.ToQuaternion(), Vector3.one);

            // Because startServiceTplane is a Pose (position, orientation), the multiplication of the first two terms
            // rotates plane orientation.  This must be undone with the last term (inverse) of the equation.
            m_unityWorldTPlane = Constants.UNITY_WORLD_T_START_SERVICE * startServiceTplane *
                                 Constants.UNITY_WORLD_T_START_SERVICE.inverse;

            Position  = m_unityWorldTPlane.GetColumn(3);
            Position += new Vector3((float)apiPlaneData.centerX, 0.0f, (float)apiPlaneData.centerY);

            Quaternion yaw = Quaternion.Euler(0.0f, -Mathf.Rad2Deg * (float)apiPlaneData.yaw, 0.0f);

            Rotation = yaw * Quaternion.LookRotation(m_unityWorldTPlane.GetColumn(2), m_unityWorldTPlane.GetColumn(1));

            m_boundaryPolygonPoints.Clear();
            int boudaryLength = m_apiPlaneData.boundaryPointNum;

            if (boudaryLength != 0)
            {
                double[] apiBoundaryPolygon = new double[boudaryLength * 2];
                Marshal.Copy(m_apiPlaneData.boundaryPolygon, apiBoundaryPolygon, 0, boudaryLength * 2);

                m_boundaryPolygonPoints.Clear();
                for (int i = 0; i < boudaryLength; ++i)
                {
                    Vector3 localPoint = new Vector3((float)apiBoundaryPolygon[2 * i],
                                                     0.0f, (float)apiBoundaryPolygon[2 * i + 1]);
                    m_boundaryPolygonPoints.Add(m_unityWorldTPlane.MultiplyPoint3x4(localPoint));
                }
            }

            // Reverse the m_boundaryPolygonPoints because the raw data is in counter-clockwise.
            // As Unity is left handed system, this should be clockwise.
            m_boundaryPolygonPoints.Reverse();
        }
        /**
         * \if english
         * Creates an anchor attached to current trackable at given pose.<b>Note: if the trackable doest not support
         * attaching anchors, null will be returned.</b>
         * \else
         * 使用应用给定的位姿创建与trackable绑定的锚点。<b>注意:如果当期trackable不支持绑定锚点,将会返回null。</b>
         * \endif
         */
        public virtual ARAnchor CreateAnchor(Pose pose)
        {
            IntPtr anchorHandle = IntPtr.Zero;

            if (!m_ndkSession.TrackableAdapter.AcquireNewAnchor(m_trackableHandle, pose, out anchorHandle))
            {
                ARDebug.LogError("failed to create anchor on trackbale");
                return(null);
            }
            return(m_ndkSession.AnchorManager.ARAnchorFactory(anchorHandle, true));
        }
Esempio n. 10
0
        //support for multiple permission request
        public static AsyncTask <AndroidPermissionsRequestResult> RequestPermission(string[] permissionNames)
        {
            if (m_currentRequest != null)
            {
                ARDebug.LogError("Do not make simultaneous permission requests.");
                return(null);
            }

            AndroidPermissionsService.Call("RequestPermissionAsync", UnityActivity, permissionNames, Instance);
            m_currentRequest = new AsyncTask <AndroidPermissionsRequestResult>(out m_onPermissionsRequestFinished);

            return(m_currentRequest);
        }
Esempio n. 11
0
        /// <summary>
        /// Requests a set of Android permissions from the user.
        /// </summary>
        /// <param name="permissionNames">A collection of permissions to be requested (e.g. android.permission.CAMERA).
        /// <b>Currently only the first permission in the collection will be requested. The behavior of this parameter
        /// will change in later versions.</b>
        /// </param>
        /// <returns>An asynchronous task the completes when the user has accepted/rejected all requested permissions
        /// and yields a <c>AndroidPermissionsRequestResult</c> that summarizes the result.  If this method is called
        /// when another permissions request is pending <c>null</c> will be returned instead.</returns>
        public static AsyncTask <AndroidPermissionsRequestResult> RequestPermission(string[] permissionNames)
        {
            if (m_currentRequest != null)
            {
                ARDebug.LogError("Attempted to make simultaneous Android permissions requests.");
                return(null);
            }

            // TODO (mtsmall): We currently only allow one permission to be requested because of the callback structure.
            PermissionsService.Call("RequestPermissionAsync", UnityActivity, new [] { permissionNames[0] }, Instance);
            m_currentRequest = new AsyncTask <AndroidPermissionsRequestResult>(out m_onPermissionsRequestFinished);

            return(m_currentRequest);
        }
        /// <summary>
        /// Requests an Android permission from the user.
        /// </summary>
        /// <param name="permissionName">The permission to be requested (e.g. android.permission.CAMERA).</param>
        /// <returns>An asynchronous task the completes when the user has accepted/rejected the requested permission
        /// and yields a {@link AndroidPermissionsRequestResult} that summarizes the result.  If this method is called
        /// when another permissions request is pending <c>null</c> will be returned instead.</returns>
        public static AsyncTask <AndroidPermissionsRequestResult> RequestPermission(string permissionName)
        {
            if (s_CurrentRequest != null)
            {
                ARDebug.LogError("Attempted to make simultaneous Android permissions requests.");
                return(null);
            }

            GetPermissionsService().Call("RequestPermissionAsync", GetUnityActivity(),
                                         new[] { permissionName }, GetInstance());
            s_CurrentRequest = new AsyncTask <AndroidPermissionsRequestResult>(out s_OnPermissionsRequestFinished);

            return(s_CurrentRequest);
        }
 private void _ConnectToService()
 {
     try
     {
         ARSession.CreateSession();
         isSessionCreated = true;
         ARSession.Config(Config);
         ARSession.Resume();
         ARSession.SetCameraTextureNameAuto();
         ARSession.SetDisplayGeometry(Screen.width, Screen.height);
     }
     catch (ARCameraPermissionDeniedException e)
     {
         isErrorHappendWhenInit = true;
         ARDebug.LogError("camera permission is denied");
         errorMessage = "This app require camera permission";
         Log();
         Invoke("_DoQuit", 0.5f);
     }
     catch (ARUnavailableDeviceNotCompatibleException e)
     {
         isErrorHappendWhenInit = true;
         errorMessage           = "This device does not support AR";
         Log();
         Invoke("_DoQuit", 0.5f);
     }
     catch (ARUnavailableServiceApkTooOldException e)
     {
         isErrorHappendWhenInit = true;
         errorMessage           = "This AR Engine is too old, please update";
         Log();
         Invoke("_DoQuit", 0.5f);
     }
     catch (ARUnavailableServiceNotInstalledException e)
     {
         isErrorHappendWhenInit = true;
         errorMessage           = "This app depend on AREngine.apk, please install it";
         Log();
         Invoke("_DoQuit", 0.5f);
     }
     catch (ARUnSupportedConfigurationException e)
     {
         isErrorHappendWhenInit = true;
         errorMessage           = "This config is not supported on this device, exit now.";
         Log();
         Invoke("_DoQuit", 0.5f);
     }
 }
Esempio n. 14
0
        public static float[] GetTransformDisplayUvCoords(float[] inUVCoords)
        {
            if (null == inUVCoords || inUVCoords.Length != 8)
            {
                ARDebug.LogError("wrong inUVCoords");
                throw new ArgumentException("inUVCoords is wrong");
            }
            if (ARSessionManager.Instance.SessionStatus != ARSessionStatus.RUNNING &&
                ARSessionManager.Instance.SessionStatus != ARSessionStatus.PAUSED)
            {
                throw new ARNotYetAvailableException();
            }

            float[] outUv = ARSessionManager.Instance.m_ndkSession.FrameAdapter.TransformDisplayUvCoords(inUVCoords);
            return(outUv);
        }
Esempio n. 15
0
        public override IEnumerator StartService(Settings settings)
        {
            if (m_ARCoreSessionConfig == null)
            {
                m_ARCoreSessionConfig = ScriptableObject.CreateInstance <ARCoreSessionConfig>();
            }

            m_ARCoreSessionConfig.EnableLightEstimation = settings.enableLightEstimation;
            m_ARCoreSessionConfig.EnablePlaneFinding    = settings.enablePlaneDetection;
            //Do we want to match framerate to the camera?
            m_ARCoreSessionConfig.MatchCameraFramerate = false;

            //Using the SessionManager instead of ARCoreSession allows us to check if the config is supported,
            //And also using the session without the need for a GameObject or an additional MonoBehaviour.
            if (m_SessionManager == null)
            {
                m_SessionManager = SessionManager.CreateSession();
                if (!IsSupported)
                {
                    ARDebug.LogError("The requested ARCore session configuration is not supported.");
                    yield break;
                }

                Session.Initialize(m_SessionManager);

                if (Session.ConnectionState != SessionConnectionState.Uninitialized)
                {
                    ARDebug.LogError("Could not create an ARCore session.  The current Unity Editor may not support this " +
                                     "version of ARCore.");
                    yield break;
                }
            }
            //We ask for permission to use the camera and wait
            var task = AskForPermissionAndConnect(m_ARCoreSessionConfig);

            yield return(task.WaitForCompletion());

            //After the operation is done, we double check if the connection was successful
            IsRunning = task.Result == SessionConnectionState.Connected;

            if (IsRunning)
            {
                TextureReader_create((int)k_ImageFormatType, k_ARCoreTextureWidth, k_ARCoreTextureHeight, true);
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Unity Awake method.
        /// </summary>
        public void Awake()
        {
            if (FindObjectsOfType <SessionComponent>().Length > 1)
            {
                ARDebug.LogError("Multiple SessionComponents present in the game scene.  Destroying the gameobject " +
                                 "of the newest one.");
                Destroy(gameObject);
                return;
            }

            // Launches a temporary recovery daemon for a known race condition in ARCore connection code.
            StartCoroutine(_AttemptRecoverIfInvalidState());

            if (m_connectOnAwake)
            {
                Connect();
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Connects to the ARCore service.
        /// </summary>
        /// <param name="sessionConfig">The session configuration to connect with.</param>
        /// <param name="onComplete">A callback for when the result of the connection attempt is known.</param>
        private void _ConnectToService(SessionConfig sessionConfig, Action <SessionConnectionState> onComplete)
        {
            // Connect the ARCore session.
            UnityTango.Config tangoConfig = _GetSessionTangoConfiguration(sessionConfig);
            if (!UnityTango.Device.Connect(tangoConfig))
            {
                ARDebug.LogError("Failed to connect the ARSession.");
                SessionManager.ConnectionState = SessionConnectionState.ConnectToServiceFailed;
                onComplete(SessionConnectionState.ConnectToServiceFailed);
                return;
            }

            if (sessionConfig.m_enableARBackground)
            {
                // _SetupVideoOverlay();
            }

            SessionManager.ConnectionState = SessionConnectionState.Connected;
            onComplete(SessionManager.ConnectionState);
        }
Esempio n. 18
0
        private void _UpdateHandBox()
        {
            var handBox = m_hand.GetHandBox();

            if (handBox.Length < 2)
            {
                ARDebug.LogError("handbox's length is {0}", handBox.Length);
                return;
            }
            Vector3 glLeftTopCorner     = handBox[0];
            Vector3 glRightBottomCorner = handBox[1];
            Vector3 glLeftBottomCorner  = new Vector3(glLeftTopCorner.x, glRightBottomCorner.y);
            Vector3 glRightTopCorner    = new Vector3(glRightBottomCorner.x, glLeftTopCorner.y);

            m_boxLineRenderer.SetPosition(0, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftTopCorner));
            m_boxLineRenderer.SetPosition(1, _TransferGLCoord2UnityWoldCoordWithDepth(glRightTopCorner));
            m_boxLineRenderer.SetPosition(2, _TransferGLCoord2UnityWoldCoordWithDepth(glRightBottomCorner));
            m_boxLineRenderer.SetPosition(3, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftBottomCorner));
            m_boxLineRenderer.SetPosition(4, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftTopCorner));
            m_boxLineRenderer.gameObject.SetActive(true);
        }
Esempio n. 19
0
        public static AsyncTask <AndroidPermissionsRequestResult> RequestPermission(
            string permissionName)
        {
            if (AndroidPermissionsManager.IsPermissionGranted(permissionName))
            {
                return(new AsyncTask <AndroidPermissionsRequestResult>(
                           new AndroidPermissionsRequestResult(
                               new string[] { permissionName }, new bool[] { true })));
            }

            if (_currentRequest != null)
            {
                ARDebug.LogError("Attempted to make simultaneous Android permissions requests.");
                return(null);
            }

            GetPermissionsService().Call("RequestPermissionAsync", GetUnityActivity(),
                                         new[] { permissionName }, GetInstance());
            _currentRequest =
                new AsyncTask <AndroidPermissionsRequestResult>(out _onPermissionsRequestFinished);

            return(_currentRequest);
        }
Esempio n. 20
0
        /// <summary>
        /// Constructor for a new PointCloud wrapping <c>rawPointCloud</c>.
        /// </summary>
        /// <param name="rawPointCloud">The raw point cloud to wrap.  If <c>rawPointCloud</c> is null, the
        /// resulting PointCloud will have <c>IsValid</c> set to false.</param>
        public PointCloud(UnityTango.PointCloudData?rawPointCloud)
        {
            if (rawPointCloud == null)
            {
                IsValid                  = false;
                m_rawPointCloud          = new UnityTango.PointCloudData();
                Pose                     = new Pose();
                m_unityWorldTDepthCamera = Matrix4x4.identity;
                return;
            }

            IsValid                  = true;
            m_rawPointCloud          = rawPointCloud.Value;
            Pose                     = new Pose();
            m_unityWorldTDepthCamera = Matrix4x4.identity;

            ApiPoseData apiPoseData = new ApiPoseData();

            if (ExternApi.TangoService_getPoseAtTime(m_rawPointCloud.timestamp,
                                                     new ApiCoordinateFramePair(Constants.START_SERVICE_T_DEPTH_FRAME_PAIR),
                                                     ref apiPoseData) == ApiServiceErrorStatus.Success)
            {
                Matrix4x4 ss_T_depth = Matrix4x4.TRS(apiPoseData.translation.ToVector3(),
                                                     apiPoseData.orientation.ToQuaternion(), Vector3.one);
                m_unityWorldTDepthCamera = Constants.UNITY_WORLD_T_START_SERVICE * ss_T_depth;
                Vector3    translation = m_unityWorldTDepthCamera.GetColumn(3);
                Quaternion rotation    = Quaternion.LookRotation(m_unityWorldTDepthCamera.GetColumn(2),
                                                                 m_unityWorldTDepthCamera.GetColumn(1));
                Pose = new Pose(translation, rotation);
            }
            else
            {
                ARDebug.LogError("Could not retrieve a pose for the point cloud.");
                Pose = new Pose();
                m_unityWorldTDepthCamera = Matrix4x4.identity;
            }
        }
        public void BuildIfNeeded(out string error)
        {
            error = "";
            if (!m_IsRawDataDirty)
            {
                return;
            }

            string cliBinaryPath;

            if (!FindCliBinaryPath(out cliBinaryPath))
            {
                return;
            }

            if (m_ARAugImageDatabaseType == AREnginesType.HUAWEI_AR_ENGINE)
            {
                BuildHWDatabase(cliBinaryPath, out error);
            }
            else
            {
                BuildGoogleDatabase(cliBinaryPath, out error);
            }

            m_IsRawDataDirty = false;
            EditorUtility.SetDirty(this);

            // Force a save to make certain build process will get updated asset.
            AssetDatabase.SaveAssets();

            const int BYTES_IN_KBYTE = 1024;

            // TODO:: Remove this log when all errors/warnings are moved to stderr for CLI tool.
            ARDebug.LogError("Built AugmentedImageDatabase '{0}' ({1} Images, {2} KBytes)", name, m_Images.Count,
                             m_RawData.Length / BYTES_IN_KBYTE);
        }
Esempio n. 22
0
        private void _UpdateHandBox()
        {
            var handBox = m_hand.GetHandBox();

            //The first one is the left top corner of the rectangle, and the second one is the right bottom corner
            if (handBox.Length < 2)
            {
                ARDebug.LogError("handbox's length is {0}", handBox.Length);
                return;
            }
            Vector3 glLeftTopCorner     = handBox[0];
            Vector3 glRightBottomCorner = handBox[1];
            Vector3 glLeftBottomCorner  = new Vector3(glLeftTopCorner.x, glRightBottomCorner.y);
            Vector3 glRightTopCorner    = new Vector3(glRightBottomCorner.x, glLeftTopCorner.y);

            float glCenterX = (glLeftTopCorner.x + glRightTopCorner.x) / 2;
            float glCenterY = (glLeftTopCorner.y + glLeftBottomCorner.y) / 2;

            Vector3 glCenter = new Vector3(glCenterX, glCenterY);

            m_boxLineRenderer.SetPosition(0, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftTopCorner));
            m_boxLineRenderer.SetPosition(1, _TransferGLCoord2UnityWoldCoordWithDepth(glRightTopCorner));
            m_boxLineRenderer.SetPosition(2, _TransferGLCoord2UnityWoldCoordWithDepth(glRightBottomCorner));
            m_boxLineRenderer.SetPosition(3, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftBottomCorner));
            m_boxLineRenderer.SetPosition(4, _TransferGLCoord2UnityWoldCoordWithDepth(glLeftTopCorner));
            m_boxLineRenderer.material = (Material)Resources.Load("Line", typeof(Material));

            m_boxLineRenderer.startColor = new Color(1, 0, 0, 1);
            m_boxLineRenderer.endColor   = new Color(1, 0, 0, 1);
            m_boxLineRenderer.gameObject.SetActive(true);

            m_spider.transform.position      = (_TransferGLCoord2UnityWoldCoordWithDepth(glCenter));
            m_lightBlast.transform.position  = (_TransferGLCoord2UnityWoldCoordWithDepth(glCenter));
            m_bulbasaur.transform.position   = (_TransferGLCoord2UnityWoldCoordWithDepth(glCenter));
            m_spiderColor.transform.position = (_TransferGLCoord2UnityWoldCoordWithDepth(glCenter));
            m_hwCube.transform.position      = (_TransferGLCoord2UnityWoldCoordWithDepth(glCenter));

            switch (m_hand.GetGestureType())
            {
            case 0:
                _DonotShow3DObjects();
                break;

            case 1:
                _DonotShow3DObjects();
                m_spider.gameObject.SetActive(true);
                break;

            case 5:
                _DonotShow3DObjects();
                m_lightBlast.gameObject.SetActive(true);
                break;

            case 6:
                _DonotShow3DObjects();
                m_spiderColor.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
                break;

            case 7:
                _DonotShow3DObjects();
                m_hwCube.gameObject.SetActive(true);
                break;

            case 10:
                _DonotShow3DObjects();
                m_bulbasaur.gameObject.SetActive(true);
                break;
            }
        }
Esempio n. 23
0
        // Use this for initialization
        void Awake()
        {
            //return;
            BaiduARHumanPose humanPose = FindObjectOfType <BaiduARHumanPose>();

            humanPose.InvokeErrorMessage(delegate(string errNum, string errMsg) {
                ARDebug.LogError("errMsg = " + errMsg);
            });


            humanPose.InvokeClearMessage(delegate(){
                ARDebug.Log("InvokeClearMessage ");
                objs.Clear();
            });

            humanPose.InvokePosMessage(delegate(List <OutPutData> lstVet)
            {
                ARDebug.Log("InvokePosMessage Init lstVet = " + lstVet.Count);
                for (int i = 0; i < lstVet.Count; i++)
                {
                    // ARDebug.Log("i = "+i + " score="+lstVet[i].score);
                    if (lstVet[i].score > 0.6)
                    {
                        GameObject item = objs.GetItem();
                        if (item == null)
                        {
                            item = CreateCircleGame();
                            objs.Add(item);
                        }
                        item.SetActive(true);
                        item.transform.position = lstVet[i].VectorWorldPos;
                    }
                }
                if (lstVet.Count < 18)
                {
                    return;
                }
                ARDebug.Log("InvokePosMessage Init22");
                Vector2 vetpos = new Vector2(lstVet[1].VectorScreenPos.x, lstVet[1].VectorScreenPos.y + (lstVet[1].VectorScreenPos.x - lstVet[2].VectorScreenPos.x));
                if (null == collideScript)
                {
                    collideScript = FindObjectOfType <CollideManage>();
                }
                collideScript.SetTexiaoPos(vetpos);


                for (int i = 0; i < lstVet.Count; i++)
                {
                    if (i == 4)  //表示右手腕
                    {
                        Vector2 vet = Vector2.zero;
                        if (IsBound(lstVet[i].VectorScreenPos)) //如果没有检测到点
                        {
                            lstVet[i].VectorScreenPos = lastVet_righthand;
                        }
                        if (IsBound(lstVet[i - 1].VectorScreenPos)) //如果没有检测到点
                        {
                            lstVet[i - 1].VectorScreenPos = lastVet_righthand;
                        }

                        GetHandPos(lstVet[i - 1].VectorScreenPos, lstVet[i].VectorScreenPos, out vet);

                        if (null == collideScript)
                        {
                            collideScript = FindObjectOfType <CollideManage>();
                        }
                        if (!IsBound(vet))
                        {
                            collideScript.HandleData(vet, HumanPoseType.POSE_RIGHTHAND);
                            lastVet_righthand = vet;
                        }
                    }
                    if (i == 7)  //表示左手腕
                    {
                        Vector2 vet = Vector2.zero;
                        if (IsBound(lstVet[i].VectorScreenPos)) //如果没有检测到点
                        {
                            lstVet[i].VectorScreenPos = lastVet_lefthand;
                        }
                        if (IsBound(lstVet[i - 1].VectorScreenPos)) //如果没有检测到点
                        {
                            lstVet[i - 1].VectorScreenPos = lastVet_lefthand;
                        }

                        GetHandPos(lstVet[i - 1].VectorScreenPos, lstVet[i].VectorScreenPos, out vet);

                        if (null == collideScript)
                        {
                            collideScript = FindObjectOfType <CollideManage>();
                        }
                        if (!IsBound(vet))
                        {
                            collideScript.HandleData(vet, HumanPoseType.POSE_LEFTHAND);
                            lastVet_lefthand = vet;
                        }
                        // collideScript.HandleData(lstVet[i].VectorScreenPos);
                    }
                    if (i == 10) //表示右脚
                    {
                        if (null == collideScript)
                        {
                            collideScript = FindObjectOfType <CollideManage>();
                        }
                        if (IsBound(lstVet[i].VectorScreenPos))
                        {
                            lstVet[i].VectorScreenPos = lastVet_rightwrist;
                        }
                        if (!IsBound(lstVet[i].VectorScreenPos))
                        {
                            collideScript.HandleData(lstVet[i].VectorScreenPos, HumanPoseType.POSE_RIGHTWRIST);
                            lastVet_rightwrist = lstVet[i].VectorScreenPos;
                        }
                    }
                    if (i == 13) //表示左脚
                    {
                        if (null == collideScript)
                        {
                            collideScript = FindObjectOfType <CollideManage>();
                        }
                        if (IsBound(lstVet[i].VectorScreenPos))
                        {
                            lstVet[i].VectorScreenPos = lastVet_leftwrist;
                        }
                        if (!IsBound(lstVet[i].VectorScreenPos))
                        {
                            collideScript.HandleData(lstVet[i].VectorScreenPos, HumanPoseType.POSE_LEFTWRIST);
                            lastVet_leftwrist = lstVet[i].VectorScreenPos;
                        }
                    }
                }
            });
        }
Esempio n. 24
0
        /// <summary>
        /// Connects an ARSession.  Note that if user permissions are needed they will be requested and thus this is an
        /// asynchronous method.
        /// </summary>
        /// <param name="sessionConfig">The session configuration.</param>
        /// <returns>An <c>AsyncTask</c> that completes when the connection has been made or failed. </returns>
        public AsyncTask <SessionConnectionState> Connect(SessionConfig sessionConfig)
        {
            const string ANDROID_CAMERA_PERMISSION_NAME = "android.permission.CAMERA";

            if (sessionConfig == null)
            {
                ARDebug.LogError("Unable to connect ARSession session due to missing ARSessionConfig.");
                SessionManager.ConnectionState = SessionConnectionState.MissingConfiguration;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            bool isSupported;
            ApiServiceErrorStatus status = TangoClientApi.TangoService_IsSupported(out isSupported);

            if (status.IsTangoFailure())
            {
                ARDebug.LogError("There was an error accessing the ARCore API.");
                SessionManager.ConnectionState = SessionConnectionState.ConnectToServiceFailed;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }
            if (!isSupported)
            {
                ARDebug.LogError("Device does not support ARCore.");
                SessionManager.ConnectionState = SessionConnectionState.DeviceNotSupported;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            // We have already connected at least once.
            if (SessionManager.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Multiple attempts to connect to the ARSession.  Note that the ARSession connection " +
                                 "spans the lifetime of the application and cannot be reconfigured.  This will change in future " +
                                 "versions of ARCore.");
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            // Create an asynchronous task for the potential permissions flow and service connection.
            Action <SessionConnectionState> onTaskComplete;
            var returnTask = new AsyncTask <SessionConnectionState>(out onTaskComplete);

            // Attempt service connection immediately if permissions are granted.
            if (AndroidPermissionsManager.IsPermissionGranted(ANDROID_CAMERA_PERMISSION_NAME))
            {
                _ConnectToService(sessionConfig, onTaskComplete);
                return(returnTask);
            }

            // Request needed permissions and attempt service connection if granted.
            var permissionsArray = new string[] { ANDROID_CAMERA_PERMISSION_NAME };

            AndroidPermissionsManager.RequestPermission(permissionsArray).ThenAction((requestResult) => {
                if (requestResult.IsAllGranted)
                {
                    _ConnectToService(sessionConfig, onTaskComplete);
                }
                else
                {
                    ARDebug.LogError("ARCore connection failed because a needed permission was rejected.");
                    SessionManager.ConnectionState = SessionConnectionState.UserRejectedNeededPermission;
                    onTaskComplete(SessionManager.ConnectionState);
                }
            });

            return(returnTask);
        }
Esempio n. 25
0
 private void LogError(NdkCameraMetadataType requestedType)
 {
     ARDebug.LogError("Error getting value from ARCameraMetadataType due to type mismatch. " +
                      "requested type = {0}, internal type = {1}\n", requestedType, m_Type);
 }
Esempio n. 26
0
        // Use this for initialization
        void Awake()
        {
            electRoot = FindObjectOfType <ARHumanExampleElectric>();
            footRoot  = FindObjectOfType <ARHumanExampleFootLift>();
            BaiduARHumanPose humanPose = FindObjectOfType <BaiduARHumanPose>();

            exampleUI = FindObjectOfType <ARExampleUI>();
            humanPose.InvokeErrorMessage(delegate(string errNum, string errMsg)
            {
                exampleUI.ErrorInfo(errNum, errMsg);
                ARDebug.LogError("errMsg = " + errMsg);
            });

            humanPose.InvokeClearMessage(delegate()
            {
                ARDebug.Log("InvokeClearMessage ");
                objs.Clear();
            });
            humanPose.InvokePosMessage(delegate(List <OutPutData> lstVet)
            {
                if (lstVet[1].score > 0 && lstVet[8].score > 0)
                {
                    ARHumanBodyDataParse.lengthShoulder = Vector2.Distance(lstVet[1].VectorScreenPos, lstVet[8].VectorScreenPos);
                    ARDebug.Log("datas lengthShoulder = " + ARHumanBodyDataParse.lengthShoulder);

                    if (lstVet[10].score > 0 && lstVet[13].score > 0)
                    {
                        float left  = Vector2.Distance(lstVet[10].VectorScreenPos, lstVet[8].VectorScreenPos);
                        float right = Vector2.Distance(lstVet[13].VectorScreenPos, lstVet[11].VectorScreenPos);
                        if (right < ARHumanBodyDataParse.lengthShoulder && left < ARHumanBodyDataParse.lengthShoulder)
                        {
                            return;
                        }
                    }
                }



                int z = (int)(ARHumanBodyDataParse.lengthShoulder / 60);

                int ZeroCount = 0;
                for (int i = 0; i < lstVet.Count; i++)
                {
                    if (lstVet[i].score > 0.6)
                    {
                        //  if ( i == 4 ||  i == 2 || i == 7  || i ==5)
                        {
                            //GameObject item = objs.GetItem();
                            //if (item == null)
                            //{
                            //    item = CreateCircleGame(z);
                            //    objs.Add(item);
                            //}
                            //item.transform.localScale = Vector3.one * z;
                            //item.SetActive(true);
                            //// item.transform.position = GameObject.Find("UICamera").GetComponent<Camera>().ScreenToWorldPoint(lstVet[i].VectorScreenPos);//lstVet[i].VectorWorldPos;
                            //item.transform.position = FindObjectOfType<ARWebCamera>().GetComponent<Camera>().ScreenToWorldPoint(lstVet[i].VectorScreenPos);//lstVet[i].VectorWorldPos;
                        }
                    }
                    else
                    {
                        ZeroCount++;
                    }
                }
                if (ZeroCount > 15)
                {
                    objs.Clear();
                }

                if (ARHumanBodyDataParse.foots[0] == null)
                {
                    ARHumanBodyDataParse.foots[0] = new Queue <float>();
                    ARHumanBodyDataParse.foots[1] = new Queue <float>();
                }
                if (ARHumanBodyDataParse.foots[0].Count < ARHumanBodyDataParse.footsize)
                {
                    ARHumanBodyDataParse.foots[0].Enqueue(lstVet[10].VectorScreenPos.y);
                    ARHumanBodyDataParse.foots[1].Enqueue(lstVet[13].VectorScreenPos.y);
                }
                else
                {
                    ARHumanBodyDataParse.foots[0].Dequeue();
                    ARHumanBodyDataParse.foots[1].Dequeue();

                    ARHumanBodyDataParse.foots[0].Enqueue(lstVet[10].VectorScreenPos.y);
                    ARHumanBodyDataParse.foots[1].Enqueue(lstVet[13].VectorScreenPos.y);
                }

                electRoot.Electric(lstVet);
                StaticPose(lstVet);

                if (lstVet[10].score > 0 && lstVet[13].score > 0)
                {
                    float left  = Vector2.Distance(lstVet[10].VectorScreenPos, lstVet[8].VectorScreenPos);
                    float right = Vector2.Distance(lstVet[13].VectorScreenPos, lstVet[11].VectorScreenPos);
                    if (right < ARHumanBodyDataParse.lengthShoulder && left < ARHumanBodyDataParse.lengthShoulder)
                    {
                        lastNosePose = lstVet[0].VectorScreenPos;
                        return;
                    }

                    if (!lastNosePose.Equals(Vector2.one))
                    {
                        float Distan = Vector2.Distance(lastNosePose, lstVet[0].VectorScreenPos);
                        lastNosePose = lstVet[0].VectorScreenPos;
                        //Debug.Log("Distan="+Distan);
                        if (Distan > ARHumanBodyDataParse.lengthShoulder / 10)
                        {
                            ARHumanBodyDataParse.foots[0].Clear();
                            ARHumanBodyDataParse.foots[1].Clear();
                            return;
                        }
                    }
                }
                lastNosePose = lstVet[0].VectorScreenPos;
                footRoot.FootLift(lstVet);
            });
        }