AR Session 흐름 제어
이 문서는 AR Session 흐름 제어를 소개하며, AR Session을 생성, 시작, 중지 및 폐기하는 방법을 포함합니다.
시작하기 전에
- AR Session의 개념과 흐름을 이해하십시오.
생성
구성의 cloud localization library appId, cloud service serverAddress, cloud service apiKey 및 apiSecret을 사용하여 APIKeyAccessData를 생성합니다.
그런 다음 생성한 APIKeyAccessData를 사용하여 MegaTrackerConfigs를 생성합니다.
다시 MegaTrackerConfigs와 구성의 licenseKey를 사용하여 SessionConfigs를 생성합니다.
마지막으로 xr-frame scene에 마운트된 EasyARMegaComponent의 createSession(sessionConfigs) 메서드를 사용하여 session을 생성합니다.
createSession() {
// 씬에 마운트된 megaComponent 가져오기
const megaElement = scene.getElementById('easyar-mega');
const megaComponent = megaElement.getComponent("easyar-mega") as easyar.EasyARMegaComponent;
// MegaTracker 클라우드 서비스 인증 구성
const apiKeyAccess = new mega.APIKeyAccessData(this.data.appId, this.data.serverAddress, this.data.apiKey, this.data.apiSecret);
const megaTrackerConfigs: easyar.MegaTrackerConfigs = {
access: apiKeyAccess
}
// Session 구성
const sessionConfigs: easyar.SessionConfigs = {
megaTrackerConfigs: megaTrackerConfigs,
licenseKey: settings.EasyARLicenseKey
}
// 인스턴스 생성
session = megaComponent.createSession(sessionConfigs);
}
이 코드는 scene에서
megaComponent를 가져온 후 구성을 사용하여 session instance를 생성하는 방법을 보여줍니다.
주의
단일 instance 제한: 하나의 scene에는 하나의 Session instance만 존재할 수 있습니다. 새 Session을 생성하기 전에 반드시 closeSession()를 호출하여 이전 instance를 폐기했는지 확인해야 하며, 그렇지 않으면 생성에 실패합니다.
시작
일반적으로 xr-frame의 AR 시스템 준비 완료 callback에서 EasyARSession의 start(options) 메서드를 사용하여 session을 시작합니다.
경고
MegaTracker는 planar AR tracker가 제공하는 데이터에 의존하므로 planar tracker 초기화가 완료되기 전에는 작동할 수 없습니다.
WXML에서 bind:ready="handleReady"를 사용하여 AR 시스템 준비 완료 이벤트를 등록합니다.
<xr-scene ar-system="modes:Plane; planeMode: 1" bind:ready="handleReady">
xr-frame 컴포넌트의 callback 함수 handleReady에서 EasyARSession의 start(options) 메서드를 사용하여 session을 시작합니다.
handleReady: function(event) {
try {
//Session 시작. 기본적으로 실패 시 5회 재시도
await session.start();
} catch (err) {
console.error(`EasyAR Session initialization failed: ${err.message}`);
return;
}
}
중지 및 폐기
xr-frame scene에 마운트된 EasyARMegaComponent의 closeSession() 메서드를 사용하여 session을 폐기합니다.
페이지를 떠날 때, 즉 컴포넌트 instance가 페이지 node tree에서 제거될 때 폐기되도록 보장하기 위해 xr-frame 컴포넌트 lifecycle의 detached에서 호출하는 것을 권장합니다.
lifetimes: {
detached: function() {
const megaElement = scene.getElementById('easyar-mega');
const megaComponent = megaElement.getComponent("easyar-mega") as easyar.EasyARMegaComponent;
megaComponent.closeSession();
}
}
Foreground/background 전환
페이지가 background로 이동할 때 EasyARSession의 pause() 메서드를 사용하여 session을 일시 중지합니다. 페이지가 foreground로 돌아올 때 EasyARSession의 resume() 메서드를 사용하여 session을 재개합니다.
/** Mini Program 페이지의 호출*/
onHide() {
if (this.ar) {
this.ar.pauseSession();
}
},
onShow() {
if (this.ar) {
this.ar.resumeSession();
}
}
/** xr-frame 컴포넌트의 함수*/
pauseSession(): void {
if (!session) { console.error("EasyAR Session is not ready"); return;}
session.pause();
},
resumeSession(): void {
if (!session) { console.error("EasyAR Session is not ready"); return;}
session.resume();
}
이 코드에서 xr-frame 컴포넌트는
pauseSession()과resumeSession()두 함수를 노출합니다.Mini Program 페이지에서는 onHide, 즉 Mini Program이 foreground에서 background로 들어갈 때
pauseSession()을 호출하여 session을 일시 중지합니다.onShow, 즉 Mini Program이 background에서 foreground로 돌아올 때
resumeSession()을 호출하여 session을 재개합니다.