找回密码
 立即注册
首页 业界区 业界 【URP】Unity[后处理]通道混合ChannelMixer

【URP】Unity[后处理]通道混合ChannelMixer

材部 昨天 20:00
【从UnityURP开始探索游戏渲染】专栏-直达
ChannelMixer是Unity URP后处理系统中用于颜色通道混合的核心效果组件,主要用于调整RGB通道的混合比例以实现特定的色彩分级效果。其发展历史可追溯至影视行业的传统调色技术,后被整合到Unity的Post Processing Stack中,并随着URP的演进成为Volume框架下的标准化模块
核心功能与参数

通道混合原理

通过修改输入颜色通道(Red/Green/Blue)对输出通道的贡献权重,实现如单色保留、色调偏移等效果。例如增加绿色通道对红色输出的影响会使绿色区域偏红。
ChannelMixer在Unity URP后处理中的底层原理基于颜色通道的线性代数变换,通过对RGB通道的权重矩阵运算实现色彩重构.
输出通道 = (R × R权重) + (G × G权重) + (B × B权重) + 常数偏移
该计算在片元着色器中逐像素执行,通过矩阵乘法实现颜色空间转换.
数学原理分解


  • 通道变换矩阵

    • 每个输出通道由3×4变换矩阵控制(包含常数项),例如红色输出通道计算为:
    • $R_{out}=R_{in}\times M_{00}+G_{in}\times M_{01}+B_{in}\times M_{02}+M_{03}$
    • 其中M_03为常数偏移量。

  • 亮度保持机制

    • 当启用Preserve Luminosity时,系统会自动归一化权重系数,确保满足:
    • $M_{00}+M_{01}+M_{02}=1.0$
    • 防止画面过曝或欠曝

URP实现示例 关键实现解析


  • 矩阵运算阶段
    片元着色器通过dot(col.rgb, _RedOut.xyz)实现向量点乘,等效于矩阵乘法。例如设置_RedOut=(0.5,0.3,0.2)时,新红色通道值为原RGB通道的50%+30%+20%混合。
  • 动态参数传递
    URP通过Volume组件将参数打包为Vector4(xyz为RGB权重,w为常数偏移),每帧更新至Shader。例如电影级调色常用配置:
    1. csharp
    2. _RedOut = new Vector4(0.8f, 0.1f, 0.1f, 0);// 强化红色主基调
    3. _GreenOut = new Vector4(0f, 1.2f, -0.2f, 0);// 增强绿色并抑制蓝色
    复制代码
  • 后处理管线集成
    RendererFeature在BeforeRenderingPostProcessing事件点插入通道混合操作,通过双次Blit避免直接修改源纹理。临时渲染目标_TempChannelMixerTexture确保混合过程可逆.
  • ChannelMixer.shader
    1. Shader "PostProcessing/ChannelMixer" {
    2. Properties {
    3. _MainTex ("Texture", 2D) = "white" {}
    4. _RedOut ("Red Output", Vector) = (1,0,0,0)
    5. _GreenOut ("Green Output", Vector) = (0,1,0,0)
    6. _BlueOut ("Blue Output", Vector) = (0,0,1,0)
    7. }
    8. SubShader {
    9. HLSLPROGRAM
    10. #pragma only_renderers d3d11
    11. #pragma exclude_renderers gles
    12. #pragma target 5.0
    13. #pragma multi_compile_postprocess
    14.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    15.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/PostProcessing.hlsl"
    16.     TEXTURE2D(_MainTex);
    17.     SAMPLER(sampler_MainTex);
    18.     float4 _RedOut;
    19.     float4 _GreenOut;
    20.     float4 _BlueOut;
    21.     struct v2f {
    22.         float2 uv : TEXCOORD0;
    23.         float4 vertex : SV_POSITION;
    24.     };
    25.     v2f vert(appdata v) {
    26.         v2f o;
    27.         o.vertex = TransformWorldToClipPos(v.vertex);
    28.         o.uv = v.uv;
    29.         return o;
    30.     }
    31.     float4 frag(v2f i) : SV_Target {
    32.         float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    33.         float3 result;
    34.         result.r = dot(col.rgb, _RedOut.xyz) + _RedOut.w;
    35.         result.g = dot(col.rgb, _GreenOut.xyz) + _GreenOut.w;
    36.         result.b = dot(col.rgb, _BlueOut.xyz) + _BlueOut.w;
    37.         return float4(result, col.a);
    38.     }
    39.     ENDHLSL
    40. }
    41. }
    复制代码
  • ChannelMixerRendererFeature.cs
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4. public class ChannelMixerRendererFeature : ScriptableRendererFeature {
    5.     class CustomRenderPass : ScriptableRenderPass {
    6.         Material _material;
    7.         RenderTargetHandle _tempTexture;
    8.         public CustomRenderPass(Material material) {
    9.             _material = material;
    10.             _tempTexture.Init("_TempChannelMixerTexture");
    11.         }
    12.         public override void Execute(ScriptableRenderContext context, ref RenderingData data) {
    13.             CommandBuffer cmd = CommandBufferPool.Get("Channel Mixer");
    14.             RenderTextureDescriptor desc = data.cameraData.cameraTargetDescriptor;
    15.             cmd.GetTemporaryRT(_tempTexture.id, desc);
    16.             Blit(cmd, source: "_CameraColorTexture", dest: _tempTexture.id, _material);
    17.             Blit(cmd, source: _tempTexture.id, dest: "_CameraColorTexture");
    18.             cmd.ReleaseTemporaryRT(_tempTexture.id);
    19.             context.ExecuteCommandBuffer(cmd);
    20.             CommandBufferPool.Release(cmd);
    21.         }
    22.     }
    23.     [System.Serializable]
    24.     public class Settings {
    25.         public Material material;
    26.     }
    27.     public Settings settings = new Settings();
    28.     CustomRenderPass _scriptablePass;
    29.     public override void Create() {
    30.         _scriptablePass = new CustomRenderPass(settings.material);
    31.         _scriptablePass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
    32.     }
    33.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data) {
    34.         renderer.EnqueuePass(_scriptablePass);
    35.     }
    36. }
    复制代码
