天天看點

設定可用的渲染管線資源

設定可用的渲染管線資源

在SRP中,你的工程需要有一個激活的渲染管線資源(Render Pipeline Asset).這一章的内容有在Editor以及運作時的情況下設定渲染管線資源的相關資訊

相容性和性能

因為SRP是高度可配置的,是以改變激活的渲染管線資源隻會導緻很小的變化,比如用同樣的渲染管線執行個體對應不同的quality等級,或者來一個大的改變,比如從通用渲染管線(URP)切換到高清晰渲染管線(HDRP).

如果你從一個激活的渲染管線資源換到另外一個使用了不同渲染管線執行個體的管線資源,你必須要保證你項目中的資源和代碼要和新的渲染管線執行個體相相容,否者你可能會遇到錯誤或者是一些無法預料的視覺效果

注意,轉換新的渲染管線資源會導緻unity銷毀目前的渲染管線執行個體,并且會去調用新的渲染管線資源中的CreatePipeline()方法.從SRP中的代碼來看,在計算上這将是一個資源密集型的操作

在UnityEidor中設定渲染管線資源

當你在編輯你的工程時,如果想要使用SRP,你需要在你的UnityEidor中設定渲染管線資源.你在UnityEidor中設定的渲染管線資源是unity預設在build player中使用的渲染管線資源.

在Graphics Settings 視窗,為你的工程設定渲染管線資源

1.通過導航到Edit > Project Settings > Graphics來打開 Graphics Settings視窗

2.在你的Project檔案夾下,找到你想要使用的渲染管線資源

3.拖拽渲染管線資源到Scriptable Render Pipeline Setting這一欄.然後,Unity會立馬使用你在渲染管線資源中定義的配置開始渲染.這包括了Game視圖,scene視圖,以及在Project面闆和Inspector面闆中顯示的材質的預覽視窗

在運作時設定渲染管線資源

如果你想要在多個渲染管線資源之間切換,你可以在runtime時設定渲染管線資源.你可以在build player中進行設定也可以在Editor下的Play模式下完成這一切

在runtime時,使用 GraphicsSettings.renderPipelineAsset API對渲染管線資源進行設定

以下的代碼展示了如何在多個渲染管線資源之間切換.

using UnityEngine;
using UnityEngine.Rendering;
 
public class SwitchRenderPipelineAsset : MonoBehaviour
{
    public RenderPipelineAsset exampleAssetA;
    public RenderPipelineAsset exampleAssetB;
 
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A)) {
            GraphicsSettings.renderPipelineAsset = exampleAssetA;
            Debug.Log("Active render pipeline asset is: " + GraphicsSettings.renderPipelineAsset.name);
        }
        else if (Input.GetKeyDown(KeyCode.B)) {
            GraphicsSettings.renderPipelineAsset = exampleAssetB;
            Debug.Log("Active render pipeline asset is: " + GraphicsSettings.renderPipelineAsset.name);
        }
    }