文档

Unity Engine


User Manual

Script Reference

Unity Engine


Create a texture as a global texture in URP

Make a texture available to all shaders and render passes.
阅读时间2 分钟最后更新于 25 天前

If you need to use a texture as the input for the shader on a GameObject, 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 _GlobalTextureprivate 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 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) { // ... } }
注意
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.