59 lines
1.6 KiB
GDScript
59 lines
1.6 KiB
GDScript
extends Node
|
|
class_name PinchDetector
|
|
|
|
@export var tracking : UltraleapDeviceNode
|
|
@export var handedness : UltraleapTypes.Chirality
|
|
@export var pinch_threshold : float = 0.7
|
|
@export var unpinch_threshold : float = 0.6
|
|
@export var debug : bool = true
|
|
@export var display : Node3D
|
|
|
|
signal pinched
|
|
signal unpinched
|
|
|
|
var is_pinched : bool = false
|
|
var hand : UltraleapHand = null
|
|
|
|
var pinch_transform : Transform3D = Transform3D.IDENTITY
|
|
|
|
func _ready():
|
|
pass
|
|
|
|
func _process(_delta):
|
|
if tracking == null:
|
|
return
|
|
|
|
var frame : UltraleapFrame = tracking.get_last_frame()
|
|
|
|
if frame == null:
|
|
return
|
|
|
|
var hand_is_visible : bool = ( frame.is_left_hand_visible && handedness == UltraleapTypes.Chirality.Left
|
|
|| frame.is_right_hand_visible && handedness == UltraleapTypes.Chirality.Right)
|
|
|
|
if !hand_is_visible:
|
|
return
|
|
|
|
hand = frame.left_hand if handedness == UltraleapTypes.Chirality.Left else frame.right_hand
|
|
|
|
var pinch_strength : float = hand.pinch_strength
|
|
|
|
var pinch_position : Vector3 = (hand.index.distal.next_joint + hand.thumb.distal.next_joint) / 2.0
|
|
var pinch_base : Basis = Basis(hand.thumb.distal.orientation.slerp(hand.index.distal.orientation, 0.5))
|
|
|
|
pinch_transform = tracking.global_transform * Transform3D(pinch_base, pinch_position)
|
|
|
|
if pinch_strength > pinch_threshold && !is_pinched:
|
|
is_pinched = true
|
|
pinched.emit()
|
|
|
|
if pinch_strength < unpinch_threshold && is_pinched:
|
|
is_pinched = false
|
|
unpinched.emit()
|
|
|
|
if debug:
|
|
display.set_pinch_strength(pinch_strength)
|
|
display.set_pinch_position(pinch_transform.origin)
|
|
display.set_pinch_rotation(pinch_transform.basis.get_rotation_quaternion())
|
|
|