- Home /
Get pixel from Rotated Texture2D
I am firing a raycast beam downward and changing the pixel it hits to red, the code works perfectly when orientated as shown in the first image, but when i rotate it 90 degrees it doesnt work correctly. The five red cubes each fire a raycast beam down into the center of the circle and change the pixel hit to red. What do i need to change to get it to work in the second orientation ?
image 1 (works):
image 2 (does not work):
My Code:
for(int i=0; i<PunchPin.Count;i++){ //looping through cubes
Debug.DrawRay(PunchPin[i].transform.position,Vector3.down,Color.cyan);
RaycastHit hit;
Physics.Raycast(PunchPin[i].transform.position,Vector3.down, out hit);
if(hit.collider !=null){
if(hit.collider.gameObject.name == PunchTape.gameObject.name){
MeshRenderer rend = hit.collider.gameObject.GetComponent<MeshRenderer>();
Texture2D tex = (Texture2D)rend.material.mainTexture;
Vector2 uv;
uv.x = (hit.point.x - hit.collider.bounds.min.x) / hit.collider.bounds.size.x;
uv.y = (hit.point.z - hit.collider.bounds.min.z) / hit.collider.bounds.size.z;
Vector2 texCoord = hit.textureCoord2;
tex.SetPixel((int)(uv.x * tex.width), (int)(uv.y * tex.height),Color.red);
tex.Apply();
}
}
}
Answer by Jake-L · Jul 01, 2015 at 07:33 PM
You rotate the image in the scene using it's transform (or RectTransform, doesn't matter). But the source texture is always unrotated, so you need to rotate the hitpoint when calculating the pixel position.
How would i do that, could you give your answer in code ?
If you rotate in 90° steps like shown in your picture above, just flip X/Y.
i have tried that but it doesnt seem to work, which lines should i change and what should i change them to
Sorry to comment on an ancient question, did anyone ever figure out how to rotate the hit point?
Your answer
Follow this Question
Related Questions
Canvas UI - Get Pixel Colour from Image 0 Answers
Trying to access a texture's pixel coords. 2 Answers
Rotated Pixel Textures 1 Answer
Get Average Colour From Texture? 1 Answer
Pull Color from pixel of a an invisible texture from Raycast. 1 Answer