OculusMotionVectorPass.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Profiling;
  4. namespace UnityEngine.Rendering.Universal.Internal
  5. {
  6. /// <summary>
  7. /// Draw motion vectors into the given color and depth target. Both come from the Oculus runtime.
  8. ///
  9. /// This will render objects that have a material and/or shader with the pass name "MotionVectors".
  10. /// </summary>
  11. public class OculusMotionVectorPass : ScriptableRenderPass
  12. {
  13. FilteringSettings m_FilteringSettings;
  14. ProfilingSampler m_ProfilingSampler;
  15. RenderTargetIdentifier motionVectorColorIdentifier;
  16. RenderTargetIdentifier motionVectorDepthIdentifier;
  17. public OculusMotionVectorPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  18. {
  19. base.profilingSampler = new ProfilingSampler(nameof(OculusMotionVectorPass));
  20. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  21. renderPassEvent = evt;
  22. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  23. }
  24. internal OculusMotionVectorPass(URPProfileId profileId, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  25. : this(profileId.GetType().Name, opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
  26. {
  27. m_ProfilingSampler = ProfilingSampler.Get(profileId);
  28. }
  29. public void Setup(
  30. RenderTargetIdentifier motionVecColorIdentifier,
  31. RenderTargetIdentifier motionVecDepthIdentifier)
  32. {
  33. this.motionVectorColorIdentifier = motionVecColorIdentifier;
  34. this.motionVectorDepthIdentifier = motionVecDepthIdentifier;
  35. }
  36. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  37. {
  38. ConfigureTarget(motionVectorColorIdentifier, motionVectorDepthIdentifier);
  39. ConfigureClear(ClearFlag.All, Color.black);
  40. }
  41. /// <inheritdoc/>
  42. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  43. {
  44. // NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
  45. // Currently there's an issue which results in mismatched markers.
  46. CommandBuffer cmd = CommandBufferPool.Get();
  47. using (new ProfilingScope(cmd, m_ProfilingSampler))
  48. {
  49. context.ExecuteCommandBuffer(cmd);
  50. cmd.Clear();
  51. Camera camera = renderingData.cameraData.camera;
  52. var filterSettings = m_FilteringSettings;
  53. var drawSettings = CreateDrawingSettings(new ShaderTagId("MotionVectors"), ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
  54. drawSettings.perObjectData = PerObjectData.MotionVectors;
  55. context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings);
  56. }
  57. context.ExecuteCommandBuffer(cmd);
  58. CommandBufferPool.Release(cmd);
  59. }
  60. }
  61. }