handle input on 2D UI in 3D space with subviewport
Asked Answered
L

3

0

If anyone has any insight on this, it would be greatly appreciated!

I've been experimenting in 3d with godot 4.2 lately, trying to implement various pieces of first person shooter interaction functionality I find interesting. I'm trying to basically create a "computer screen" within the game, that when the player is in front of it and hits the "interact" keybind on it, their mouse cursor will become visible, and be confined within the "computer screen", without changing the players perspective at all.

I'm using a viewport to display a series control nodes for a basic gui, and am using it as a ViewportTexture for a quad mesh.

When I hit the "interact" keybind, I toggle the mouse mode to visible.... This is pretty much as far as I ha ve gotten, since when the mouse_mode is visible, the CollisionShape3D that contains the viewport doesn't seem to receive any input signals, or mouse_entered / exited. I can put a script on the viewport to manually listen for the input, and I can see that the click events are positioned based on the main viewport (meaning, they are all Vector2's, not actually being represented in the 3d space). But I don't really know how to get those to register within it, i.e. clicking on the control nodes.

The only thing the scripts are doing is getting is listening for inputs and printing them out so I could try various methods, so nothing really substantial.

I based my node setup on the Viewport2Din3D node from the wonderful Godot XR Toolkit, although that one seems to handle the clicks from the vr controller by sending a Vector3 position as input, while mine is triggering 2d input events... maybe I will have to experiment with creating raycast that come out from the mouse position to emulate this.

Leguminous answered 19/2 at 20:7 Comment(0)
L
0

Well, somehow I totally missed the "ray-casting" section of the docs, and it seems like this can provide exactly what im looking for. I didn't think to check the physics section for this for some reason. For anyone trying to do something similar, i think this info will help
https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html

Leguminous answered 20/2 at 4:41 Comment(0)
L
0

After multiple hours of messing around with the doc i linked above, I am still having difficulty getting the input to be passed from the 3d work to the 2d viewport. I made it as far as getting the ray intersection to happen with the viewport on mouse click, and I have converted the coordinates of the ray_intersection from 3d to 2d, but now I am once again trying to figure out what to do with all this information. I know subviewport has a push_input function, but that only works for forwarding inputs events, not dictionaries containing ray_intersection results...

Am I over thinking this? Any advice genuinely appreciated. Gonna go take a break from pulling my hair out

Leguminous answered 25/2 at 0:20 Comment(0)
L
0

As it turns out, this has already been solved in an example project. Originally, I attempted to convert the "gui_in_3d" example project from the asset library from godot 3 -> 4. This ended up not being nessesary... as they updated the example project on the git repo... but it hasn't been synced with the asset library I guess... these should really be updated

https://github.com/godotengine/godot-demo-projects/tree/master/viewport/gui_in_3d

Leguminous answered 25/2 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.