UNPKG

@plattar/plattar-ar-adapter

Version:

Plattar AR Adapter for interfacing with Google & Apple WebAR

136 lines (135 loc) 6.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelAR = void 0; const plattar_api_1 = require("@plattar/plattar-api"); const plattar_analytics_1 = require("@plattar/plattar-analytics"); 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")); /** * Performs AT Functionality using Plattar FileModel types */ class ModelAR extends launcher_ar_1.LauncherAR { constructor(options) { super(); // analytics instance this._analytics = null; if (!options.modelID) { throw new Error("ModelAR.constructor(modelID) - modelID must be defined"); } this._options = options; this._ar = null; } get modelID() { return this._options.modelID; } _SetupAnalytics(model) { let analytics = null; const project = model.relationships.find(plattar_api_1.Project); // setup scene stuff (if any) if (project) { analytics = new plattar_analytics_1.Analytics(project.id); analytics.origin = plattar_api_1.Server.location().type; analytics.data.push("type", "model-ar"); analytics.data.push("sdkVersion", version_1.default); analytics.data.push("applicationId", project.id); analytics.data.push("applicationTitle", project.attributes.title); analytics.data.push("modelId", model.id); analytics.data.push("modelTitle", model.attributes.title); this._analytics = analytics; if (this._options.useARBanner) { this.options.banner = { title: project.attributes.title, subtitle: model.attributes.title, button: 'Visit' }; } } } /** * Initialise the ModelAR 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("ModelAR.init() - cannot proceed as AR not available in context")); } const model = new plattar_api_1.FileModel(this.modelID); model.include(plattar_api_1.Project); model.get().then((model) => { // setup the analytics data this._SetupAnalytics(model); // we need to define our AR module here // we are in Safari/Quicklook mode here if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) { // model needs to have either USDZ or REALITY files defined // we load REALITY stuff first if available if (model.attributes.reality_filename && util_1.Util.canRealityViewer()) { this._ar = new reality_viewer_1.default(); this._ar.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.reality_filename; this._ar.banner = this.options.banner; return accept(this); } // otherwise, load the USDZ stuff second if available if (model.attributes.usdz_filename && util_1.Util.canQuicklook()) { this._ar = new quicklook_viewer_1.default(); this._ar.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.usdz_filename; this._ar.banner = this.options.banner; return accept(this); } return reject(new Error("ModelAR.init() - cannot proceed as ModelFile does not have a defined .usdz or .reality file")); } // check android if (util_1.Util.canSceneViewer()) { const arviewer = new scene_viewer_1.default(); arviewer.modelUrl = plattar_api_1.Server.location().cdn + model.attributes.path + model.attributes.original_filename; arviewer.isVertical = this.options.anchor === "vertical" ? true : false; arviewer.banner = this.options.banner; this._ar = arviewer; return accept(this); } // 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("ModelAR.init() - could not initialise AR correctly, check values")); }).catch(reject); }); } /** * Launches the internal AR instance using an appropriate version of AR Viewers */ start() { if (!this._ar) { throw new Error("ModelAR.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 Model 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.ModelAR = ModelAR;