/// <summary> /// Detects new output frames and updates the camera, then passes the output frame through an event to update the foreground /// </summary> /// <param name="timeStep"></param> protected override void OnUpdate(float timeStep) { if (paused) { return; } Optional <OutputFrame> optionalOframe = OutputFrameBuffer.peek(); if (optionalOframe.OnSome) { OutputFrame oframe = optionalOframe.Some; Optional <InputFrame> optionalIframe = oframe.inputFrame(); if (optionalIframe.OnSome) { InputFrame iframe = optionalIframe.Some; CameraParameters cameraParameters = iframe.cameraParameters(); if (cameraParameters != null) { Image image = iframe.image(); float aspectRatio = (float)(DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Height); int rotation = 0; switch (DeviceDisplay.MainDisplayInfo.Rotation) { case DisplayRotation.Rotation90: rotation = 90; break; case DisplayRotation.Rotation180: rotation = 180; break; case DisplayRotation.Rotation270: rotation = 270; break; } if (iframe.index() != previousInputFrameIndex) { Matrix44F ip = cameraParameters.imageProjection(aspectRatio, rotation, true, false); Matrix4 iprj = ip.ToUrhoMatrix(); bgCamera.SetProjection(iprj); EasyAR.Buffer buffer = image.buffer(); try { backgroundUpdater.UpdateTexture(Application, image.format(), image.width(), image.height(), buffer); } finally { buffer.Dispose(); } previousInputFrameIndex = iframe.index(); } ARFrameUpdated?.Invoke(oframe, cameraParameters, aspectRatio, rotation); image.Dispose(); cameraParameters.Dispose(); } iframe.Dispose(); } oframe.Dispose(); } base.OnUpdate(timeStep); }
protected override void OnUpdate(float timeStep) { if (paused) { return; } if (Camera == null) { throw new Exception("ARCore.Camera property was not set"); } try { var frame = Session.Update(); if (paused) //in case if Config.UpdateMode.LatestCameraImage is not used { return; } var camera = frame.Camera; if (camera.TrackingState != TrackableTrackingState.Tracking) { return; } var far = 100f; var near = 0.01f; float[] projmx = new float[16]; camera.GetProjectionMatrix(projmx, 0, near, far); var prj = new Urho.Matrix4( projmx[0], projmx[4], projmx[8], projmx[12], projmx[1], projmx[5], projmx[9], projmx[13], projmx[2], projmx[6], projmx[10], projmx[14], projmx[3], projmx[7], projmx[11], projmx[15] ); //some OGL -> DX conversion (Urho accepts DX format on all platforms) prj.M34 /= 2f; prj.M33 = far / (far - near); prj.M43 *= -1; //prj.M13 = 0; //center of projection //prj.M23 = 0; Camera.SetProjection(prj); float[] viewmx = new float[16]; camera.GetViewMatrix(viewmx, 0); var view = new Urho.Matrix4( viewmx[0], viewmx[4], viewmx[8], viewmx[12], viewmx[1], viewmx[5], viewmx[9], viewmx[13], viewmx[2], viewmx[6], viewmx[10], viewmx[14], viewmx[3], viewmx[7], viewmx[11], viewmx[15]); // some magic: view.Invert(); view.Transpose(); var rot = view.Rotation; rot.Z *= -1; var cameraNode = Camera.Node; cameraNode.Position = new Vector3(view.Row3.X, view.Row3.Y, -view.Row3.Z); cameraNode.Rotation = rot; ARFrameUpdated?.Invoke(frame); } catch (Exception exc) { Log.Write(LogLevel.Warning, "ARCore error: " + exc); } }
protected override void OnUpdate(float timeStep) { if (paused) { return; } if (Camera == null) { throw new Exception("ARCore.Camera property was not set"); } try { if (Session == null) { return; } var frame = Session.Update(); if (paused) //in case if Config.UpdateMode.LatestCameraImage is not used { return; } var camera = frame.Camera; if (camera.TrackingState != TrackingState.Tracking) { return; } var far = 100f; var near = 0.01f; float[] projmx = new float[16]; camera.GetProjectionMatrix(projmx, 0, near, far); var prj = new Urho.Matrix4( projmx[0], projmx[4], projmx[8], projmx[12], projmx[1], projmx[5], projmx[9], projmx[13], projmx[2], projmx[6], projmx[10], projmx[14], projmx[3], projmx[7], projmx[11], projmx[15] ); //some OGL -> DX conversion (Urho accepts DX format on all platforms) prj.M34 /= 2f; prj.M33 = far / (far - near); prj.M43 *= -1; //prj.M13 = 0; //center of projection //prj.M23 = 0; Camera.SetProjection(prj); TransformByPose(Camera.Node, camera.DisplayOrientedPose); ARFrameUpdated?.Invoke(frame); } catch (Exception exc) { Log.Write(LogLevel.Warning, "ARCore error: " + exc); } }