Version: Unity 6 (6000.0)
Language : English
Import a texture into the render graph system in URP
Transfer a texture between render passes in URP

Use a texture in a render pass in URP

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.

You can’t both read from and write to the same texture in a render pass. Refer to Change the render target during a render pass for more information.

Set a texture as an input

To set a texture as an input for a custom render pass, follow these steps:

  1. In the RecordRenderGraph method, add a texture handle field to the data your pass uses.

    For example:

    // Create the data your pass uses
    public class MyPassData
    {
        // Add a texture handle
        public TextureHandle textureToUse;
    }
    
  2. Set the texture handle to the texture you want to use.

    For example:

    // Add the texture handle to the data
    RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
    TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
    passData.textureToUse = textureHandle;
    
  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 such as Blitter.BlitTexture.

Set a texture as the render target

To set a texture as the output for commands such as Blit, in the RecordRenderGraph method, use the SetRenderAttachment method. The SetRenderAttachment method sets the texture as write-only by default.

For example, the following command creates a temporary texture and sets it as the render target for the render pass:

// Create texture properties
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);

// Create the texture
TextureHandle targetTexture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);

// Set the texture as the render target
// The second parameter is the index the shader uses to access the texture
builder.SetRenderAttachment(targetTexture, 0);

In your SetRenderFunc method, you can now write to the texture using APIs such as Blitter.BlitTexture.

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.

If you need to draw objects to the render target, refer to Draw objects in a render pass for additional information.

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. Refer to Use Compatibility Mode APIs in render graph render passes for more information and an example.

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.

If you blitA shorthand term for “bit block transfer”. A blit operation is the process of transferring blocks of data from one place in memory to another.
See in Glossary
between several textures with different properties, rendering might be slow because URP can’t merge the blits into a single native render pass. Use the AddUnSafePass API and the SetRenderTarget() method instead.

Examples

Refer to the following:

Import a texture into the render graph system in URP
Transfer a texture between render passes in URP
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961