Skip to content

Commit d1da3fc

Browse files
committed
Implement XR_ANDROID_raycast
1 parent 806eecc commit d1da3fc

24 files changed

+1265
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="OpenXRAndroidHitResult" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
3+
<brief_description>
4+
Contains information related to a raycast hit data (from [code]XR_ANDROID_raycast[/code]).
5+
</brief_description>
6+
<description>
7+
Contains information related to a raycast hit data (from [code]XR_ANDROID_raycast[/code]).
8+
See also [OpenXRAndroidRaycastExtension]
9+
</description>
10+
<tutorials>
11+
</tutorials>
12+
<methods>
13+
<method name="get_pose" qualifiers="const">
14+
<return type="Transform3D" />
15+
<description>
16+
Get the [Transform3D] of the raycast hit.
17+
</description>
18+
</method>
19+
<method name="get_tracker" qualifiers="const">
20+
<return type="OpenXRAndroidTracker" />
21+
<description>
22+
Get the [OpenXRAndroidTracker] that is associated with hit result.
23+
</description>
24+
</method>
25+
</methods>
26+
</class>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="OpenXRAndroidRaycastExtension" inherits="OpenXRExtensionWrapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
3+
<brief_description>
4+
Implementation for raycasting with the xr runtime (from [code]XR_ANDROID_raycast[/code]).
5+
</brief_description>
6+
<description>
7+
Implementation for raycasting with the xr runtime (from [code]XR_ANDROID_raycast[/code]).
8+
</description>
9+
<tutorials>
10+
</tutorials>
11+
<methods>
12+
<method name="raycast">
13+
<return type="OpenXRAndroidHitResult[]" />
14+
<param index="0" name="trackable_types" type="Array" />
15+
<param index="1" name="origin" type="Vector3" />
16+
<param index="2" name="trajectory" type="Vector3" />
17+
<param index="3" name="max_results" type="int" />
18+
<description>
19+
Performs a raycast against the [Array] of [enum TrackableType]s, from [param origin] in the direction of [param trajectory].
20+
Returns an [Array] of up to [param max_results] [OpenXRAndroidHitResult]s.
21+
</description>
22+
</method>
23+
</methods>
24+
<constants>
25+
<constant name="TRACKABLE_TYPE_PLANE" value="0" enum="TrackableType">
26+
</constant>
27+
<constant name="TRACKABLE_TYPE_DEPTH" value="1" enum="TrackableType">
28+
</constant>
29+
</constants>
30+
</class>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**************************************************************************/
2+
/* openxr_android_hit_result.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "classes/openxr_android_hit_result.h"
32+
33+
#include <godot_cpp/variant/utility_functions.hpp>
34+
35+
#include "extensions/openxr_android_trackables_extension.h"
36+
37+
using namespace godot;
38+
39+
OpenXRAndroidHitResult::OpenXRAndroidHitResult() {}
40+
OpenXRAndroidHitResult::~OpenXRAndroidHitResult() {}
41+
42+
bool OpenXRAndroidHitResult::set_hit_result(const XrRaycastHitResultANDROID &p_hit_result) {
43+
switch ((int)p_hit_result.type) {
44+
case XR_TRACKABLE_TYPE_PLANE_ANDROID: {
45+
OpenXRAndroidTrackablesExtension *wrapper = OpenXRAndroidTrackablesExtension::get_singleton();
46+
if (wrapper == nullptr) {
47+
UtilityFunctions::printerr("OpenXR: expected non-null OpenXRAndroidTrackablesExtension; returning null plane trackable");
48+
trackable = Ref<OpenXRAndroidTracker>();
49+
} else {
50+
// NOTE: don't bothing updating the tracker (if it already exists) since perhaps this
51+
// tracker's data is ultimately ignored by the caller. Like any other OpenXRAndroidTracker,
52+
// calling a getter function will ensure latest state if it's stale.
53+
trackable = wrapper->get_or_create_tracker_and_update(p_hit_result.trackable, XR_TRACKABLE_TYPE_PLANE_ANDROID, false);
54+
}
55+
break;
56+
}
57+
58+
case XR_TRACKABLE_TYPE_DEPTH_ANDROID:
59+
// DEPTH hit results should not return a trackable
60+
if (p_hit_result.trackable != XR_NULL_TRACKABLE_ANDROID) {
61+
UtilityFunctions::print_verbose("OpenXR: unexpected trackable received for DEPTH hit result");
62+
}
63+
64+
trackable = Ref<OpenXRAndroidTracker>();
65+
break;
66+
67+
case XR_TRACKABLE_TYPE_NOT_VALID_ANDROID:
68+
case XR_TRACKABLE_TYPE_OBJECT_ANDROID:
69+
case XR_TRACKABLE_TYPE_MAX_ENUM_ANDROID:
70+
default:
71+
UtilityFunctions::printerr("OpenXR: unsupported hit trackable type: ", p_hit_result.type);
72+
return false;
73+
}
74+
75+
pose.origin.x = p_hit_result.pose.position.x;
76+
pose.origin.y = p_hit_result.pose.position.y;
77+
pose.origin.z = p_hit_result.pose.position.z;
78+
pose.basis = Basis{ Quaternion{ p_hit_result.pose.orientation.x, p_hit_result.pose.orientation.y, p_hit_result.pose.orientation.z, p_hit_result.pose.orientation.w } };
79+
80+
return true;
81+
}
82+
83+
Ref<OpenXRAndroidTracker> OpenXRAndroidHitResult::get_tracker() const {
84+
return trackable;
85+
}
86+
87+
const Transform3D &OpenXRAndroidHitResult::get_pose() const {
88+
return pose;
89+
}
90+
91+
void OpenXRAndroidHitResult::_bind_methods() {
92+
ClassDB::bind_method(D_METHOD("get_tracker"), &OpenXRAndroidHitResult::get_tracker);
93+
ClassDB::bind_method(D_METHOD("get_pose"), &OpenXRAndroidHitResult::get_pose);
94+
}

0 commit comments

Comments
 (0)