Version: 6000.3
语言: 中文
URP 中的兼容模式
URP 中兼容模式下的可脚本渲染器功能示例

在 URP 中以兼容模式编写可编写脚本的渲染通道

注意: Unity 不再开发或改进不使用渲染图 API 的渲染路径。在开发新的图形功能时,请改用渲染图 API。要使用此页面上的说明,请在 URP 图形设置(项目设置>图形)中启用兼容模式(渲染图禁用)。

在通用渲染管线获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管线中进行选择,或编写自己的渲染管线。更多信息
请参阅术语表
(URP),请按照下列步骤作:

  1. 创建一个继承ScriptableRenderPass类。 例如:

    using UnityEngine.Rendering;
    using UnityEngine.Rendering.Universal;
    
    public class ExampleRenderPass : ScriptableRenderPass
    {
    }
    
  2. 在类中,为渲染通道中使用的材质和纹理添加变量。

    例如,以下代码设置纹理的句柄,以及用于配置纹理的描述符。

    private RTHandle textureHandle;
    private RenderTextureDescriptor textureDescriptor;
    
  3. 覆盖Configure方法来设置渲染通道。Unity 在执行渲染通道之前调用此方法。

    例如:

    public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    {
        //Set the texture size to be the same as the camera target size.
        textureDescriptor.width = cameraTextureDescriptor.width;
        textureDescriptor.height = cameraTextureDescriptor.height;
    
        //Check if the descriptor has changed, and reallocate the texture handle if necessary.
        RenderingUtils.ReAllocateIfNeeded(ref textureHandle, textureDescriptor);
    }
    
  4. 覆盖Execute方法替换为渲染命令。Unity 每帧调用此方法一次相机在场景中创建特定视点图像的组件。输出要么绘制到屏幕上,要么作为纹理捕获。更多信息
    请参阅术语表
    .

    例如:

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        // Get a CommandBuffer from pool
        CommandBuffer cmd = CommandBufferPool.Get();
    
        // Add rendering commands to the CommandBuffer
        ...
    
        // Execute the command buffer and release it back to the pool
        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }
    

将渲染通道注入渲染循环

要在兼容模式下将渲染通道注入渲染循环,请参阅以下内容:

有关完整示例,请参阅兼容模式下的可脚本渲染器功能示例

其他资源

URP 中的兼容模式
URP 中兼容模式下的可脚本渲染器功能示例