Version: Unity 6.0 (6000.0)
言語 : 日本語
Add a texture to the frame data in URP
URP でレンダーグラフシステムにテクスチャをインポートする

Create a texture as a global texture in URP

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 _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 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
URP でレンダーグラフシステムにテクスチャをインポートする
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961