Version: 6000.3
语言: 中文
将摄像机的输出渲染到URP中的渲染纹理
URP 中的摄像机渲染顺序

渲染到 URP 渲染循环之外的渲染纹理

要触发相机在场景中创建特定视点图像的组件。输出要么绘制到屏幕上,要么作为纹理捕获。更多信息
请参阅术语表
渲染到渲染纹理(render texture一种特殊类型的纹理,在运行时创建和更新。要使用它们,请先创建一个新的渲染纹理,并指定要渲染到其中的摄像机之一。然后,你可以在材质中使用渲染纹理,就像使用常规纹理一样。更多信息
请参阅术语表
环球之外渲染管线(Render Pipeline) 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表
(URP) 渲染循环,使用SingleCameraRequestSubmitRenderRequestC# 脚本中的 API。

按着这些次序:

  1. 创建UniversalRenderPipeline.SingleCameraRequest类型。例如:

    UniversalRenderPipeline.SingleCameraRequest request = new UniversalRenderPipeline.SingleCameraRequest();
    
  2. 检查相机是否支持渲染请求类型,使用RenderPipeline.SupportsRenderRequest应用程序接口。例如,要检查主摄像头:

    Camera mainCamera = Camera.main;
    
    if (RenderPipeline.SupportsRenderRequest(mainCamera, request))
    {
        ...
    }
    
  3. 将相机的目标设置为RenderTexture对象,使用destination参数。例如:

    request.destination = myRenderTexture;
    
  4. 使用 SubmitRenderRequest API 渲染到渲染纹理。例如:

    RenderPipeline.SubmitRenderRequest(mainCamera, request);
    

要确保所有摄像机在渲染到渲染纹理之前完成渲染,请使用以下任一方法:

以下示例将多个摄像机渲染到多个渲染纹理。要使用该示例,请执行以下步骤:

  1. 在 Unity 项目中,将代码添加到名为SingleCameraRenderRequest.cs.
  2. 将脚本添加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    在你的场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,您可以放置环境、障碍物和装饰,实质上是分批设计和构建游戏。更多信息
    请参阅术语表
    .
  3. 检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
    请参阅术语表
    窗口中,分配摄像机并渲染纹理。确保摄像机的数量与渲染纹理的数量相同。
  4. 进入播放模式。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class SingleCameraRenderRequest : MonoBehaviour
{
    public Camera[] cameras;
    public RenderTexture[] renderTextures;

    void Start()
    {
        // Make sure all data is valid before you start the component
        if (cameras == null || cameras.Length == 0 || renderTextures == null || cameras.Length != renderTextures.Length)
        {
            Debug.LogError("Invalid setup");
            return;
        }

        // Start the asynchronous coroutine
        StartCoroutine(RenderSingleRequestNextFrame());
        
        // Call a method called OnEndContextRendering when a camera finishes rendering
        RenderPipelineManager.endContextRendering += OnEndContextRendering;
    }

    void OnEndContextRendering(ScriptableRenderContext context, List<Camera> cameras)
    {
        // Create a log to show cameras have finished rendering
        Debug.Log("All cameras have finished rendering.");
    }

    void OnDestroy()
    {
        // End the subscription to the callback
        RenderPipelineManager.endContextRendering -= OnEndContextRendering;
    }

    IEnumerator RenderSingleRequestNextFrame()
    {
        // Wait for the main camera to finish rendering
        yield return new WaitForEndOfFrame();

        // Enqueue one render request for each camera
        SendSingleRenderRequests();

        // Wait for the end of the frame
        yield return new WaitForEndOfFrame();

        // Restart the coroutine
        StartCoroutine(RenderSingleRequestNextFrame());
    }

    void SendSingleRenderRequests()
    {
        //Iterates over the cameras array.        
        for (int i = 0; i < cameras.Length; i++)
        {
            UniversalRenderPipeline.SingleCameraRequest request =
                new UniversalRenderPipeline.SingleCameraRequest();

            // Check if the active render pipeline supports the render request
            if (RenderPipeline.SupportsRenderRequest(cameras[i], request))
            {
                // Set the destination of the camera output to the matching RenderTexture
                request.destination = renderTextures[i];
                
                // Render the camera output to the RenderTexture synchronously
                RenderPipeline.SubmitRenderRequest(cameras[i], request);

                // At this point, the RenderTexture in renderTextures[i] contains the scene rendered from the point
                // of view of the Camera in cameras[i]
            }
        }
    }
}

其他资源

将摄像机的输出渲染到URP中的渲染纹理
URP 中的摄像机渲染顺序