0 votes

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

in Engine by (19 points)

2 Answers

0 votes

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.

by (1,564 points)

thank you very much . but how did you assume max_x = -9999 ??

0 votes

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.

Or I think you can use a shader.

by (26 points)

won't it cost much memory??

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.