UNPKG

@bezlepkin/nativescript-ar

Version:

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

191 lines (190 loc) 8.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARCommonNode = void 0; const core_1 = require("@nativescript/core"); const ar_common_1 = require("../../ar-common"); class ARCommonNode { constructor(options, node) { this.android = node; this.onTapHandler = options.onTap; this.onLongPressHandler = options.onLongPress; if (options.parentNode) { if (!this.onTapHandler) { this.onTapHandler = (interaction => options.parentNode.onTap(interaction.touchPosition)); } if (!this.onLongPressHandler) { this.onLongPressHandler = (interaction => 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: (hitResult, motionEvent) => { const duration = motionEvent.getEventTime() - motionEvent.getDownTime(); const 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 }); } } })); } static createNode(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(); } moveTo(to) { const 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)); } moveBy(by) { const 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)); } getPosition() { const pos = this.android.getLocalPosition(); return { x: pos.x, y: pos.y, z: pos.z }; } getWorldPosition() { const pos = this.android.getWorldPosition(); return { x: pos.x, y: pos.y, z: pos.z }; } setPosition(pos) { this.android.setLocalPosition(new com.google.ar.sceneform.math.Vector3(pos.x, pos.y, pos.z)); } setWorldPosition(pos) { this.android.setWorldPosition(new com.google.ar.sceneform.math.Vector3(pos.x, pos.y, pos.z)); } getDistanceTo(otherPosition) { return 0; } rotateBy(by) { const currentRotation = this.android.getLocalRotation(); const 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)); } setRotation(rot) { this.android.setLocalRotation(new com.google.ar.sceneform.math.Quaternion(new com.google.ar.sceneform.math.Vector3(rot.x, rot.y, rot.z))); } lookAtWorldPosition(worldPos) { const 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()); } lookAtPosition(localPos) { const 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()); } lookAtNode(node) { this.lookAtWorldPosition(node.getWorldPosition()); } getPositionOnScreen() { const screenPoint = ARCommonNode.camera.worldToScreenPoint(this.android.getWorldPosition()); return { x: screenPoint.x / ARCommonNode.screenScale, y: screenPoint.y / ARCommonNode.screenScale }; } scaleBy(by) { const 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))); } scaleTo(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))); } onTap(touchPosition) { this.onTapHandler && this.onTapHandler({ node: this, touchPosition }); } onLongPress(touchPosition) { this.onLongPressHandler && this.onLongPressHandler({ node: this, touchPosition }); } onPan(touchPosition) { this.onPanHandler && this.onPanHandler({ node: this, touchPosition }); } setVisible(visible) { this.android.setEnabled(visible); } allowDragging() { return this.draggingEnabled; } allowRotating() { return this.rotatingEnabled; } allowScaling() { return this.scalingEnabled; } remove() { this.android.setParent(null); } static getDefaultMaterial() { return new Promise((resolve, reject) => { if (ARCommonNode.defaultMaterial) { resolve(ARCommonNode.defaultMaterial); return; } com.google.ar.sceneform.rendering.MaterialFactory.makeOpaqueWithColor(core_1.Utils.ad.getApplicationContext(), new com.google.ar.sceneform.rendering.Color(android.graphics.Color.MAGENTA)) .thenAccept(new java.util.function.Consumer({ accept: material => { ARCommonNode.defaultMaterial = material; resolve(material); } })) .exceptionally(new java.util.function.Function({ apply: error => reject(error) })); }); } static degToRadians(degrees) { return degrees * (3.14159265359 / 180); } } exports.ARCommonNode = ARCommonNode; ARCommonNode.screenScale = core_1.Screen.mainScreen.scale;