Topic was automatically imported from the old Question2Answer platform.
Asked By
abdullahzubair109
Old Version
Published before Godot 3 was released.
I have a solar system. Planets are rotating around sun. It is a 3d project. When player clicks on a planet, I want to show a border around it.Is there any way to calculate the center ( not geometric center of the planet mesh) of the planet image ( as it appears naturally on the viewport, a 2d image projected from 3d scene)??
Here’s a naive method of drawing a 2D box around a projected 3D mesh:
-Get the vertices of the mesh, transform them into 2D screen coordinates and find the min- and max-coordinates (this assumes the mesh only has one surface):
#Here mesh_inst is a MeshInstance and cam is the Camera node
var tf = mesh_inst.get_global_transform()
var mesh_tool = MeshDataTool.new()
mesh_tool.create_from_surface(mesh_inst.get_mesh(), 0)
var max_x = -9999
var min_x = 9999
var max_y = -9999
var min_y = 9999
for i in range(mesh_tool.get_vertex_count()):
var point_2d = cam.unproject_position(tf.xform(mesh_tool.get_vertex(i)))
max_x = max(point_2d.x, max_x)
min_x = min(point_2d.x, min_x)
max_y = max(point_2d.y, max_y)
min_y = min(point_2d.y, min_y)
-Draw the box according to the min- and max-coordinates.
thank you very much . but how did you assume max_x = -9999 ??
Make for everyplanet a duplicate of itself, grow up it a bit, set the render back, and use a colour. Make it not visible, and by script enable it when the planet is clicked.