private static bool TryGetLastFrameFromExtensions(out XRCameraFrame frame) { ARCoreExtensions extensions = ARCoreExtensions._instance; ARCameraManager cameraManager = extensions.CameraManager; var cameraParams = new XRCameraParams { zNear = cameraManager.GetComponent <Camera>().nearClipPlane, zFar = cameraManager.GetComponent <Camera>().farClipPlane, screenWidth = Screen.width, screenHeight = Screen.height, screenOrientation = Screen.orientation }; if (!cameraManager.subsystem.TryGetLatestFrame( cameraParams, out frame)) { Debug.LogWarning( "The current XRCameraFrame is not available, try again later."); return(false); } if (frame.timestampNs == 0 || frame.FrameHandle() == IntPtr.Zero) { Debug.LogWarning( "The current XRCameraFrame is not ready, try again later."); return(false); } return(true); }
/// <summary> /// Get the 4x4 image display matrix for the camera frame. This is used by the texture /// populated based on CPU images /// to calculate the display coordinates. /// </summary> /// <param name="frame">The XRCameraFrame instance.</param> /// <returns>The 4x4 image display matrix.</returns> public static Matrix4x4 GetImageDisplayMatrix(this XRCameraFrame frame) { // Unity Screen Coordinate: Android Screen Coordinate (flipped Y-Axis): // (0, 1) (1, 1) (0, 0) (1, 0) // |----------------| |----------------| // | | | | // | | | | // | | | | // | | | | // | | | | // | | | | // | | | | // |----------------| |----------------| // (0, 0) (1, 0) (0, 1) (1, 1) IntPtr sessionHandle = ARCoreExtensions._instance.currentARCoreSessionHandle; // X-Axis (1, 0) in Unity view maps to (1, 1) on Android screen. Vector2 affineBasisX = new Vector2(1.0f, 1.0f); // Y-Axis (0, 1) in Unity view maps to (0, 0) on Android screen. Vector2 affineBasisY = new Vector2(0.0f, 0.0f); // Origin (0, 0) in Unity view maps to (0, 1) on Android screen. Vector2 affineOrigin = new Vector2(0.0f, 1.0f); Vector2 transformedX = FrameApi.TransformCoordinates2d( sessionHandle, frame.FrameHandle(), ApiCoordinates2dType.ViewNormalized, ApiCoordinates2dType.ImageNormalized, ref affineBasisX); Vector2 transformedY = FrameApi.TransformCoordinates2d( sessionHandle, frame.FrameHandle(), ApiCoordinates2dType.ViewNormalized, ApiCoordinates2dType.ImageNormalized, ref affineBasisY); Vector2 transformedOrigin = FrameApi.TransformCoordinates2d( sessionHandle, frame.FrameHandle(), ApiCoordinates2dType.ViewNormalized, ApiCoordinates2dType.ImageNormalized, ref affineOrigin); Matrix4x4 imageMatrix = Matrix4x4.identity; imageMatrix[0, 0] = transformedX.x - transformedOrigin.x; imageMatrix[0, 1] = transformedX.y - transformedOrigin.y; imageMatrix[1, 0] = transformedY.x - transformedOrigin.x; imageMatrix[1, 1] = transformedY.y - transformedOrigin.y; imageMatrix[2, 0] = transformedOrigin.x; imageMatrix[2, 1] = transformedOrigin.y; return(imageMatrix); }