0
\$\begingroup\$

I'm new to custom effects (ex. pixel shaders) in monogame, but I've researched a bit and made some test effects.

I've noticed that the color variable "Color.[color]" doesn't change the color of the texture when an effect is active.

example:

I can't draw text that uses the effect, because the text doesn't turn black, it stays white.

This is my custom shader file:

sampler2D TextureSampler = sampler_state
{
    Texture = <Texture>;
};

float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0, float4 texCoord : TEXCOORD0) : SV_TARGET0
{
    float4 color = tex2D(TextureSampler, texCoord);

    float high = .6;
    float low = .4;

    if (color.r > high) color.r = 1;
    else if (color.r < low) color.r = 0;

    if (color.g > high) color.g = 1;
    else if (color.g < low) color.g = 0;

    if (color.b > high) color.b = 1;
    else if (color.b < low) color.b = 0;

    return color;
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

And this is an example of the color change that doesn't happen:

spriteBatch.DrawString(font, [text], [position], Color.Black);

When the program starts (with the effect on), the text stays white and doesn't turn black. The effect works on all the other textures, when they don't need a color change.

Is this because of the sampler in the effect file?

EDIT

My question is actually if I can use the "Color.[color]" variable with a custom effect, or do I need to add a color parameter to the effect?

I ask this because I also have a button class that draws buttons with text on it. The draw function for the button draws the text and the button at almost the same time. color change for the text is written in that button class (ex. Color.Black). But when I use the custom shader, it doesn't work.

I'm also not sure if this will work with hlsl parameters for the colors, because I don't know how to seperate the button and the text with a hlsl shader, and only change the color of the text.

\$\endgroup\$
4
  • \$\begingroup\$ Possible duplicate of Mapping "Color" parameter from SpriteBatch.DrawString() function to a custom shader \$\endgroup\$
    – user35344
    Commented Oct 30, 2016 at 11:27
  • \$\begingroup\$ @Tyyppi_77 No, that's another question. That question uses hlsl parameters that pass a color variable to the shader. My question is if I can use the default "Color.[color]" variable with a custom variable. \$\endgroup\$
    – Jelle
    Commented Oct 30, 2016 at 14:14
  • \$\begingroup\$ That's what both the title suggests and what OP asks later on in the question. Yes, the first portion of the question shows a workaround, but what OP really asks is the same question that you are asking. \$\endgroup\$
    – user35344
    Commented Oct 30, 2016 at 14:22
  • \$\begingroup\$ Sorry, forgot to add something to my question. The text that I draw is drawn on a button, at the same time as the button. So I can't use a color parameter for that shader, otherwise the button would also be drawn in that color. I'm going to edit my question a little bit so it's more clear. \$\endgroup\$
    – Jelle
    Commented Oct 30, 2016 at 14:44

1 Answer 1

1
\$\begingroup\$

Something like this should work, I use it for drawing my sprites with sharpDX toolkit.spritebatch so I'm hoping with some changes it may work for you.

HLSL:

Texture2D<float4> Texture : register(t0);

// SpriteBatch expects that default texture sampler parameter will have   name 'TextureSampler'
sampler TextureSampler : register(s0);

// SpriteBatch expects that default vertex transform parameter will have name 'MatrixTransform'
row_major float4x4 MatrixTransform;



void VSMain(
inout float4 color : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
}

struct PixelShaderOutput
{

float4 Color : SV_Target0;

};


// Blurs the input image horizontally
PixelShaderOutput PS(float4 color : COLOR, float2 UV : TEXCOORD0) : SV_Target0
{

PixelShaderOutput output = (PixelShaderOutput) 0;
float4 c = Texture.Sample(TextureSampler, UV);

output.Color.rgb = color.rgb * c.rgb;
output.Color.a = c.a;


return output ;
}



technique Tech
{
pass P0
{

    SetVertexShader(CompileShader(vs_5_0, VSMain()));
    SetPixelShader(CompileShader(ps_5_0, PS()));
}
}

Or something like this could work(made a change to your code)

sampler2D TextureSampler = sampler_state
{
Texture = <Texture>;
};

float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0,    float4 texCoord : TEXCOORD0) : SV_TARGET0
{
float4 color = tex2D(TextureSampler, texCoord);

float high = .6;
float low = .4;

if (color.r > high) color.r = 1;
else if (color.r < low) color.r = 0;

if (color.g > high) color.g = 1;
else if (color.g < low) color.g = 0;

if (color.b > high) color.b = 1;
else if (color.b < low) color.b = 0;

return color1 * color;
}

technique Technique1
{
pass Pass1
{
    PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
}
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .