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?