SetPixels and Alpha8 - most efficient method?

I need to set an Alpha8 texture quickly (I generate it on the fly). I see that SetPixels will work with it, but that takes a Color array (note: the float value kind with RGBA). SetPixels32 works with 8-bit RGBA’s. What I was hoping for was something like a SetPixels8, or Color8 or ColorGray type of API (none exist that I know of).

So do I really need to allocate a 32-bit color array for my 8-bit bitmap? Or a full RGBA pixel array that converts to float and back? Is there a better way?

2 Answers

2

Try creating your texture as a byte array and use an Alpha8 texture…

Create the texture

texture = new Texture2D(width, height, TextureFormat.Alpha8, false);

Create a buffer

pixelBuffer = new byte[width * height];

Fill the buffer with the pixel values (0 - 255)…

Load the buffer into the texture

texture.LoadRawTextureData(buffer);
texture.Apply();

To get the pixel vale in your shader use the alpha value of the texture

tex2D(_MainTex, i.texcoord).a

This works for us.

Yes, this works very well for us too. Thank you for this answer!

This almost works, but i feel like it copy 1/4 of the picture over and over on the same picture. How could I fill the pixel values with only the alpha of a texture efficiently ? For now, I have textureA.LoadRawTextureData(Texture2DRGB.GetRawTextureData());

SetPixels is the only way. Sounds like a good candidate for a feature request though.