How to convert ui_in_3d demo project to works with ArrayMesh

Godot Version

4.2.2

Question

demo

func _mouse_input_event(_camera: Camera3D, event: InputEvent, event_position: Vector3, _normal: Vector3, _shape_idx: int):
	# Get mesh size to detect edges and make conversions. This code only support PlaneMesh and QuadMesh.
	var quad_mesh_size = node_quad.mesh.size

I want to make this code work in ArrayMesh.

I assume, that you use the ArrayMesh in a MeshInstance3D and that you display the SubViewport somewhere in that MeshInstance3D.
Additionally I assume that you have an Area3D with a BoxShape3D collision shape that is located where the SubViewport is displayed in the MeshInstance3D. This Area3D has the _mouse_input_event function.

You need to adopt the code, so that the mouse coordinates in the _mouse_input_event are converted to coordinates of the SubViewport. In the demo this is covered for the quad in the following code: godot-demo-projects/viewport/gui_in_3d/gui_3d.gd at 95cfb076d1a3d59bdb84e604d4712144af0b6a3d · godotengine/godot-demo-projects · GitHub

You would need to adjust that code:

  • Starting at the 3D-coordinates where the mouse collides with the rectangle collision shape of the Area3D
  • convert to an 2D-vector with x and y coordinates in the range [0-1], representing the position in the rectangle collision shape by dividing by the size of the side of the BoxShape3D, that represents the area of the SubViewport
  • convert to the size of the SubViewport by multiplying with the size of the SubViewport

One possible point of failure: Have you set up the BoxShape3D so that it aligns exactly with where the SubViewport is displayed?