You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Rework XR positional trackers
This commit is contained in:
@@ -37,29 +37,37 @@ void XRPositionalTracker::_bind_methods() {
|
||||
BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT);
|
||||
BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT);
|
||||
|
||||
// this class is read only from GDScript, so we only have access to getters..
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_type"), &XRPositionalTracker::get_tracker_type);
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_id"), &XRPositionalTracker::get_tracker_id);
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_name"), &XRPositionalTracker::get_tracker_name);
|
||||
ClassDB::bind_method(D_METHOD("get_joy_id"), &XRPositionalTracker::get_joy_id);
|
||||
ClassDB::bind_method(D_METHOD("is_tracking_orientation"), &XRPositionalTracker::is_tracking_orientation);
|
||||
ClassDB::bind_method(D_METHOD("get_orientation"), &XRPositionalTracker::get_orientation);
|
||||
ClassDB::bind_method(D_METHOD("is_tracking_position"), &XRPositionalTracker::is_tracking_position);
|
||||
ClassDB::bind_method(D_METHOD("get_position"), &XRPositionalTracker::get_position);
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);
|
||||
ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &XRPositionalTracker::get_transform);
|
||||
ClassDB::bind_method(D_METHOD("get_mesh"), &XRPositionalTracker::get_mesh);
|
||||
ClassDB::bind_method(D_METHOD("set_tracker_type", "type"), &XRPositionalTracker::set_tracker_type);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "type"), "set_tracker_type", "get_tracker_type");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_name"), &XRPositionalTracker::get_tracker_name);
|
||||
ClassDB::bind_method(D_METHOD("set_tracker_name", "name"), &XRPositionalTracker::set_tracker_name);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "name"), "set_tracker_name", "get_tracker_name");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_desc"), &XRPositionalTracker::get_tracker_desc);
|
||||
ClassDB::bind_method(D_METHOD("set_tracker_desc", "description"), &XRPositionalTracker::set_tracker_desc);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_tracker_desc", "get_tracker_desc");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);
|
||||
ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose);
|
||||
ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose);
|
||||
ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose);
|
||||
ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity"), &XRPositionalTracker::set_pose);
|
||||
ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input);
|
||||
ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input);
|
||||
ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));
|
||||
ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));
|
||||
ADD_SIGNAL(MethodInfo("input_value_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));
|
||||
ADD_SIGNAL(MethodInfo("input_axis_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector")));
|
||||
|
||||
// these functions we don't want to expose to normal users but do need to be callable from GDNative
|
||||
ClassDB::bind_method(D_METHOD("_set_tracker_type", "type"), &XRPositionalTracker::set_tracker_type);
|
||||
ClassDB::bind_method(D_METHOD("_set_tracker_name", "name"), &XRPositionalTracker::set_tracker_name);
|
||||
ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &XRPositionalTracker::set_joy_id);
|
||||
ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &XRPositionalTracker::set_orientation);
|
||||
ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &XRPositionalTracker::set_rw_position);
|
||||
ClassDB::bind_method(D_METHOD("_set_mesh", "mesh"), &XRPositionalTracker::set_mesh);
|
||||
ClassDB::bind_method(D_METHOD("get_rumble"), &XRPositionalTracker::get_rumble);
|
||||
ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &XRPositionalTracker::set_rumble);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rumble"), "set_rumble", "get_rumble");
|
||||
};
|
||||
|
||||
@@ -67,13 +75,6 @@ void XRPositionalTracker::set_tracker_type(XRServer::TrackerType p_type) {
|
||||
if (type != p_type) {
|
||||
type = p_type;
|
||||
hand = XRPositionalTracker::TRACKER_HAND_UNKNOWN;
|
||||
|
||||
XRServer *xr_server = XRServer::get_singleton();
|
||||
ERR_FAIL_NULL(xr_server);
|
||||
|
||||
// get a tracker id for our type
|
||||
// note if this is a controller this will be 3 or higher but we may change it later.
|
||||
tracker_id = xr_server->get_free_tracker_id_for_type(p_type);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -81,7 +82,8 @@ XRServer::TrackerType XRPositionalTracker::get_tracker_type() const {
|
||||
return type;
|
||||
};
|
||||
|
||||
void XRPositionalTracker::set_tracker_name(const String &p_name) {
|
||||
void XRPositionalTracker::set_tracker_name(const StringName &p_name) {
|
||||
// Note: this should not be changed after the tracker is registered with the XRServer!
|
||||
name = p_name;
|
||||
};
|
||||
|
||||
@@ -89,85 +91,13 @@ StringName XRPositionalTracker::get_tracker_name() const {
|
||||
return name;
|
||||
};
|
||||
|
||||
int XRPositionalTracker::get_tracker_id() const {
|
||||
return tracker_id;
|
||||
};
|
||||
void XRPositionalTracker::set_tracker_desc(const String &p_desc) {
|
||||
description = p_desc;
|
||||
}
|
||||
|
||||
void XRPositionalTracker::set_joy_id(int p_joy_id) {
|
||||
joy_id = p_joy_id;
|
||||
};
|
||||
|
||||
int XRPositionalTracker::get_joy_id() const {
|
||||
return joy_id;
|
||||
};
|
||||
|
||||
bool XRPositionalTracker::is_tracking_orientation() const {
|
||||
return tracking_orientation;
|
||||
};
|
||||
|
||||
void XRPositionalTracker::set_orientation(const Basis &p_orientation) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
tracking_orientation = true; // obviously we have this
|
||||
orientation = p_orientation;
|
||||
};
|
||||
|
||||
Basis XRPositionalTracker::get_orientation() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
return orientation;
|
||||
};
|
||||
|
||||
bool XRPositionalTracker::is_tracking_position() const {
|
||||
return tracking_position;
|
||||
};
|
||||
|
||||
void XRPositionalTracker::set_position(const Vector3 &p_position) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
XRServer *xr_server = XRServer::get_singleton();
|
||||
ERR_FAIL_NULL(xr_server);
|
||||
real_t world_scale = xr_server->get_world_scale();
|
||||
ERR_FAIL_COND(world_scale == 0);
|
||||
|
||||
tracking_position = true; // obviously we have this
|
||||
rw_position = p_position / world_scale;
|
||||
};
|
||||
|
||||
Vector3 XRPositionalTracker::get_position() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
XRServer *xr_server = XRServer::get_singleton();
|
||||
ERR_FAIL_NULL_V(xr_server, rw_position);
|
||||
real_t world_scale = xr_server->get_world_scale();
|
||||
|
||||
return rw_position * world_scale;
|
||||
};
|
||||
|
||||
void XRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
tracking_position = true; // obviously we have this
|
||||
rw_position = p_rw_position;
|
||||
};
|
||||
|
||||
Vector3 XRPositionalTracker::get_rw_position() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
return rw_position;
|
||||
};
|
||||
|
||||
void XRPositionalTracker::set_mesh(const Ref<Mesh> &p_mesh) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
mesh = p_mesh;
|
||||
};
|
||||
|
||||
Ref<Mesh> XRPositionalTracker::get_mesh() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
|
||||
return mesh;
|
||||
};
|
||||
String XRPositionalTracker::get_tracker_desc() const {
|
||||
return description;
|
||||
}
|
||||
|
||||
XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const {
|
||||
return hand;
|
||||
@@ -182,33 +112,98 @@ void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHan
|
||||
ERR_FAIL_COND((type != XRServer::TRACKER_CONTROLLER) && (p_hand != XRPositionalTracker::TRACKER_HAND_UNKNOWN));
|
||||
|
||||
hand = p_hand;
|
||||
if (hand == XRPositionalTracker::TRACKER_HAND_LEFT) {
|
||||
if (!xr_server->is_tracker_id_in_use_for_type(type, 1)) {
|
||||
tracker_id = 1;
|
||||
};
|
||||
} else if (hand == XRPositionalTracker::TRACKER_HAND_RIGHT) {
|
||||
if (!xr_server->is_tracker_id_in_use_for_type(type, 2)) {
|
||||
tracker_id = 2;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Transform3D XRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const {
|
||||
Transform3D new_transform;
|
||||
bool XRPositionalTracker::has_pose(const StringName &p_action_name) const {
|
||||
return poses.has(p_action_name);
|
||||
}
|
||||
|
||||
new_transform.basis = get_orientation();
|
||||
new_transform.origin = get_position();
|
||||
Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const {
|
||||
Ref<XRPose> pose;
|
||||
|
||||
if (p_adjust_by_reference_frame) {
|
||||
XRServer *xr_server = XRServer::get_singleton();
|
||||
ERR_FAIL_NULL_V(xr_server, new_transform);
|
||||
if (poses.has(p_action_name)) {
|
||||
pose = poses[p_action_name];
|
||||
}
|
||||
|
||||
new_transform = xr_server->get_reference_frame() * new_transform;
|
||||
};
|
||||
return pose;
|
||||
}
|
||||
|
||||
return new_transform;
|
||||
};
|
||||
void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) {
|
||||
// only update this if we were tracking this pose
|
||||
if (poses.has(p_action_name)) {
|
||||
// We just set tracking data as invalid, we leave our current transform and velocity data as is so controllers don't suddenly jump to origin.
|
||||
poses[p_action_name]->set_has_tracking_data(false);
|
||||
}
|
||||
}
|
||||
|
||||
void XRPositionalTracker::set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity) {
|
||||
Ref<XRPose> new_pose;
|
||||
|
||||
new_pose.instantiate();
|
||||
new_pose->set_name(p_action_name);
|
||||
new_pose->set_has_tracking_data(true);
|
||||
new_pose->set_transform(p_transform);
|
||||
new_pose->set_linear_velocity(p_linear_velocity);
|
||||
new_pose->set_angular_velocity(p_angular_velocity);
|
||||
|
||||
poses[p_action_name] = new_pose;
|
||||
emit_signal("pose_changed", new_pose);
|
||||
|
||||
// TODO discuss whether we also want to create and emit an InputEventXRPose event
|
||||
}
|
||||
|
||||
Variant XRPositionalTracker::get_input(const StringName &p_action_name) const {
|
||||
if (inputs.has(p_action_name)) {
|
||||
return inputs[p_action_name];
|
||||
} else {
|
||||
return Variant();
|
||||
}
|
||||
}
|
||||
|
||||
void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) {
|
||||
bool changed = false;
|
||||
|
||||
// XR inputs
|
||||
|
||||
if (inputs.has(p_action_name)) {
|
||||
changed = inputs[p_action_name] != p_value;
|
||||
} else {
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
// store the new value
|
||||
inputs[p_action_name] = p_value;
|
||||
|
||||
// emit signals to let the rest of the world know
|
||||
switch (p_value.get_type()) {
|
||||
case Variant::BOOL: {
|
||||
bool pressed = p_value;
|
||||
if (pressed) {
|
||||
emit_signal("button_pressed", p_action_name);
|
||||
} else {
|
||||
emit_signal("button_released", p_action_name);
|
||||
}
|
||||
|
||||
// TODO discuss whether we also want to create and emit an InputEventXRButton event
|
||||
} break;
|
||||
case Variant::FLOAT: {
|
||||
emit_signal("input_value_changed", p_action_name, p_value);
|
||||
|
||||
// TODO discuss whether we also want to create and emit an InputEventXRValue event
|
||||
} break;
|
||||
case Variant::VECTOR2: {
|
||||
emit_signal("input_axis_changed", p_action_name, p_value);
|
||||
|
||||
// TODO discuss whether we also want to create and emit an InputEventXRAxis event
|
||||
} break;
|
||||
default: {
|
||||
// ???
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
real_t XRPositionalTracker::get_rumble() const {
|
||||
return rumble;
|
||||
@@ -225,10 +220,6 @@ void XRPositionalTracker::set_rumble(real_t p_rumble) {
|
||||
XRPositionalTracker::XRPositionalTracker() {
|
||||
type = XRServer::TRACKER_UNKNOWN;
|
||||
name = "Unknown";
|
||||
joy_id = -1;
|
||||
tracker_id = 0;
|
||||
tracking_orientation = false;
|
||||
tracking_position = false;
|
||||
hand = TRACKER_HAND_UNKNOWN;
|
||||
rumble = 0.0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user