Version: Unity 6 (6000.0)
Language : English
Textures in the Render Graph system in URP
Import a texture into the render graph system in URP

Create a texture in the render graph system in URP

To create a texture in the render graph system, use the UniversalRenderer.CreateRenderGraphTexture API.

When the Universal Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
(URP) optimizes the render graph, it might not create a texture if the final frame doesn’t use the texture, to reduce the memory and bandwidth the render passes use. For more information about how URP optimizes render graphs, refer to Introduction to the render graph system.

For more information about using a texture in multiple frames or on multiple camerasA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
, for example a texture asset you imported in your project, refer to Import a texture into the render graph system.

Create a texture

To create a texture, in the RecordRenderGraph method of your ScriptableRenderPass class, follow these steps:

  1. Create a RenderTextureDescriptor object with the texture properties you need.
  2. Use the UniversalRenderer.CreateRenderGraphTexture method to create a texture and return a texture handle.

For example, the following creates a texture the same size as the screen.

RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);

You can then use the texture in the same custom render pass.

Only the current camera can access the texture. To access the texture somewhere else, for example from another camera or in custom rendering code, import a texture instead.

The render graph system manages the lifetime of textures you create with CreateRenderGraphTexture, so you don’t need to manually release the memory they use when you’re finished with them.

Example

The following Scriptable Renderer Feature contains an example render pass that creates a texture and clears it to yellow. For more information about adding the render pass to the render pipeline, refer to Inject a pass using a Scriptable Renderer Feature.

Use the Frame Debugger to check the texture the render pass adds.

using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;

public class CreateYellowTextureFeature : ScriptableRendererFeature
{
    CreateYellowTexture customPass;

    public override void Create()
    {
        customPass = new CreateYellowTexture();
        customPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(customPass);
    }

    class CreateYellowTexture : ScriptableRenderPass
    {
        class PassData
        {
            internal TextureHandle cameraColorTexture;
        }

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Create yellow texture", out var passData))
            {
                // Create texture properties that match the screen size
                RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);

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

                // Set the texture as the render target
                builder.SetRenderAttachment(texture, 0, AccessFlags.Write);
    
                builder.AllowPassCulling(false);

                builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
            }
        }

        static void ExecutePass(PassData data, RasterGraphContext context)
        {          
            // Clear the render target to yellow
            context.cmd.ClearRenderTarget(true, true, Color.yellow);            
        }
    }

}

For another example, refer to the example called OutputTexture in the Universal Render Pipeline (URP) package samples.

Additional resources

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