- Home /
 
Changing color of Texture2d with transparency
Hi,
I would like to change the color of the texture 2d image drawn in OnGUI function using GUI.drawtexture. When i change the color using get & set pixel functionality, color of whole image immaterial of its transparency area changed. I would like to change the color of non transparent area alone.
public Texture2D baseTex; public Color[] colors = new Color[1];
void Start() { colors = Color.cyan;
  int mipCount = Mathf.Min(1, baseTex.mipmapCount);
     
     // tint each mip level
     for( var mip = 0; mip < mipCount; ++mip ) 
     {
         var cols = baseTex.GetPixels( mip );
         for( var i = 0; i < cols.Length; ++i ) 
         {
             cols[i] = Color.Lerp(cols[i], colors[mip], 1f);
         }
         baseTex.SetPixels( cols, mip );
     }
     // actually apply all SetPixels, don't recalculate mip levels
     baseTex.Apply(false);
 
               }
void OnGUI() { GUI.DrawTexture(new Rect(100, 0, baseTex.width zoom, baseTex.height zoom), baseTex); }
When i apply the texture in a transparent material its working fine, but i need to draw the color changed texture in OnGUI. Please help me out to resolve the issue.
Regards, Ben
Answer by CarterG81 · Jun 20, 2015 at 03:23 AM
Unless I'm misunderstanding the question, you want to change only non-transparent pixels?
I believe that should be as easy as a simple IF statement, using the pixel's ALPHA.
if(alpha = 255) setpixel.
else { don't
Hi,
How can i find transparent or non-transparent pixel of a texture
Your answer
 
             Follow this Question
Related Questions
Scramble GetPixels function... 1 Answer
Resetting a Material's Texture After GetPixels/SetPixels 1 Answer
Convert 2D array to a 1D array 2 Answers
Merging Textures At Runtime - Not Correct? 1 Answer
Painting stencil on a surface. 6 Answers