关于Unity中Bloom的记录
关于Unity中Bloom的记录
1. 提取亮部
其中, 提取亮部的部分, 只看代码完全不知道做了什么.
1
2
3
4
5
half brightness = Max3(color.r, color.g, color.b);
half softness = clamp(brightness - Threshold + ThresholdKnee, 0.0, 2.0 * ThresholdKnee);
softness = (softness * softness) / (4.0 * ThresholdKnee + 1e-4);
half multiplier = max(brightness - Threshold, softness) / max(brightness, 1e-4);
color *= multiplier;
通过Desmos的绘图功能做出的图表可以直观的看到Unity做了什么. 其中x轴为亮度brightness. t为亮度阈值Threshold), j为ThresholdKnee, p为softness(第一个),
通常我们取Bloom亮部的做法是如橙色线部分, 亮度减去亮度阈值后低于0的部分取0, 高于0的部分取亮度减去亮度阈值.
如果我们需要平滑这个过渡, 那么就要想办法做出紫色线部分, 然后用max(紫色线, 橙色线), 来得到最终曲线.
目前的关键在于紫色线的函数如何得到.
Step1
首先, 对于一个折线内做一个曲线并相交, 函数应该形如
\[y= \frac { {x}^{n}}{a}\]我们将 $x-t$ 看作一个整体(或者你可以理解为将 $t=0$ ), 将 $n$ 暂定为2, 并通过反推, 实际上可以简化为新方程组求解:
\[\begin{cases} y=x \\ y= \frac { {(x+j)}^{2}} {aj} \\ \end{cases}\]如果这个方程组有唯一解, 根据一元二次方程的通解定义,
//TODO 补完公式
可以算出
当 $a=4$ 或者 $a=0$ 时可以有唯一解, 当然由于 $a$ 在分母, 则 $a \ne 0$ , 那么 $a=4$ , 也是代码中(4.0 * ThresholdKnee + 1e-4)中的4.0的由来.
此时, $x=j$ , 则交点 $p$ 的值为$2 \cdot j$ 即2.0 * ThresholdKnee.
当然, Unity在E:\Projects\LeiCanYRenderCore\Library\PackageCache\com.unity.render-pipelines.universal@14.0.8\Runtime\Passes\PostProcessPass.cs中, 直接强制定义了float thresholdKnee = threshold * 0.5f;, 我们也可以借此做简化工作.
2. 高斯模糊
解释一下采样部分的”神奇数字”的来源.
参考网页:
