aframe-gamepad-controls.min.js 5.0 KB

1
  1. !function(t){function e(n){if(i[n])return i[n].exports;var o=i[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",i(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,i){var n=i(2),o=i(3),a=200,s=Math.PI/2,r=.2;t.exports={GamepadButton:n,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":!0},flyEnabled:{"default":!1},invertAxisY:{"default":!1},easing:{"default":20},acceleration:{"default":65},sensitivity:{"default":.04},pitchAxis:{"default":"x",oneOf:["x","y","z"]},yawAxis:{"default":"y",oneOf:["x","y","z"]},rollAxis:{"default":"z",oneOf:["x","y","z"]},debug:{"default":!1}},init:function(){this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.pitch=new THREE.Object3D,this.yaw=new THREE.Object3D,this.yaw.position.y=10,this.yaw.add(this.pitch),this.buttons={},this.getGamepad()||console.warn("Gamepad #%d not found. Connect controller and press any button to continue.",this.data.controller)},tick:function(t,e){this.updateRotation(e),this.updatePosition(e),this.updateButtonState()},updatePosition:function(t){var e=this.data,i=e.acceleration,n=e.easing,o=this.velocity,s=e.rollAxis,d=e.pitchAxis,u=this.el,c=this.getGamepad();if(t>a)return o[s]=0,void(o[d]=0);o[s]-=o[s]*n*t/1e3,o[d]-=o[d]*n*t/1e3;var h=u.getAttribute("position");if(e.enabled&&e.movementEnabled&&c){var l=this.getDpad(),p=l.x||this.getJoystick(0).x,E=l.y||this.getJoystick(0).y;Math.abs(p)>r&&(o[d]+=p*i*t/1e3),Math.abs(E)>r&&(o[s]+=E*i*t/1e3)}var f=this.getMovementVector(t);u.object3D.translateX(f.x),u.object3D.translateY(f.y),u.object3D.translateZ(f.z),u.setAttribute("position",{x:h.x+f.x,y:h.y+f.y,z:h.z+f.z})},getMovementVector:function(t){if(this._getMovementVector)return this._getMovementVector(t);var e=new THREE.Euler(0,0,0,"YXZ"),i=new THREE.Vector3;return this._getMovementVector=function(t){return i.copy(this.el.getAttribute("rotation")),this.direction.copy(this.velocity),this.direction.multiplyScalar(t/1e3),i?(this.data.flyEnabled||(i.x=0),e.set(THREE.Math.degToRad(i.x),THREE.Math.degToRad(i.y),0),this.direction.applyEuler(e),this.direction):this.direction},this._getMovementVector(t)},updateRotation:function(){if(this._updateRotation)return this._updateRotation();var t,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,o=0,a=0,d=1e-4,u=500;return this._updateRotation=function(){if(this.data.lookEnabled&&this.getGamepad()){if(t=Date.now(),e.copy(this.el.getAttribute("rotation")||e),e.distanceToSquared(i)>d&&e.distanceToSquared(n)>d)return i.copy(e),void(a=t);if(i.copy(e),!(u>t-a)){var c=this.getJoystick(1);Math.abs(c.x)<=r&&(c.x=0),Math.abs(c.y)<=r&&(c.y=0),this.data.invertAxisY&&(c.y=-c.y),a>o&&!c.lengthSq()||(c.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=c.x,this.pitch.rotation.x-=c.y,this.pitch.rotation.x=Math.max(-s,Math.min(s,this.pitch.rotation.x)),this.el.setAttribute("rotation",{x:THREE.Math.radToDeg(this.pitch.rotation.x),y:THREE.Math.radToDeg(this.yaw.rotation.y),z:0}),n.copy(this.el.getAttribute("rotation")),o=t)}}},this._updateRotation()},updateButtonState:function(){var t=this.getGamepad();if(this.data.enabled&&t)for(var e=0;e<t.buttons.length;e++)t.buttons[e].pressed&&!this.buttons[e]?this.emit(new o("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new o("gamepadbuttonup",e,t.buttons[e])),this.buttons[e]=t.buttons[e].pressed;else Object.keys(this.buttons)&&(this.buttons={})},emit:function(t){this.el.emit(t.type,t),this.el.emit(t.type+":"+t.index,new o(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads&&navigator.getGamepads()[this.data.controller],e=this.el.sceneEl.components["proxy-controls"],i=e&&e.isConnected()&&e.getGamepad(this.data.controller);return i||t},getButton:function(t){return this.getGamepad().buttons[t]},getAxis:function(t){return this.getGamepad().axes[t]},getJoystick:function(t){var e=this.getGamepad();switch(t){case 0:return new THREE.Vector2(e.axes[0],e.axes[1]);case 1:return new THREE.Vector2(e.axes[2],e.axes[3]);default:throw new Error('Unexpected joystick index "%d".',t)}},getDpad:function(){var t=this.getGamepad();return t.buttons[n.DPAD_RIGHT]?new THREE.Vector2((t.buttons[n.DPAD_RIGHT].pressed?1:0)+(t.buttons[n.DPAD_LEFT].pressed?-1:0),(t.buttons[n.DPAD_UP].pressed?-1:0)+(t.buttons[n.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id}}},function(t,e){t.exports=Object.assign(function(){},{FACE_1:0,FACE_2:1,FACE_3:2,FACE_4:3,L_SHOULDER_1:4,R_SHOULDER_1:5,L_SHOULDER_2:6,R_SHOULDER_2:7,SELECT:8,START:9,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15,VENDOR:16})},function(t,e){function i(t,e,i){this.type=t,this.index=e,this.pressed=i.pressed,this.value=i.value}t.exports=i}]);