Skip to content

Commit

Permalink
Add Tangible Tracking Camera support
Browse files Browse the repository at this point in the history
Added enabling the tangible camera and reading frames and pose
Added the T5ImageCapture node to interface with the camera 
Update example.gd and example.csharp to demo new features
  • Loading branch information
patrickdown committed May 22, 2024
1 parent a0207ea commit 504a3cc
Show file tree
Hide file tree
Showing 21 changed files with 683 additions and 10 deletions.
6 changes: 5 additions & 1 deletion example.csharp/ExampleRig.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=8 format=3 uid="uid://ba8h6c1mtb3h0"]
[gd_scene load_steps=9 format=3 uid="uid://ba8h6c1mtb3h0"]

[ext_resource type="PackedScene" uid="uid://dpbt52d0p5wjw" path="res://addons/tiltfive/scenes/T5XRRig.tscn" id="1_x7gas"]
[ext_resource type="PackedScene" uid="uid://b1cd3jc00rhal" path="res://addons/tiltfive/assets/T5GlassesModel.tscn" id="2_dp1ep"]
[ext_resource type="Script" path="res://WandControl.cs" id="2_epf7w"]
[ext_resource type="PackedScene" uid="uid://dnx42xctfl3mx" path="res://Controls.tscn" id="2_ge6xw"]
[ext_resource type="PackedScene" uid="uid://fipea8dbocg4" path="res://addons/tiltfive/assets/T5WandModel.tscn" id="5_j53ao"]
[ext_resource type="Script" path="res://addons/tiltfive/scenes/T5ImageCaptureCS.cs" id="6_mxd08"]

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_tnkdi"]
albedo_color = Color(0.580392, 0.396078, 0.278431, 1)
Expand All @@ -28,3 +29,6 @@ selected = SubResource("StandardMaterial3D_kgxv6")
transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0.585525, -0.00207818, 0.223126)

[node name="T5-wand" parent="Origin/Wand_1" index="1" instance=ExtResource("5_j53ao")]

[node name="T5ImageCapture" type="T5ImageCapture" parent="Origin" index="2"]
script = ExtResource("6_mxd08")
50 changes: 50 additions & 0 deletions example.csharp/addons/tiltfive/scenes/T5ImageCaptureCS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Godot;
using System;

public partial class T5ImageCaptureCS : Node3D
{
public bool startCapture()
{
return Call("start_capture").AsBool();
}

public void stopCapture()
{
Call("stop_capture");
}

public bool acquireBuffer()
{
return Call("acquire_buffer").AsBool();
}

public void releaseBuffer()
{
Call("release_buffer");
}

public byte[] getImageData()
{
return Call("get_image_data").As<byte[]>();
}

public Transform3D getCameraTransform()
{
return Call("get_camera_transform").As<Transform3D>();
}

public Vector2I getImageSize()
{
return Call("get_image_size").As<Vector2I>();
}

public int getImageStride()
{
return Call("get_image_stride").As<int>();
}

public int getFrameIlluminationMode()
{
return Call("get_frame_illumination_mode").As<int>();
}
}
8 changes: 8 additions & 0 deletions example.csharp/example.csharp.csproj.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>
82 changes: 82 additions & 0 deletions example.csharp/main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Godot;
using System;

public partial class main : Node3D
{
TextureRect cameraView;
Image cameraImage;
ImageTexture cameraTexture;
T5ImageCaptureCS imageCapture;
bool isCapturing = false;
Vector2I currentImageSize = Vector2I.Zero;

public override void _Ready() {
cameraView = GetNode<TextureRect>("ScreenUI/CameraView");
}

public override void _Process(double delta)
{
if (imageCapture != null && isCapturing)
{
if (imageCapture.acquireBuffer())
{
byte[] imageData = imageCapture.getImageData();
Vector2I imageSize = imageCapture.getImageSize();

if(cameraImage == null || imageSize != currentImageSize)
{
cameraImage = Image.CreateFromData(imageSize.X, imageSize.Y, false, Image.Format.R8, imageData);
cameraTexture = ImageTexture.CreateFromImage(cameraImage);
cameraView.Texture = cameraTexture;
currentImageSize = imageSize;
}
else
{
cameraImage.SetData(imageSize.X, imageSize.Y, false, Image.Format.R8, imageData);
cameraTexture.Update(cameraImage);
}
imageCapture.releaseBuffer();
}
}
}

public override void _Input(InputEvent evt)
{
if (evt.IsActionPressed("toggle_camera") && imageCapture != null)
{
if (!isCapturing && imageCapture.startCapture())
{
isCapturing = true;
cameraView.Visible = true;
}
else if (isCapturing)
{
imageCapture.stopCapture();
isCapturing = false;
cameraView.Visible = false;
}
}
}

private void _on_t_5_manager_xr_rig_was_added(SubViewport rig)
{
var rigImageCapture = rig.GetNode<T5ImageCaptureCS>("Origin/T5ImageCapture");
if(imageCapture == null) {
imageCapture = rigImageCapture;
}
}

private void _on_t_5_manager_xr_rig_will_be_removed(SubViewport rig)
{
var rigImageCapture = rig.GetNode<T5ImageCaptureCS>("Origin/T5ImageCapture");
if(imageCapture != null && imageCapture == rigImageCapture) {
if(isCapturing)
{
imageCapture.stopCapture();
isCapturing = false;
cameraView.Visible = false;
}
imageCapture = null;
}
}
}
35 changes: 34 additions & 1 deletion example.csharp/main.tscn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[gd_scene load_steps=10 format=3 uid="uid://cc7yui6nxllyl"]
[gd_scene load_steps=11 format=3 uid="uid://cc7yui6nxllyl"]

