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