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.
To create a texture, in the RecordRenderGraph
method of your ScriptableRenderPass
class, follow these steps:
RenderTextureDescriptor
object with the texture properties you need.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.
When creating a texture from another texture’s descriptor, prefer RenderGraph.CreateTexture over CreateRenderGraphTexture. This ensures that you avoid subtle bugs that can arise if you don’t exactly know what the RenderTextureDescriptor describes. For example, avoid using the following pattern:
var desc = cameraData.cameraTargetDescriptor; //this is a RenderTextureDescriptor
desc.graphicsFormat = GraphicsFormat.None;
var textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc)
Instead, use the following more robust pattern:
var textureDesc = resourceData.cameraDepthTexture.GetDescriptor(renderGraph);
var textureHandle = RenderGraph.CreateTexture(textureDesc);
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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.