Author Topic: hit.textureCoord with UITexture  (Read 7215 times)

ddfire

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
hit.textureCoord with UITexture
« on: July 01, 2013, 09:52:34 PM »
hi
i am using UITexture and Vector2 pixelUV = hit.textureCoord; always return 0
how can i fix it?
thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: hit.textureCoord with UITexture
« Reply #1 on: July 02, 2013, 12:05:05 AM »
RaycastHit will only contain that information if it hits a renderer as far as I know. Raycasting into NGUI colliders won't give you anything because they don't have a renderer. You need to use math to calculate the UVs yourself.

ddfire

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: hit.textureCoord with UITexture
« Reply #2 on: July 02, 2013, 12:13:31 AM »
Any idea where to start?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: hit.textureCoord with UITexture
« Reply #3 on: July 02, 2013, 11:13:21 AM »
Raycasting gives you a world position (hit.point). Knowing the widget's transform, use InverseTransformPoint function to convert it to local space. Account for the pivot if you need to (if it's bottom left, you don't). The position should now be in 0-1 range -- which coincidentally matches your UV coordinates of the texture.

ddfire

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: hit.textureCoord with UITexture [SOLVED]
« Reply #4 on: July 03, 2013, 12:35:16 AM »
MR. You are a genius!!!!
Thanks you VERY VERY MUCH!!!!

This function will return the UV Coordinates of the hit to a UITexture
  1.        
  2. Texture2D tex;
  3. tex = UItarget.material.mainTexture as Texture2D;
  4. public Vector2 getUVPoint2(Vector3 origin)
  5. {
  6.         RaycastHit hit;
  7.         if (Physics.Raycast(origin, Vector3.forward, out hit)){
  8.                 Vector3 pixelUV = UItarget.transform.InverseTransformPoint(hit.point);
  9.                 pixelUV.x *= tex.width;
  10.                 pixelUV.y *= tex.height;
  11.                 return pixelUV;
  12.         }
  13.         return new Vector2(-1,-1);
  14. }
  15.