A game about forced loneliness, made by TACStudios
at master 86 lines 3.7 kB view raw
1using System; 2using System.Collections.Generic; 3using UnityEngine.Experimental.Rendering; 4 5namespace UnityEngine.Rendering 6{ 7 /// <summary> 8 /// Utility class to connect SRP to automated test framework. 9 /// </summary> 10 public static class XRGraphicsAutomatedTests 11 { 12 // XR tests can be enabled from the command line. Cache result to avoid GC. 13 static bool activatedFromCommandLine 14 { 15#if UNITY_EDITOR 16 get => Array.Exists(Environment.GetCommandLineArgs(), arg => arg == "-xr-reuse-tests"); 17#elif XR_REUSE_TESTS_STANDALONE 18 get => true; 19#else 20 get => false; 21#endif 22 } 23 24 /// <summary> 25 /// Used by render pipelines to initialize XR tests. 26 /// </summary> 27 public static bool enabled { get; } = activatedFromCommandLine; 28 29 /// <summary> 30 /// Set by automated test framework and read by render pipelines. 31 /// </summary> 32 public static bool running = false; 33 34 // Helper function to override the XR default layout using settings of new camera 35 internal static void OverrideLayout(XRLayout layout, Camera camera) 36 { 37#if ENABLE_VR && ENABLE_XR_MODULE 38 if (enabled && running) 39 { 40 var camProjMatrix = camera.projectionMatrix; 41 var camViewMatrix = camera.worldToCameraMatrix; 42 43 if (camera.TryGetCullingParameters(false, out var cullingParams)) 44 { 45 cullingParams.stereoProjectionMatrix = camProjMatrix; 46 cullingParams.stereoViewMatrix = camViewMatrix; 47 cullingParams.stereoSeparationDistance = 0.0f; 48 49 List<(Camera, XRPass)> xrPasses = layout.GetActivePasses(); 50 for (int passId = 0; passId < xrPasses.Count; passId++) 51 { 52 var xrPass = xrPasses[passId].Item2; 53 xrPass.AssignCullingParams(xrPass.cullingPassId, cullingParams); 54 55 for (int viewId = 0; viewId < xrPass.viewCount; viewId++) 56 { 57 var projMatrix = camProjMatrix; 58 var viewMatrix = camViewMatrix; 59 60 bool isFirstViewMultiPass = xrPasses.Count == 2 && passId == 0; 61 bool isFirstViewSinglePass = xrPasses.Count == 1 && viewId == 0; 62 63 if (isFirstViewMultiPass || isFirstViewSinglePass) 64 { 65 // Modify the render viewpoint and frustum of the first view in order to 66 // distinguish it from the final view used for image comparison. 67 // This is a trick to help detect issues related to view indexing. 68 var planes = projMatrix.decomposeProjection; 69 planes.left *= 0.44f; 70 planes.right *= 0.88f; 71 planes.top *= 0.11f; 72 planes.bottom *= 0.33f; 73 projMatrix = Matrix4x4.Frustum(planes); 74 viewMatrix *= Matrix4x4.Translate(new Vector3(.34f, 0.25f, -0.08f)); 75 } 76 77 XRView xrView = new XRView(projMatrix, viewMatrix, Matrix4x4.identity, false, xrPass.GetViewport(viewId), null, xrPass.GetTextureArraySlice(viewId)); 78 xrPass.AssignView(viewId, xrView); 79 } 80 } 81 } 82 } 83#endif 84 } 85 } 86}