DrawSkyboxPass.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. namespace UnityEngine.Rendering.Universal
  2. {
  3. /// <summary>
  4. /// Draw the skybox into the given color buffer using the given depth buffer for depth testing.
  5. ///
  6. /// This pass renders the standard Unity skybox.
  7. /// </summary>
  8. public class DrawSkyboxPass : ScriptableRenderPass
  9. {
  10. public DrawSkyboxPass(RenderPassEvent evt)
  11. {
  12. base.profilingSampler = new ProfilingSampler(nameof(DrawSkyboxPass));
  13. renderPassEvent = evt;
  14. }
  15. /// <inheritdoc/>
  16. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  17. {
  18. CameraData cameraData = renderingData.cameraData;
  19. Camera camera = cameraData.camera;
  20. var activeDebugHandler = GetActiveDebugHandler(renderingData);
  21. if (activeDebugHandler != null)
  22. {
  23. // TODO: The skybox needs to work the same as the other shaders, but until it does we'll not render it
  24. // when certain debug modes are active (e.g. wireframe/overdraw modes)
  25. if (activeDebugHandler.IsScreenClearNeeded)
  26. {
  27. return;
  28. }
  29. }
  30. #if ENABLE_VR && ENABLE_XR_MODULE
  31. // XRTODO: Remove this code once Skybox pass is moved to SRP land.
  32. if (cameraData.xr.enabled)
  33. {
  34. // Setup Legacy XR buffer states
  35. if (cameraData.xr.singlePassEnabled)
  36. {
  37. // Setup legacy skybox stereo buffer
  38. camera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Left, cameraData.GetProjectionMatrix(0));
  39. camera.SetStereoViewMatrix(Camera.StereoscopicEye.Left, cameraData.GetViewMatrix(0));
  40. camera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Right, cameraData.GetProjectionMatrix(1));
  41. camera.SetStereoViewMatrix(Camera.StereoscopicEye.Right, cameraData.GetViewMatrix(1));
  42. CommandBuffer cmd = CommandBufferPool.Get();
  43. // Use legacy stereo instancing mode to have legacy XR code path configured
  44. cmd.SetSinglePassStereo(SystemInfo.supportsMultiview ? SinglePassStereoMode.Multiview : SinglePassStereoMode.Instancing);
  45. context.ExecuteCommandBuffer(cmd);
  46. cmd.Clear();
  47. // Calling into built-in skybox pass
  48. context.DrawSkybox(camera);
  49. // Disable Legacy XR path
  50. cmd.SetSinglePassStereo(SinglePassStereoMode.None);
  51. context.ExecuteCommandBuffer(cmd);
  52. // We do not need to submit here due to special handling of stereo matrices in core.
  53. // context.Submit();
  54. CommandBufferPool.Release(cmd);
  55. camera.ResetStereoProjectionMatrices();
  56. camera.ResetStereoViewMatrices();
  57. }
  58. else
  59. {
  60. camera.projectionMatrix = cameraData.GetProjectionMatrix(0);
  61. camera.worldToCameraMatrix = cameraData.GetViewMatrix(0);
  62. context.DrawSkybox(camera);
  63. // XRTODO: remove this call because it creates issues with nested profiling scopes
  64. // See examples in UniversalRenderPipeline.RenderSingleCamera() and in ScriptableRenderer.Execute()
  65. context.Submit(); // Submit and execute the skybox pass before resetting the matrices
  66. camera.ResetProjectionMatrix();
  67. camera.ResetWorldToCameraMatrix();
  68. }
  69. }
  70. else
  71. #endif
  72. {
  73. context.DrawSkybox(camera);
  74. }
  75. }
  76. }
  77. }