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:
UseGlobalTexture() or UseAllGlobalTextures() API.nameID.
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.