Add switch for distortion and clean some code
This commit is contained in:
parent
7b4ba61509
commit
039b21ccbe
File diff suppressed because one or more lines are too long
|
@ -1,20 +1,6 @@
|
|||
[gd_resource type="Sky" load_steps=7 format=3 uid="uid://dlont1iyrrl3g"]
|
||||
[gd_resource type="Sky" load_steps=2 format=3 uid="uid://dlont1iyrrl3g"]
|
||||
|
||||
[ext_resource type="Shader" path="res://shaders/SkyImages.gdshader" id="1_biwbg"]
|
||||
[ext_resource type="Texture2D" uid="uid://64qjvjymo7oj" path="res://textures/LeftImage.tres" id="2_uqhtk"]
|
||||
[ext_resource type="Texture2D" uid="uid://7h3wom5btmhp" path="res://textures/LeftDistortion.tres" id="3_11h6l"]
|
||||
[ext_resource type="Texture2D" uid="uid://t5jb84gwhfx4" path="res://textures/RightImage.tres" id="3_kniuh"]
|
||||
[ext_resource type="Texture2D" uid="uid://dwx41gu1dkwgt" path="res://textures/RightDistortion.tres" id="5_fpw2i"]
|
||||
|
||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_7aiux"]
|
||||
shader = ExtResource("1_biwbg")
|
||||
shader_parameter/undistort = true
|
||||
shader_parameter/default_colour = Color(0, 0, 0, 1)
|
||||
shader_parameter/gammaExponent = 1.6
|
||||
shader_parameter/left = ExtResource("2_uqhtk")
|
||||
shader_parameter/right = ExtResource("3_kniuh")
|
||||
shader_parameter/left_distortion = ExtResource("3_11h6l")
|
||||
shader_parameter/right_distortion = ExtResource("5_fpw2i")
|
||||
[ext_resource type="Material" uid="uid://bistsvvb3ab2n" path="res://materials/SkyImagesMaterialtres.tres" id="1_flhx8"]
|
||||
|
||||
[resource]
|
||||
sky_material = SubResource("ShaderMaterial_7aiux")
|
||||
sky_material = ExtResource("1_flhx8")
|
||||
|
|
17
demo/materials/SkyImagesMaterialtres.tres
Normal file
17
demo/materials/SkyImagesMaterialtres.tres
Normal file
|
@ -0,0 +1,17 @@
|
|||
[gd_resource type="ShaderMaterial" load_steps=6 format=3 uid="uid://bistsvvb3ab2n"]
|
||||
|
||||
[ext_resource type="Shader" path="res://shaders/SkyImages.gdshader" id="1_1ksh0"]
|
||||
[ext_resource type="Texture2D" uid="uid://64qjvjymo7oj" path="res://textures/LeftImage.tres" id="2_m8pcx"]
|
||||
[ext_resource type="Texture2D" uid="uid://7h3wom5btmhp" path="res://textures/LeftDistortion.tres" id="3_x1vnt"]
|
||||
[ext_resource type="Texture2D" uid="uid://t5jb84gwhfx4" path="res://textures/RightImage.tres" id="4_ltv2w"]
|
||||
[ext_resource type="Texture2D" uid="uid://dwx41gu1dkwgt" path="res://textures/RightDistortion.tres" id="5_abkdj"]
|
||||
|
||||
[resource]
|
||||
shader = ExtResource("1_1ksh0")
|
||||
shader_parameter/undistort = true
|
||||
shader_parameter/default_colour = Color(0, 0, 0, 1)
|
||||
shader_parameter/gammaExponent = 1.6
|
||||
shader_parameter/left = ExtResource("2_m8pcx")
|
||||
shader_parameter/right = ExtResource("4_ltv2w")
|
||||
shader_parameter/left_distortion = ExtResource("3_x1vnt")
|
||||
shader_parameter/right_distortion = ExtResource("5_abkdj")
|
|
@ -97,5 +97,14 @@ layout_mode = 2
|
|||
layout_mode = 2
|
||||
text = "FPS: 1"
|
||||
|
||||
[node name="CheckButton" type="CheckButton" parent="."]
|
||||
offset_left = 18.0
|
||||
offset_top = 64.0
|
||||
offset_right = 140.0
|
||||
offset_bottom = 95.0
|
||||
button_pressed = true
|
||||
text = "Undistort"
|
||||
|
||||
[connection signal="item_selected" from="OptionButton" to="." method="button_item_selected"]
|
||||
[connection signal="item_selected" from="Devices" to="." method="device_selected"]
|
||||
[connection signal="toggled" from="CheckButton" to="." method="_on_undistort_button_toggled"]
|
||||
|
|
|
@ -59,12 +59,6 @@ func _process(delta):
|
|||
get_node("RightHand").show()
|
||||
set_hand_transform(frame.right_hand, UltraleapTypes.Chirality.Right)
|
||||
|
||||
if Input.is_action_just_pressed("next_device") and device_index < tracking.devices.size() - 1:
|
||||
device_index += 1
|
||||
|
||||
if Input.is_action_just_pressed("previous_device") and device_index > 0:
|
||||
device_index -= 1
|
||||
|
||||
if Input.is_action_just_pressed("toggle_rotation"):
|
||||
autorotate = !autorotate
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ extends CanvasLayer
|
|||
|
||||
signal tracking_mode_request(tracking_mode : UltraleapTypes.TrackingMode)
|
||||
signal device_selection(serial : String)
|
||||
signal undistort_toggled(enabled : bool)
|
||||
|
||||
var current_device : UltraleapDevice
|
||||
|
||||
|
@ -75,3 +76,6 @@ func device_selected(index):
|
|||
|
||||
func tracking_mode_to_str(tracking_mode : UltraleapTypes.TrackingMode):
|
||||
return ["Desktop", "HMD", "Screentop"][tracking_mode]
|
||||
|
||||
func _on_undistort_button_toggled(button_pressed):
|
||||
emit_signal("undistort_toggled", button_pressed)
|
||||
|
|
|
@ -5,75 +5,66 @@ enum Side { Left, Right }
|
|||
|
||||
@export var side : ImagePuller.Side = Side.Left
|
||||
@export var tracker : UltraleapHandTracking
|
||||
@export var img : Image
|
||||
@export var data_img : Image
|
||||
@export var distortion_texture : Texture2D
|
||||
|
||||
@onready var bg_data : PackedByteArray = PackedByteArray()
|
||||
@onready var data : PackedByteArray
|
||||
@onready var distortion_matrix : PackedVector2Array
|
||||
var img : Image = Image.new()
|
||||
var data_img : Image = Image.new()
|
||||
|
||||
var bg_data : PackedByteArray
|
||||
var data : PackedByteArray
|
||||
|
||||
var last_size : Vector2i = Vector2i(0, 0)
|
||||
var last_matrix_version = 999
|
||||
|
||||
var device_index : int = 0
|
||||
|
||||
|
||||
var view_size : Vector2i
|
||||
var image_size : Vector2i
|
||||
|
||||
|
||||
func _process(_delta):
|
||||
var view_size : Vector2i = get_viewport().size
|
||||
view_size.x /= 2
|
||||
|
||||
var length = min(view_size.x, view_size.y)
|
||||
|
||||
# Repaint on viewport size change
|
||||
if (view_size != last_size):
|
||||
_paint_black(view_size)
|
||||
last_size = view_size
|
||||
texture.set_image(img)
|
||||
if length == 0:
|
||||
return
|
||||
|
||||
if not Engine.is_editor_hint():
|
||||
if Input.is_action_just_pressed("next_device") and device_index < tracker.devices.size() - 1:
|
||||
device_index += 1
|
||||
|
||||
if Input.is_action_just_pressed("previous_device") and device_index > 0:
|
||||
device_index -= 1
|
||||
|
||||
if tracker.devices.size() == 0:
|
||||
return
|
||||
|
||||
var image : UltraleapImage
|
||||
var device : UltraleapDevice = tracker.devices[device_index]
|
||||
|
||||
# Get the image. If in view, left lens pointing you is the right lens for the camera
|
||||
if tracker.tracking_mode == UltraleapTypes.TrackingMode.Desktop:
|
||||
image = device.get_right_image() if side == Side.Left else device.get_left_image()
|
||||
else:
|
||||
image = device.get_right_image() if side == Side.Right else device.get_left_image()
|
||||
|
||||
|
||||
# Get image size
|
||||
image_size = image.get_size()
|
||||
data = image.get_data()
|
||||
flip_h = tracker.tracking_mode == UltraleapTypes.TrackingMode.Desktop
|
||||
|
||||
data = image.get_data()
|
||||
# Process the new distortion matrix if there's a new one
|
||||
if image.get_matrix_version() != last_matrix_version:
|
||||
last_matrix_version = image.get_matrix_version()
|
||||
distortion_matrix = image.get_distortion_matrix()
|
||||
var distortion_img : Image = Image.new()
|
||||
distortion_img.set_data(64, 64, false, Image.FORMAT_RGF, distortion_matrix.to_byte_array())
|
||||
distortion_texture.set_image(distortion_img)
|
||||
var distortion_matrix : PackedVector2Array = image.get_distortion_matrix()
|
||||
if distortion_matrix.size() == 64 * 64:
|
||||
process_distortion_matrix(distortion_matrix)
|
||||
last_matrix_version = image.get_matrix_version()
|
||||
|
||||
if data.size() == 384 * 384:
|
||||
data_img.set_data(384, 384, false, Image.FORMAT_L8, data)
|
||||
if data.size() > 0:
|
||||
data_img.set_data(image_size.x, image_size.y, false, Image.FORMAT_L8, data)
|
||||
data_img.resize(length, length, Image.INTERPOLATE_NEAREST)
|
||||
|
||||
var x : int = view_size.x - length if side == Side.Left else 0
|
||||
var y : int = (view_size.y - length) / 2
|
||||
|
||||
img.blit_rect(data_img, Rect2i(0, 0, length, length), Vector2(x, y))
|
||||
|
||||
|
||||
last_size = image_size
|
||||
texture.set_image(img)
|
||||
|
||||
func _paint_black(view_size):
|
||||
var _data_size = bg_data.resize((int)(view_size.x * view_size.y))
|
||||
bg_data.fill(1)
|
||||
|
||||
img.set_data(view_size.x, view_size.y, false, Image.FORMAT_L8, bg_data)
|
||||
|
||||
func on_hud_device_selection(serial):
|
||||
var inc : int = 0
|
||||
|
@ -83,8 +74,19 @@ func on_hud_device_selection(serial):
|
|||
return
|
||||
inc += 1
|
||||
|
||||
func _ready():
|
||||
var view_size : Vector2i = get_viewport().size
|
||||
|
||||
func process_distortion_matrix(matrix : PackedVector2Array):
|
||||
var distortion_img : Image = Image.new()
|
||||
distortion_img.set_data(64, 64, false, Image.FORMAT_RGF, matrix.to_byte_array())
|
||||
distortion_texture.set_image(distortion_img)
|
||||
|
||||
|
||||
func _on_view_size_changed(new_view_size : Vector2i):
|
||||
view_size = new_view_size
|
||||
view_size.x /= 2
|
||||
|
||||
_paint_black(view_size)
|
||||
|
||||
|
||||
func _paint_black(surf_size : Vector2i):
|
||||
bg_data.resize(surf_size.x * surf_size.y)
|
||||
img.set_data(surf_size.x, surf_size.y, false, Image.FORMAT_L8, bg_data)
|
||||
|
|
|
@ -4,18 +4,25 @@ extends Node3D
|
|||
@export var camera : Camera3D
|
||||
@export var hands : DebugHands
|
||||
@export_node_path("CanvasLayer") var hud
|
||||
@export var sky : ShaderMaterial
|
||||
|
||||
signal view_size_changed(view_size : Vector2i)
|
||||
|
||||
var current_device : UltraleapDevice
|
||||
var device_index : int
|
||||
var view_size : Vector2i
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass
|
||||
view_size = get_viewport().size
|
||||
emit_signal("view_size_changed", view_size)
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta):
|
||||
pass
|
||||
if view_size != get_viewport().size:
|
||||
view_size = get_viewport().size
|
||||
emit_signal("view_size_changed", view_size)
|
||||
|
||||
|
||||
func _on_device_selection(serial):
|
||||
|
@ -79,3 +86,7 @@ func move_camera_based_on_tracking_mode(tracking_mode : UltraleapTypes.TrackingM
|
|||
hands.rotation = Vector3(deg_to_rad(180), 0, 0)
|
||||
camera.position = hands.position
|
||||
camera.rotation = Vector3(deg_to_rad(-90), deg_to_rad(180), 0)
|
||||
|
||||
|
||||
func on_undistort_toggled(enabled):
|
||||
sky.set_shader_parameter("undistort", enabled)
|
||||
|
|
|
@ -13,9 +13,9 @@ uniform vec3 default_colour : source_color;
|
|||
uniform float gammaExponent = 1.0;
|
||||
|
||||
vec3 get_pixel(sampler2D left_texture, sampler2D right_texture, sampler2D left_disto, sampler2D right_disto, float is_left_side, vec2 uv, bool use_undistortion) {
|
||||
if (undistort) {
|
||||
if (use_undistortion) {
|
||||
vec2 distortionIndex = is_left_side * texture(left_disto, vec2(uv.x * 2.0f, 1.0 - uv.y)).xy
|
||||
+ (1.0 - is_left_side) * texture(right_disto, vec2(uv.x * 2.0f, 1.0 - uv.y)).xy;
|
||||
+ (1.0 - is_left_side) * texture(right_disto, vec2((uv.x - 0.5f) * 2.0f, 1.0 - uv.y)).xy;
|
||||
float hIndex = distortionIndex.r;
|
||||
float vIndex = distortionIndex.g;
|
||||
|
||||
|
@ -28,7 +28,7 @@ vec3 get_pixel(sampler2D left_texture, sampler2D right_texture, sampler2D left_d
|
|||
}
|
||||
}
|
||||
return is_left_side * texture(left_texture, vec2(uv.x * 2.0f, uv.y)).xyz
|
||||
+ (1.0 - is_left_side) * texture(right_texture, vec2(uv.x * 2.0f, uv.y)).xyz;
|
||||
+ (1.0 - is_left_side) * texture(right_texture, vec2((uv.x - 0.5f) * 2.0f, uv.y)).xyz;
|
||||
}
|
||||
|
||||
void sky() {
|
||||
|
|
|
@ -12,11 +12,15 @@ void UltraleapImage::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("get_data"), &UltraleapImage::get_data);
|
||||
ClassDB::bind_method(D_METHOD("get_distortion_matrix"), &UltraleapImage::get_distortion_matrix);
|
||||
ClassDB::bind_method(D_METHOD("get_matrix_version"), &UltraleapImage::get_matrix_version);
|
||||
ClassDB::bind_method(D_METHOD("get_size"), &UltraleapImage::get_size);
|
||||
}
|
||||
|
||||
void UltraleapImage::fill_images_data(Ref<UltraleapImage> left, Ref<UltraleapImage> right, const LEAP_IMAGE* images) {
|
||||
uint32_t width = images->properties.width;
|
||||
uint32_t height = images->properties.height;
|
||||
left->size.x = images->properties.width;
|
||||
left->size.y = images->properties.height;
|
||||
|
||||
right->size.x = (images + 1)->properties.width;
|
||||
right->size.y = (images + 1)->properties.height;
|
||||
|
||||
if (images->data == nullptr) {
|
||||
UtilityFunctions::print("Oops null pointer for the image");
|
||||
|
@ -27,7 +31,8 @@ void UltraleapImage::fill_images_data(Ref<UltraleapImage> left, Ref<UltraleapIma
|
|||
PackedInt64Array temp_left = PackedInt64Array();
|
||||
PackedInt64Array temp_right = PackedInt64Array();
|
||||
|
||||
uint32_t tot_pixels = height * width;
|
||||
// From there we'll assume that both images have the same number of pixels
|
||||
uint32_t tot_pixels = left->size.x * left->size.y;
|
||||
|
||||
uint64_t* data_left = (uint64_t*)((uint8_t*)images->data + images->offset);
|
||||
uint64_t* data_right = (uint64_t*)((uint8_t*)(images + 1)->data + (images + 1)->offset);
|
||||
|
|
|
@ -19,12 +19,14 @@ class UltraleapImage : public Resource {
|
|||
public:
|
||||
PackedByteArray data = PackedByteArray();
|
||||
PackedVector2Array distortion_matrix = PackedVector2Array();
|
||||
Vector2i size = Vector2i();
|
||||
|
||||
// Getters / Setters
|
||||
|
||||
PackedByteArray get_data() { return data; }
|
||||
PackedVector2Array get_distortion_matrix() { return distortion_matrix; }
|
||||
uint64_t get_matrix_version() { return matrix_version; }
|
||||
Vector2i get_size() { return size; }
|
||||
|
||||
static void fill_images_data(Ref<UltraleapImage> left, Ref<UltraleapImage> right, const LEAP_IMAGE* images);
|
||||
|
||||
|
|
Loading…
Reference in a new issue