[ext_resource type="Script" path="res://main.cs" id="1_55n0b"]
[ext_resource type="Script" path="res://addons/tiltfive/T5Manager.cs" id="1_e8x2j"]
[ext_resource type="PackedScene" uid="uid://ba8h6c1mtb3h0" path="res://ExampleRig.tscn" id="2_vyjmk"]

Expand All @@ -24,6 +25,7 @@ albedo_color = Color(0.0313726, 0, 1, 1)
albedo_color = Color(0.45098, 0, 1, 1)

[node name="Main" type="Node3D"]
script = ExtResource("1_55n0b")

[node name="T5Manager" type="Node3D" parent="." node_paths=PackedStringArray("startLocation")]
script = ExtResource("1_e8x2j")
Expand Down Expand Up @@ -72,3 +74,34 @@ transform = Transform3D(0.99099, 0.062492, 0.118462, -0.133934, 0.462383, 0.8765
[node name="SpectatorCamera" type="Camera3D" parent="."]
transform = Transform3D(0.518176, -0.550674, 0.654409, -7.45058e-09, 0.765146, 0.643857, -0.855274, -0.333631, 0.396481, 8.04684, 5.20446, 5.82711)
cull_mask = 3

[node name="ScreenUI" type="Control" parent="."]
visibility_layer = 4
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="CameraView" type="TextureRect" parent="ScreenUI"]
visible = false
visibility_layer = 4
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="Label" type="Label" parent="ScreenUI"]
visibility_layer = 4
layout_mode = 1
offset_left = 16.0
offset_top = 18.0
offset_right = 196.0
offset_bottom = 41.0
text = "C - Toggle Camera View"

[connection signal="XRRigWasAdded" from="T5Manager" to="." method="_on_t_5_manager_xr_rig_was_added"]
[connection signal="XRRigWillBeRemoved" from="T5Manager" to="." method="_on_t_5_manager_xr_rig_will_be_removed"]
8 changes: 8 additions & 0 deletions example.csharp/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ project/assembly_name="example.csharp"

enabled=PackedStringArray("res://addons/tiltfive/plugin.cfg")

[input]

toggle_camera={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":67,"key_label":0,"unicode":99,"echo":false,"script":null)
]
}

[xr]

shaders/enabled=true
5 changes: 5 additions & 0 deletions example.gd/addons/tiltfive/scenes/T5XRRig.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var _gameboard_size := AABB()
var _origin : T5Origin3D
var _camera : T5Camera3D
var _wand : T5Controller3D
var _image_capture : T5ImageCapture

## Get the ID attached to a pair of Tilt Five glasses
func get_glasses_id() -> StringName:
Expand All @@ -32,10 +33,14 @@ func get_camera() -> T5Camera3D:
func get_wand() -> T5Controller3D:
return _wand

func get_image_capture() -> T5ImageCapture:
return _image_capture

func _enter_tree():
_origin = $Origin
_camera = $Origin/Camera
_wand = $Origin/Wand_1
_image_capture = $Origin.get_node("T5ImageCapture")

func _process(_delta):
if _wand: _wand.visible = _wand.get_has_tracking_data()
56 changes: 52 additions & 4 deletions example.gd/main.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
extends Node3D

func _on_t5_manager_glasses_scene_was_added(glasses):
print("Scene ", glasses.name, " added")
var image_capture: T5ImageCapture
var is_capturing := false
var image_size: Vector2i
var camera_image: Image
@onready var camera_view: TextureRect = $ScreenUI/CameraView

