UNPKG

nativescript-ar

Version:

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

193 lines (192 loc) 9.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var platform_1 = require("tns-core-modules/platform"); var utils = require("tns-core-modules/utils/utils"); var ar_common_1 = require("../../ar-common"); var ARCommonNode = (function () { function ARCommonNode(options, node) { var _this = this; this.android = node; 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); }); } } this.draggingEnabled = options.draggingEnabled; this.rotatingEnabled = options.rotatingEnabled; this.scalingEnabled = options.scalingEnabled; if (options.rotation) { this.rotateBy(options.rotation); } if (options.scale) { this.android.setLocalScale(new com.google.ar.sceneform.math.Vector3(typeof options.scale === "number" ? options.scale : options.scale.x, typeof options.scale === "number" ? options.scale : options.scale.y, typeof options.scale === "number" ? options.scale : options.scale.z)); } if (options.position) { this.android.setLocalPosition(new com.google.ar.sceneform.math.Vector3(options.position.x, options.position.y, options.position.z)); } this.id = (JSON.stringify(options.position) + "_" + new Date().getTime()); this.android.setOnTapListener(new com.google.ar.sceneform.Node.OnTapListener({ onTap: function (hitResult, motionEvent) { var duration = motionEvent.getEventTime() - motionEvent.getDownTime(); var nativePosition = _this.android.getLocalPosition(); _this.position = { x: nativePosition.x, y: nativePosition.y, z: nativePosition.z }; if (duration > 700) { _this.onLongPress({ x: hitResult.getPoint().x, y: hitResult.getPoint().y }); } else { if (node instanceof com.google.ar.sceneform.ux.BaseTransformableNode) { node.select(); } _this.onTap({ x: hitResult.getPoint().x, y: hitResult.getPoint().y }); } } })); } ARCommonNode.createNode = function (options, fragment) { if (!ARCommonNode.camera) { ARCommonNode.camera = fragment.getArSceneView().getScene().getCamera(); } if ((options.draggingEnabled || options.rotatingEnabled || options.scalingEnabled)) { return new com.google.ar.sceneform.ux.TransformableNode(fragment.getTransformationSystem()); } return new com.google.ar.sceneform.Node(); }; ARCommonNode.prototype.moveTo = function (to) { var currentPosition = this.android.getLocalPosition(); this.android.setLocalPosition(new com.google.ar.sceneform.math.Vector3(to.x !== undefined ? to.x : currentPosition.x, to.y !== undefined ? to.y : currentPosition.y, to.z !== undefined ? to.z : currentPosition.z)); }; ARCommonNode.prototype.moveBy = function (by) { var currentPosition = this.android.getLocalPosition(); this.android.setLocalPosition(new com.google.ar.sceneform.math.Vector3(currentPosition.x + by.x, currentPosition.y + by.y, currentPosition.z + by.z)); }; ARCommonNode.prototype.getPosition = function () { var pos = this.android.getLocalPosition(); return { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.getWorldPosition = function () { var pos = this.android.getWorldPosition(); return { x: pos.x, y: pos.y, z: pos.z }; }; ARCommonNode.prototype.setPosition = function (pos) { this.android.setLocalPosition(new com.google.ar.sceneform.math.Vector3(pos.x, pos.y, pos.z)); }; ARCommonNode.prototype.setWorldPosition = function (pos) { this.android.setWorldPosition(new com.google.ar.sceneform.math.Vector3(pos.x, pos.y, pos.z)); }; ARCommonNode.prototype.getDistanceTo = function (otherPosition) { return 0; }; ARCommonNode.prototype.rotateBy = function (by) { var currentRotation = this.android.getLocalRotation(); var rotateBy = new com.google.ar.sceneform.math.Quaternion(new com.google.ar.sceneform.math.Vector3(by.x, by.y, by.z)); this.android.setLocalRotation(com.google.ar.sceneform.math.Quaternion.multiply(currentRotation, rotateBy)); }; ARCommonNode.prototype.setRotation = function (rot) { this.android.setLocalRotation(new com.google.ar.sceneform.math.Quaternion(new com.google.ar.sceneform.math.Vector3(rot.x, rot.y, rot.z))); }; ARCommonNode.prototype.lookAtWorldPosition = function (worldPos) { var direction = com.google.ar.sceneform.math.Vector3.subtract(this.android.getWorldPosition(), new com.google.ar.sceneform.math.Vector3(worldPos.x, worldPos.y, worldPos.z)); this.android.setLookDirection(direction, com.google.ar.sceneform.math.Vector3.up()); }; ARCommonNode.prototype.lookAtPosition = function (localPos) { var direction = com.google.ar.sceneform.math.Vector3.subtract(this.android.getWorldPosition(), this.android.localToWorldPoint(new com.google.ar.sceneform.math.Vector3(localPos.x, localPos.y, localPos.z))); this.android.setLookDirection(direction, com.google.ar.sceneform.math.Vector3.up()); }; ARCommonNode.prototype.lookAtNode = function (node) { this.lookAtWorldPosition(node.getWorldPosition()); }; ARCommonNode.prototype.getPositionOnScreen = function () { var screenPoint = ARCommonNode.camera.worldToScreenPoint(this.android.getWorldPosition()); return { x: screenPoint.x / ARCommonNode.screenScale, y: screenPoint.y / ARCommonNode.screenScale }; }; ARCommonNode.prototype.scaleBy = function (by) { var currentScale = this.android.getLocalScale(); this.android.setLocalScale(new com.google.ar.sceneform.math.Vector3(currentScale.x + (by instanceof ar_common_1.ARScale ? by.x : by), currentScale.y + (by instanceof ar_common_1.ARScale ? by.y : by), currentScale.z + (by instanceof ar_common_1.ARScale ? by.z : by))); }; ARCommonNode.prototype.scaleTo = function (scale) { this.android.setLocalScale(new com.google.ar.sceneform.math.Vector3((scale instanceof ar_common_1.ARScale ? scale.x : scale), (scale instanceof ar_common_1.ARScale ? scale.y : scale), (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.android.setEnabled(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.android.setParent(null); }; ARCommonNode.getDefaultMaterial = function () { return new Promise(function (resolve, reject) { if (ARCommonNode.defaultMaterial) { resolve(ARCommonNode.defaultMaterial); return; } com.google.ar.sceneform.rendering.MaterialFactory.makeOpaqueWithColor(utils.ad.getApplicationContext(), new com.google.ar.sceneform.rendering.Color(android.graphics.Color.MAGENTA)) .thenAccept(new java.util.function.Consumer({ accept: function (material) { ARCommonNode.defaultMaterial = material; resolve(material); } })) .exceptionally(new java.util.function.Function({ apply: function (error) { return reject(error); } })); }); }; ARCommonNode.degToRadians = function (degrees) { return degrees * (3.14159265359 / 180); }; ARCommonNode.screenScale = platform_1.screen.mainScreen.scale; return ARCommonNode; }()); exports.ARCommonNode = ARCommonNode;