|
| 1 | +/* global AFRAME, THREE */ |
| 2 | +const inherit = AFRAME.utils.extendDeep; |
| 3 | +const physicsCore = require("super-hands/reaction_components/prototypes/physics-grab-proto.js"); |
| 4 | +const buttonsCore = require("super-hands/reaction_components/prototypes/buttons-proto.js"); |
| 5 | +// new object with all core modules |
| 6 | +const base = inherit({}, physicsCore, buttonsCore); |
| 7 | +AFRAME.registerComponent( |
| 8 | + "grabbable-toggle", |
| 9 | + inherit(base, { |
| 10 | + schema: { |
| 11 | + maxGrabbers: { type: "int", default: NaN }, |
| 12 | + invert: { default: false }, |
| 13 | + suppressY: { default: false }, |
| 14 | + primaryReleaseEvents: { default: ["primary_hand_release"] }, |
| 15 | + secondaryReleaseEvents: { default: ["secondary_hand_release"] } |
| 16 | + }, |
| 17 | + init: function() { |
| 18 | + this.GRABBED_STATE = "grabbed"; |
| 19 | + this.GRAB_EVENT = "grab-start"; |
| 20 | + this.UNGRAB_EVENT = "grab-end"; |
| 21 | + this.grabbed = false; |
| 22 | + this.grabbers = []; |
| 23 | + this.constraints = new Map(); |
| 24 | + this.deltaPositionIsValid = false; |
| 25 | + this.grabDistance = undefined; |
| 26 | + this.grabDirection = { x: 0, y: 0, z: -1 }; |
| 27 | + this.grabOffset = { x: 0, y: 0, z: 0 }; |
| 28 | + // persistent object speeds up repeat setAttribute calls |
| 29 | + this.destPosition = { x: 0, y: 0, z: 0 }; |
| 30 | + this.deltaPosition = new THREE.Vector3(); |
| 31 | + this.targetPosition = new THREE.Vector3(); |
| 32 | + this.physicsInit(); |
| 33 | + |
| 34 | + this.el.addEventListener(this.GRAB_EVENT, e => this.start(e)); |
| 35 | + this.el.addEventListener(this.UNGRAB_EVENT, e => this.end(e)); |
| 36 | + this.el.addEventListener("mouseout", e => this.lostGrabber(e)); |
| 37 | + |
| 38 | + this.toggle = false; |
| 39 | + this.lastGrabber = null; |
| 40 | + }, |
| 41 | + update: function() { |
| 42 | + this.physicsUpdate(); |
| 43 | + this.xFactor = this.data.invert ? -1 : 1; |
| 44 | + this.zFactor = this.data.invert ? -1 : 1; |
| 45 | + this.yFactor = (this.data.invert ? -1 : 1) * !this.data.suppressY; |
| 46 | + }, |
| 47 | + tick: (function() { |
| 48 | + const q = new THREE.Quaternion(); |
| 49 | + const v = new THREE.Vector3(); |
| 50 | + |
| 51 | + return function() { |
| 52 | + let entityPosition; |
| 53 | + if (this.grabber) { |
| 54 | + // reflect on z-axis to point in same direction as the laser |
| 55 | + this.targetPosition.copy(this.grabDirection); |
| 56 | + this.targetPosition |
| 57 | + .applyQuaternion(this.grabber.object3D.getWorldQuaternion(q)) |
| 58 | + .setLength(this.grabDistance) |
| 59 | + .add(this.grabber.object3D.getWorldPosition(v)) |
| 60 | + .add(this.grabOffset); |
| 61 | + if (this.deltaPositionIsValid) { |
| 62 | + // relative position changes work better with nested entities |
| 63 | + this.deltaPosition.sub(this.targetPosition); |
| 64 | + entityPosition = this.el.getAttribute("position"); |
| 65 | + this.destPosition.x = entityPosition.x - this.deltaPosition.x * this.xFactor; |
| 66 | + this.destPosition.y = entityPosition.y - this.deltaPosition.y * this.yFactor; |
| 67 | + this.destPosition.z = entityPosition.z - this.deltaPosition.z * this.zFactor; |
| 68 | + this.el.setAttribute("position", this.destPosition); |
| 69 | + } else { |
| 70 | + this.deltaPositionIsValid = true; |
| 71 | + } |
| 72 | + this.deltaPosition.copy(this.targetPosition); |
| 73 | + } |
| 74 | + }; |
| 75 | + })(), |
| 76 | + remove: function() { |
| 77 | + this.el.removeEventListener(this.GRAB_EVENT, this.start); |
| 78 | + this.el.removeEventListener(this.UNGRAB_EVENT, this.end); |
| 79 | + this.physicsRemove(); |
| 80 | + }, |
| 81 | + start: function(evt) { |
| 82 | + if (evt.defaultPrevented || !this.startButtonOk(evt)) { |
| 83 | + return; |
| 84 | + } |
| 85 | + // room for more grabbers? |
| 86 | + let grabAvailable = !Number.isFinite(this.data.maxGrabbers) || this.grabbers.length < this.data.maxGrabbers; |
| 87 | + if (Number.isFinite(this.data.maxGrabbers) && !grabAvailable && this.grabbed) { |
| 88 | + this.grabbers[0].components["super-hands"].onGrabEndButton(); |
| 89 | + grabAvailable = true; |
| 90 | + } |
| 91 | + if (this.grabbers.indexOf(evt.detail.hand) === -1 && grabAvailable) { |
| 92 | + if (!evt.detail.hand.object3D) { |
| 93 | + console.warn("grabbable entities must have an object3D"); |
| 94 | + return; |
| 95 | + } |
| 96 | + this.grabbers.push(evt.detail.hand); |
| 97 | + // initiate physics if available, otherwise manual |
| 98 | + if (!this.physicsStart(evt) && !this.grabber) { |
| 99 | + this.grabber = evt.detail.hand; |
| 100 | + this.resetGrabber(); |
| 101 | + } |
| 102 | + // notify super-hands that the gesture was accepted |
| 103 | + if (evt.preventDefault) { |
| 104 | + evt.preventDefault(); |
| 105 | + } |
| 106 | + this.grabbed = true; |
| 107 | + this.el.addState(this.GRABBED_STATE); |
| 108 | + } |
| 109 | + }, |
| 110 | + end: function(evt) { |
| 111 | + const handIndex = this.grabbers.indexOf(evt.detail.hand); |
| 112 | + if (evt.defaultPrevented || !this.endButtonOk(evt)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const type = evt.detail && evt.detail.buttonEvent ? evt.detail.buttonEvent.type : null; |
| 117 | + |
| 118 | + if (this.toggle && this.lastGrabber !== this.grabbers[0]) { |
| 119 | + this.toggle = false; |
| 120 | + this.lastGrabber = null; |
| 121 | + } |
| 122 | + |
| 123 | + if (handIndex !== -1) { |
| 124 | + this.grabber = this.grabbers[0]; |
| 125 | + } |
| 126 | + |
| 127 | + if ((this.isPrimaryRelease(type) && !this.toggle) || this.isSecondaryRelease(type)) { |
| 128 | + this.toggle = true; |
| 129 | + this.lastGrabber = this.grabbers[0]; |
| 130 | + return; |
| 131 | + } else if (this.toggle && this.isPrimaryRelease(type)) { |
| 132 | + this.toggle = false; |
| 133 | + this.lastGrabber = null; |
| 134 | + } |
| 135 | + |
| 136 | + if (handIndex !== -1) { |
| 137 | + this.grabbers.splice(handIndex, 1); |
| 138 | + this.grabber = this.grabbers[0]; |
| 139 | + } |
| 140 | + |
| 141 | + this.physicsEnd(evt); |
| 142 | + if (!this.resetGrabber()) { |
| 143 | + this.grabbed = false; |
| 144 | + this.el.removeState(this.GRABBED_STATE); |
| 145 | + } |
| 146 | + if (evt.preventDefault) { |
| 147 | + evt.preventDefault(); |
| 148 | + } |
| 149 | + }, |
| 150 | + resetGrabber: (() => { |
| 151 | + const objPos = new THREE.Vector3(); |
| 152 | + const grabPos = new THREE.Vector3(); |
| 153 | + return function() { |
| 154 | + if (!this.grabber) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + const raycaster = this.grabber.getAttribute("raycaster"); |
| 158 | + this.deltaPositionIsValid = false; |
| 159 | + this.grabDistance = this.el.object3D |
| 160 | + .getWorldPosition(objPos) |
| 161 | + .distanceTo(this.grabber.object3D.getWorldPosition(grabPos)); |
| 162 | + if (raycaster) { |
| 163 | + this.grabDirection = raycaster.direction; |
| 164 | + this.grabOffset = raycaster.origin; |
| 165 | + } |
| 166 | + return true; |
| 167 | + }; |
| 168 | + })(), |
| 169 | + lostGrabber: function(evt) { |
| 170 | + const i = this.grabbers.indexOf(evt.relatedTarget); |
| 171 | + // if a queued, non-physics grabber leaves the collision zone, forget it |
| 172 | + if (i !== -1 && evt.relatedTarget !== this.grabber && !this.physicsIsConstrained(evt.relatedTarget)) { |
| 173 | + this.grabbers.splice(i, 1); |
| 174 | + } |
| 175 | + }, |
| 176 | + |
| 177 | + isPrimaryRelease(type) { |
| 178 | + return this.data.primaryReleaseEvents.indexOf(type) !== -1; |
| 179 | + }, |
| 180 | + |
| 181 | + isSecondaryRelease(type) { |
| 182 | + return this.data.secondaryReleaseEvents.indexOf(type) !== -1; |
| 183 | + } |
| 184 | + }) |
| 185 | +); |
0 commit comments