参数说明


  • Output Channel‌:选择要调整的目标通道(Red/Green/Blue)
  • Red/Green/Blue Contribution‌:各输入通道对当前输出通道的影响权重(范围-2到2)
  • Preserve Luminosity‌:保持整体亮度不变,避免过度曝光。
实现流程示例

ChannelMixerExample.cs
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. public class ChannelMixerExample : VolumeComponent, IPostProcessComponent {
  5.     public Vector3Parameter redOut = new(new Vector3(1, 0, 0));
  6.     public Vector3Parameter greenOut = new(new Vector3(0, 1, 0));
  7.     public Vector3Parameter blueOut = new(new Vector3(0, 0, 1));
  8.     public bool IsActive() => true;
  9.     public bool IsTileCompatible() => false;
  10. }
复制代码
ChannelMixerRenderPass.cs
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. public class ChannelMixerRenderPass : ScriptableRenderPass {
  5.     private Material _material;
  6.     private ChannelMixerExample _volume;
  7.     public override void Execute(ScriptableRenderContext context, ref RenderingData data) {
  8.         _volume = VolumeManager.instance.stack.GetComponent<ChannelMixerExample>();
  9.         if (_volume == null) return;
  10.         var cmd = CommandBufferPool.Get("ChannelMixer");
  11.         _material.SetVector("_RedOut", _volume.redOut.value);
  12.         _material.SetVector("_GreenOut", _volume.greenOut.value);
  13.         _material.SetVector("_BlueOut", _volume.blueOut.value);
  14.         Blit(cmd, ref data, _material, 0);
  15.         context.ExecuteCommandBuffer(cmd);
  16.     }
  17. }
复制代码
实际应用案例

电影感调色

将蓝色通道注入红色输出(如设置RedOut为(0.8, 0, 0.2)),可创造类似《黑客帝国》的青色阴影效果。
风格化渲染

通过反转绿色通道贡献(GreenOut设为(-0.5, 1, 0)),实现赛博朋克风格的色彩偏移。
黑白滤镜

统一各通道权重(如RedOut=(0.3,0.6,0.1))可生成高质量灰度图像,比直接去饱和度更可控。
胶片模拟


  • 设置BlueOut=(0,0,0.9,0.1)使蓝色通道轻微压缩,模拟柯达胶片特性
色盲辅助


  • 将红色通道注入绿色输出(GreenOut=(0.5,0.5,0,0))提升红绿色盲辨识度
风格化渲染


  • 负值权重创造互补色效果,如RedOut=(1, -0.2, 0, 0)产生赛博朋克色调
操作步骤


  • 在Volume中添加Channel Mixer效果
  • 调整目标通道的RGB权重三角滑块
  • 启用Preserve Luminosity避免过曝
该技术现已成为URP标准管线的一部分,通过Volume系统实现非破坏性调整,比传统Shader方案更易集成到美术工作流中
<blockquote>
【从UnityURP开始探索游戏渲染】专栏-直达
(欢迎点赞留言探讨,更多人加入进来能更加完善这个探索的过程,
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册