UNPKG

@bezlepkin/nativescript-ar

Version:

NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.

100 lines (99 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARUIView = void 0; const core_1 = require("@nativescript/core"); const arcommon_1 = require("./arcommon"); const pixelsPerMeter = 200 * core_1.Screen.mainScreen.scale; const main_queue = dispatch_get_current_queue(); class ARUIView extends arcommon_1.ARCommonNode { static create(options, sceneView, renderer) { return new ARUIView(options, sceneView, renderer); } constructor(options, sceneView, renderer) { try { const view = options.view; const node = SCNNode.node(); dispatch_async(main_queue, () => { if (view.parent) { view.ios.removeFromSuperview(); view.parent = null; } if (view instanceof core_1.View && (!view.ios)) { view._setupUI({}); view.loadView(view); } const nativeView = view.ios || view; view.measure(0, 0); if (!options.dimensions) { options.dimensions = { x: view.getMeasuredWidth() / pixelsPerMeter, y: view.getMeasuredHeight() / pixelsPerMeter }; } const cameraRatio = Math.max(sceneView.session.currentFrame.camera.imageResolution.height, sceneView.session.currentFrame.camera.imageResolution.width) / Math.min(sceneView.session.currentFrame.camera.imageResolution.height, sceneView.session.currentFrame.camera.imageResolution.width); const screenRatio = Math.max(core_1.Screen.mainScreen.heightPixels, core_1.Screen.mainScreen.widthPixels) / Math.min(core_1.Screen.mainScreen.heightPixels, core_1.Screen.mainScreen.widthPixels); const yStretchCompensation = (cameraRatio + screenRatio) / 2; console.log("yStretchCompensation: " + yStretchCompensation); const dimensions = (typeof options.dimensions !== "number" ? options.dimensions : { x: options.dimensions, y: options.dimensions * yStretchCompensation, }); const materialPlane = SCNPlane.planeWithWidthHeight(dimensions.x, dimensions.y * yStretchCompensation); materialPlane.cornerRadius = options.chamferRadius || 0; const planeViewController = ArPlaneViewController.alloc().initWithViewAndPlane(view, materialPlane); const planeNode = SCNNode.nodeWithGeometry(materialPlane); planeViewController.loadView(); planeNode.position = { x: 0, y: dimensions.y / 2, z: 0 }; planeNode.eulerAngles = { x: 0, y: Math.PI, z: 0 }; node.eulerAngles = { x: 0, y: Math.PI, z: 0 }; node.addChildNode(planeNode); }); super(options, node, renderer); } catch (e) { console.error(e); } } } exports.ARUIView = ARUIView; class ArPlaneViewController extends UIViewController { initWithViewAndPlane(view, plane) { super.init(); this.childView = view; this.materialPlane = plane; this.view.hidden = false; return this; } loadView() { super.loadView(); try { if (!this.childView.ios) { this.childView._setupUI({}); this.childView.loadView(this.childView); this.childView.requestLayout(); } this.view.addSubview(this.childView.ios); const frame = this.view.frame; const frameT = this.view.transform; const layout = this.childView.ios.frame; const layoutT = this.childView.ios.transform; this.view.opaque = false; this.materialPlane.firstMaterial.diffuse.contents = this.view; this.materialPlane.firstMaterial.doubleSided = true; } catch (e) { console.error(e); } } }