ドキュメント

Unity Engine


User Manual

Script Reference

Unity Engine


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.
注
To copy between textures with less code, use the
AddBlitPass
method instead. For more information, refer to Blit using the render graph system.

Set a texture as an input

To set a texture as an input for a custom render pass, follow these steps:
  1. 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;}
  2. 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.
  3. Call the
    UseTexture
    method to set the texture as an input.
    For example:
    builder.UseTexture(passData.textureToUse, AccessFlags.Read);
In your
SetRenderFunc
method, you can now use the
TextureHandle
object in the pass data as an input for APIs.

Set a texture as the render target

To set a texture as the render target, in the
RecordRenderGraph
method, use the
SetRenderAttachment
method. The
SetRenderAttachment
method sets the texture as write-only by default.
builder.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
UseTexture
and
SetRenderAttachment
on the same texture in an
AddRasterRenderPass
render pass. Refer to Change the render target during a render pass for more information.
If 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
    builder.SetRenderAttachment
    during the second render pass to change the render target.
  • Use the
    UnsafePass
    API so you can use the
    SetRenderTarget
    API in the
    SetRenderFunc
    method. For more information, refer to Use the CommandBuffer interface in a render graph.
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.
AddRasterRenderPass
can only merge render passes into a single native render pass if the textures you use have very similar properties. If you use several textures with different properties, use the
AddUnsafePass
API and the
SetRenderTarget()
method instead. Rendering might be slower because there will be more render passes.

Examples

Refer to the following: