本文首发于洪流学堂微信公众号。
洪流学堂,学Unity快人几步
本系列已经完结,但后续可能有更新。洪流学堂公众号回复 节点 ,获取ShaderGraph节点详解PDF文件(带目
录)。
注意 节点中很多输入为In(1)的其实是动态长度的Vector,可以接收Vector1~Vector4的值哦
Artistic Nodes
Adjustment 调整
Channel Mixer
根据输入的RGB值及各个通道的权重,输出权重加成后的RGB值。
举例:输入RGB为(0.8,0.6,0.4),假设输出R通道的比重设置为(0.1, 0.2, 0.3),则输出R的值为
0.8x0.1+0.6x0.2+0.4x0.3 = 0.32
_Node_OutRed = float3 (OutRedInRed, OutRedInGreen, OutRedInBlue);
_Node_OutGreen = float3 (OutGreenInRed, OutGreenInGreen, OutGreenInBlue);
_Node_OutBlue = float3 (OutBlueInRed, OutBlueInGreen, OutBlueInBlue);
Out = float3(dot(In, _Node_OutRed), dot(In, _Node_OutGreen), dot(In, _Node_OutBlue));
Contrast
根据输入In及Contrast调节对比度。Contrast为1时输出In,Contrast为0时输出In的中值。
float midpoint = pow(0.5, 2.2);
Out = (In - midpoint) * Contrast + midpoint;
Hue
根据Offset调节色相。 可以根据Degrees调节即(-180,180) 或者根据Normalized调节即(-1,1)
Degree代码(Normalized类似):
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 P = lerp(float4(In.bg, K.wz), float4(In.gb, K.xy), step(In.b, In.g));
float4 Q = lerp(float4(P.xyw, In.r), float4(In.r, P.yzx), step(P.x, In.r));
float D = Q.x - min(Q.w, Q.y);
float E = 1e-10;
float3 hsv = float3(abs(Q.z + (Q.w - Q.y)/(6.0 * D + E)), D / (Q.x + E), Q.x);
float hue = hsv.x + Offset / 360;
hsv.x = (hue < 0)
? hue + 1
: (hue > 1)
? hue - 1
: hue;
float4 K2 = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 P2 = abs(frac(hsv.xxx + K2.xyz) * 6.0 - K2.www);
Out = hsv.z * lerp(K2.xxx, saturate(P2 - K2.xxx), hsv.y);
Invert Colors
反转颜色,可以选择单个或多个反转的通道
Replace Color
替换颜色
In:输入的颜色 From:要替换的颜色 To:替换成的颜色 Range:类似PS里的容差值 Fuzziness:软化选区的边缘
float Distance = distance(From, In);
Out = lerp(To, In, saturate((Distance - Range) / max(Fuzziness, 1e-5f)));
Saturation
饱和度。Saturation为1时输出原颜色,Saturation为0时为完全不饱和色。
float luma = dot(In, float3(0.2126729, 0.7151522, 0.0721750));
Out = luma.xxx + Saturation.xxx * (In - luma.xxx);
White Balance
白平衡
Temperature 让颜色变黄或者变蓝 Tint 让颜色变粉或者变绿
// Range ~[-1.67;1.67] works best
float t1 = Temperature * 10 / 6;
float t2 = Tint * 10 / 6;
// Get the CIE xy chromaticity of the reference white point.
// Note: 0.31271 = x value on the D65 white point
float x = 0.31271 - t1 * (t1 < 0 ? 0.1 : 0.05);
float standardIlluminantY = 2.87 * x - 3 * x * x - 0.27509507;
float y = standardIlluminantY + t2 * 0.05;
// Calculate the coefficients in the LMS space.
float3 w1 = float3(0.949237, 1.03542, 1.08728); // D65 white point
// CIExyToLMS
float Y = 1;
float X = Y * x / y;
float Z = Y * (1 - x - y) / y;
float L = 0.7328 * X + 0.4296 * Y - 0.1624 * Z;
float M = -0.7036 * X + 1.6975 * Y + 0.0061 * Z;
float S = 0.0030 * X + 0.0136 * Y + 0.9834 * Z;
float3 w2 = float3(L, M, S);
float3 balance = float3(w1.x / w2.x, w1.y / w2.y, w1.z / w2.z);
float3x3 LIN_2_LMS_MAT = {
3.90405e-1, 5.49941e-1, 8.92632e-3,
7.08416e-2, 9.63172e-1, 1.35775e-3,
2.31082e-2, 1.28021e-1, 9.36245e-1
};