- Home /
Is it possible to determine a color of a pixel under if that pixel is under a game object?
Hi I'm trying to make a canvas and a stencil. The canvas is texture2d and the stencil is a gameObject. Is there any possible way to tell each pixel of the texture under the gameObject to be permanently white?
Thanks
Answer by Dragate · Oct 06, 2017 at 06:22 AM
Hello,
the general methology I'd use would be something like this:
1) Raycast from stencil to canvas making sure the ray is alligned with an axis so you get the pixel EXACTLY UNDER the stencil. Basically, make sure the stencil is on the straight line perpendicular to the texture (I hope I make sense).
2) Get the RaycastHit, which includes the textureCoord variable. This will tell you where in the texture you hit. It returns values from 0 to 1. (0,0) is lower left of the texture and (1,1) is upper right.
3) Multiply textureCoord values with texture width and height respectively so you get the exact xy coordinates of the pixel in the texture and round the results to the nearest integer.
4) Now you can set that pixel's color with Texture2D.SetPixel(int x, int y, Color.white);
Your answer
Follow this Question
Related Questions
How can I efficiently determine that certain parts of a texture have a specific color? 1 Answer
SetPixels32 called with invalid number if pixels in the array 0 Answers
Assigning script-generated textures 0 Answers
How to enable antialiasing for Texture2D 0 Answers
Is there a way to apply a texture to a 2d collider? 1 Answer