소스 검색

loading the right model

Raanan Weber 5 년 전
부모
커밋
ce1f9e1be0
1개의 변경된 파일101개의 추가작업 그리고 4개의 파일을 삭제
  1. 101 4
      src/Gamepads/Controllers/windowsMotionController.ts

+ 101 - 4
src/Gamepads/Controllers/windowsMotionController.ts

@@ -106,9 +106,10 @@ export class WindowsMotionController extends WebVRController {
     private static readonly GAMEPAD_ID_PATTERN = /([0-9a-zA-Z]+-[0-9a-zA-Z]+)$/;
 
     private _loadedMeshInfo: Nullable<LoadedMeshInfo>;
-    private readonly _mapping = {
+    protected readonly _mapping = {
         // Semantic button names
         buttons: ['thumbstick', 'trigger', 'grip', 'menu', 'trackpad'],
+        // trigger, grip, trackpad, thumbstick, menu
 
         // A mapping of the button name to glTF model node name
         // that should be transformed by button value.
@@ -137,6 +138,7 @@ export class WindowsMotionController extends WebVRController {
             'TOUCHPAD_TOUCH_X',
             'TOUCHPAD_TOUCH_Y'
         ],
+        // upside down in webxr
         pointingPoseMeshName: PoseEnabledController.POINTING_POSE
     };
 
@@ -205,10 +207,10 @@ export class WindowsMotionController extends WebVRController {
         return this.onTrackpadValuesChangedObservable;
     }
 
-    private _updateTrackpad() {
+    protected _updateTrackpad() {
         if (this.browserGamepad.axes && (this.browserGamepad.axes[2] != this.trackpad.x || this.browserGamepad.axes[3] != this.trackpad.y)) {
-            this.trackpad.x = this.browserGamepad["axes"][2];
-            this.trackpad.y = this.browserGamepad["axes"][3];
+            this.trackpad.x = this.browserGamepad["axes"][this._mapping.axisMeshNames.indexOf('TOUCHPAD_TOUCH_X')];
+            this.trackpad.y = this.browserGamepad["axes"][this._mapping.axisMeshNames.indexOf('TOUCHPAD_TOUCH_Y')];
             this.onTrackpadValuesChangedObservable.notifyObservers(this.trackpad);
         }
     }
@@ -535,9 +537,104 @@ export class WindowsMotionController extends WebVRController {
         super.dispose();
 
         this.onTrackpadChangedObservable.clear();
+        this.onTrackpadValuesChangedObservable.clear();
     }
 }
 
+export class XRWindowsMotionController extends WindowsMotionController {
+
+    protected readonly _mapping = {
+        // Semantic button names
+        buttons: ['trigger', 'grip', 'trackpad', 'thumbstick', 'menu'],
+        // trigger, grip, trackpad, thumbstick, menu
+
+        // A mapping of the button name to glTF model node name
+        // that should be transformed by button value.
+        buttonMeshNames: {
+            'trigger': 'SELECT',
+            'menu': 'MENU',
+            'grip': 'GRASP',
+            'thumbstick': 'THUMBSTICK_PRESS',
+            'trackpad': 'TOUCHPAD_PRESS'
+        },
+        // This mapping is used to translate from the Motion Controller to Babylon semantics
+        buttonObservableNames: {
+            'trigger': 'onTriggerStateChangedObservable',
+            'menu': 'onSecondaryButtonStateChangedObservable',
+            'grip': 'onMainButtonStateChangedObservable',
+            'thumbstick': 'onThumbstickStateChangedObservable',
+            'trackpad': 'onTrackpadChangedObservable'
+        },
+        // A mapping of the axis name to glTF model node name
+        // that should be transformed by axis value.
+        // This array mirrors the browserGamepad.axes array, such that
+        // the mesh corresponding to axis 0 is in this array index 0.
+        axisMeshNames: [
+            'TOUCHPAD_TOUCH_X',
+            'TOUCHPAD_TOUCH_Y',
+            'THUMBSTICK_X',
+            'THUMBSTICK_Y'
+        ],
+        // upside down in webxr
+        pointingPoseMeshName: PoseEnabledController.POINTING_POSE
+    };
+
+    constructor(gamepadInfo: any) {
+        super(gamepadInfo);
+    }
+
+    public get thumbstickValues() {
+        return this.rightStick;
+    }
+
+    /**
+     * Fired when the thumbstick on this controller is clicked
+     */
+    public onThumbstickStateChangedObservable = new Observable<ExtendedGamepadButton>();
+    /**
+     * Fired when the thumbstick on this controller is modified
+     */
+    public onThumbstickValuesChangedObservable = new Observable<StickValues>();
+
+    /**
+     * Fired when the touchpad button on this controller is modified
+     */
+    public onTrackpadChangedObservable = this.onPadStateChangedObservable;
+
+    /**
+     * Fired when the touchpad values on this controller are modified
+     */
+    public onTrackpadValuesChangedObservable = this.onPadValuesChangedObservable;
+
+    /**
+     * Fired when the thumbstick button on this controller is modified
+     * here to prevent breaking changes
+     */
+    public get onThumbstickButtonStateChangedObservable(): Observable<ExtendedGamepadButton> {
+        return this.onThumbstickStateChangedObservable;
+    }
+
+    /**
+     * updating the thumbstick(!) and not the trackpad.
+     * This is named this way due to the difference between WebVR and XR and to avoid
+     * changing the parent class.
+     */
+    protected _updateTrackpad() {
+        if (this.browserGamepad.axes && (this.browserGamepad.axes[2] != this.thumbstickValues.x || this.browserGamepad.axes[3] != this.thumbstickValues.y)) {
+            this.trackpad.x = this.browserGamepad["axes"][2];
+            this.trackpad.y = this.browserGamepad["axes"][3];
+            this.onThumbstickValuesChangedObservable.notifyObservers(this.trackpad);
+        }
+    }
+
+    public dispose() {
+        super.dispose();
+        this.onThumbstickStateChangedObservable.clear();
+        this.onThumbstickValuesChangedObservable.clear();
+    }
+
+}
+
 PoseEnabledControllerHelper._ControllerFactories.push({
     canCreate: (gamepadInfo) => {
         return gamepadInfo.id.indexOf(WindowsMotionController.GAMEPAD_ID_PREFIX) === 0;