Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Mapping a point in world space to a UV of a sphere

Discussion in 'Shaders' started by stanislavdol, Oct 14, 2019.

  1. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    282
    Hi. So the question is in the title.
    I have an arbitrary point in space inside a sphere, let it be point A. I need to sample a texture color at the surface point closest to my point A. Imagine a vector going from a center of a sphere to the surface through point A . I need to know the color where the vector hits the surface.

    My initial idea was to transform point A to spherical coordinates and have angles represent uv coordintes(mapped to 0-1 range). However, I can't seem to get it to work.

    Any help would be greatly appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,243
  3. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    282
    That's exactly what I was doing. For some reason texture appears tilted(rotated). And using 1-uv(x or y) or -uv(x or y) doesn't seem to help.

    It is rotated 90 degrees around z axis(the one looking at you).
    I can "rotate" it around vertical axis by changing uv.x+ _Rotate. However, I can't seem to find a way to rotate it around z axis.

    For now I overcome this by rotating vertices in shader and later rotating the transform back. However, this seems like a dirty fix.
     
    Last edited: Oct 15, 2019
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,243
    That sounds like you have a mesh that’s imported from a program using z up instead of Unity’s y up. In which case take the code in the above link and swap the .z and .y