Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

texture coord from rayhit on mesh

Discussion in 'Scripting' started by ury2ok2000, Jul 13, 2017.

  1. ury2ok2000

    ury2ok2000

    Joined:
    Dec 29, 2012
    Posts:
    46
    Hello, I have some code that is almost working. When i click on this mesh quad i have setup it uses setpixel32 to put a red box on the clicked point (it uses setpixel32 on a child sprite texture2d). I use raycasthit providing the coordinates from screenpointtoray(Input.mousePosition). It works perfectly if i click on the exact middle. However, the further away i am from the middle the further the box is from where i click. Please see code below:

    RaycastHit hit;
    Vector2 PixelUV;
    if (!Physics.Raycast(CamToScreen.ScreenPointToRay(Input.mousePosition), out hit))
    return;
    PixelUV = hit.textureCoord;
    PixelUV.x *= 128;
    PixelUV.y *= 128;

    // I then send the PixelUV.x - 5 and PixelUV.y-5 with a 10x10 square to the setpixel32 function

    The parent object is a mesh with scale set to 1.28. Has a mesh collider and the script to catch the click. There is a child object that has the sprite and the texture. The texture is 128x128. Any clue as to what I'm doing wrong?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    This looks ok to me, if you want to post an example project I can have a look.

    The only other thing I would do before using the UV is to clamp it to 0..1

    e.g.

    PixelUV = hit.textureCoord;
    PixelUV.x = PixelUV.x % 1.0f;
    PixelUV.y = PixelUV.y % 1.0f;
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    UV coordinates are from 0 to 1. You should multiply them by the height and width of the texture to get the actual pixel locations on the texture itself.
     
  4. ury2ok2000

    ury2ok2000

    Joined:
    Dec 29, 2012
    Posts:
    46
    Thank you for the replies. I'm starting to suspect that the issue might be that the Parent Mesh is scaled to 1.28 so as to cover the 128 pixel sprite that is a child of it. Since i grab my texture point from that mesh and then try to apply it to the sprite, maybe that is why I'm getting bad results the further away from center that I am. I'll keep at it, and post the solution when i find it.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  6. ury2ok2000

    ury2ok2000

    Joined:
    Dec 29, 2012
    Posts:
    46
    Ok I was able to get it to work, however i did not use raycast.
    instead I created a grid of textures to cover screen using screencoord to do this (so 0,0 was in bottom left of screen). The calculation does rely on the sprite itself having a Pivot of bottom left too.

    Code (CSharp):
    1.  if (sr == null) sr = GetComponent<SpriteRenderer>();
    2.         float pixelWidth = sr.sprite.rect.width;
    3.         float pixelHeight = sr.sprite.rect.height;
    4.         float unitsToPixels = 100f;
    5.         Debug.Log("Cell name=" + name);
    6.      
    7.  
    8.         Pos = transform.InverseTransformPoint(Pos);
    9.  
    10.         int xPixel, yPixel;
    11.  
    12.         xPixel = (int)Mathf.RoundToInt(Pos.x * unitsToPixels);
    13.         yPixel = (int)Mathf.RoundToInt(Pos.y * unitsToPixels);
    xPixel and yPixel is equal to the pixel coord of the sprite where the Pos is (Pos is sent to the method and is the world coord of the pixel spot i am targeting).