UNPKG

@bezlepkin/nativescript-ar

Version:

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

186 lines (185 loc) 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARCommonNode = void 0; const ar_common_1 = require("../../ar-common"); class ARCommonNode { constructor(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 = (interaction => options.parentNode.onTap(interaction.touchPosition)); } if (!this.onLongPressHandler) { this.onLongPressHandler = (interaction => 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; } moveTo(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, }; } moveBy(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 }; } getPosition() { const pos = this.ios.position; return { x: pos.x, y: pos.y, z: pos.z }; } getWorldPosition() { const pos = this.ios.worldPosition; return { x: pos.x, y: pos.y, z: pos.z }; } getDistanceTo(otherPosition) { let otherObjectWorldPos = this.renderer.unprojectPoint({ x: otherPosition.x, y: otherPosition.y, z: otherPosition.z }); const pos = this.ios.worldPosition; let xd = otherObjectWorldPos.x - pos.x; let yd = otherObjectWorldPos.y - pos.y; let zd = otherObjectWorldPos.z - pos.z; return sqrt(xd * xd + yd * yd + zd * zd); } setPosition(pos) { this.ios.position = { x: pos.x, y: pos.y, z: pos.z }; } setWorldPosition(pos) { this.ios.worldPosition = { x: pos.x, y: pos.y, z: pos.z }; } rotateBy(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) }; } setRotation(rot) { this.ios.eulerAngles = { x: ARCommonNode.degToRadians(rot.x), y: ARCommonNode.degToRadians(rot.y), z: ARCommonNode.degToRadians(rot.z) }; } lookAtWorldPosition(worldPos) { this.ios.lookAt(worldPos); } lookAtPosition(localPos) { const worldPos = this.ios.convertPositionToNode(localPos, null); this.lookAtWorldPosition(worldPos); } lookAtNode(node) { this.lookAtWorldPosition(node.getWorldPosition()); } getPositionOnScreen() { if (!this.renderer) { return { x: 0, y: 0 }; } const positionOnScreen = this.renderer.projectPoint(this.ios.worldPosition); return { x: positionOnScreen.x, y: positionOnScreen.y }; } scaleBy(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) }; } scaleTo(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) }; } 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.ios.hidden = !visible; } allowDragging() { return this.draggingEnabled; } allowRotating() { return this.rotatingEnabled; } allowScaling() { return this.scalingEnabled; } remove() { this.ios.removeFromParentNode(); } static degToRadians(degrees) { return degrees * (3.14159265359 / 180); } } exports.ARCommonNode = ARCommonNode;