Read or write to a texture in a render pass in URP
Set a texture as an input or output for a render pass.
読み終わるまでの所要時間 3 分最終更新 23日前
You can use the render graph system API to set a texture as an input or output for a custom render pass, so you can read from or write to it.
Set a texture as an input
To set a texture as an input for a custom render pass, follow these steps:
-
In your pass data, add a texture handle field to the data your pass uses.For example:// Create the data your pass usespublic class MyPassData{ // Add a texture handle public TextureHandle textureToUse;}
-
Set the texture handle to the texture you want to use. For more information, refer to Create a temporary texture for a single frame or Import a texture into the render graph system.
-
Call themethod to set the texture as an input.
UseTextureFor example:builder.UseTexture(passData.textureToUse, AccessFlags.Read);
In your method, you can now use the object in the pass data as an input for APIs.
SetRenderFuncTextureHandleSet a texture as the render target
To set a texture as the render target, in the method, use the method. The method sets the texture as write-only by default.
RecordRenderGraphSetRenderAttachmentSetRenderAttachmentbuilder.SetRenderAttachment(textureHandle, 0);
You don't need to add the texture to your pass data. The render graph system sets up the texture for you automatically before it executes the render pass.
You can't use and on the same texture in an render pass. Refer to Change the render target during a render pass for more information.
UseTextureSetRenderAttachmentAddRasterRenderPassIf you need to draw objects to the render target, refer to Draw objects in a render pass.
Change the render target during a render pass
You can't change which texture URP writes to during a render graph system render pass.
You can do either of the following instead:
- Create a second custom render pass, and use during the second render pass to change the render target.
builder.SetRenderAttachment - Use the API so you can use the
UnsafePassAPI in theSetRenderTargetmethod. For more information, refer to Use the CommandBuffer interface in a render graph.SetRenderFunc
You can use these methods to read from and write to the same texture, by first copying from the texture to a temporary texture you create, then copying back.
AddRasterRenderPassAddUnsafePassSetRenderTarget()Examples
Refer to the following:
- Write a render pass for an example that creates a texture then blits to it.
- Use a texture in multiple render passes