# 【叠加算法】
以下示例代码都是UnityShader 里的CG代码
// PS 里的[差值]混合模式
float4 psBlend_chaZhi(float4 A, float4 B)
{
float r = abs(A.r - B.r);
float g = abs(A.g - B.g);
float b = abs(A.b - B.b);
return float4(r,g,b,1);
}
// PS 里的[排除]混合模式
float4 psBlend_paiChu(float4 A, float4 B)
{
float r = A.r+B.r-(A.r*B.r)/0.5;
float g = A.g+B.g-(A.g*B.g)/0.5;
float b = A.b+B.b-(A.b*B.b)/0.5;
return float4(r,g,b,1);
}
// PS 里的[实色混合]混合模式
float4 psBlend_shiSeHunHe(float4 A, float4 B)
{
float r = A.r + B.r >= 1 ? 1 : 0;
float g = A.g + B.g >= 1 ? 1 : 0;
float b = A.b + B.b >= 1 ? 1 : 0;
return float4(r,g,b,1);
}
// PS 里的[线性光]混合模式
float4 psBlend_xianXingGuang(float4 A, float4 B)
{
float r = A.r+2*B.r-1;
float g = A.g+2*B.g-1;
float b = A.b+2*B.b-1;
return float4(r,g,b,1);
}
// PS 里的[点光]混合模式(效果与PS有差异)
float4 psBlend_dianGuang(float4 A, float4 B)
{
float r = B.r <= 0.5 ? min(A.r,2*B.r):min(A.r,2*B.r - 1);
float g = B.g <= 0.5 ? min(A.g,2*B.g):min(A.g,2*B.g - 1);
float b = B.b <= 0.5 ? min(A.b,2*B.b):min(A.b,2*B.b - 1);
return saturate(float4(r,g,b,1));
}
// PS 里的[柔光]混合模式
float4 psBlend_rouGuang(float4 A, float4 B)
{
float r = B.r <= 0.5 ? (A.r*B.r)/0.5 + pow(A.r/1,2) * (1-2*B.r) : (A.r * (1-B.r))/0.5 + sqrt(A.r/1) * (2*B.r - 1);
float g = B.g <= 0.5 ? (A.g*B.g)/0.5 + pow(A.g/1,2) * (1-2*B.g) : (A.g * (1-B.g))/0.5 + sqrt(A.g/1) * (2*B.g - 1);;
float b = B.b <= 0.5 ? (A.b*B.b)/0.5 + pow(A.b/1,2) * (1-2*B.b) : (A.b * (1-B.b))/0.5 + sqrt(A.b/1) * (2*B.b - 1);;
return float4(r,g,b,1);
}
// PS 里的[强光]混合模式
float4 psBlend_qiangGuang(float4 A, float4 B)
{
float r = B.r <= 0.5 ? (A.r * B.r) / 0.5 : 1 - ((1-A.r) * (1-B.r))/0.5;
float g = B.g <= 0.5 ? (A.g * B.g) / 0.5 : 1 - ((1-A.g) * (1-B.g))/0.5;
float b = B.b <= 0.5 ? (A.b * B.b) / 0.5 : 1 - ((1-A.b) * (1-B.b))/0.5;
return float4(r,g,b,1);
}
// PS 里的[叠加]混合模式
float4 psBlend_DieJia(float4 A, float4 B)
{
float r = A.r < 0.5 ? (A.r * B.r) / 0.5 : 1 - ((1-A.r)*(1-B.r))/0.5;
float g = A.g < 0.5 ? (A.g * B.g) / 0.5 : 1 - ((1-A.g)*(1-B.g))/0.5;
float b = A.b < 0.5 ? (A.b * B.b) / 0.5 : 1 - ((1-A.b)*(1-B.b))/0.5;
return float4(r,g,b,1);
}
// PS 里的[线性减淡(添加)]混合模式
float4 psBlend_xianXingJianDan(float4 A, float4 B)
{
float r = A.r + B.r;
float g = A.g + B.g;
float b = A.b + B.b;
return float4(r,g,b,1);
}
// PS 里的[线性加深]混合模式
float4 psBlend_xianXingJiaShen(float4 A, float4 B)
{
float r = A.r + B.r - 1;
float g = A.g + B.g - 1;
float b = A.b + B.b - 1;
return saturate(float4(r,g,b,1));
}
// PS 里的[颜色减淡]混合模式
float4 psBlend_yanSeJianDan(float4 A, float4 B)
{
float r = A.r + (A.r * B.r)/(1-B.r);
float g = A.g + (A.g * B.g)/(1-B.g);
float b = A.b + (A.b * B.b)/(1-B.b);
return float4(r,g,b,1);
}
// PS 里的[颜色加深]混合模式
float4 psBlend_yanSeJiaShen(float4 A, float4 B)
{
float r = A.r - ((1-A.r) * (1-B.r))/B.r;
float g = A.g - ((1-A.g) * (1-B.g))/B.g;
float b = A.b - ((1-A.b) * (1-B.b))/B.b;
return saturate(float4(r,g,b,1));
}
// PS 里的[滤色]混合模式
float4 psBlend_lvSe(float4 A, float4 B)
{
float r = 1 - ((1-A.r)*(1-B.r))/1;
float g = 1 - ((1-A.g)*(1-B.g))/1;
float b = 1 - ((1-A.b)*(1-B.b))/1;
return float4(r,g,b,1);
}
// PS 里的[正片叠底]混合模式
float4 psBlend_zhengPianDieDi(float4 A, float4 B)
{
float r = A.r * B.r;
float g = A.g * B.g;
float b = A.b * B.b;
return float4(r,g,b,1);
}
// PS 里的[变亮]混合模式
float4 psBlend_bianLiang(float4 A, float4 B)
{
float r = max(A.r,B.r);
float g = max(A.g,B.g);
float b = max(A.b,B.b);
return float4(r,g,b,1);
}
// PS 里的[变暗]混合模式
float4 psBlend_bianAn(float4 A, float4 B)
{
float r = min(A.r, B.r);
float g = min(A.g, B.g);
float b = min(A.b, B.b);
return float4(r,g,b,1);
}
// PS 里的[亮光]混合模式
float4 psBlend_liangGuang(float4 A, float4 B)
{
float A_R = A.r;
float B_R = B.r;
float C_R = 0;
if(B_R <= 0.5){
C_R = A_R - ((1-A_R) * (1-2*B_R))/(2*B_R);
}else{
C_R = A_R + (A_R * (2*B_R - 1))/2*(1-B_R);
}
float A_G = A.g;
float B_G = B.g;
float C_G = 0;
if(B_G <= 0.5){
C_G = A_G - ((1-A_G) * (1-2*B_G))/2*B_G;
}else{
C_G = A_G + (A_G * (2*B_G - 1))/(2*(1-B_G));
}
float A_B = A.b;
float B_B = B.b;
float C_B = 0;
if(B_B <= 0.5){
C_B = A_B - ((1-A_B) * (1-2*B_B))/2*B_B;
}else{
C_B = A_B + (A_B * (2*B_B - 1))/(2*(1-B_B));
}
float A_Alpha = A.a;
float B_Alpha = B.a;
float C_Alpha = 0;
if(B_Alpha <= 0.5){
C_Alpha = A_Alpha - ((1-A_Alpha) * (1-2*B_Alpha))/2*B_Alpha;
}else{
C_Alpha = A_Alpha + (A_Alpha * (2*B_Alpha - 1))/(2*(1-B_Alpha));
}
//return float4(C_R,C_G,C_B,C_Alpha);
return saturate(float4(C_R,C_G,C_B,C_Alpha));
}