Filling color with texture ; effect or SetData ?

For this small project I’m doing on android, I need to replace every pixel in a texture in a specific color (255, 192, 62 in my case) to its equivalent in position in another texture. I basically want to texture a unified color.

First thing I researched for this was using effects. I never used them before so I wrote this small effect thanks to some tutorials and a lot of courage (discovered HLSL with this):

Texture2D SpriteTexture;
sampler2D SpriteTextureSampler = sampler_state
{
    Texture = <SpriteTexture>;
};
Texture2D jam_texture; // Texture I want to replace/fill my color with
sampler2D jam_sampler = sampler_state
{
Texture = <jam_texture>;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
};

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR
{
float4 pixel_color = tex2D(SpriteTextureSampler, input.TextureCoordinates);
if (pixel_color.b * 255 != float(62))
    return pixel_color;
else
{
    float4 jam_color = tex2D(jam_sampler, input.TextureCoordinates);
    return jam_color;
}
}
technique SpriteDrawing
{
    pass
    {
        PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
    }
};`

This works for a regular texture2D, but since I want to replace the color in animated sprites, the position of the texture was relative to the spritesheet and not to the current frame of the animation : jam_texture was at a different position for each frame in the spritesheet. I tried adding an offset to TextureCoordinates but couldn’t get it to work, because I don’t actually understand what input.TextureCoordinates refers to : is that the position on the screen of the Texture on which we apply the effect?

This also led my to wonder: is this the best approch or should I rather make a small algorithm to directly SetData on the texture?

input.TextureCoordindate refers to the the position in the texture (0-1) - if you setup your “filler-texture” the same way, as you do the animations you should be fine, as “tex2D” just gets the pixel at TextureCoordindate in the texture (sampler)

But you are only comparing the Blue Component of the color - so you’re not filtering a whole color but all colors with a blue-component of 62.

You may also be aware, that that texture is actually sampled or mipmapped, so you may not get that exact pixel value in the shader but a sampled one between to texture-pixels (you’d have to align to texels and all that stuff)

So, I would suggest to do that "filling in advance with setdata, because it’s the easiest way to get the exact pixel color

Thanks a lot, I ended up making it work using a shader. :smile:

This is true but since my effect is very light it’s not noticeable in my case.