No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyRenderFeature.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. using UnityEngine.Rendering.RenderGraphModule.Util;
  5. using UnityEngine.Rendering.Universal;
  6. // This example copies the active color texture to a new texture. This example is for API demonstrative purposes,
  7. // so the new texture is not used anywhere else in the frame, you can use the frame debugger to verify its contents.
  8. public class CopyRenderFeature : ScriptableRendererFeature
  9. {
  10. class CopyRenderPass : ScriptableRenderPass
  11. {
  12. string m_PassName = "Copy To or From Temp Texture";
  13. public CopyRenderPass()
  14. {
  15. //The pass will read the current color texture. That needs to be an intermediate texture. It's not supported to use the BackBuffer as input texture.
  16. //By setting this property, URP will automatically create an intermediate texture.
  17. //It's good practice to set it here and not from the RenderFeature. This way, the pass is selfcontaining and you can use it to directly enqueue the pass from a monobehaviour without a RenderFeature.
  18. requiresIntermediateTexture = true;
  19. }
  20. // This is where the renderGraph handle can be accessed.
  21. // Each ScriptableRenderPass can use the RenderGraph handle to add multiple render passes to the render graph
  22. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  23. {
  24. // UniversalResourceData contains all the texture handles used by the renderer, including the active color and depth textures
  25. // The active color and depth textures are the main color and depth buffers that the camera renders into
  26. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  27. // The destination texture is created here,
  28. // the texture is created with the same dimensions as the active color texture
  29. var source = resourceData.activeColorTexture;
  30. var destinationDesc = renderGraph.GetTextureDesc(source);
  31. destinationDesc.name = $"CameraColor-{m_PassName}";
  32. destinationDesc.clearBuffer = false;
  33. TextureHandle destination = renderGraph.CreateTexture(destinationDesc);
  34. if (RenderGraphUtils.CanAddCopyPassMSAA())
  35. {
  36. // This simple pass copies the active color texture to a new texture.
  37. renderGraph.AddCopyPass(resourceData.activeColorTexture, destination, passName: m_PassName);
  38. //Need to copy back otherwise the pass gets culled since the result of the previous copy is not read. This is just for demonstration purposes.
  39. renderGraph.AddCopyPass(destination, resourceData.activeColorTexture, passName: m_PassName);
  40. }
  41. else
  42. {
  43. Debug.Log("Can't add the copy pass due to MSAA");
  44. }
  45. }
  46. }
  47. CopyRenderPass m_CopyRenderPass;
  48. /// <inheritdoc/>
  49. public override void Create()
  50. {
  51. m_CopyRenderPass = new CopyRenderPass();
  52. // Configures where the render pass should be injected.
  53. m_CopyRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
  54. }
  55. // Here you can inject one or multiple render passes in the renderer.
  56. // This method is called when setting up the renderer once per-camera.
  57. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  58. {
  59. renderer.EnqueuePass(m_CopyRenderPass);
  60. }
  61. }