Shader for distorting a texture as if it were on a sphere

I’m trying to add 3D spheres with simple textures to my UI. Currently I’m doing this using cameras and render textures but this is not very performant. I’m thinking there must be a way to take a flat texture and a set of euler angles and produce a distortion which makes the texture appear as though it’s on a rotated sphere.

Note that in the attached image, my examples are on an actual sphere. However in the UI it’d probably just be projected onto a square sprite which I then add a circular mask to.

I’m already somewhat capable with simple shaders but I’m not sure what to look into to start with this one. I’d appreciate if someone could point me in the right direction. Thanks.

Well, if you actually talk about just displaying a single quad in screen space with a special shader that actually shows a sphere with a texture mapped onto it, this is possible but not that trivial. You essentially do a manual ray-tracing in the fragment shader. You just need to specify all relevant information from outside. Also I barely use the gameobject based UI system so I’m not sure if you can actually use a custom material / shader for your element. However instead of normal UI element you could also use a normal Quad. I currently do not have the time to modify my raytrace shader I’ve written some time ago. I used it to render a to-scale earth sphere.

Since using an actual sphere mesh is always imprecise I created this raytrace shader. Together with the shader I used this script to draw a fullscreen overlay quad procedurally which essentially overlayed the raytraced earth sphere in the main camera view. It also updates the relevant shader variables like the position of the earth sphere and the rotation. Of course you wouldn’t need most of that if you have a fix view and position of your virtual sphere. Note that I generate the usual UV-sphere texture coordinates in the shader. So the usual cylindrical mapping is used. Which means the top and bottom of the texture will be mapped to the north / south pole and have the greatest distortion in the mapping.

For moving texture around in shader you can manipulate uv’s in shader, for morphing quad into sphere you can change position of vertices in vertex shader… but such huge modification is probably not going to work well… maybe it can be checked what exactly is causing performance issues in current solution

Why not take the offset of yozr texture and scale and rotate always pointing to center?