Skip to content

Commit

Permalink
Merge branch 'camera_support' into ahead_of_godotvr
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdown committed May 19, 2024
2 parents 9d79a6c + a839513 commit a22b548
Show file tree
Hide file tree
Showing 20 changed files with 681 additions and 13 deletions.
8 changes: 6 additions & 2 deletions example.csharp/ExampleRig.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=9 format=3 uid="uid://ba8h6c1mtb3h0"]
[gd_scene load_steps=10 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="Script" path="res://ExampleRig.cs" id="2_af07c"]
[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 @@ -31,7 +32,10 @@ transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0.585525, -0.00207818, 0.2

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

[node name="Pivot" type="Node3D" parent="Origin" index="2"]
[node name="T5ImageCapture" type="T5ImageCapture" parent="Origin" index="2"]
script = ExtResource("6_mxd08")

[node name="Pivot" type="Node3D" parent="Origin" index="3"]

[node name="GlassesName" type="Label3D" parent="Origin/Pivot" index="0"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 4.46927)
Expand Down
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>
76 changes: 76 additions & 0 deletions example.csharp/main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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)
{
imageCapture = rig.GetNode<T5ImageCaptureCS>("Origin/T5ImageCapture");
}

private void _on_t_5_manager_xr_rig_will_be_removed(SubViewport rig)
{
if(imageCapture != null && 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"]
18 changes: 17 additions & 1 deletion example.csharp/project.godot
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="T5Example.csharp"
config/tags=PackedStringArray("tiltfive")
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2", "C#", "Forward Plus")
config/icon="res://icon.png"
config/tags=PackedStringArray("tiltfive")

[autoload]

Expand All @@ -20,6 +28,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 Down Expand Up @@ -36,10 +37,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()
54 changes: 50 additions & 4 deletions example.gd/main.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
extends Node3D

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_xr_rig_was_added(xr_rig):
print("Scene for glasses ", xr_rig.get_glasses_id(), " added")
# 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 _on_t5_manager_xr_rig_will_be_removed(xr_rig):
print("Scene for glasses ", xr_rig.get_glasses_id(), " removed")
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()
37 changes: 34 additions & 3 deletions 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 @@ -120,5 +122,34 @@ surface_material_override/0 = SubResource("StandardMaterial3D_qrhlq")
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

[connection signal="xr_rig_was_added" from="T5Manager" to="." method="_on_t5_manager_xr_rig_was_added"]
[connection signal="xr_rig_will_be_removed" from="T5Manager" to="." method="_on_t5_manager_xr_rig_will_be_removed"]
[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"]
5 changes: 5 additions & 0 deletions example.gd/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 Down
Loading

0 comments on commit a22b548

Please sign in to comment.