Version: Unity 6.2 (6000.2)
Language : English
Add a texture to the frame data in URP
Import a texture into a render pass in URP

Create a texture as a global texture in URP

If you need to use a texture as the input for the shaderA program that runs on the GPU. More info
See in Glossary
on a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
, you can set a texture as a global texture. A global texture is available to all shaders and render passes.

Setting a texture as a global texture can make rendering slower. Refer to SetGlobalTexture.

Don’t use an unsafe render pass and CommandBuffer.SetGlobal to set a texture as a global texture, because it might cause errors.

To set a global texture, in the RecordRenderGraph method, use the SetGlobalTextureAfterPass method.

For example:

// Allocate a global shader texture called _GlobalTexture
private int globalTextureID = Shader.PropertyToID("_GlobalTexture")

using (var builder = renderGraph.AddRasterRenderPass<PassData>("MyPass", out var passData)){

    // Set a texture to the global texture
    builder.SetGlobalTextureAfterPass(texture, globalTextureID);
}

If you don’t already call SetRenderFunc, you must also add an empty render function. For example:

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

You can now:

  • Access the texture in a different render pass with the UseGlobalTexture() or UseAllGlobalTextures() API.
  • Use the texture on any material in your sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
    See in Glossary
    by referencing its nameID.

Access a global texture

Access a specific texture set as a global texture in a different render pass with the nameID of the texture (retrieve it with Shader.PropertyToId). Make sure to set the appropriate access flags depending on how the pass uses the global texture.

For example:

    class AccessGlobalTexturePass : ScriptableRenderPass
    {

        // The nameID of the globalTexture you want to use - which you have set in a previous pass
        private int globalTextureID = Shader.PropertyToID("_GlobalTexture")

        class PassData
        {
            // No local pass data needed
        }       

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Fetch texture and draw triangle", out var passData))
            {
                
                // Set the inputs and outputs of your pass
                // builder.SetRenderAttachment(/*...*/);
                // builder.UseTexture(/*...*/);

                // Use the global texture in this pass
                builder.UseGlobalTexture(globalTextureID, AccessFlags.Read);

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

        static void ExecutePass(PassData data, RasterGraphContext context)
        {
            // ...
        }
    }

Note: You can also use all the global textures in your pass with builder.UseAllGlobalTextures(true); instead of a single one. However, this uses more memory.

Add a texture to the frame data in URP
Import a texture into a render pass in URP