Skip to content

Commit c066aac

Browse files
committed
Add snippets for ARCore for Jetpack XR Overview page: anchors, persistent anchors, planes
1 parent 0217ca1 commit c066aac

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.arcore
18+
19+
import androidx.xr.arcore.Anchor
20+
import androidx.xr.arcore.AnchorCreateSuccess
21+
import androidx.xr.runtime.Session
22+
import java.util.UUID
23+
24+
private suspend fun persistAnchor(anchor: Anchor) {
25+
// [START androidxr_arcore_anchor_persist]
26+
val uuid = anchor.persist()
27+
// [END androidxr_arcore_anchor_persist]
28+
}
29+
30+
private fun loadAnchor(session: Session, uuid: UUID) {
31+
// [START androidxr_arcore_anchor_load]
32+
when (val result = Anchor.load(session, uuid)) {
33+
is AnchorCreateSuccess -> {
34+
// Loading was successful. The anchor is stored in result.anchor.
35+
}
36+
else -> {
37+
// handle failure
38+
}
39+
}
40+
// [END androidxr_arcore_anchor_load]
41+
}
42+
43+
private fun unpersistAnchor(session: Session, uuid: UUID) {
44+
// [START androidxr_arcore_anchor_unpersist]
45+
Anchor.unpersist(session, uuid)
46+
// [END androidxr_arcore_anchor_unpersist]
47+
}
48+
49+
private fun getPersistedAnchorUuids(session: Session) {
50+
// [START androidxr_arcore_anchor_get_uuids]
51+
val uuids = Anchor.getPersistedAnchorUuids(session)
52+
// [END androidxr_arcore_anchor_get_uuids]
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.arcore
18+
19+
import androidx.xr.arcore.Anchor
20+
import androidx.xr.arcore.AnchorCreateSuccess
21+
import androidx.xr.arcore.Trackable
22+
import androidx.xr.runtime.Session
23+
import androidx.xr.runtime.math.Pose
24+
import androidx.xr.scenecore.AnchorEntity
25+
import androidx.xr.scenecore.Entity
26+
27+
private fun createAnchorAtPose(session: Session, pose: Pose) {
28+
val pose = Pose()
29+
// [START androidxr_arcore_anchor_create]
30+
when (val result = Anchor.create(session, pose)) {
31+
is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
32+
else -> { /* handle failure */ }
33+
}
34+
// [END androidxr_arcore_anchor_create]
35+
}
36+
37+
private fun createAnchorAtTrackable(trackable: Trackable<*>) {
38+
val pose = Pose()
39+
// [START androidxr_arcore_anchor_create_trackable]
40+
when (val result = trackable.createAnchor(pose)) {
41+
is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
42+
else -> { /* handle failure */ }
43+
}
44+
// [END androidxr_arcore_anchor_create_trackable]
45+
}
46+
47+
private fun attachEntityToAnchor(
48+
session: androidx.xr.scenecore.Session,
49+
entity: Entity,
50+
anchor: Anchor
51+
) {
52+
// [START androidxr_arcore_entity_tracks_anchor]
53+
AnchorEntity.create(session, anchor).apply {
54+
setParent(session.activitySpace)
55+
addChild(entity)
56+
}
57+
// [END androidxr_arcore_entity_tracks_anchor]
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.arcore
18+
19+
import androidx.xr.arcore.Plane
20+
import androidx.xr.runtime.Session
21+
import androidx.xr.runtime.math.Pose
22+
import androidx.xr.runtime.math.Ray
23+
24+
private suspend fun subscribePlanes(session: Session) {
25+
// [START androidxr_arcore_planes_subscribe]
26+
Plane.subscribe(session).collect { planes ->
27+
// Planes have changed; update plane rendering
28+
}
29+
// [END androidxr_arcore_planes_subscribe]
30+
}
31+
32+
private fun hitTestTable(session: Session) {
33+
val scenecoreSession: androidx.xr.scenecore.Session = null!!
34+
val pose = scenecoreSession.spatialUser.head?.transformPoseTo(Pose(), scenecoreSession.perceptionSpace) ?: return
35+
val ray = Ray(pose.translation, pose.forward)
36+
// [START androidxr_arcore_hitTest]
37+
val results = androidx.xr.arcore.hitTest(session, ray)
38+
// When interested in the first Table hit:
39+
val tableHit = results.firstOrNull {
40+
val trackable = it.trackable
41+
trackable is Plane && trackable.state.value.label == Plane.Label.Table
42+
}
43+
// [END androidxr_arcore_hitTest]
44+
}

0 commit comments

Comments
 (0)