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 to set a texture as a global texture, because it might cause errors.
CommandBuffer.SetGlobalTo set a global texture, in the method, use the method.
RecordRenderGraphSetGlobalTextureAfterPassFor 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 , you must also add an empty render function. For example:
SetRenderFuncbuilder.SetRenderFunc(static (PassData data, RasterGraphContext context) => { });
You can now:
- Access the texture in a different render pass with the or
UseGlobalTexture()API.UseAllGlobalTextures() - Use the texture on any material in your scene by referencing its .
nameID
Access a global texture
Access a specific texture set as a global texture in a different render pass with the of the texture (retrieve it with ). Make sure to set the appropriate access flags depending on how the pass uses the global texture.
nameIDShader.PropertyToIdFor 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) { // ... } }