/// <summary> /// Configures the camera. /// </summary> /// <returns><c>true</c>, if camera was configured, <c>false</c> otherwise.</returns> /// <param name="camera">Camera.</param> /// <param name="eyePositionOffset">Eye position offset.</param> bool ConfigureCamera(Camera camera, float eyePositionOffset) { // Always set camera fov and aspect ration camera.fieldOfView = VerticalFOV; camera.aspect = AspectRatio; // Push params also into the mesh distortion instance (if there is one) bool cameraRight = (camera == CameraRight) ? true : false; OVRLensCorrection lc = camera.GetComponent <OVRLensCorrection>(); camera.GetComponent <OVRCamera>().UpdateDistortionMeshParams(ref lc, cameraRight, FlipCorrectionInY); // Set camera variables that pertain to the neck and eye position // NOTE: We will want to add a scale vlue here in the event that the player // grows or shrinks in the world. This keeps head modelling behaviour // accurate camera.GetComponent <OVRCamera>().NeckPosition = NeckPosition; Vector3 EyePosition = EyeCenterPosition; EyePosition.x = eyePositionOffset; camera.GetComponent <OVRCamera>().EyePosition = EyePosition; // Background color camera.backgroundColor = BackgroundColor; // Clip Planes camera.nearClipPlane = NearClipPlane; camera.farClipPlane = FarClipPlane; return(true); }
/// <summary> /// UpdateDistortionMeshParams /// Query the camera fielf of view and then set up the appropriate values /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateDistortionMeshParams(ref OVRLensCorrection lc, bool rightEye, bool flipY) { float fovH = GetHorizontalFOV(); eyeMesh.SetFOV(fovH, camera.fieldOfView); eyeMesh.UpdateParams(ref lc, rightEye, flipY); }
/// <summary> /// UpdateDistortionMeshParams /// Query the camera fielf of view and then set up the appropriate values /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateDistortionMeshParams(ref OVRLensCorrection lc, bool rightEye, bool flipY) { if (CameraController == null) { return; } float fovH = GetHorizontalFOV(); eyeMesh.SetFOV(fovH, camera.fieldOfView); eyeMesh.UpdateParams(ref lc, rightEye, flipY, CameraController.UseCameraTexture); }
/// <summary> /// Generates the mesh. /// This should only be done when fov changes. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void GenerateMesh(ref OVRLensCorrection lc, bool rightEye, bool flipY) { int numVerts = 0; int numIndicies = 0; // Generate OVR mesh for given eye OVR_GenerateDistortionMesh(ref numVerts, ref numIndicies, rightEye); // create space to copy mesh into DistMeshVert[] meshVerts = new DistMeshVert[numVerts]; triIndices = new int[numIndicies]; DistScaleOffsetUV scaleOffset = new DistScaleOffsetUV(); // Copy mesh into above data OVR_CopyDistortionMesh(meshVerts, triIndices, ref scaleOffset, rightEye, flipY); // Set material scale and offset values lc._DMScale.x = scaleOffset.Scale_x; lc._DMScale.y = scaleOffset.Scale_y; lc._DMOffset.x = scaleOffset.Offset_x; lc._DMOffset.y = scaleOffset.Offset_y; // Copy local mesh into proper Unity mesh structure mesh = new Mesh(); positions = new Vector3[numVerts]; uvR = new Vector2[numVerts]; uvG = new Vector2[numVerts]; uvB = new Vector3[numVerts]; for (int i = 0; i < numVerts; i++) { positions[i].x = meshVerts[i].ScreenPosNDC_x; positions[i].y = meshVerts[i].ScreenPosNDC_y; positions[i].z = meshVerts[i].Shade; // overload for shading on edges uvR[i].x = meshVerts[i].TanEyeAnglesR_u; uvR[i].y = meshVerts[i].TanEyeAnglesR_v; uvG[i].x = meshVerts[i].TanEyeAnglesG_u; uvG[i].y = meshVerts[i].TanEyeAnglesG_v; uvB[i].x = meshVerts[i].TanEyeAnglesB_u; uvB[i].y = meshVerts[i].TanEyeAnglesB_v; } mesh.vertices = positions; mesh.uv = uvR; mesh.uv1 = uvG; mesh.normals = uvB; mesh.triangles = triIndices; // Destory internal distorion meshes OVR_DestroyDistortionMesh(); }
/// <summary> /// Generates the mesh. /// This should only be done when fov changes. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void GenerateMesh(ref OVRLensCorrection lc, bool rightEye, bool flipY) { int numVerts = 0; int numIndicies = 0; // Generate OVR mesh for given eye OVR_GenerateDistortionMesh(ref numVerts, ref numIndicies, rightEye); // create space to copy mesh into DistMeshVert[] meshVerts = new DistMeshVert[numVerts]; triIndices = new int[numIndicies]; DistScaleOffsetUV scaleOffset = new DistScaleOffsetUV(); // Copy mesh into above data OVR_CopyDistortionMesh(meshVerts, triIndices, ref scaleOffset, rightEye, flipY); // Set material scale and offset values lc._DMScale.x = scaleOffset.Scale_x; lc._DMScale.y = scaleOffset.Scale_y; lc._DMOffset.x = scaleOffset.Offset_x; lc._DMOffset.y = scaleOffset.Offset_y; // Copy local mesh into proper Unity mesh structure mesh = new Mesh(); positions = new Vector3[numVerts]; uvR = new Vector2[numVerts]; uvG = new Vector2[numVerts]; uvB = new Vector3[numVerts]; for(int i = 0; i < numVerts; i++) { positions[i].x = meshVerts[i].ScreenPosNDC_x; positions[i].y = meshVerts[i].ScreenPosNDC_y; positions[i].z = meshVerts[i].Shade; // overload for shading on edges uvR[i].x = meshVerts[i].TanEyeAnglesR_u; uvR[i].y = meshVerts[i].TanEyeAnglesR_v; uvG[i].x = meshVerts[i].TanEyeAnglesG_u; uvG[i].y = meshVerts[i].TanEyeAnglesG_v; uvB[i].x = meshVerts[i].TanEyeAnglesB_u; uvB[i].y = meshVerts[i].TanEyeAnglesB_v; } mesh.vertices = positions; mesh.uv = uvR; mesh.uv2 = uvG; mesh.normals = uvB; mesh.triangles = triIndices; // Destory internal distorion meshes OVR_DestroyDistortionMesh(); }
// SetCameraLensCorrection void ConfigureCameraLensCorrection(ref Camera camera) { // Get the distortion scale and aspect ratio to use when calculating distortion shader float distortionScale = 1.0f / OVRDevice.DistortionScale(); float aspectRatio = OVRDevice.CalculateAspectRatio(); // These values are different in the SDK World Demo; Unity renders each camera to a buffer // that is normalized, so we will respect this rule when calculating the distortion inputs float NormalizedWidth = 1.0f; float NormalizedHeight = 1.0f; OVRLensCorrection lc = camera.GetComponent <OVRLensCorrection>(); lc._Scale.x = (NormalizedWidth / 2.0f) * distortionScale; lc._Scale.y = (NormalizedHeight / 2.0f) * distortionScale * aspectRatio; lc._ScaleIn.x = (2.0f / NormalizedWidth); lc._ScaleIn.y = (2.0f / NormalizedHeight) / aspectRatio; lc._HmdWarpParam.x = DistK0; lc._HmdWarpParam.y = DistK1; lc._HmdWarpParam.z = DistK2; }
/// <summary> /// Generates the mesh. /// This should only be done when fov changes. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void GenerateMesh(ref OVRLensCorrection lc, bool rightEye, bool flipY, bool useCameraTexture) { // We only need to create the mesh once and re-use components when camera // is dirty bool create = true; if (!mesh) { mesh = new Mesh(); mesh.MarkDynamic(); } else { create = false; } int numVerts = 0; int numIndicies = 0; // Generate OVR mesh for given eye OVR_GenerateDistortionMesh(ref numVerts, ref numIndicies, rightEye); // create space to copy mesh into if (create) { meshVerts = new DistMeshVert[numVerts]; triIndices = new int[numIndicies]; scaleOffset = new DistScaleOffsetUV(); } // Copy mesh into above data OVR_CopyDistortionMesh(meshVerts, triIndices, ref scaleOffset, rightEye, flipY); // Set material scale and offset values lc._DMScale.x = (useCameraTexture) ? scaleOffset.Scale_x : 0.5f * scaleOffset.Scale_x; lc._DMScale.y = scaleOffset.Scale_y; lc._DMOffset.x = (useCameraTexture) ? scaleOffset.Offset_x : scaleOffset.Offset_x + ((rightEye) ? 0.25f : -0.25f); lc._DMOffset.y = scaleOffset.Offset_y; // Copy local mesh into proper Unity mesh structure if (create) { positions = new Vector3[numVerts]; uvR = new Vector2[numVerts]; uvG = new Vector2[numVerts]; uvB = new Vector3[numVerts]; } for (int i = 0; i < numVerts; i++) { positions[i].x = meshVerts[i].ScreenPosNDC_x; positions[i].y = meshVerts[i].ScreenPosNDC_y; positions[i].z = meshVerts[i].Shade; // overload for shading on edges uvR[i].x = meshVerts[i].TanEyeAnglesR_u; uvR[i].y = meshVerts[i].TanEyeAnglesR_v; uvG[i].x = meshVerts[i].TanEyeAnglesG_u; uvG[i].y = meshVerts[i].TanEyeAnglesG_v; uvB[i].x = meshVerts[i].TanEyeAnglesB_u; uvB[i].y = meshVerts[i].TanEyeAnglesB_v; uvB[i].z = meshVerts[i].TimewarpLerp; // overload for timewarp lerp } mesh.vertices = positions; mesh.uv = uvR; mesh.uv1 = uvG; mesh.normals = uvB; mesh.triangles = triIndices; // Destory internal distorion meshes OVR_DestroyDistortionMesh(); }
/// <summary> /// Updates the parameters. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateParams(ref OVRLensCorrection lc, bool rightEye, bool flipY, bool useCameraTexture) { GenerateMesh(ref lc, rightEye, flipY, useCameraTexture); }
/// <summary> /// UpdateDistortionMeshParams /// Query the camera fielf of view and then set up the appropriate values /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateDistortionMeshParams(ref OVRLensCorrection lc, bool rightEye, bool flipY) { float fovH = GetHorizontalFOV(); eyeMesh.SetFOV(fovH, GetComponent<Camera>().fieldOfView); eyeMesh.UpdateParams(ref lc, rightEye, flipY); }
/// <summary> /// Generates the mesh. /// This should only be done when fov changes. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void GenerateMesh(ref OVRLensCorrection lc, bool rightEye, bool flipY) { // We only need to create the mesh once and re-use components when camera // is dirty bool create = true; if (!mesh) { mesh = new Mesh (); mesh.MarkDynamic(); } else create = false; int numVerts = 0; int numIndicies = 0; // Generate OVR mesh for given eye OVR_GenerateDistortionMesh(ref numVerts, ref numIndicies, rightEye); // create space to copy mesh into if (create) { meshVerts = new DistMeshVert[numVerts]; triIndices = new int[numIndicies]; scaleOffset = new DistScaleOffsetUV (); } // Copy mesh into above data bool needsFlip = (SystemInfo.graphicsDeviceVersion.Contains ("GL")) ? flipY : !flipY; OVR_CopyDistortionMesh(meshVerts, triIndices, ref scaleOffset, rightEye, needsFlip); // Set material scale and offset values lc._DMScale.x = 0.5f * scaleOffset.Scale_x; lc._DMScale.y = scaleOffset.Scale_y; lc._DMOffset.x = scaleOffset.Offset_x + ((rightEye) ? 0.25f : -0.25f); lc._DMOffset.y = scaleOffset.Offset_y; // Copy local mesh into proper Unity mesh structure if (create) { positions = new Vector3[numVerts]; uvR = new Vector2[numVerts]; uvG = new Vector2[numVerts]; uvB = new Vector3[numVerts]; } for(int i = 0; i < numVerts; i++) { positions[i].x = meshVerts[i].ScreenPosNDC_x; positions[i].y = meshVerts[i].ScreenPosNDC_y; positions[i].z = meshVerts[i].Shade; // overload for shading on edges uvR[i].x = meshVerts[i].TanEyeAnglesR_u; uvR[i].y = meshVerts[i].TanEyeAnglesR_v; uvG[i].x = meshVerts[i].TanEyeAnglesG_u; uvG[i].y = meshVerts[i].TanEyeAnglesG_v; uvB[i].x = meshVerts[i].TanEyeAnglesB_u; uvB[i].y = meshVerts[i].TanEyeAnglesB_v; } mesh.vertices = positions; mesh.uv = uvR; mesh.uv1 = uvG; mesh.normals = uvB; mesh.triangles = triIndices; // Destory internal distorion meshes OVR_DestroyDistortionMesh(); }
/// <summary> /// Updates the parameters. /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateParams(ref OVRLensCorrection lc, bool rightEye, bool flipY) { GenerateMesh(ref lc, rightEye, flipY); }
/// <summary> /// Configures the camera. /// </summary> /// <returns><c>true</c>, if camera was configured, <c>false</c> otherwise.</returns> /// <param name="camera">Camera.</param> /// <param name="eyePositionOffset">Eye position offset.</param> bool ConfigureCamera(Camera camera, float eyePositionOffset) { #if (!UNITY_ANDROID || UNITY_EDITOR) OVRCamera cam = camera.GetComponent <OVRCamera>(); if (UpdateDistortionDirtyFlag) { // Always set camera fov and aspect ration camera.fieldOfView = VerticalFOV; camera.aspect = AspectRatio; // Push params also into the mesh distortion instance (if there is one) OVRLensCorrection lc = camera.GetComponent <OVRLensCorrection>(); cam.UpdateDistortionMeshParams(ref lc, (camera == CameraRight), FlipCorrectionInY); if (!UseCameraTexture && SystemInfo.graphicsDeviceVersion.Contains("Direct3D")) // this doesn't work with graphics emulation enabled (ie for Android) { lc._DMScale = new Vector2(lc._DMScale.x, -lc._DMScale.y); } } if (UpdateCamerasDirtyFlag) { // Background color camera.backgroundColor = BackgroundColor; // Clip Planes camera.nearClipPlane = NearClipPlane; camera.farClipPlane = FarClipPlane; // Set camera variables that pertain to the neck and eye position // NOTE: We will want to add a scale value here in the event that the player // grows or shrinks in the world. This keeps head modelling behaviour // accurate cam.NeckPosition = NeckPosition; cam.EyePosition = new Vector3(eyePositionOffset, 0f, 0f); } #else // Camera.targetTexture will be set each frame in Update to a different buffer // to allow overlap with async time warp. camera.fieldOfView = 90.0f; camera.aspect = 1.0f; camera.rect = new Rect(0, 0, 1, 1); // Does this matter when targetTexture is set? if (UpdateCamerasDirtyFlag) { // Background color camera.backgroundColor = BackgroundColor; // Clip Planes camera.nearClipPlane = NearClipPlane; camera.farClipPlane = FarClipPlane; // If we don't clear the color buffer with a glClear, tiling GPUs // will be forced to do an "unresolve" and read back the color buffer information. // The clear is free on PowerVR, and possibly Mali, but it is a performance cost // on Adreno, and we would be better off if we had the ability to discard/invalidate // the color buffer instead of clearing. // NOTE: The color buffer is not being invalidated in skybox mode, forcing an additional, // wasted color buffer read before the skybox is drawn. camera.clearFlags = (HasSkybox) ? CameraClearFlags.Skybox : CameraClearFlags.SolidColor; } #endif return(true); }
/// <summary> /// UpdateDistortionMeshParams /// Query the camera fielf of view and then set up the appropriate values /// </summary> /// <param name="lc">Lc.</param> /// <param name="rightEye">If set to <c>true</c> right eye.</param> /// <param name="flipY">If set to <c>true</c> flip y.</param> public void UpdateDistortionMeshParams (ref OVRLensCorrection lc, bool rightEye, bool flipY) { if (CameraController == null) return; float fovH = GetHorizontalFOV(); eyeMesh.SetFOV(fovH, camera.fieldOfView); eyeMesh.UpdateParams(ref lc, rightEye, flipY, CameraController.UseCameraTexture); }