func _on_t5_manager_glasses_scene_will_be_removed(glasses):
print("Scene ", glasses.name, " removed")
# Grab the T5ImageCapture out of the xr rig
func _on_t_5_manager_xr_rig_was_added(xr_rig: T5XRRig):
var new_image_capture = xr_rig.get_image_capture()
if not image_capture and new_image_capture:
image_capture = new_image_capture

func _on_t_5_manager_xr_rig_will_be_removed(xr_rig):
var old_image_capture = xr_rig.get_image_capture()
if image_capture and image_capture == old_image_capture:
if is_capturing:
image_capture.stop_capture()
is_capturing = false
camera_view.visible = false
image_capture = null

# Toggle the camera on and off
func _input(event):
if image_capture == null:
return
if not event.is_action_pressed("toggle_camera"):
return
if not is_capturing and image_capture.start_capture():
is_capturing = true
camera_view.visible = true
else:
image_capture.stop_capture()
is_capturing = false
camera_view.visible = false

func _process(_delta):
if not is_capturing:
return
# Test to see if a image buffer is available
if image_capture.acquire_buffer():
# get the image data
var buffer := image_capture.get_image_data()
# Get the size
var new_size = image_capture.get_image_size()
# If is the first time of the size has changed
if camera_image == null or image_size != new_size:
image_size = new_size
camera_image = Image.create_from_data(image_size.x, image_size.y, false, Image.FORMAT_R8, buffer)
camera_view.texture = ImageTexture.create_from_image(camera_image)
else:
camera_image.set_data(image_size.x, image_size.y, false, Image.FORMAT_R8, buffer)
camera_view.texture.update(camera_image)
image_capture.release_buffer()
36 changes: 35 additions & 1 deletion example.gd/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=17 format=3 uid="uid://ckbe6draoen0x"]
[gd_scene load_steps=18 format=3 uid="uid://ckbe6draoen0x"]

[ext_resource type="Script" path="res://main.gd" id="1_xvgge"]
[ext_resource type="Script" path="res://addons/tiltfive/T5Manager.gd" id="2_dibvp"]
Expand Down Expand Up @@ -34,6 +34,8 @@ albedo_color = Color(0.862745, 0, 0.0235294, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_qrhlq"]
albedo_color = Color(0.741176, 0, 0.686275, 1)

[sub_resource type="ImageTexture" id="ImageTexture_0jitc"]

[node name="Main" type="Node3D"]
script = ExtResource("1_xvgge")

Expand Down Expand Up @@ -119,3 +121,35 @@ surface_material_override/0 = SubResource("StandardMaterial3D_qrhlq")
[node name="SpectatorCam" type="Camera3D" parent="."]
transform = Transform3D(0.670983, -0.138786, 0.728368, 0, 0.982326, 0.187176, -0.741472, -0.125592, 0.659125, 14.0459, 4.9572, 12.9908)
cull_mask = 3

[node name="ScreenUI" type="Control" parent="."]
visibility_layer = 2
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="CameraView" type="TextureRect" parent="ScreenUI"]
visible = false
visibility_layer = 2
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = SubResource("ImageTexture_0jitc")

[node name="Label" type="Label" parent="ScreenUI"]
visibility_layer = 4
layout_mode = 1
offset_left = 17.0
offset_top = 14.0
offset_right = 197.0
offset_bottom = 37.0
text = "C - Toggle Camera View"

[connection signal="xr_rig_was_added" from="T5Manager" to="." method="_on_t_5_manager_xr_rig_was_added"]
[connection signal="xr_rig_will_be_removed" from="T5Manager" to="." method="_on_t_5_manager_xr_rig_will_be_removed"]
8 changes: 7 additions & 1 deletion example.gd/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="T5Example.gd"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.1")
config/features=PackedStringArray("4.2")
run/max_fps=60
config/icon="res://icon.png"

Expand All @@ -31,6 +31,11 @@ trigger={
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":5,"axis_value":1.0,"script":null)
]
}
toggle_camera={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":67,"key_label":0,"unicode":99,"echo":false,"script":null)
]
}

[layer_names]

Expand All @@ -41,3 +46,4 @@ trigger={
[xr]

shaders/enabled=true
tilt_five/debug_logging=true
2 changes: 2 additions & 0 deletions example.gd/scenes/ExampleXRRig.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2.77558, 0)
mesh = SubResource("BoxMesh_aaxuw")
surface_material_override/0 = SubResource("StandardMaterial3D_iuako")

[node name="T5ImageCapture" type="T5ImageCapture" parent="Origin" index="3"]

[connection signal="button_pressed" from="Origin/Wand_1" to="Origin/Wand_1" method="_on_button_pressed"]
[connection signal="button_released" from="Origin/Wand_1" to="Origin/Wand_1" method="_on_button_released"]
Loading

0 comments on commit 504a3cc

Please sign in to comment.