I’m trying to calculate a bounding box for a 3d mesh so I can draw a box around it in screen-space I’ve got something close to right but not quite right, anyone see what needs to be changed?
static Rect2 GetScreenRect(MeshInstance3D mesh)
{
Aabb box = mesh.GetAabb();
Vector3 worldStart = mesh.GlobalTransform * box.Position;
Vector3 worldEnd = mesh.GlobalTransform * box.End;
//Global.CurrentEnvironment.MainCamera just points to the camera thats rendering to the screen
Vector2 start = Global.CurrentEnvironment.MainCamera.UnprojectPosition(worldStart);
Vector2 end = Global.CurrentEnvironment.MainCamera.UnprojectPosition(worldEnd);
return new Rect2(start, Mathf.Abs(end.X - start.X), Mathf.Abs(end.Y - start.Y));
}
public override void _Process(double delta) //in a NinePatchRect control
{
base._Process(delta);
Rect2 r = GetScreenRect(unit.Mesh);
Position = r.Position + r.Size * Vector2.Up;
Size = r.Size;
}
It might be doable with a shader, but I ideally want this info in main memory and it would certainly be wasteful to calculate it in a shader and then retrieve the info. (plus then I’d have to learn how Godot handles shaders which is on my todo list but pretty far down at the moment haha)
I am using it to spawn Controls over the models so I can keyboard/gamepad navigate to them. So having it in main memory is important
If there’s a direct way of making a Node3D keyboard/gamepad navigable it would sidestep the whole issue, but I cant find one, so placing a control on top of them seems the simplest way of achieving that goal