I have a target texture on camera which is a render texture. However, I want to change the colors of specific texture pixels in a certain way, I did it using Texture2D methods, but now the problem is how to copy the extracted data to the texture renderer?
Texture2D and Texture Render have the same format and dimensions, but the methods below still have no result, as the camera remains a old render texture.
Graphics.CopyTexture(texture2D, rd);//Graphics.Blit(texture2D, rd)
In general, everything looks like this I show below in the code. I’m trying to read the pixel data from the texture render to texture2d (I don’t know if it’s successful, because by default the texture is still black). And then I successfully change the pixels to white in texture2d and try to copy this result to the texture render, but it somehow fails.
texture2D = new Texture2D(rd.width, rd.height, TextureFormat.ARGB32, false);
RenderTexture.active = rd;
Graphics.CopyTexture(rd, texture2D);//texture2D.ReadPixels(new Rect(0, 0, rd.width, rd.height), 0, 0);
texture2D.Apply();
for (int w = 0; w < rd.width; w++)
for (int h = 0; h < rd.height; h++)
texture2D.SetPixel(w, h, new Color(1, 1, 1));
texture2D.Apply();
Debug.Log(texture2D.GetPixel(0, 0));
Graphics.CopyTexture(texture2D, rd);//Graphics.Blit(texture2D, rd);
RenderTexture.active = null;
gameObject.GetComponent<Camera>().targetTexture = rd;
I’ve also read that when using Blit you need to double-buffer using the buffer texture, otherwise using the function will lead to unexpected results, but this code is only called once, so I think it should work.