@plattar/plattar-ar-adapter
Version:
Plattar AR Adapter for interfacing with Google & Apple WebAR
154 lines (153 loc) • 7.13 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawAR = void 0;
const plattar_analytics_1 = require("@plattar/plattar-analytics");
const plattar_api_1 = require("@plattar/plattar-api");
const util_1 = require("../util/util");
const quicklook_viewer_1 = __importDefault(require("../viewers/quicklook-viewer"));
const reality_viewer_1 = __importDefault(require("../viewers/reality-viewer"));
const scene_viewer_1 = __importDefault(require("../viewers/scene-viewer"));
const launcher_ar_1 = require("./launcher-ar");
const version_1 = __importDefault(require("../version"));
/**
* Allows launching AR Experiences provided a single remote 3D Model file
*/
class RawAR extends launcher_ar_1.LauncherAR {
constructor(options) {
super();
// analytics instance
this._analytics = null;
if (!options.modelLocation) {
throw new Error("RawAR.constructor(modelLocation) - modelLocation must be defined");
}
const lowerLoc = options.modelLocation.toLowerCase();
if (lowerLoc.endsWith("usdz") || lowerLoc.endsWith("glb") || lowerLoc.endsWith("gltf") || lowerLoc.endsWith("reality")) {
this._options = options;
this._ar = null;
}
else {
throw new Error("RawAR.constructor(modelLocation) - modelLocation must be one of gltf, glb, usdz or reality");
}
}
get modelLocation() {
return this._options.modelLocation;
}
_SetupAnalytics() {
return new Promise((accept, _reject) => {
const sceneID = this._options.sceneID;
if (!sceneID) {
return accept();
}
const scene = new plattar_api_1.Scene(sceneID);
scene.include(plattar_api_1.Project);
scene.get().then((scene) => {
const analytics = new plattar_analytics_1.Analytics(scene.attributes.application_id);
analytics.origin = plattar_api_1.Server.location().type;
this._analytics = analytics;
analytics.data.push("type", "scene-ar");
analytics.data.push("sdkVersion", version_1.default);
analytics.data.push("sceneId", scene.id);
analytics.data.push("sceneTitle", scene.attributes.title);
const application = scene.relationships.find(plattar_api_1.Project);
// setup application stuff (if any)
if (application) {
analytics.data.push("applicationId", application.id);
analytics.data.push("applicationTitle", application.attributes.title);
if (this._options.useARBanner) {
this.options.banner = {
title: application.attributes.title,
subtitle: scene.attributes.title,
button: 'Visit'
};
}
}
accept();
}).catch((_err) => {
accept();
});
});
}
/**
* Initialise the RawAR instance. This returns a Promise that resolves
* successfully if initialisation is successful, otherwise it will fail.
*
* filure can occur for a number of reasons but it generally means that AR
* cannot be performed.
*/
init() {
return new Promise((accept, reject) => {
if (!util_1.Util.canAugment()) {
return reject(new Error("RawAR.init() - cannot proceed as AR not available in context"));
}
// send the analytics (if any)
this._SetupAnalytics().then(() => {
const modelLocation = this._options.modelLocation;
const lowerLoc = modelLocation.toLowerCase();
// we need to define our AR module here
// we are in Safari/Quicklook mode here
if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
// load the reality experience if dealing with reality file
if (lowerLoc.endsWith("reality") && util_1.Util.canRealityViewer()) {
this._ar = new reality_viewer_1.default();
this._ar.modelUrl = modelLocation;
this._ar.banner = this.options.banner;
return accept(this);
}
// load the usdz experience if dealing with usdz file
if (lowerLoc.endsWith("usdz") && util_1.Util.canQuicklook()) {
this._ar = new quicklook_viewer_1.default();
this._ar.modelUrl = modelLocation;
return accept(this);
}
return reject(new Error("RawAR.init() - cannot proceed as model is not a .usdz or .reality file"));
}
// check android
if (util_1.Util.canSceneViewer()) {
if (lowerLoc.endsWith("glb") || lowerLoc.endsWith("gltf")) {
const arviewer = new scene_viewer_1.default();
arviewer.modelUrl = modelLocation;
arviewer.isVertical = this.options.anchor === "vertical" ? true : false;
arviewer.banner = this.options.banner;
this._ar = arviewer;
return accept(this);
}
return reject(new Error("RawAR.init() - cannot proceed as model is not a .glb or .gltf file"));
}
// otherwise, we didn't have AR available - it should never really reach this stage as this should be caught
// earlier in the process
return reject(new Error("RawAR.init() - could not initialise AR correctly, check values"));
});
});
}
/**
* Launches the internal AR instance using an appropriate version of AR Viewers
*/
start() {
if (!this._ar) {
throw new Error("RawAR.start() - cannot proceed as AR instance is null");
}
const analytics = this._analytics;
if (analytics) {
analytics.data.push("device", this._ar.device);
analytics.data.push("eventCategory", this._ar.nodeType);
analytics.data.push("eventAction", "Start Scene Augment");
analytics.write();
analytics.startRecordEngagement();
}
// this was initialised via the init() function
this._ar.start();
}
canQuicklook() {
return this._ar && this._ar.nodeType === "Quick Look" ? true : false;
}
canRealityViewer() {
return this._ar && this._ar.nodeType === "Reality Viewer" ? true : false;
}
canSceneViewer() {
return this._ar && this._ar.nodeType === "Scene Viewer" ? true : false;
}
}
exports.RawAR = RawAR;