diff --git a/src/components/cursor-hand.js b/src/components/cursor-hand.js index e0c8e6e653a725b28824bc24514e26da79328449..84a6a5fc47f1bb6b78976a2c7756e233c13c52d0 100644 --- a/src/components/cursor-hand.js +++ b/src/components/cursor-hand.js @@ -2,12 +2,16 @@ AFRAME.registerComponent("cursor-hand", { dependencies: ['raycaster'], schema: { cursor: {type: "selector"}, - maxDistance: {type: "number", default: 3} + maxDistance: {type: "number", default: 3}, + minDistance: {type: "number", default: 1} }, init: function() { this.isGrabbing = false; + this.wasIntersecting = false; this.currentDistance = this.data.maxDistance; + this.currentDistanceMod = 0; + this.enabled = true; document.addEventListener("mousedown", (e) => { this.data.cursor.emit("action_grab"); @@ -16,18 +20,39 @@ AFRAME.registerComponent("cursor-hand", { document.addEventListener("mouseup", (e) => { this.data.cursor.emit("action_release"); }); + + document.addEventListener("wheel", (e) => { + const updated = this.currentDistanceMod + e.deltaY/10; + this.currentDistanceMod = Math.min(Math.max(0, updated), this.data.maxDistance - this.data.minDistance); + }); + + window.addEventListener('enter-vr', e => { + if (AFRAME.utils.device.checkHeadsetConnected() || AFRAME.utils.device.isMobile()) { + this.enabled = false; + this.data.cursor.setAttribute("visible", false); + } + }); + + window.addEventListener('exit-vr', e => { + this.enabled = true; + this.data.cursor.setAttribute("visible", true); + }); }, tick: function() { + if (!this.enabled) { + return; + } + const isGrabbing = this.data.cursor.components["super-hands"].state.has("grab-start"); let isIntersecting = false; if (!isGrabbing) { const intersections = this.el.components.raycaster.intersections; - if(intersections.length > 0) { + if(intersections.length > 0 && intersections[0].distance <= this.data.maxDistance) { isIntersecting = true; const point = intersections[0].point; this.data.cursor.setAttribute('position', point); - this.currentDistance = Math.min(intersections[0].distance, this.data.maxDistance); + this.currentDistance = intersections[0].distance; } else { this.currentDistance = this.data.maxDistance; } @@ -37,10 +62,18 @@ AFRAME.registerComponent("cursor-hand", { const head = this.el.object3D; const origin = head.getWorldPosition(); let direction = head.getWorldDirection(); - direction.multiplyScalar(-this.currentDistance); + direction.multiplyScalar(-(this.currentDistance - this.currentDistanceMod)); let point = new THREE.Vector3(); point.addVectors(origin, direction); this.data.cursor.setAttribute("position", {x: point.x, y: point.y, z: point.z}); } + + if ((isGrabbing || isIntersecting) && !this.wasIntersecting) { + this.wasIntersecting = true; + this.data.cursor.setAttribute("material", "color: #00FF00"); + } else if ((!isGrabbing && !isIntersecting) && this.wasIntersecting) { + this.wasIntersecting = false; + this.data.cursor.setAttribute("material", "color: #00EFFF"); + } } });