|
| 1 | +# Render Requests |
| 2 | + |
| 3 | +For a general documentation see the [Core Package](https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@latest/User-Render-Requests.html) about Render Requests. |
| 4 | + |
| 5 | +## Use UniversalRenderPipeline.SingleCameraRequest |
| 6 | + |
| 7 | +`UniversalRenderPipeline.SingleCameraRequest` renders a single camera, without taking into account the full stack of cameras. |
| 8 | + |
| 9 | +You can still hook into callbacks from [RenderPipelineManager](https://docs.unity3d.com/ScriptReference/Rendering.RenderPipelineManager.html). |
| 10 | + |
| 11 | +The following code sample shows that you can hook into [RenderPipelineManager.endContextRendering](https://docs.unity3d.com/ScriptReference/Rendering.RenderPipelineManager-endContextRendering.html) `UniversalRenderPipeline.SingleCameraRequest` |
| 12 | + |
| 13 | +To try out this example: |
| 14 | + |
| 15 | +- Attach the script to a **GameObject** in the **Scene**. |
| 16 | +- Configure the **cams** and **rts**. |
| 17 | +- Set **useSingleCameraRequestValues** to true or false depending on which type of render request you want to use. |
| 18 | +- Select **Enter Play Mode**. |
| 19 | +- See the **Console** Log. |
| 20 | + |
| 21 | +``` |
| 22 | +using System.Collections; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Text; |
| 25 | +using UnityEngine; |
| 26 | +using UnityEngine.Rendering; |
| 27 | +using UnityEngine.Rendering.Universal; |
| 28 | +
|
| 29 | +public class SingleCameraRenderRequestExample : MonoBehaviour |
| 30 | +{ |
| 31 | + public Camera[] cams; |
| 32 | + public RenderTexture[] rts; |
| 33 | +
|
| 34 | + void Start() |
| 35 | + { |
| 36 | + if (cams == null || cams.Length == 0 || rts == null || cams.Length != rts.Length) |
| 37 | + { |
| 38 | + Debug.LogError("Invalid setup"); |
| 39 | + return; |
| 40 | + } |
| 41 | +
|
| 42 | + StartCoroutine(RenderSingleRequestNextFrame()); |
| 43 | + RenderPipelineManager.endContextRendering += OnEndContextRendering; |
| 44 | + } |
| 45 | +
|
| 46 | + void OnEndContextRendering(ScriptableRenderContext context, List<Camera> cameras) |
| 47 | + { |
| 48 | + var stb = new StringBuilder($"Cameras Count from EndContextRendering: <b> {cameras.Count}</b>."); |
| 49 | + foreach (var cam in cameras) |
| 50 | + { |
| 51 | + stb.AppendLine($"- {cam.name}"); |
| 52 | + } |
| 53 | + Debug.Log(stb.ToString()); |
| 54 | + } |
| 55 | +
|
| 56 | + void OnDestroy() |
| 57 | + { |
| 58 | + RenderPipelineManager.endContextRendering -= OnEndContextRendering; |
| 59 | + } |
| 60 | +
|
| 61 | + IEnumerator RenderSingleRequestNextFrame() |
| 62 | + { |
| 63 | + yield return new WaitForEndOfFrame(); |
| 64 | +
|
| 65 | + SendSingleRenderRequests(); |
| 66 | +
|
| 67 | + yield return new WaitForEndOfFrame(); |
| 68 | +
|
| 69 | + StartCoroutine(RenderSingleRequestNextFrame()); |
| 70 | + } |
| 71 | +
|
| 72 | + void SendSingleRenderRequests() |
| 73 | + { |
| 74 | + for (int i = 0; i < cams.Length; i++) |
| 75 | + { |
| 76 | + UniversalRenderPipeline.SingleCameraRequest request = |
| 77 | + new UniversalRenderPipeline.SingleCameraRequest(); |
| 78 | +
|
| 79 | + // Check if the request is supported by the active render pipeline |
| 80 | + if (RenderPipeline.SupportsRenderRequest(cams[i], request)) |
| 81 | + { |
| 82 | + request.destination = rts[i]; |
| 83 | + RenderPipeline.SubmitRenderRequest(cams[i], request); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
0 commit comments