private static float _copysign(float sizeval, float signval) { if (Mathf.Sign(signval) != 1f) { return(-Mathf.Abs(sizeval)); } return(Mathf.Abs(sizeval)); }
public static void TakeStereoScreenshot(uint screenshotHandle, GameObject target, int cellSize, float ipd, ref string previewFilename, ref string VRFilename) { Texture2D texture2D = new Texture2D(4096, 4096, TextureFormat.ARGB32, false); Stopwatch stopwatch = new Stopwatch(); Camera camera = null; stopwatch.Start(); Camera camera2 = target.GetComponent <Camera>(); if (camera2 == null) { if (camera == null) { camera = new GameObject().AddComponent <Camera>(); } camera2 = camera; } Texture2D texture2D2 = new Texture2D(2048, 2048, TextureFormat.ARGB32, false); RenderTexture renderTexture = new RenderTexture(2048, 2048, 24); RenderTexture targetTexture = camera2.targetTexture; bool orthographic = camera2.orthographic; float fieldOfView = camera2.fieldOfView; float aspect = camera2.aspect; StereoTargetEyeMask stereoTargetEye = camera2.stereoTargetEye; camera2.stereoTargetEye = StereoTargetEyeMask.None; camera2.fieldOfView = 60f; camera2.orthographic = false; camera2.targetTexture = renderTexture; camera2.aspect = 1f; camera2.Render(); RenderTexture.active = renderTexture; texture2D2.ReadPixels(new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0); RenderTexture.active = null; camera2.targetTexture = null; UnityEngine.Object.DestroyImmediate(renderTexture); SteamVR_SphericalProjection steamVR_SphericalProjection = camera2.gameObject.AddComponent <SteamVR_SphericalProjection>(); Vector3 localPosition = target.transform.localPosition; Quaternion localRotation = target.transform.localRotation; Vector3 position = target.transform.position; Quaternion lhs = Quaternion.Euler(0f, target.transform.rotation.eulerAngles.y, 0f); Transform transform = camera2.transform; int num = 1024 / cellSize; float num2 = 90f / (float)num; float num3 = num2 / 2f; RenderTexture renderTexture2 = new RenderTexture(cellSize, cellSize, 24); renderTexture2.wrapMode = TextureWrapMode.Clamp; renderTexture2.antiAliasing = 8; camera2.fieldOfView = num2; camera2.orthographic = false; camera2.targetTexture = renderTexture2; camera2.aspect = aspect; camera2.stereoTargetEye = StereoTargetEyeMask.None; for (int i = 0; i < num; i++) { float num4 = 90f - (float)i * num2 - num3; int num5 = 4096 / renderTexture2.width; float num6 = 360f / (float)num5; float num7 = num6 / 2f; int num8 = i * 1024 / num; for (int j = 0; j < 2; j++) { if (j == 1) { num4 = -num4; num8 = 2048 - num8 - cellSize; } for (int k = 0; k < num5; k++) { float num9 = -180f + (float)k * num6 + num7; int destX = k * 4096 / num5; int num10 = 0; float num11 = -ipd / 2f * Mathf.Cos(num4 * 0.017453292f); for (int l = 0; l < 2; l++) { if (l == 1) { num10 = 2048; num11 = -num11; } Vector3 b = lhs * Quaternion.Euler(0f, num9, 0f) * new Vector3(num11, 0f, 0f); transform.position = position + b; Quaternion quaternion = Quaternion.Euler(num4, num9, 0f); transform.rotation = lhs * quaternion; Vector3 vector = quaternion * Vector3.forward; float num12 = num9 - num6 / 2f; float num13 = num12 + num6; float num14 = num4 + num2 / 2f; float num15 = num14 - num2; float y = (num12 + num13) / 2f; float x = (Mathf.Abs(num14) < Mathf.Abs(num15)) ? num14 : num15; Vector3 vector2 = Quaternion.Euler(x, num12, 0f) * Vector3.forward; Vector3 vector3 = Quaternion.Euler(x, num13, 0f) * Vector3.forward; Vector3 vector4 = Quaternion.Euler(num14, y, 0f) * Vector3.forward; Vector3 vector5 = Quaternion.Euler(num15, y, 0f) * Vector3.forward; Vector3 vector6 = vector2 / Vector3.Dot(vector2, vector); Vector3 a = vector3 / Vector3.Dot(vector3, vector); Vector3 vector7 = vector4 / Vector3.Dot(vector4, vector); Vector3 a2 = vector5 / Vector3.Dot(vector5, vector); Vector3 a3 = a - vector6; Vector3 a4 = a2 - vector7; float magnitude = a3.magnitude; float magnitude2 = a4.magnitude; float num16 = 1f / magnitude; float num17 = 1f / magnitude2; Vector3 uAxis = a3 * num16; Vector3 vAxis = a4 * num17; steamVR_SphericalProjection.Set(vector, num12, num13, num14, num15, uAxis, vector6, num16, vAxis, vector7, num17); camera2.aspect = magnitude / magnitude2; camera2.Render(); RenderTexture.active = renderTexture2; texture2D.ReadPixels(new Rect(0f, 0f, (float)renderTexture2.width, (float)renderTexture2.height), destX, num8 + num10); RenderTexture.active = null; } float flProgress = ((float)i * ((float)num5 * 2f) + (float)k + (float)(j * num5)) / ((float)num * ((float)num5 * 2f)); OpenVR.Screenshots.UpdateScreenshotProgress(screenshotHandle, flProgress); } } } OpenVR.Screenshots.UpdateScreenshotProgress(screenshotHandle, 1f); previewFilename += ".png"; VRFilename += ".png"; texture2D2.Apply(); File.WriteAllBytes(previewFilename, ImageConversion.EncodeToPNG(texture2D)); texture2D.Apply(); File.WriteAllBytes(VRFilename, ImageConversion.EncodeToPNG(texture2D)); if (camera2 != camera) { camera2.targetTexture = targetTexture; camera2.orthographic = orthographic; camera2.fieldOfView = fieldOfView; camera2.aspect = aspect; camera2.stereoTargetEye = stereoTargetEye; target.transform.localPosition = localPosition; target.transform.localRotation = localRotation; } else { camera.targetTexture = null; } UnityEngine.Object.DestroyImmediate(renderTexture2); UnityEngine.Object.DestroyImmediate(steamVR_SphericalProjection); stopwatch.Stop(); UnityEngine.Debug.Log(string.Format("Screenshot took {0} seconds.", stopwatch.Elapsed)); if (camera != null) { UnityEngine.Object.DestroyImmediate(camera.gameObject); } UnityEngine.Object.DestroyImmediate(texture2D2); UnityEngine.Object.DestroyImmediate(texture2D); }