Search Unity

Vector3 to Vector2 UV coordinates

Discussion in 'Scripting' started by DiffusionalArts, Sep 4, 2017.

  1. DiffusionalArts

    DiffusionalArts

    Joined:
    May 23, 2013
    Posts:
    5
    I am attempting to get access to the UV coordinates of a (skinned) mesh tbased on where it was raycasted so that I made edit the texture using the Vector2 UV coords. I am aware of "RaycastHit.TextureCoord" and it works fantastic- however it is limited to only work IF the mesh has a mesh collider attached, and considering I am raycasting onto a skinned mesh, having a mesh collider attached is impossible.

    How could I go about taking the Vector3 point in space and translating that to a Vector2 UV point in texture space?


    Thanks for any insights!
     
    Last edited: Sep 7, 2017
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
  3. DiffusionalArts

    DiffusionalArts

    Joined:
    May 23, 2013
    Posts:
    5
    Hey, I coincidentally implemented this approach just recently and got great results! Unfortunately there's still quite a bit of overhead when the bake functions called- and even more so the higher poly you go :(
     
  4. neon_teebar

    neon_teebar

    Joined:
    Jun 1, 2017
    Posts:
    46
    I have a system that doesn't get slower with high poly meshes because it's shader based: Super Raycast.

    You can get the uv like:
    Code (CSharp):
    1. RaycastHitRenderer hitInfo;
    2. if (SuperRaycast.PhysicsRaycast(ray, out hitInfo))
    3. {
    4.     // UV results
    5.     Debug.Log(hitInfo.uv);
    6. }