提问人:distino 提问时间:11/11/2023 更新时间:11/11/2023 访问量:7
替换 HLSL 着色器中的特定颜色
Replace specific color in HLSL shader
问:
我正在尝试编辑现有的着色器,使其仅适用于 1 种颜色,然后制作它,以便这 1 种颜色可以替换为不同的颜色。
这是原始代码
sampler s0 : register(s0);
float4 p0 : register(c0);
float4 p1 : register(c1);
#define width (p0[0])
#define height (p0[1])
#define counter (p0[2])
#define clock (p0[3])
#define one_over_width (p1[0])
#define one_over_height (p1[1])
#define PI acos(-1)
#define cutoffR 0.40
#define cutoffG 0.025
#define cutoffB 0.25
#define cutoffY 0.25
#define acceptanceAmplification 5
#define showR 1
#define showG 1
#define showB 1
#define showY 1
float4 main(float2 tex : TEXCOORD0) : COLOR
{
float4 color = tex2D(s0, tex);
float luminance = (color.r + color.g + color.b)/3;
float4 gray = {luminance,luminance,luminance, 1};
float redness = max ( min ( color.r - color.g , color.r - color.b ) / color.r , 0);
float blueness = max ( min ( color.b - color.r , color.b - color.g ) / color.b , 0);
float greenness = max ( min ( color.g - redness, color.g - blueness), 0);
float rgLuminance = (color.r*1.4 + color.g*0.6)/2;
float rgDiff = abs(color.r-color.g)*1.4;
float yellowness = 0.1+rgLuminance*1.2 - color.b - rgDiff;
float4 accept;
accept.r = showR * (redness - cutoffR);
accept.g = showG * (greenness - cutoffG);
accept.b = showB * (blueness - cutoffB);
accept[3] = showY * (yellowness - cutoffY);
float acceptance = max (accept.r, max(accept.g, max(accept.b, max(accept[3],0))));
float modAcceptance = min (acceptance * acceptanceAmplification, 1);
float4 result;
result = modAcceptance * gray + (1.0-modAcceptance) * color;
return result;
}
来源 : github
我改变了
#define cutoffR 0.40
#define cutoffG 0.025
#define cutoffB 0.25
#define cutoffY 0.25
自
#define cutoffR 1
#define cutoffG 1
#define clip(B) 0.25
#define cutoffY 1
因此,它只影响蓝色,因为我试图将蓝色变成紫色。现在它正在变成蓝色,我不知道如何进一步进行。
我稍微更改了代码
sampler s0 : register(s0);
float4 p0 : register(c0);
float4 p1 : register(c1);
#define width (p0[0])
#define height (p0[1])
#define counter (p0[2])
#define clock (p0[3])
#define one_over_width (p1[0])
#define one_over_height (p1[1])
#define PI acos(-1)
#define cutoffR 1
#define cutoffG 1
#define clip(B) 0.25
#define cutoffY 1
#define acceptanceAmplification 5
#define showR 1
#define showG 1
#define showB 1
#define showY 1
float4 main(float2 tex : TEXCOORD0) : COLOR
{
float4 color = tex2D(s0, tex);
float luminance = (color.r + color.g + color.b)/3;
float4 color2 = (0);
float redness = max ( min ( color.r - color.g , color.r - color.b ) / color.r , 0);
float blueness = max ( min ( color.b - color.r , color.b - color.g ) / color.b , 0);
float greenness = max ( min ( color.g - redness, color.g - blueness), 0);
float4 accept;
accept.r = showR * (redness - cutoffR);
accept.g = showG * (greenness - cutoffG);
accept.b = showB * (blueness - clip(B));
float acceptance = max (accept.r, max(accept.g, max(accept.b,(0))));
float modAcceptance = min (acceptance * acceptanceAmplification, 1);
float4 result;
result = modAcceptance * color2 + (1.0-modAcceptance) * color;
return result;
}
交换
float rgDiff = abs(color.r-color.g)*1.4;
自
float4 color2 = (0);
和
result = modAcceptance * gray + (1.0-modAcceptance) * color;
自
result = modAcceptance * color2 + (1.0-modAcceptance) * color;
我现在可以在 color2 = () 中输入一个介于 0-1 之间的值,该值将颜色从黑色更改为白色,但仅此而已。我不知道如何将其替换为特定颜色。
答: 暂无答案
评论