- Home /
Custom shader or texture2d? (Best way to color swap)
Here's my little buddy. He likes white but not black. What's the best way to swap out the black for another color? Custom shader or texture2D?
Answer by Jwizard93 · Jun 29, 2017 at 10:25 PM
Texture2D makes it very easy. I don't know anything about Mip Mapping so lets assume I have unchecked generate Mip Maps in my texture import settings.
// get all the pixels into an array of colors
Color[] originalColors = myTexture.GetPixels();
//find all the black ones and make them blue
for (int i = 0; i < originalColors.Length; i++)
{
if (originalColors[i] == Color.black)
{
originalColors[i] = Color.blue;
}
}
//set the colors of the texture to the new colors
myTexture.SetPixels(originalColors);
//very important
myTexture.Apply();
If you do have mip maps the functions mentioned above expect an integer index of the lowest map level I beleive. Something like that.
Ofcourse a well written custom shader will execute much more quickly.
Texture2D = Easier
Shader = Faster
Thanks!
Your answer
Follow this Question
Related Questions
Changing color of texture with transparency efficiently 0 Answers
How to redye pixels of a texture? 1 Answer
Normalizing World Coordinates in Shader & Looking at Reference Texture Returns Incorrect Color 1 Answer
How to create an own Texture i.e. how to dye pixels? 0 Answers
change part of texture with color or other texture 0 Answers