nativescript-ar
Version:
NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.
109 lines (108 loc) • 4.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var platform_1 = require("tns-core-modules/platform");
var view_1 = require("tns-core-modules/ui/core/view");
var arcommon_1 = require("./arcommon");
var pixelsPerMeter = 200 * platform_1.screen.mainScreen.scale;
var main_queue = dispatch_get_current_queue();
var ARUIView = (function (_super) {
__extends(ARUIView, _super);
function ARUIView(options, sceneView, renderer) {
var _this = this;
try {
var view_2 = options.view;
var node_1 = SCNNode.node();
dispatch_async(main_queue, function () {
if (view_2.parent) {
view_2.ios.removeFromSuperview();
view_2.parent = null;
}
if (view_2 instanceof view_1.View && (!view_2.ios)) {
view_2._setupUI({});
view_2.loadView(view_2);
}
var nativeView = view_2.ios || view_2;
view_2.measure(0, 0);
if (!options.dimensions) {
options.dimensions = {
x: view_2.getMeasuredWidth() / pixelsPerMeter, y: view_2.getMeasuredHeight() / pixelsPerMeter
};
}
var 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);
var screenRatio = Math.max(platform_1.screen.mainScreen.heightPixels, platform_1.screen.mainScreen.widthPixels) / Math.min(platform_1.screen.mainScreen.heightPixels, platform_1.screen.mainScreen.widthPixels);
var yStretchCompensation = (cameraRatio + screenRatio) / 2;
console.log("yStretchCompensation: " + yStretchCompensation);
var dimensions = (typeof options.dimensions !== "number" ? options.dimensions : {
x: options.dimensions,
y: options.dimensions * yStretchCompensation,
});
var materialPlane = SCNPlane.planeWithWidthHeight(dimensions.x, dimensions.y * yStretchCompensation);
materialPlane.cornerRadius = options.chamferRadius || 0;
var planeViewController = ArPlaneViewController.alloc().initWithViewAndPlane(view_2, materialPlane);
var 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_1.eulerAngles = {
x: 0,
y: Math.PI,
z: 0
};
node_1.addChildNode(planeNode);
});
_this = _super.call(this, options, node_1, renderer) || this;
}
catch (e) {
console.error(e);
}
return _this;
}
ARUIView.create = function (options, sceneView, renderer) {
return new ARUIView(options, sceneView, renderer);
};
return ARUIView;
}(arcommon_1.ARCommonNode));
exports.ARUIView = ARUIView;
var ArPlaneViewController = (function (_super) {
__extends(ArPlaneViewController, _super);
function ArPlaneViewController() {
return _super !== null && _super.apply(this, arguments) || this;
}
ArPlaneViewController.prototype.initWithViewAndPlane = function (view, plane) {
_super.prototype.init.call(this);
this.childView = view;
this.materialPlane = plane;
this.view.hidden = false;
return this;
};
ArPlaneViewController.prototype.loadView = function () {
_super.prototype.loadView.call(this);
try {
if (!this.childView.ios) {
this.childView._setupUI({});
this.childView.loadView(this.childView);
this.childView.requestLayout();
}
this.view.addSubview(this.childView.ios);
var frame = this.view.frame;
var frameT = this.view.transform;
var layout = this.childView.ios.frame;
var 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);
}
};
return ArPlaneViewController;
}(UIViewController));