Spaces:
Running
Running
File size: 5,516 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import { Vector3 } from '../../math/Vector3.js';
import { Group } from '../../objects/Group.js';
const _moveEvent = { type: 'move' };
class WebXRController {
constructor() {
this._targetRay = null;
this._grip = null;
this._hand = null;
}
getHandSpace() {
if (this._hand === null) {
this._hand = new Group();
this._hand.matrixAutoUpdate = false;
this._hand.visible = false;
this._hand.joints = {};
this._hand.inputState = { pinching: false };
}
return this._hand;
}
getTargetRaySpace() {
if (this._targetRay === null) {
this._targetRay = new Group();
this._targetRay.matrixAutoUpdate = false;
this._targetRay.visible = false;
this._targetRay.hasLinearVelocity = false;
this._targetRay.linearVelocity = new Vector3();
this._targetRay.hasAngularVelocity = false;
this._targetRay.angularVelocity = new Vector3();
}
return this._targetRay;
}
getGripSpace() {
if (this._grip === null) {
this._grip = new Group();
this._grip.matrixAutoUpdate = false;
this._grip.visible = false;
this._grip.hasLinearVelocity = false;
this._grip.linearVelocity = new Vector3();
this._grip.hasAngularVelocity = false;
this._grip.angularVelocity = new Vector3();
}
return this._grip;
}
dispatchEvent(event) {
if (this._targetRay !== null) {
this._targetRay.dispatchEvent(event);
}
if (this._grip !== null) {
this._grip.dispatchEvent(event);
}
if (this._hand !== null) {
this._hand.dispatchEvent(event);
}
return this;
}
disconnect(inputSource) {
this.dispatchEvent({ type: 'disconnected', data: inputSource });
if (this._targetRay !== null) {
this._targetRay.visible = false;
}
if (this._grip !== null) {
this._grip.visible = false;
}
if (this._hand !== null) {
this._hand.visible = false;
}
return this;
}
update(inputSource, frame, referenceSpace) {
let inputPose = null;
let gripPose = null;
let handPose = null;
const targetRay = this._targetRay;
const grip = this._grip;
const hand = this._hand;
if (inputSource && frame.session.visibilityState !== 'visible-blurred') {
if (targetRay !== null) {
inputPose = frame.getPose(inputSource.targetRaySpace, referenceSpace);
if (inputPose !== null) {
targetRay.matrix.fromArray(inputPose.transform.matrix);
targetRay.matrix.decompose(targetRay.position, targetRay.rotation, targetRay.scale);
if (inputPose.linearVelocity) {
targetRay.hasLinearVelocity = true;
targetRay.linearVelocity.copy(inputPose.linearVelocity);
} else {
targetRay.hasLinearVelocity = false;
}
if (inputPose.angularVelocity) {
targetRay.hasAngularVelocity = true;
targetRay.angularVelocity.copy(inputPose.angularVelocity);
} else {
targetRay.hasAngularVelocity = false;
}
this.dispatchEvent(_moveEvent);
}
}
if (hand && inputSource.hand) {
handPose = true;
for (const inputjoint of inputSource.hand.values()) {
// Update the joints groups with the XRJoint poses
const jointPose = frame.getJointPose(inputjoint, referenceSpace);
if (hand.joints[inputjoint.jointName] === undefined) {
// The transform of this joint will be updated with the joint pose on each frame
const joint = new Group();
joint.matrixAutoUpdate = false;
joint.visible = false;
hand.joints[inputjoint.jointName] = joint;
// ??
hand.add(joint);
}
const joint = hand.joints[inputjoint.jointName];
if (jointPose !== null) {
joint.matrix.fromArray(jointPose.transform.matrix);
joint.matrix.decompose(joint.position, joint.rotation, joint.scale);
joint.jointRadius = jointPose.radius;
}
joint.visible = jointPose !== null;
}
// Custom events
// Check pinchz
const indexTip = hand.joints['index-finger-tip'];
const thumbTip = hand.joints['thumb-tip'];
const distance = indexTip.position.distanceTo(thumbTip.position);
const distanceToPinch = 0.02;
const threshold = 0.005;
if (hand.inputState.pinching && distance > distanceToPinch + threshold) {
hand.inputState.pinching = false;
this.dispatchEvent({
type: 'pinchend',
handedness: inputSource.handedness,
target: this,
});
} else if (!hand.inputState.pinching && distance <= distanceToPinch - threshold) {
hand.inputState.pinching = true;
this.dispatchEvent({
type: 'pinchstart',
handedness: inputSource.handedness,
target: this,
});
}
} else {
if (grip !== null && inputSource.gripSpace) {
gripPose = frame.getPose(inputSource.gripSpace, referenceSpace);
if (gripPose !== null) {
grip.matrix.fromArray(gripPose.transform.matrix);
grip.matrix.decompose(grip.position, grip.rotation, grip.scale);
if (gripPose.linearVelocity) {
grip.hasLinearVelocity = true;
grip.linearVelocity.copy(gripPose.linearVelocity);
} else {
grip.hasLinearVelocity = false;
}
if (gripPose.angularVelocity) {
grip.hasAngularVelocity = true;
grip.angularVelocity.copy(gripPose.angularVelocity);
} else {
grip.hasAngularVelocity = false;
}
}
}
}
}
if (targetRay !== null) {
targetRay.visible = inputPose !== null;
}
if (grip !== null) {
grip.visible = gripPose !== null;
}
if (hand !== null) {
hand.visible = handPose !== null;
}
return this;
}
}
export { WebXRController };
|