private void InitARFrameBuffer() { mImage = new UnityARImageFrameData(); int yBufSize = mARCamera.videoParams.yWidth * mARCamera.videoParams.yHeight; mImage.y.data = Marshal.AllocHGlobal(yBufSize); mImage.y.width = (ulong)mARCamera.videoParams.yWidth; mImage.y.height = (ulong)mARCamera.videoParams.yHeight; mImage.y.stride = (ulong)mARCamera.videoParams.yWidth; // This does assume the YUV_NV21 format int vuBufSize = mARCamera.videoParams.yWidth * mARCamera.videoParams.yWidth / 2; mImage.vu.data = Marshal.AllocHGlobal(vuBufSize); mImage.vu.width = (ulong)mARCamera.videoParams.yWidth / 2; mImage.vu.height = (ulong)mARCamera.videoParams.yHeight / 2; mImage.vu.stride = (ulong)mARCamera.videoParams.yWidth; mSession.SetCapturePixelData(true, mImage.y.data, mImage.vu.data); }
/// <summary> /// Sends an image frame and its corresponding camera pose to LibPlacenote mapping/localization module /// </summary> /// <param name="frameData">Image frame data.</param> /// <param name="position">Position of the camera at the time frameData is captured</param> /// <param name="rotation">Quaternion of the camera at the time frameData is captured.</param> /// <param name="screenOrientation"> /// Fill in this parameter with screenOrientation from the current UnityVideoParams structure. /// Used to correct for the extra rotation applied by the Unity ARKit Plugin on the ARKit pose transform. /// </param> public void SendARFrame(UnityARImageFrameData frameData, Vector3 position, Quaternion rotation, int screenOrientation) { Matrix4x4 orientRemovalMat = Matrix4x4.zero; orientRemovalMat.m22 = orientRemovalMat.m33 = 1; switch (screenOrientation) { // portrait case 1: orientRemovalMat.m01 = 1; orientRemovalMat.m10 = -1; break; case 2: orientRemovalMat.m01 = -1; orientRemovalMat.m10 = 1; break; // landscape case 3: // do nothing orientRemovalMat = Matrix4x4.identity; break; case 4: orientRemovalMat.m00 = -1; orientRemovalMat.m11 = -1; break; default: Debug.LogError("Unrecognized screen orientation"); return; } Matrix4x4 rotationMat = Matrix4x4.TRS(new Vector3(0, 0, 0), rotation, new Vector3(1, 1, 1)); rotationMat = rotationMat * orientRemovalMat; rotation = PNUtility.MatrixOps.QuaternionFromMatrix(rotationMat); PNTransformUnity pose = new PNTransformUnity(); pose.position.x = position.x; pose.position.y = position.y; pose.position.z = position.z; pose.rotation.x = rotation.x; pose.rotation.y = rotation.y; pose.rotation.z = rotation.z; pose.rotation.w = rotation.w; PNImagePlaneUnity yPlane = new PNImagePlaneUnity(); yPlane.width = (int)frameData.y.width; yPlane.height = (int)frameData.y.height; yPlane.stride = (int)frameData.y.stride; yPlane.buf = frameData.y.data; PNImagePlaneUnity vuPlane = new PNImagePlaneUnity(); vuPlane.width = (int)frameData.vu.width; vuPlane.height = (int)frameData.vu.height; vuPlane.stride = (int)frameData.vu.stride; vuPlane.buf = frameData.vu.data; #if !UNITY_EDITOR PNSetFrame(ref yPlane, ref vuPlane, ref pose); #endif }