Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:09:03+00:00 2026-06-08T17:09:03+00:00

Right now im using a function in my sprite class called SetTextureColour: public void

  • 0

Right now im using a function in my sprite class called SetTextureColour:

    public void SetTextureColour(Color Colour)
    {
        Color[] data = new Color[Texture.Width * Texture.Height];
        Texture.GetData(data);

        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] == Color.White)
            {
                data[i] = Colour;
            }
        }
        Texture.SetData(data);
    }

To change the colour of every white pixel in the sprite’s texture to the specified colour, this works fine but the problem I have is that it changes the colours for every sprite which shares that texture, instead of just that individual sprite’s. Does anyone have a solution so that I can change the pixels of only that sprites texture, so I could use the function specifying different colours for multiple sprites which have the same texture.

Thanks in advance.

EDIT:

Upon advice I tried to do this using a pixel shader, although I have never used them before I attempted to do so, so far I have this as suggested-

sampler TextureSampler : register(s0);
float3 key_color;
float3 new_color;

float4 ChangePixel(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
  float4 newColor = tex2D(TextureSampler, texCoord);

  if (distance(key_color, color.rgb)<0.001f)
  {
     color.rgb = new_color;
  } 

  return newColor;
} 

technique PixelChange
{
    pass Pass0
    {
        PixelShader = compile ps_2_0 ChangePixel();
    }
}

Which is loaded using load content, I also set its technique using this-

        Effect1.CurrentTechnique = Effect1.Techniques["PixelChange"];

I then set the values using these two lines-

        Effect1.Parameters["key_color"].SetValue(Color.White.ToVector3());
        Effect1.Parameters["new_color"].SetValue(Color.Red.ToVector3());

But it seems to do nothing, any chance someone could help me out at this? As I said this is the first time using shaders so I could use some aid.

One last thing, the effect is used in spriteBatch.Begin, so it isnt a question of the effect not being applied.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:09:04+00:00Added an answer on June 8, 2026 at 5:09 pm

    You can use a shader that do that for you…

    sampler texture : register(s0);
    float3 key_color = float3(1,1,1);
    float3 new_color;
    
    float4 pixel_shader_that_replaces_color(in float4 color:COLOR0, in float2 coords:TEXCOORDS) : COLOR0
    {
          float4 color = tex2D(texture, coords);
          if (distance(key_color, color.rgb)<0.001f)
          {
             color.rgb = new_color;
          } 
          return color;
    } 
    

    In your code you have to load the new effect… and for each color you want to draw replaced do this…

     effect.SetValue("key_color", Color.White.ToVector3());
     effect.SetValue("new_color", yourcolor.ToVector3());
     spritebatch.Begin(-...., , ,, effect);
         // Inside this all your texture pixels with Color.White will be replaced to newColor
     spritebatch.End()
    

    You have to call sprite.Begin for each new_color you want.. but I think is better than setting the texture data…

    PD: I’m writing the code on the fly… it needs some extra work… like the vertex shader that you can find inside the stock sample , but these are the basics 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm using: r.forEach( function (el) { el.scale(0.5, 0.5, 0.0, 0.0); }); to
I'm having a problem right now with a function we're using in PHP to
I am using a function that returns a char*, and right now I am
Right now I am using this function for emoticons: function emoticons($text) { $icons =
Right now I'm using this: $('#go-to-top').each(function(){ $(this).click(function(){ $('html').animate({ scrollTop: 0 }, 'slow'); return true;
so right now I'm using a function that will set a value to true
Right now, I want to increase the size of the array using a function.
We are using a Java EE application and we are right now using Informix
Right now I am using sqlite within a ios application, and I want to
Right now I am using a custom list view and I am inflating an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.