Skip to content

Commit 142d9f5

Browse files
committed
Port entities snippets from DAC
1 parent 4493417 commit 142d9f5

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.example.xr.scenecore
2+
3+
import androidx.xr.runtime.math.Pose
4+
import androidx.xr.runtime.math.Quaternion
5+
import androidx.xr.runtime.math.Vector3
6+
import androidx.xr.scenecore.AnchorPlacement
7+
import androidx.xr.scenecore.Dimensions
8+
import androidx.xr.scenecore.Entity
9+
import androidx.xr.scenecore.InputEvent
10+
import androidx.xr.scenecore.InteractableComponent
11+
import androidx.xr.scenecore.MovableComponent
12+
import androidx.xr.scenecore.PlaneSemantic
13+
import androidx.xr.scenecore.PlaneType
14+
import androidx.xr.scenecore.ResizableComponent
15+
import androidx.xr.scenecore.Session
16+
import java.util.concurrent.Executors
17+
18+
private fun setPoseExample(entity: Entity) {
19+
// [START androidxr_scenecore_entity_setPoseExample]
20+
// Place the entity forward 2 meters
21+
val newPosition = Vector3(0f, 0f, -2f)
22+
// Rotate the entity by 180 degrees on the up axis (upside-down)
23+
val newOrientation = Quaternion.fromEulerAngles(0f, 0f, 180f)
24+
// Update the position and rotation on the entity
25+
entity.setPose(Pose(newPosition, newOrientation))
26+
// [END androidxr_scenecore_entity_setPoseExample]
27+
}
28+
29+
private fun hideEntity(entity: Entity) {
30+
// [START androidxr_scenecore_entity_hideEntity]
31+
// Hide the entity
32+
entity.setHidden(true)
33+
// [END androidxr_scenecore_entity_hideEntity]
34+
}
35+
36+
private fun entitySetScale(entity: Entity) {
37+
// [START androidxr_scenecore_entity_entitySetScale]
38+
// Double the size of the entity
39+
entity.setScale(2f)
40+
// [END androidxr_scenecore_entity_entitySetScale]
41+
}
42+
43+
private fun moveableComponentExample(session: Session, entity: Entity) {
44+
// [START androidxr_scenecore_moveableComponentExample]
45+
val anchorPlacement = AnchorPlacement.createForPlanes(
46+
planeTypeFilter = setOf(PlaneSemantic.FLOOR, PlaneSemantic.TABLE),
47+
planeSemanticFilter = setOf(PlaneType.VERTICAL)
48+
)
49+
50+
val movableComponent = MovableComponent.create(
51+
session = session,
52+
systemMovable = false,
53+
scaleInZ = false,
54+
anchorPlacement = setOf(anchorPlacement)
55+
)
56+
entity.addComponent(movableComponent)
57+
// [END androidxr_scenecore_moveableComponentExample]
58+
}
59+
60+
private fun resizableComponentExample(session: Session, entity: Entity) {
61+
// [START androidxr_scenecore_resizableComponentExample]
62+
val resizableComponent = ResizableComponent.create(session)
63+
resizableComponent.minimumSize = Dimensions(177f, 100f, 1f)
64+
resizableComponent.fixedAspectRatio = 16f / 9f // Specify a 16:9 aspect ratio
65+
entity.addComponent(resizableComponent)
66+
// [END androidxr_scenecore_resizableComponentExample]
67+
}
68+
69+
private fun interactableComponentExample(session: Session, entity: Entity) {
70+
// [START androidxr_scenecore_interactableComponentExample]
71+
val executor = Executors.newSingleThreadExecutor()
72+
val interactableComponent = InteractableComponent.create(session, executor) {
73+
//when the user disengages with the entity with their hands
74+
if (it.source == InputEvent.SOURCE_HANDS && it.action == InputEvent.ACTION_UP) {
75+
// increase size with right hand and decrease with left
76+
if (it.pointerType == InputEvent.POINTER_TYPE_RIGHT) {
77+
entity.setScale(1.5f)
78+
} else if (it.pointerType == InputEvent.POINTER_TYPE_LEFT) {
79+
entity.setScale(0.5f)
80+
}
81+
}
82+
}
83+
entity.addComponent(interactableComponent)
84+
// [END androidxr_scenecore_interactableComponentExample]
85+
}

0 commit comments

Comments
 (0)