-
Notifications
You must be signed in to change notification settings - Fork 79
PointerAndUI
Implementing a pointer and interacting with a UI
Our toolkit comes with a solution to implement a laser pointer that can interact with a UI.
You can use the laser pointer function stand alone and have it interact with 3D objects in your scene simply by adding a static body and collision shape to the 3D object.
Alternatively you can use the Viewport 2D in 3D object to introduce a 2D UI, the laser pointer will mimic a mouse and you can thus use a stock standard Godot 2D scene to render on the viewport. This also allows for testing the 2D interface within Godot by simply running the 2D scene and using your mouse on the desktop.
The pointer is implemented as a function scene. Simply add the scene to the ARVRController node that needs to act as a pointer. You can add the pointer to the controller node for both hands and switch between hands by enabling/disabling them but that falls outside of this writeup.
When the pointer interacts with physics bodies or areas it will send a signal or call a method related to our pointer feature if implemented on that body or area.
By adding a script to your physics body that extends XRToolsInteractableBody
or a script to your area that extends 'XRToolsInteractableArea' the required signals are added to that object and you can simply implement that logic.
For convenience our toolkit has an object that fully that fully implements the pointer logic to simulate a 2D interface you can interact with in XR.
First we need to create the 2D interface we want to show. We do this by creating a separate Godot 2D scene. For now I've created something very simple like so:
Simply add the Viewport2Din3D node to your scene and configure its size. Now you can assign your 2D scene so it gets rendered to the viewport.
When you run your game you can interact with the UI with your pointer.
note if you edit your 2D scene while your 3D scene is still open you may not see your changes in the 3D scene right away. Godot currently does not trigger a reload in the 3D scene. It will show the edited 2D scene after you reload the 3D scene.
Pointer function
Property | Description |
---|---|
Enabled | When ticked the pointer function is enabled |
Show Laser | When ticked and our pointer is enabled we draw our laser pointer |
Show Target | When ticked and our pointer is enabled we draw our target dot where our laser hits an object |
Active button | Controller button the user can press to interact with the target |
Y Offset | Draws our laser pointer at an offset |
Distance | The distance we cast our laser pointer |
Collision mask | The collision layers we interact with |
Collide with Bodies | Our laser pointer interacts with physics bodies |
Collide with Areas | Our laser pointer interacts with areas bodies |
XRToolsInteractableBody/XRToolsInteractableArea
Signal | Description |
---|---|
pointer_entered(body) | The pointer entered this body/area |
pointer_exited(body) | The pointer no longer points at this body/area |
pointer_moved(body, from, to) | The pointer moved while pointing at this body/area* |
pointer_pressed(body, at) | The button was pressed while pointing at this body/area |
pointer_released(body, at) | The button was released* |
note If the button is pressed pointer_moved
and pointer_released
will identify the body that we collide with when the button was pressed.
Viewport 2D in 3D
Property | Description |
---|---|
Screen size | Size of the screen in your VR space |
Viewport size | Size of the viewport on which the 2D UI is rendered |
Transparent | If ticked the viewport supports transparency |
Scene | The Godot 2D scene to render onto the viewport |
Collision layer | The collision layer(s) our viewport is a part off |