0
\$\begingroup\$

I am working on a game. The camera is freely rotatable by the user. The background is displayed from an equirectangular image. I need to know which pixel of the background image is located under the cursor at any moment. My research leads to equations mapping a flat image onto a spherical surface but I need the opposite and occasionally run into the 'reverse projection' problem, usually presented in a scholarly paper. However, I've been using an old game engine that does exactly that, although I'm having problems decoding the source code to see how it was done. Perhaps it was a hack that was approximate enough? (for anybody using Godot, I would LOVE a pointer to some function that already does that for me)

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

We can attack this in stages.

First, we convert the point clicked on the screen into a direction in the 3D world, the same way we'd do if we were casting a ray from the camera for 3D picking. For instance, if we want to do this on mouse click, we can basically copy the example from that page of the documentation:

func _input(event): if event is InputEventMouseButton and event.pressed and event.button_index == 1: var camera3d = $Camera3D var direction = camera3d.project_ray_normal(event.position) 

Next, we convert that direction into spherical coordinates. I don't have Godot in front of me atm, but I'll assume project_ray_normal returns a unit vector. If not, normalize direction before proceeding:

var latitude = asin(direction.y) var longitude = atan2(direction.z, direction.x) 

This gives you the latitude angle in the range \$- \frac \pi 2\$ to \$+ \frac \pi 2\$ and the longitude angle in the range -π to π. You can then rescale this into the pixel dimensions of your image:

var x = (longitude/PI * 0.5 + 0.5) * width var y = (latitude/PI + 0.5) * height 

If you find the x coordinates you get are starting at the wrong place or wrapping the wrong way, you can fix this by exchanging or negating the arguments to atan2 (I don't know which mapping you're using, so I can't say which of the 8 permutations is right for your needs).

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.