Home Game Development projection – How do I determine which pixel is under the cursor when looking at an equirectangular image through a camera?

projection – How do I determine which pixel is under the cursor when looking at an equirectangular image through a camera?

0
projection – How do I determine which pixel is under the cursor when looking at an equirectangular image through a camera?

[ad_1]

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).

[ad_2]