Search code examples
c++mathrotationgeometryunreal-engine4

How to calculate sphere rotation based off of UV coordinates


I have a texture that I am wrapping around a sphere similar to this image on Wikipedia.

https://upload.wikimedia.org/wikipedia/commons/0/04/UVMapping.png

What I am trying to achieve is to take a UV coordinate from the texture let's say (0.8,0.8) which would roughly be around Russia in the above example.

With this UV somehow calculate the rotation I would need to apply to the sphere to have that UV centred on the sphere.

Could someone point me in the right direction of the math equation I would need to calculate this?

Edit - it was pointed out that I am actually looking for the rotation of the sphere so the uv is centered towards the camera. So starting with the rotation of 0,0,0 my camera is pointed at the uv (0,0.5)

Thanks


Solution

  • This particular type of spherical mapping has the very convenient property that the UV coordinates are linearly proportional to the corresponding polar coordinates.

    Let's assume for convenience that:

    • UV (0.5, 0.5) corresponds to the Greenwich Meridian line / Equator - i.e. (0° N, 0° E)
    • The mesh is initially axis-aligned
    • The texture is centered at spherical coordinates (θ, φ) = (π/2, 0) - i.e. the X-axis

    A diagram to demonstrate:

    enter image description here

    Using the boundary conditions:

    • U = 0 -> φ = -π
    • U = 1 -> φ = +π
    • V = 1 -> θ = 0
    • V = 0 -> θ = π

    We can deduce the required equations, and the corresponding direction vector r in the sphere's local space:

    enter image description here

    Assuming the sphere has rotation matrix R and is centered at c, simply use lookAt with:

    • Position c + d * (R * r)
    • Direction -(R * r)