(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.OculusHandPointerModel = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, THREE) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.OculusHandPointerModel = void 0; THREE = _interopRequireWildcard(THREE); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var PINCH_MAX = 0.05; var PINCH_THRESHOLD = 0.02; var PINCH_MIN = 0.01; var POINTER_ADVANCE_MAX = 0.02; var POINTER_OPACITY_MAX = 1; var POINTER_OPACITY_MIN = 0.4; var POINTER_FRONT_RADIUS = 0.002; var POINTER_REAR_RADIUS = 0.01; var POINTER_REAR_RADIUS_MIN = 0.003; var POINTER_LENGTH = 0.035; var POINTER_SEGMENTS = 16; var POINTER_RINGS = 12; var POINTER_HEMISPHERE_ANGLE = 110; var YAXIS = new THREE.Vector3(0, 1, 0); var ZAXIS = new THREE.Vector3(0, 0, 1); var CURSOR_RADIUS = 0.02; var CURSOR_MAX_DISTANCE = 1.5; var OculusHandPointerModel = /*#__PURE__*/function (_THREE$Object3D) { _inherits(OculusHandPointerModel, _THREE$Object3D); var _super = _createSuper(OculusHandPointerModel); function OculusHandPointerModel(hand, controller) { var _this; _classCallCheck(this, OculusHandPointerModel); _this = _super.call(this); _this.hand = hand; _this.controller = controller; _this.motionController = null; _this.envMap = null; _this.mesh = null; _this.pointerGeometry = null; _this.pointerMesh = null; _this.pointerObject = null; _this.pinched = false; _this.attached = false; _this.cursorObject = null; _this.raycaster = null; hand.addEventListener('connected', function (event) { var xrInputSource = event.data; if (xrInputSource.hand) { _this.visible = true; _this.xrInputSource = xrInputSource; _this.createPointer(); } }); return _this; } _createClass(OculusHandPointerModel, [{ key: "_drawVerticesRing", value: function _drawVerticesRing(vertices, baseVector, ringIndex) { var segmentVector = baseVector.clone(); for (var i = 0; i < POINTER_SEGMENTS; i++) { segmentVector.applyAxisAngle(ZAXIS, Math.PI * 2 / POINTER_SEGMENTS); var vid = ringIndex * POINTER_SEGMENTS + i; vertices[3 * vid] = segmentVector.x; vertices[3 * vid + 1] = segmentVector.y; vertices[3 * vid + 2] = segmentVector.z; } } }, { key: "_updatePointerVertices", value: function _updatePointerVertices(rearRadius) { var vertices = this.pointerGeometry.attributes.position.array; // first ring for front face var frontFaceBase = new THREE.Vector3(POINTER_FRONT_RADIUS, 0, -1 * (POINTER_LENGTH - rearRadius)); this._drawVerticesRing(vertices, frontFaceBase, 0); // rings for rear hemisphere var rearBase = new THREE.Vector3(Math.sin(Math.PI * POINTER_HEMISPHERE_ANGLE / 180) * rearRadius, Math.cos(Math.PI * POINTER_HEMISPHERE_ANGLE / 180) * rearRadius, 0); for (var i = 0; i < POINTER_RINGS; i++) { this._drawVerticesRing(vertices, rearBase, i + 1); rearBase.applyAxisAngle(YAXIS, Math.PI * POINTER_HEMISPHERE_ANGLE / 180 / (POINTER_RINGS * -2)); } // front and rear face center vertices var frontCenterIndex = POINTER_SEGMENTS * (1 + POINTER_RINGS); var rearCenterIndex = POINTER_SEGMENTS * (1 + POINTER_RINGS) + 1; var frontCenter = new THREE.Vector3(0, 0, -1 * (POINTER_LENGTH - rearRadius)); vertices[frontCenterIndex * 3] = frontCenter.x; vertices[frontCenterIndex * 3 + 1] = frontCenter.y; vertices[frontCenterIndex * 3 + 2] = frontCenter.z; var rearCenter = new THREE.Vector3(0, 0, rearRadius); vertices[rearCenterIndex * 3] = rearCenter.x; vertices[rearCenterIndex * 3 + 1] = rearCenter.y; vertices[rearCenterIndex * 3 + 2] = rearCenter.z; this.pointerGeometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); // verticesNeedUpdate = true; } }, { key: "createPointer", value: function createPointer() { var i, j; var vertices = new Array(((POINTER_RINGS + 1) * POINTER_SEGMENTS + 2) * 3).fill(0); // const vertices = []; var indices = []; this.pointerGeometry = new THREE.BufferGeometry(); this.pointerGeometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); this._updatePointerVertices(POINTER_REAR_RADIUS); // construct faces to connect rings for (i = 0; i < POINTER_RINGS; i++) { for (j = 0; j < POINTER_SEGMENTS - 1; j++) { indices.push(i * POINTER_SEGMENTS + j, i * POINTER_SEGMENTS + j + 1, (i + 1) * POINTER_SEGMENTS + j); indices.push(i * POINTER_SEGMENTS + j + 1, (i + 1) * POINTER_SEGMENTS + j + 1, (i + 1) * POINTER_SEGMENTS + j); } indices.push((i + 1) * POINTER_SEGMENTS - 1, i * POINTER_SEGMENTS, (i + 2) * POINTER_SEGMENTS - 1); indices.push(i * POINTER_SEGMENTS, (i + 1) * POINTER_SEGMENTS, (i + 2) * POINTER_SEGMENTS - 1); } // construct front and rear face var frontCenterIndex = POINTER_SEGMENTS * (1 + POINTER_RINGS); var rearCenterIndex = POINTER_SEGMENTS * (1 + POINTER_RINGS) + 1; for (i = 0; i < POINTER_SEGMENTS - 1; i++) { indices.push(frontCenterIndex, i + 1, i); indices.push(rearCenterIndex, i + POINTER_SEGMENTS * POINTER_RINGS, i + POINTER_SEGMENTS * POINTER_RINGS + 1); } indices.push(frontCenterIndex, 0, POINTER_SEGMENTS - 1); indices.push(rearCenterIndex, POINTER_SEGMENTS * (POINTER_RINGS + 1) - 1, POINTER_SEGMENTS * POINTER_RINGS); var material = new THREE.MeshBasicMaterial(); material.transparent = true; material.opacity = POINTER_OPACITY_MIN; this.pointerGeometry.setIndex(indices); this.pointerMesh = new THREE.Mesh(this.pointerGeometry, material); this.pointerMesh.position.set(0, 0, -1 * POINTER_REAR_RADIUS); this.pointerObject = new THREE.Object3D(); this.pointerObject.add(this.pointerMesh); this.raycaster = new THREE.Raycaster(); // create cursor var cursorGeometry = new THREE.SphereGeometry(CURSOR_RADIUS, 10, 10); var cursorMaterial = new THREE.MeshBasicMaterial(); cursorMaterial.transparent = true; cursorMaterial.opacity = POINTER_OPACITY_MIN; this.cursorObject = new THREE.Mesh(cursorGeometry, cursorMaterial); this.pointerObject.add(this.cursorObject); this.add(this.pointerObject); } }, { key: "_updateRaycaster", value: function _updateRaycaster() { if (this.raycaster) { var pointerMatrix = this.pointerObject.matrixWorld; var tempMatrix = new THREE.Matrix4(); tempMatrix.identity().extractRotation(pointerMatrix); this.raycaster.ray.origin.setFromMatrixPosition(pointerMatrix); this.raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix); } } }, { key: "_updatePointer", value: function _updatePointer() { this.pointerObject.visible = this.controller.visible; var indexTip = this.hand.joints['index-finger-tip']; var thumbTip = this.hand.joints['thumb-tip']; var distance = indexTip.position.distanceTo(thumbTip.position); var position = indexTip.position.clone().add(thumbTip.position).multiplyScalar(0.5); this.pointerObject.position.copy(position); this.pointerObject.quaternion.copy(this.controller.quaternion); this.pinched = distance <= PINCH_THRESHOLD; var pinchScale = (distance - PINCH_MIN) / (PINCH_MAX - PINCH_MIN); var focusScale = (distance - PINCH_MIN) / (PINCH_THRESHOLD - PINCH_MIN); if (pinchScale > 1) { this._updatePointerVertices(POINTER_REAR_RADIUS); this.pointerMesh.position.set(0, 0, -1 * POINTER_REAR_RADIUS); this.pointerMesh.material.opacity = POINTER_OPACITY_MIN; } else if (pinchScale > 0) { var rearRadius = (POINTER_REAR_RADIUS - POINTER_REAR_RADIUS_MIN) * pinchScale + POINTER_REAR_RADIUS_MIN; this._updatePointerVertices(rearRadius); if (focusScale < 1) { this.pointerMesh.position.set(0, 0, -1 * rearRadius - (1 - focusScale) * POINTER_ADVANCE_MAX); this.pointerMesh.material.opacity = POINTER_OPACITY_MIN + (1 - focusScale) * (POINTER_OPACITY_MAX - POINTER_OPACITY_MIN); } else { this.pointerMesh.position.set(0, 0, -1 * rearRadius); this.pointerMesh.material.opacity = POINTER_OPACITY_MIN; } } else { this._updatePointerVertices(POINTER_REAR_RADIUS_MIN); this.pointerMesh.position.set(0, 0, -1 * POINTER_REAR_RADIUS_MIN - POINTER_ADVANCE_MAX); this.pointerMesh.material.opacity = POINTER_OPACITY_MAX; } this.cursorObject.material.opacity = this.pointerMesh.material.opacity; } }, { key: "updateMatrixWorld", value: function updateMatrixWorld(force) { _get(_getPrototypeOf(OculusHandPointerModel.prototype), "updateMatrixWorld", this).call(this, force); if (this.pointerGeometry) { this._updatePointer(); this._updateRaycaster(); } } }, { key: "isPinched", value: function isPinched() { return this.pinched; } }, { key: "setAttached", value: function setAttached(attached) { this.attached = attached; } }, { key: "isAttached", value: function isAttached() { return this.attached; } }, { key: "intersectObject", value: function intersectObject(object) { var recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (this.raycaster) { return this.raycaster.intersectObject(object, recursive); } } }, { key: "intersectObjects", value: function intersectObjects(objects) { var recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (this.raycaster) { return this.raycaster.intersectObjects(objects, recursive); } } }, { key: "checkIntersections", value: function checkIntersections(objects) { var recursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (this.raycaster && !this.attached) { var intersections = this.raycaster.intersectObjects(objects, recursive); var direction = new THREE.Vector3(0, 0, -1); if (intersections.length > 0) { var intersection = intersections[0]; var distance = intersection.distance; this.cursorObject.position.copy(direction.multiplyScalar(distance)); } else { this.cursorObject.position.copy(direction.multiplyScalar(CURSOR_MAX_DISTANCE)); } } } }, { key: "setCursor", value: function setCursor(distance) { var direction = new THREE.Vector3(0, 0, -1); if (this.raycaster && !this.attached) { this.cursorObject.position.copy(direction.multiplyScalar(distance)); } } }]); return OculusHandPointerModel; }(THREE.Object3D); _exports.OculusHandPointerModel = OculusHandPointerModel; });