简体   繁体   中英

Unity3D: Make texture from one pixel of RenderTexture

I have a single texture which I have rendered from a camera. I want to take a number single pixel values from this texture and make new textures (1-by-1 pixel) which I want to then apply to some objects.

Is there a way of defining a texture whose colorBuffer is just a length 1 array from another colorBuffer ? Is there perhaps a simple way of writing a shader that will do the same task efficiently? Any help appreciated.

GetPixel() and SetPixel() are what you are looking for:

Color pixel = yourTexture2D.GetPixel(x, z);
Texture2D newTexture = new Texture2D(1,1);
newTexture.SetPixel(0, 0, pixel);
newTexture.Apply();

Please note that Texture2D.Apply() is cpu expensive, but proportional to the size of the texture so you can probably get away with that.

Also note that you may have to adjust the coordinates x and z in relation to the size of yourTexture2D , like this:

int x = transform.position.x / size.x * yourTexture2D.width;

For more details on this adjustment, check the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM