瀏覽代碼

allow using online or local controller repo

Raanan Weber 5 年之前
父節點
當前提交
5ef98f1347

+ 1 - 1
src/Cameras/XR/motionController/webXRGenericMotionController.ts

@@ -76,7 +76,7 @@ export class WebXRGenericTriggerMotionController extends WebXRAbstractMotionCont
     public profileId = WebXRGenericTriggerMotionController.ProfileId;
 
     constructor(scene: Scene, gamepadObject: IMinimalMotionControllerObject, handness: MotionControllerHandness) {
-        super(scene, GenericTriggerLayout["left-right-none"], gamepadObject, handness);
+        super(scene, GenericTriggerLayout[handness], gamepadObject, handness);
     }
 
     protected _processLoadedModel(meshes: AbstractMesh[]): void {

+ 0 - 23
src/Cameras/XR/motionController/webXRHTCViveMotionController.ts

@@ -163,29 +163,6 @@ const HTCViveLayout: IMotionControllerLayoutMap = {
     }
 };
 
-// const HTCViveLegacyLayout: IMotionControllerLayoutMap = {
-//     "left-right-none": {
-//         "selectComponentId": "xr-standard-trigger",
-//         "components": {
-//             "xr-standard-trigger": { "type": "trigger" },
-//             "xr-standard-squeeze": { "type": "squeeze" },
-//             "xr-standard-touchpad": { "type": "touchpad" }
-//         },
-//         "gamepad": {
-//             "mapping": "",
-//             "buttons": [
-//                 "xr-standard-touchpad",
-//                 "xr-standard-trigger",
-//                 "xr-standard-squeeze"
-//             ],
-//             "axes": [
-//                 { "componentId": "xr-standard-touchpad", "axis": "x-axis" },
-//                 { "componentId": "xr-standard-touchpad", "axis": "y-axis" }
-//             ]
-//         }
-//     }
-// };
-
 /**
  * The motion controller class for the standard HTC-Vive controllers
  */

+ 63 - 44
src/Cameras/XR/motionController/webXRMotionControllerManager.ts

@@ -21,6 +21,8 @@ export type MotionControllerConstructor = (xrInput: XRInputSource, scene: Scene)
  */
 export class WebXRMotionControllerManager {
     public static BaseRepositoryUrl = "https://immersive-web.github.io/webxr-input-profiles/packages/viewer/dist";
+    public static UseOnlineRepository: boolean = true;
+    public static PrioritizeOnlineRepository: boolean = true;
     private static _AvailableControllers: { [type: string]: MotionControllerConstructor } = {};
     private static _Fallbacks: { [profileId: string]: string[] } = {};
 
@@ -57,62 +59,66 @@ export class WebXRMotionControllerManager {
         }
         profileArray.push(...(xrInput.profiles || []));
 
-        // try using the gamepad id
+        // emulator support
+        if (profileArray.length && !profileArray[0]) {
+            // remove the first "undefined" that the emulator is adding
+            profileArray.pop();
+        }
+
+        // legacy support - try using the gamepad id
         if (xrInput.gamepad && xrInput.gamepad.id) {
             switch (xrInput.gamepad.id) {
                 case (xrInput.gamepad.id.match(/oculus touch/gi) ? xrInput.gamepad.id : undefined):
-                    // oculus in gamepad id - legacy mapping
-                    // return Promise.resolve(this._AvailableControllers["oculus-touch-legacy"](xrInput, scene));
+                    // oculus in gamepad id
                     profileArray.push("oculus-touch-v2");
                     break;
-                case (xrInput.gamepad.id.match(/Spatial Controller/gi) ? xrInput.gamepad.id : undefined):
-                    // oculus in gamepad id - legacy mapping
-                    // return Promise.resolve(this._AvailableControllers["microsoft-mixed-reality"](xrInput, scene));
-                    break;
-                case (xrInput.gamepad.id.match(/openvr/gi) ? xrInput.gamepad.id : undefined):
-                    // oculus in gamepad id - legacy mapping
-                    // return Promise.resolve(this._AvailableControllers["htc-vive-legacy"](xrInput, scene));
-                    break;
             }
         }
 
         // make sure microsoft/windows mixed reality works correctly
         const windowsMRIdx = profileArray.indexOf("windows-mixed-reality");
         if (windowsMRIdx !== -1) {
-            profileArray.splice(windowsMRIdx, 1, "microsoft-mixed-reality");
+            profileArray.splice(windowsMRIdx, 0, "microsoft-mixed-reality");
         }
 
-        // for (let i = 0; i < profileArray.length; ++i) {
-        //     const constructionFunction = this._AvailableControllers[profileArray[i]];
-        //     if (constructionFunction) {
-        //         return Promise.resolve(constructionFunction(xrInput, scene));
-        //     }
-        // }
-
         if (!profileArray.length) {
-            profileArray.push("generic-button");
+            profileArray.push("generic-trigger");
+        }
+
+        if (this.UseOnlineRepository) {
+            const firstFunction = this.PrioritizeOnlineRepository ? this._LoadProfileFromRepository : this._LoadProfilesFromAvailableControllers;
+            const secondFunction = this.PrioritizeOnlineRepository ? this._LoadProfilesFromAvailableControllers : this._LoadProfileFromRepository;
+
+            return firstFunction.call(this, profileArray, xrInput, scene).catch(() => {
+                return secondFunction.call(this, profileArray, xrInput, scene);
+            });
+
+        } else {
+            // use only available functions
+            return this._LoadProfilesFromAvailableControllers(profileArray, xrInput, scene);
+        }
+    }
+
+    private static _LoadProfilesFromAvailableControllers(profileArray: string[], xrInput: XRInputSource, scene: Scene) {
+        // check fallbacks
+        for (let i = 0; i < profileArray.length; ++i) {
+            // defensive
+            if (!profileArray[i]) {
+                continue;
+            }
+            const fallbacks = this.FindFallbackWithProfileId(profileArray[i]);
+            for (let j = 0; j < fallbacks.length; ++j) {
+                const constructionFunction = this._AvailableControllers[fallbacks[j]];
+                if (constructionFunction) {
+                    return Promise.resolve(constructionFunction(xrInput, scene));
+                }
+            }
         }
 
-        // TODO use local repository, if asked by the user
-
-        return this._LoadProfileFromRepository(profileArray, xrInput, scene);
-        // // check fallbacks
-        // for (let i = 0; i < profileArray.length; ++i) {
-        //     const fallbacks = this.FindFallbackWithProfileId(profileArray[i]);
-        //     for (let j = 0; j < fallbacks.length; ++j) {
-        //         const constructionFunction = this._AvailableControllers[fallbacks[j]];
-        //         if (constructionFunction) {
-        //             return Promise.resolve(constructionFunction(xrInput, scene));
-        //         }
-        //     }
-        // }
-        // return the most generic thing we have
-        // return this._AvailableControllers[WebXRGenericTriggerMotionController.ProfileId](xrInput, scene);
-
-        // Check if there is an online profile and use it if possible
+        throw new Error(`no controller requested was found in the available controllers list`);
     }
 
-    private static _ProfilesList: { [profile: string]: string };
+    private static _ProfilesList: Promise<{ [profile: string]: string }>;
 
     // cache for loading
     private static _ProfileLoadingPromises: { [profileName: string]: Promise<IMotionControllerProfile> } = {};
