- Home /
Changing texture to pixels several problems
There are several issues I have with the following code that all can be wrapped up with "it doesn't work". I'm hoping to use this to convert parts of an image to grayscale.
Super slow. I did this similar code in xCode last year and it flew. Texture is 512x256.
The texture color doesn't change. It should appear a white but it remains unchanged on the screen
I had originally been doing a GetPixel and multiplying out the RGB values by .3/59/.11 to get the grayscale look. it looked something like (color.r .3f). It always returned 0. In the watch window it was appearing as a long instead of a float. Why would a float float return as long?
Texture2D texture = (Texture2D)Plain.renderer.material.mainTexture;
for (int iy = 0; iy < texture.height; iy++) {
for (int ix = 0; ix < texture.width; ix++) {
texture.SetPixel (ix, iy, new Color (0, 0, 0));
} }
texture.Apply ();
Plain.renderer.material.mainTexture = texture;
Any help would be appreciated.
Answer by Eric5h5 · Jul 26, 2012 at 06:01 PM
What you have with the "texture" variable is just a reference, so 1) you don't need to do "Plain.renderer.material.mainTexture = texture" afterward, but 2) you don't actually want it to be a reference anyway, because then it's working on the actual asset...you should use Instantiate or create a new texture using the Texture2D constructor.
As for speed, aside from not working on the original asset, use SetPixels instead of SetPixel (or better yet, SetPixels32). Also try not to allocate new structs inside loops if you don't have to.
There's already a .grayscale value for Color, so just use that. float*float does not return long, it returns float, so I think you must be mistaken about that.
Ok, thanks for the push in the right direction. I still don't think I understand the difference between working on the "actual asset" vs creating a new Texture2D object. Seems to me that they are both Texture2D objects in memory, I should be able to manipulate them the same.
Your suggestion has worked, I am able now to render out the image the way I wanted. I am confused on Color32 vs Color object. Color32 is made up of bytes (which is how I always thought about Color objects. Color is floats. What's the conversion? Why the floats? Is Color just able to handle a many more colors than Color32? Why not just have a Color64?
I ended up with this code in my loop. Where colors is a Color32[];
Color c = texture.GetPixel (ix, iy); colors [ix + (iy * texture.width)] = new Color(c.grayscale, c.grayscale, c.grayscale);
If you just reference renderer.material.mainTexture then you will change that texture in your project permanently. (Unless mainTexture already comes from an instantiated or created texture of course.) Color32 uses 1 byte for each rgba. Color uses a float for each rgba. I expect the original intent with Color was to normalize colors so that rgba would always be 0..1 regardless of bit depth, but realistically the hardware just uses 1 byte anyway and this hasn't improved in many years, so using floats in the end just makes memory requirements go up by 4X and adds overhead for the float conversion.