UNPKG

nativescript-ar

Version:

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

186 lines (185 loc) 6.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ar_common_1 = require("../../ar-common"); var ARCommonNode = (function () { function ARCommonNode(options, node, renderer) { this.renderer = renderer; this.draggingEnabled = options.draggingEnabled; this.rotatingEnabled = options.rotatingEnabled; this.scalingEnabled = options.scalingEnabled; node.position = this.position = options.position; if (options.rotation) { node.eulerAngles = { x: ARCommonNode.degToRadians(options.rotation.x), y: ARCommonNode.degToRadians(options.rotation.y), z: ARCommonNode.degToRadians(options.rotation.z) }; } node.name = this.id = (JSON.stringify(options.position) + "_" + JSON.stringify(options.scale) + "_" + JSON.stringify(options.rotation) + "_" + new Date().getTime()); this.onTapHandler = options.onTap; this.onLongPressHandler = options.onLongPress; if (options.parentNode) { if (!this.onTapHandler) { this.onTapHandler = (function (interaction) { return options.parentNode.onTap(interaction.touchPosition); }); } if (!this.onLongPressHandler) { this.onLongPressHandler = (function (interaction) { return options.parentNode.onLongPress(interaction.touchPosition); }); } } if (options.mass) { node.physicsBody = SCNPhysicsBody.bodyWithTypeShape(1, null); node.physicsBody.mass = options.mass || 0; node.physicsBody.categoryBitMask = 1; } if (options.scale) { node.scale = (options.scale instanceof ar_common_1.ARScale || options.scale.x ? options.scale : { x: options.scale, y: options.scale, z: options.scale }); } this.ios = node; } ARCommonNode.prototype.moveTo = function (to) { this.ios.position = { x: to.x !== undefined ? to.x : this.ios.position.x, y: to.y !== undefined ? to.y : this.ios.position.y, z: to.z !== undefined ? to.z : this.ios.position.z, }; }; ARCommonNode.prototype.moveBy = function (by) { this.ios.position = { x: this.ios.position.x + by.x, y: this.ios.position.y + by.y, z: this.ios.position.z + by.z }; }; ARCommonNode.prototype.getPosition = function () { var pos = this.ios.position; return { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.getWorldPosition = function () { var pos = this.ios.worldPosition; return { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.getDistanceTo = function (otherPosition) { var otherObjectWorldPos = this.renderer.unprojectPoint({ x: otherPosition.x, y: otherPosition.y, z: otherPosition.z }); var pos = this.ios.worldPosition; var xd = otherObjectWorldPos.x - pos.x; var yd = otherObjectWorldPos.y - pos.y; var zd = otherObjectWorldPos.z - pos.z; return sqrt(xd * xd + yd * yd + zd * zd); }; ARCommonNode.prototype.setPosition = function (pos) { this.ios.position = { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.setWorldPosition = function (pos) { this.ios.worldPosition = { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.rotateBy = function (by) { this.ios.eulerAngles = { x: this.ios.eulerAngles.x + ARCommonNode.degToRadians(by.x), y: this.ios.eulerAngles.y + ARCommonNode.degToRadians(by.y), z: this.ios.eulerAngles.z + ARCommonNode.degToRadians(by.z) }; }; ARCommonNode.prototype.setRotation = function (rot) { this.ios.eulerAngles = { x: ARCommonNode.degToRadians(rot.x), y: ARCommonNode.degToRadians(rot.y), z: ARCommonNode.degToRadians(rot.z) }; }; ARCommonNode.prototype.lookAtWorldPosition = function (worldPos) { this.ios.lookAt(worldPos); }; ARCommonNode.prototype.lookAtPosition = function (localPos) { var worldPos = this.ios.convertPositionToNode(localPos, null); this.lookAtWorldPosition(worldPos); }; ARCommonNode.prototype.lookAtNode = function (node) { this.lookAtWorldPosition(node.getWorldPosition()); }; ARCommonNode.prototype.getPositionOnScreen = function () { if (!this.renderer) { return { x: 0, y: 0 }; } var positionOnScreen = this.renderer.projectPoint(this.ios.worldPosition); return { x: positionOnScreen.x, y: positionOnScreen.y }; }; ARCommonNode.prototype.scaleBy = function (by) { this.ios.scale = { x: this.ios.scale.x + (by instanceof ar_common_1.ARScale ? by.x : by), y: this.ios.scale.y + (by instanceof ar_common_1.ARScale ? by.y : by), z: this.ios.scale.z + (by instanceof ar_common_1.ARScale ? by.z : by) }; }; ARCommonNode.prototype.scaleTo = function (scale) { this.ios.scale = { x: (scale instanceof ar_common_1.ARScale ? scale.x : scale), y: (scale instanceof ar_common_1.ARScale ? scale.y : scale), z: (scale instanceof ar_common_1.ARScale ? scale.z : scale) }; }; ARCommonNode.prototype.onTap = function (touchPosition) { this.onTapHandler && this.onTapHandler({ node: this, touchPosition: touchPosition }); }; ARCommonNode.prototype.onLongPress = function (touchPosition) { this.onLongPressHandler && this.onLongPressHandler({ node: this, touchPosition: touchPosition }); }; ARCommonNode.prototype.onPan = function (touchPosition) { this.onPanHandler && this.onPanHandler({ node: this, touchPosition: touchPosition }); }; ARCommonNode.prototype.setVisible = function (visible) { this.ios.hidden = !visible; }; ARCommonNode.prototype.allowDragging = function () { return this.draggingEnabled; }; ARCommonNode.prototype.allowRotating = function () { return this.rotatingEnabled; }; ARCommonNode.prototype.allowScaling = function () { return this.scalingEnabled; }; ARCommonNode.prototype.remove = function () { this.ios.removeFromParentNode(); }; ARCommonNode.degToRadians = function (degrees) { return degrees * (3.14159265359 / 180); }; return ARCommonNode; }()); exports.ARCommonNode = ARCommonNode;