使用者授權頭部追蹤後,應用程式就能透過 ARCore for Jetpack XR 擷取頭部姿勢資訊。頭部姿勢資訊可協助應用程式打造更直覺的體驗,例如追蹤使用者視野的視窗。
建立 Jetpack XR 的 ARCore 工作階段
透過 Jetpack XR 工作階段的 ARCore 取得頭部姿勢資訊。
請參閱「瞭解工作階段的生命週期」一文,取得 Session
。
設定工作階段
XR 工作階段預設不會啟用頭部追蹤功能。如要啟用頭部追蹤功能,請設定工作階段並設定 HeadTrackingMode.LAST_KNOWN
模式:
val newConfig = session.config.copy( headTracking = Config.HeadTrackingMode.LAST_KNOWN, ) when (val result = session.configure(newConfig)) { is SessionConfigureSuccess -> TODO(/* Success! */) is SessionConfigureConfigurationNotSupported -> TODO(/* Some combinations of configurations are not valid. Handle this failure case. */) else -> TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */) }
擷取頭部姿勢資料
頭部姿勢資料會透過 RenderViewpoint
公開。
RenderViewpoint
說明裝置特定視點的姿勢和視野。裝置可提供左側、右側或單聲道視角,視裝置功能而定。
如要取得單一視角資料:
val mono = RenderViewpoint.mono(session) ?: return mono.state.collect { state -> val fov = state.fieldOfView val viewpointPose = state.pose }
頭部追蹤的應用
應用程式可使用頭部追蹤功能,讓實體保持在使用者視野內,適用於需要使用者查看或移動的應用程式。
請避免在使用者視野中使用頭部鎖定實體,因為這可能會導致暈動病。請改用實體移動,在短時間後追蹤使用者的頭部:
val viewpointPose = RenderViewpoint.left(session)!!.state lifecycleScope.launch { while (true) { delay(2000) val start = panel.getPose() val startTime = session.state.value.timeMark val pose = session.scene.perceptionSpace.transformPoseTo( viewpointPose.value.pose, session.scene.activitySpace ) val target = Pose(pose.translation + pose.forward * 1f, pose.rotation) while (true) { val ratio = (session.state.value.timeMark - startTime).inWholeMilliseconds / 500f panel.setPose(Pose.lerp(start, target, ratio)) if (ratio > 1f) break } } }