- Home /
Question by
Wannabeuk · Jun 07, 2020 at 03:32 PM ·
raycasttexturecoord
Selecting a pixel on a spheres texture via click
I'm trying to get the clicked pixel location on a sphere. I've used the following code :
if (Input.GetMouseButtonUp(0))
{
RaycastHit hit;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
return;
}
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 50000000, Color.red, 100f);
Renderer renderer = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if(renderer == null || renderer.sharedMaterial == null || renderer.sharedMaterial.mainTexture == null || meshCollider == null)
{
return;
}
Texture2D tex = renderer.material.mainTexture as Texture2D;
Vector2 pixelLocation = hit.textureCoord;
pixelLocation.x *= tex.width;
pixelLocation.y *= tex.height;
//This line simply recolours the pixel at the above location to red.
generator.DrawSelected(newtiles);
}
The issue i'm having that i also have the following script on the sphere object
void Update()
{
if (rotate)
{
transform.Rotate(Vector3.up, RotationSpeed * Time.deltaTime);
}
}
private void OnMouseEnter()
{
rotate = false;
}
private void OnMouseExit()
{
rotate = true;
}
The issue is that if the sphere is allowed to rotate at any point before clicking (as in, the rotationSpeed is anything above 0) the coordinates returned from hit.textureCoord are innactuate. They appear to be offset along the x axis. If the sphere isn't allowed to rotate the coordinate is returned correctly. Using the debug ray i can see the raycast hit's the correct location, But the recoloured pixel is not where the raycast hit.
I'm stumped as to what is causing this.
Comment