Version: 6000.3
语言: 中文
在URP中通过脚本注入渲染通道
URP 的注入点参考

在 URP 中将渲染通道限制为场景区域

要将渲染通道限制为场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,您可以放置环境、障碍物和装饰,实质上是分批设计和构建游戏。更多信息
请参阅术语表
,向场景添加体积,然后将代码添加到渲染通道和着色器在 GPU 上运行的程序。更多信息
请参阅术语表
检查相机在场景中创建特定视点图像的组件。输出要么绘制到屏幕上,要么作为纹理捕获。更多信息
请参阅术语表
在卷内。

按着这些次序:

  1. 更新着色器代码,以根据布尔值启用或禁用自定义渲染效果。

    例如,将以下代码添加到着色器:

    Pass
    {
        ...
    
        // Add a variable to enable or disable your custom rendering effect
        float _Enabled;
    
        ...
    
        float4 Frag(Varyings input) : SV_Target0
        {
            ...
    
            // Return the color with the effect if the variable is 1, or the original color if the variable is 0
            if (_Enabled == 1){
                return colorWithEffect;
            } else {
                return originalColor;
            }
        }
    }
    
  2. 创建一个实现VolumeComponent类。 这将创建一个可以添加到卷的卷覆盖组件。

    using UnityEngine;
    using UnityEngine.Rendering;
    
    public class MyVolumeOverride : VolumeComponent
    {
    }
    
  3. 在“层次结构”窗口中,选择“添加”(+) 按钮,然后选择“游戏对象”>“体积”>“盒体”

  4. 检查器一个Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
    请参阅术语表
    窗口,在 Volume 下,选择 New 以创建新的卷配置文件。

  5. 选择 Add override,然后选择您的卷覆盖组件,例如 My Volume Override

  6. 将属性添加到卷覆盖脚本。Unity 在卷覆盖的 Inspector 窗口中添加该属性。

    例如:

    public class MyVolumeOverride : VolumeComponent
    {
        // Add an 'Effect Enabled' checkbox to the Volume Override, with a default value of true.
        public BoolParameter effectEnabled = new BoolParameter(true);
    }
    
  7. 在自定义凭证中,使用GetComponentAPI 获取卷覆盖组件并检查属性的值。

    例如:

    class myCustomPass : ScriptableRenderPass
    {
    
        ...
    
        public void Setup(Material material)
        {
            // Get the volume override component
            MyVolumeOverride myOverride = VolumeManager.instance.stack.GetComponent<MyVolumeOverride>();
    
            // Get the value of the 'Effect Enabled' property
            bool effectStatus = myOverride.effectEnabled.overrideState ? myOverride.effectEnabled.value : false;
        }
    }
    
  8. 将属性的值传递给添加到着色器代码的变量。

    例如:

    class myCustomPass : ScriptableRenderPass
    {
    
        ...
    
        public void Setup(Material material)
        {
            MyVolumeOverride myOverride = VolumeManager.instance.stack.GetComponent<MyVolumeOverride>();
            bool effectStatus = myOverride.effectEnabled.overrideState ? myOverride.effectEnabled.value : false;
    
            // Pass the value to the shader
            material.SetFloat("_Enabled", effectStatus ? 1 : 0);
        }
    }
    

现在,当摄像机位于体积内时,自定义渲染效果将启用,当摄像机位于体积外时,将禁用。

其他资源

在URP中通过脚本注入渲染通道
URP 的注入点参考