Hello Guest

Author Topic: Check texture alpha at RaycastHit...  (Read 6827 times)

betonos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Check texture alpha at RaycastHit...
« on: August 31, 2012, 10:06:14 am »
Hi,

I'm trying to pick an object pixel-perfect. But I don't want mesh colliders of any kind on the object, because I have lot of theese objects. So what I want is to do Raycast and then check texture alpha value at RaycastHit position. But the problem is, the textureCoord field has value of (0;0) no matter where the hit occurs. This is due to the fact that the object has no mesh collider (according to Unity documentation).

How do I calculate UV values for the atlas texture and get the correct pixel alpha value at the hit position?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #1 on: September 03, 2012, 11:51:08 pm »
Yes it is possible to do this. The transformation is slightly complicated, but totally doable. To keep things simple, you should limit this to rectangular sprites as opposed to supporting everything else 2D Toolkit supports. Basically you need to work out the position of the coordinate in local UV space relative to the bounding box of the sprite. After you get this (this will be the UV) you can work out the coordinate of intersection by looking at the points in GetCurrentSpriteDef().uvs and then read the pixel.

Is this something anyone else would find useful? I have a idea on how to simplify this massively and I could explore it further if it is something that people would want.

betonos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #2 on: September 04, 2012, 11:02:37 am »
OK, I've figured it out in the meantime (and did it the way you suggest).

The problem that I am facing now is the following:

if I don't disable alpha trimming in atlas and the sprites get trimmed the following bug occurs:
1) sprite bounds are calculated for trimmed sprite - this is OK
2) but the gameobject.transform.InverseTransformPoint transforms according to the original, untrimmed size of the object.

This problem is shown here in detai:


aaronfable

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #3 on: April 10, 2013, 01:11:42 am »
I am trying to do the exact same thing as betonos here and have run into the same problems. If there were a way to simplify this further it would greatly help. If not, if you could help explain how properly to "work out the position of the coordinate in local UV space relative to the bounding box of the sprite" as InverseTransformPoint isn't cutting it alone. By that do you mean that I need to construct a matrix where the size and offset is based on the bounding box within its parents' coordinate space?

My definition uvs[] looks like (0.8,0.0), (0.9,0.0), (0.8,0.1), (0.9,0.1). If I had the point in bounding box space it'd be 0.0-1.0. Do I need to make a bounding box based on the uvs bounding coordinates? Trying to figure out how to get at these offsets effectively.

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #4 on: April 12, 2013, 12:38:16 am »
The reason there isn't any straightforward solution to this is that sprites in tk2d can be any shape - they are not limited to square, and  they are certainly not limited to being sequential in the atlas. When using dicing, the sprite is split up into many many tiny sub-sprites and merged back automatically. They will most certainly not form the same "rectangle" in atlas space.

However, if you don't care about these features it should be easy enough to work out the UV you've hit. The only problem here is coping with sprites rotated in the atlas. In tk2dClippedSprite, look at the SetGeometry function. That function documents and shows you how the vertices are set up for all supported formats (there is more than one rotated format supported). Obviously if you only use tk2dSprites from a sprite collection, then you don't need to implement the TPackerCW path. Again, if your needs are specific, its easy to customise. A generic solution which Just Works is significantly harder.

gladieweb

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #5 on: January 07, 2015, 02:05:49 pm »
i need to get uv from raycasthit but have same problem (0,0) can someone give an example to understand better. thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Check texture alpha at RaycastHit...
« Reply #6 on: January 07, 2015, 06:43:33 pm »
You can't use raycasthit, that only works when the object has a mesh collider as mentioned before. You could add a mesh collider with the same mesh but that would be massively wasteful.

You'll need to inverse transform the hit position into sprite local position, and then work out the uv position as described in the previous post - with the constraints described there as well. Look at tk2dSpriteGeomGen.SetClippedSpriteGeom, that pretty much describes the structure of the vertex data.