@@ -127,12 +133,16 @@ export class WebXRMotionControllerManager {
         }).then((profilesList: { [profile: string]: string }) => {
             // load the right profile
             for (let i = 0; i < profileArray.length; ++i) {
+                // defensive
+                if (!profileArray[i]) {
+                    continue;
+                }
                 if (profilesList[profileArray[i]]) {
                     return profileArray[i];
                 }
             }
 
-            return "generic-button";
+            throw new Error(`neither controller ${profileArray[0]} nor all fallbacks were found in the repository,`);
         }).then((profileToLoad: string) => {
             // load the profile
             if (!this._ProfileLoadingPromises[profileToLoad]) {
@@ -145,16 +155,22 @@ export class WebXRMotionControllerManager {
 
     }
 
-    public static ClearProfileCache() {
+    /**
+     * Clear the cache used for profile loading and reload when requested again
+     */
+    public static ClearProfilesCache() {
         delete this._ProfilesList;
         this._ProfileLoadingPromises = {};
     }
 
+    /**
+     * Will update the list of profiles available in the repository
+     */
     public static UpdateProfilesList() {
-        return Tools.LoadFileAsync(this.BaseRepositoryUrl + '/profiles/profilesList.json', false).then((data) => {
-            this._ProfilesList = JSON.parse(data.toString());
-            return this._ProfilesList;
+        this._ProfilesList = Tools.LoadFileAsync(this.BaseRepositoryUrl + '/profiles/profilesList.json', false).then((data) => {
+            return JSON.parse(data.toString());
         });
+        return this._ProfilesList;
     }
 
     /**
@@ -163,7 +179,10 @@ export class WebXRMotionControllerManager {
      * @return an array with corresponding fallback profiles
      */
     public static FindFallbackWithProfileId(profileId: string): string[] {
-        return this._Fallbacks[profileId] || [];
+        const returnArray = this._Fallbacks[profileId] || [];
+
+        returnArray.unshift(profileId);
+        return returnArray;
     }
 
     /**

+ 150 - 151
src/Cameras/XR/motionController/webXROculusTouchMotionController.ts

@@ -10,7 +10,155 @@ import { Scene } from '../../../scene';
 import { Mesh } from '../../../Meshes/mesh';
 import { Quaternion } from '../../../Maths/math.vector';
 
-// https://github.com/immersive-web/webxr-input-profiles/blob/master/packages/registry/profiles/microsoft/microsoft-mixed-reality.json
+/**
+ * The motion controller class for oculus touch (quest, rift).
+ * This class supports legacy mapping as well the standard xr mapping
+ */
+export class WebXROculusTouchMotionController extends WebXRAbstractMotionController {
+    /**
+     * The base url used to load the left and right controller models
+     */
+    public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculus/';
+    /**
+     * The name of the left controller model file
+     */
+    public static MODEL_LEFT_FILENAME: string = 'left.babylon';
+    /**
+     * The name of the right controller model file
+     */
+    public static MODEL_RIGHT_FILENAME: string = 'right.babylon';
+
+    /**
+     * Base Url for the Quest controller model.
+     */
+    public static QUEST_MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculusQuest/';
+
+    public profileId = "oculus-touch";
+
+    private _modelRootNode: AbstractMesh;
+
+    constructor(scene: Scene,
+        gamepadObject: IMinimalMotionControllerObject,
+        handness: MotionControllerHandness,
+        legacyMapping: boolean = false,
+        private _forceLegacyControllers: boolean = false) {
+        super(scene, legacyMapping ? OculusTouchLegacyLayouts[handness] : OculusTouchLayouts[handness], gamepadObject, handness);
+    }
+
+    protected _processLoadedModel(_meshes: AbstractMesh[]): void {
+
+        /*const isQuest = this._isQuest();
+        const triggerDirection = this.handness === 'right' ? -1 : 1;
+
+        this.layout.gamepad!.buttons.forEach((buttonName) => {
+            const comp = buttonName && this.getComponent(buttonName);
+            if (comp) {
+                comp.onButtonStateChanged.add((component) => {
+
+                    if (!this.rootMesh) { return; }
+
+                    switch (buttonName) {
+                        case "xr-standard-trigger": // index trigger
+                            if (!isQuest) {
+                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).rotation.x = -component.value * 0.20;
+                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.y = -component.value * 0.005;
+                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.z = -component.value * 0.005;
+                            }
+                            return;
+                        case "xr-standard-squeeze":  // secondary trigger
+                            if (!isQuest) {
+                                (<AbstractMesh>(this._modelRootNode.getChildren()[4])).position.x = triggerDirection * component.value * 0.0035;
+                            }
+                            return;
+                        case "xr-standard-thumbstick": // thumbstick
+                            return;
+                        case "a-button":
+                        case "x-button":
+                            if (!isQuest) {
+                                if (component.pressed) {
+                                    (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = -0.001;
+                                }
+                                else {
+                                    (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = 0;
+                                }
+                            }
+                            return;
+                        case "b-button":
+                        case "y-button":
+                            if (!isQuest) {
+                                if (component.pressed) {
+                                    (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = -0.001;
+                                }
+                                else {
+                                    (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = 0;
+                                }
+                            }
+                            return;
+                    }
+                }, undefined, true);
+            }
+        });*/
+    }
+
+    protected _getFilenameAndPath(): { filename: string; path: string; } {
+        let filename = "";
+        if (this.handness === 'left') {
+            filename = WebXROculusTouchMotionController.MODEL_LEFT_FILENAME;
+        }
+        else { // Right is the default if no hand is specified
+            filename = WebXROculusTouchMotionController.MODEL_RIGHT_FILENAME;
+        }
+
+        let path = this._isQuest() ? WebXROculusTouchMotionController.QUEST_MODEL_BASE_URL : WebXROculusTouchMotionController.MODEL_BASE_URL;
+        return {
+            filename,
+            path
+        };
+    }
+
+    /**
+     * Is this the new type of oculus touch. At the moment both have the same profile and it is impossible to differentiate
+     * between the touch and touch 2.
+     */
+    private _isQuest() {
+        // this is SADLY the only way to currently check. Until proper profiles will be available.
+        return !!navigator.userAgent.match(/Quest/gi) && !this._forceLegacyControllers;
+    }
+
+    protected _updateModel(): void {
+        // no-op. model is updated using observables.
+    }
+
+    protected _getModelLoadingConstraints(): boolean {
+        return true;
+    }
+
+    protected _setRootMesh(meshes: AbstractMesh[]): void {
+        this.rootMesh = new Mesh(this.profileId + " " + this.handness, this.scene);
+        this.rootMesh.rotationQuaternion = Quaternion.FromEulerAngles(0, Math.PI, 0);
+
+        meshes.forEach((mesh) => { mesh.isPickable = false; });
+        if (this._isQuest()) {
+            this._modelRootNode = meshes[0];
+        } else {
+            this._modelRootNode = meshes[1];
+            this.rootMesh.position.y = 0.034;
+            this.rootMesh.position.z = 0.052;
+        }
+        this._modelRootNode.parent = this.rootMesh;
+    }
+
+}
+
+// register the profile
+WebXRMotionControllerManager.RegisterController("oculus-touch", (xrInput: XRInputSource, scene: Scene) => {
+    return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness);
+});
+
+WebXRMotionControllerManager.RegisterController("oculus-touch-legacy", (xrInput: XRInputSource, scene: Scene) => {
+    return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness, true);
+});
+
 const OculusTouchLayouts: IMotionControllerLayoutMap = {
     "left": {
         "selectComponentId": "xr-standard-trigger",
@@ -295,153 +443,4 @@ const OculusTouchLegacyLayouts: IMotionControllerLayoutMap = {
         "rootNodeName": "oculus-touch-v2-right",
         "assetPath": "right.glb"
     }
-};
-
-/**
- * The motion controller class for oculus touch (quest, rift).
- * This class supports legacy mapping as well the standard xr mapping
- */
-export class WebXROculusTouchMotionController extends WebXRAbstractMotionController {
-    /**
-     * The base url used to load the left and right controller models
-     */
-    public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculus/';
-    /**
-     * The name of the left controller model file
-     */
-    public static MODEL_LEFT_FILENAME: string = 'left.babylon';
-    /**
-     * The name of the right controller model file
-     */
-    public static MODEL_RIGHT_FILENAME: string = 'right.babylon';
-
-    /**
-     * Base Url for the Quest controller model.
-     */
-    public static QUEST_MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculusQuest/';
-
-    public profileId = "oculus-touch";
-
-    private _modelRootNode: AbstractMesh;
-
-    constructor(scene: Scene,
-        gamepadObject: IMinimalMotionControllerObject,
-        handness: MotionControllerHandness,
-        legacyMapping: boolean = false,
-        private _forceLegacyControllers: boolean = false) {
-        super(scene, legacyMapping ? OculusTouchLegacyLayouts[handness] : OculusTouchLayouts[handness], gamepadObject, handness);
-    }
-
-    protected _processLoadedModel(_meshes: AbstractMesh[]): void {
-
-        /*const isQuest = this._isQuest();
-        const triggerDirection = this.handness === 'right' ? -1 : 1;
-
-        this.layout.gamepad!.buttons.forEach((buttonName) => {
-            const comp = buttonName && this.getComponent(buttonName);
-            if (comp) {
-                comp.onButtonStateChanged.add((component) => {
-
-                    if (!this.rootMesh) { return; }
-
-                    switch (buttonName) {
-                        case "xr-standard-trigger": // index trigger
-                            if (!isQuest) {
-                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).rotation.x = -component.value * 0.20;
-                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.y = -component.value * 0.005;
-                                (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.z = -component.value * 0.005;
-                            }
-                            return;
-                        case "xr-standard-squeeze":  // secondary trigger
-                            if (!isQuest) {
-                                (<AbstractMesh>(this._modelRootNode.getChildren()[4])).position.x = triggerDirection * component.value * 0.0035;
-                            }
-                            return;
-                        case "xr-standard-thumbstick": // thumbstick
-                            return;
-                        case "a-button":
-                        case "x-button":
-                            if (!isQuest) {
-                                if (component.pressed) {
-                                    (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = -0.001;
-                                }
-                                else {
-                                    (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = 0;
-                                }
-                            }
-                            return;
-                        case "b-button":
-                        case "y-button":
-                            if (!isQuest) {
-                                if (component.pressed) {
-                                    (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = -0.001;
-                                }
-                                else {
-                                    (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = 0;
-                                }
-                            }
-                            return;
-                    }
-                }, undefined, true);
-            }
-        });*/
-    }
-
-    protected _getFilenameAndPath(): { filename: string; path: string; } {
-        let filename = "";
-        if (this.handness === 'left') {
-            filename = WebXROculusTouchMotionController.MODEL_LEFT_FILENAME;
-        }
-        else { // Right is the default if no hand is specified
-            filename = WebXROculusTouchMotionController.MODEL_RIGHT_FILENAME;
-        }
-
-        let path = this._isQuest() ? WebXROculusTouchMotionController.QUEST_MODEL_BASE_URL : WebXROculusTouchMotionController.MODEL_BASE_URL;
-        return {
-            filename,
-            path
-        };
-    }
-
-    /**
-     * Is this the new type of oculus touch. At the moment both have the same profile and it is impossible to differentiate
-     * between the touch and touch 2.
-     */
-    private _isQuest() {
-        // this is SADLY the only way to currently check. Until proper profiles will be available.
-        return !!navigator.userAgent.match(/Quest/gi) && !this._forceLegacyControllers;
-    }
-
-    protected _updateModel(): void {
-        // no-op. model is updated using observables.
-    }
-
-    protected _getModelLoadingConstraints(): boolean {
-        return true;
-    }
-
-    protected _setRootMesh(meshes: AbstractMesh[]): void {
-        this.rootMesh = new Mesh(this.profileId + " " + this.handness, this.scene);
-        this.rootMesh.rotationQuaternion = Quaternion.FromEulerAngles(0, Math.PI, 0);
-
-        meshes.forEach((mesh) => { mesh.isPickable = false; });
-        if (this._isQuest()) {
-            this._modelRootNode = meshes[0];
-        } else {
-            this._modelRootNode = meshes[1];
-            this.rootMesh.position.y = 0.034;
-            this.rootMesh.position.z = 0.052;
-        }
-        this._modelRootNode.parent = this.rootMesh;
-    }
-
-}
-
-// register the profile
-WebXRMotionControllerManager.RegisterController("oculus-touch", (xrInput: XRInputSource, scene: Scene) => {
-    return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness);
-});
-
-WebXRMotionControllerManager.RegisterController("oculus-touch-legacy", (xrInput: XRInputSource, scene: Scene) => {
-    return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness, true);
-});
+};

+ 15 - 2
src/Cameras/XR/webXRInput.ts

@@ -27,7 +27,12 @@ export interface IWebXRInputOptions {
      *
      * Instead, use the controllers available in babylon itself.
      */
-    useOnlyLocalControllers?: boolean;
+    disableOnlineControllerRepository?: boolean;
+
+    /**
+     * A custom URL for the controllers repository
+     */
+    customControllersRepositoryURL?: string;
 }
 /**
  * XR input used to track XR inputs such as controllers/rays
@@ -82,8 +87,16 @@ export class WebXRInput implements IDisposable {
             });
         });
 
-        if (!this.options.useOnlyLocalControllers) {
+        if (this.options.customControllersRepositoryURL) {
+            WebXRMotionControllerManager.BaseRepositoryUrl = this.options.customControllersRepositoryURL;
+        }
+
+        if (!this.options.disableOnlineControllerRepository) {
+            WebXRMotionControllerManager.UseOnlineRepository = true;
+            // pre-load the profiles list to load the controllers quicker afterwards
             WebXRMotionControllerManager.UpdateProfilesList();
+        } else {
+            WebXRMotionControllerManager.UseOnlineRepository = false;
         }
     }