Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply reference frame transform to OpenXRCompositionLayer nodes #92339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/classes/XRServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
Emitted when an interface is removed.
</description>
</signal>
<signal name="reference_frame_changed">
<description>
Emitted when the reference frame transform changes.
</description>
</signal>
<signal name="tracker_added">
<param index="0" name="tracker_name" type="StringName" />
<param index="1" name="type" type="int" />
Expand Down
10 changes: 10 additions & 0 deletions modules/openxr/scene/openxr_composition_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ void OpenXRCompositionLayer::update_fallback_mesh() {
should_update_fallback_mesh = true;
}

XrPosef OpenXRCompositionLayer::get_openxr_pose() {
Transform3D reference_frame = XRServer::get_singleton()->get_reference_frame();
Transform3D transform = reference_frame.inverse() * get_transform();
Quaternion quat(transform.basis.orthonormalized());
return {
{ (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w },
{ (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z }
};
}

bool OpenXRCompositionLayer::is_viewport_in_use(SubViewport *p_viewport) {
for (const OpenXRCompositionLayer *other_composition_layer : composition_layer_nodes) {
if (other_composition_layer != this && other_composition_layer->is_inside_tree() && other_composition_layer->get_layer_viewport() == p_viewport) {
Expand Down
2 changes: 2 additions & 0 deletions modules/openxr/scene/openxr_composition_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class OpenXRCompositionLayer : public Node3D {

void update_fallback_mesh();

XrPosef get_openxr_pose();

static Vector<OpenXRCompositionLayer *> composition_layer_nodes;
bool is_viewport_in_use(SubViewport *p_viewport);

Expand Down
10 changes: 6 additions & 4 deletions modules/openxr/scene/openxr_composition_layer_cylinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() {
aspect_ratio, // aspectRatio
};
openxr_layer_provider = memnew(OpenXRViewportCompositionLayerProvider((XrCompositionLayerBaseHeader *)&composition_layer));
XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerCylinder::update_transform));
}

OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() {
Expand Down Expand Up @@ -131,14 +132,15 @@ Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() {
void OpenXRCompositionLayerCylinder::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
Transform3D transform = get_transform();
Quaternion quat(transform.basis.orthonormalized());
composition_layer.pose.orientation = { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w };
composition_layer.pose.position = { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z };
update_transform();
} break;
}
}

void OpenXRCompositionLayerCylinder::update_transform() {
composition_layer.pose = get_openxr_pose();
}

void OpenXRCompositionLayerCylinder::set_radius(float p_radius) {
ERR_FAIL_COND(p_radius <= 0);
radius = p_radius;
Expand Down
2 changes: 2 additions & 0 deletions modules/openxr/scene/openxr_composition_layer_cylinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class OpenXRCompositionLayerCylinder : public OpenXRCompositionLayer {

void _notification(int p_what);

void update_transform();

virtual Ref<Mesh> _create_fallback_mesh() override;

public:
Expand Down
10 changes: 6 additions & 4 deletions modules/openxr/scene/openxr_composition_layer_equirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ OpenXRCompositionLayerEquirect::OpenXRCompositionLayerEquirect() {
-lower_vertical_angle, // lowerVerticalAngle
};
openxr_layer_provider = memnew(OpenXRViewportCompositionLayerProvider((XrCompositionLayerBaseHeader *)&composition_layer));
XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerEquirect::update_transform));
}

OpenXRCompositionLayerEquirect::~OpenXRCompositionLayerEquirect() {
Expand Down Expand Up @@ -139,14 +140,15 @@ Ref<Mesh> OpenXRCompositionLayerEquirect::_create_fallback_mesh() {
void OpenXRCompositionLayerEquirect::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
Transform3D transform = get_transform();
Quaternion quat(transform.basis.orthonormalized());
composition_layer.pose.orientation = { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w };
composition_layer.pose.position = { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z };
update_transform();
} break;
}
}

void OpenXRCompositionLayerEquirect::update_transform() {
composition_layer.pose = get_openxr_pose();
}

void OpenXRCompositionLayerEquirect::set_radius(float p_radius) {
ERR_FAIL_COND(p_radius <= 0);
radius = p_radius;
Expand Down
2 changes: 2 additions & 0 deletions modules/openxr/scene/openxr_composition_layer_equirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class OpenXRCompositionLayerEquirect : public OpenXRCompositionLayer {

void _notification(int p_what);

void update_transform();

virtual Ref<Mesh> _create_fallback_mesh() override;

public:
Expand Down
10 changes: 6 additions & 4 deletions modules/openxr/scene/openxr_composition_layer_quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() {
{ (float)quad_size.x, (float)quad_size.y }, // size
};
openxr_layer_provider = memnew(OpenXRViewportCompositionLayerProvider((XrCompositionLayerBaseHeader *)&composition_layer));
XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerQuad::update_transform));
}

OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() {
Expand All @@ -72,14 +73,15 @@ Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() {
void OpenXRCompositionLayerQuad::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
Transform3D transform = get_transform();
Quaternion quat(transform.basis.orthonormalized());
composition_layer.pose.orientation = { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w };
composition_layer.pose.position = { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z };
update_transform();
} break;
}
}

void OpenXRCompositionLayerQuad::update_transform() {
composition_layer.pose = get_openxr_pose();
}

void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) {
quad_size = p_size;
composition_layer.size = { (float)quad_size.x, (float)quad_size.y };
Expand Down
2 changes: 2 additions & 0 deletions modules/openxr/scene/openxr_composition_layer_quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class OpenXRCompositionLayerQuad : public OpenXRCompositionLayer {

void _notification(int p_what);

void update_transform();

virtual Ref<Mesh> _create_fallback_mesh() override;

public:
Expand Down
4 changes: 4 additions & 0 deletions servers/xr_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void XRServer::_bind_methods() {
BIND_ENUM_CONSTANT(RESET_BUT_KEEP_TILT);
BIND_ENUM_CONSTANT(DONT_RESET_ROTATION);

ADD_SIGNAL(MethodInfo("reference_frame_changed"));

ADD_SIGNAL(MethodInfo("interface_added", PropertyInfo(Variant::STRING_NAME, "interface_name")));
ADD_SIGNAL(MethodInfo("interface_removed", PropertyInfo(Variant::STRING_NAME, "interface_name")));

Expand Down Expand Up @@ -213,11 +215,13 @@ void XRServer::center_on_hmd(RotationMode p_rotation_mode, bool p_keep_height) {

reference_frame = new_reference_frame.inverse();
set_render_reference_frame(reference_frame);
emit_signal(SNAME("reference_frame_changed"));
}

void XRServer::clear_reference_frame() {
reference_frame = Transform3D();
set_render_reference_frame(reference_frame);
emit_signal(SNAME("reference_frame_changed"));
}

void XRServer::_set_render_reference_frame(const Transform3D &p_reference_frame) {
Expand Down
Loading