浏览代码

Merge pull request #7994 from BabylonJS/master

Nightly
Raanan Weber 5 年之前
父节点
当前提交
bc17709390

+ 13 - 0
dist/preview release/babylon.d.ts

@@ -71040,6 +71040,19 @@ type XREye =
     | "left"
     | "right";
 
+type XREventType =
+    | "devicechange"
+    | "visibilitychange"
+    | "end"
+    | "inputsourceschange"
+    | "select"
+    | "selectstart"
+    | "selectend"
+    | "squeeze"
+    | "squeezestart"
+    | "squeezeend"
+    | "reset";
+
 interface XRSpace extends EventTarget {
 
 }

文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/babylon.js


+ 65 - 34
dist/preview release/babylon.max.js

@@ -173140,6 +173140,7 @@ var WebXRControllerPointerSelection = /** @class */ (function (_super) {
             // REMOVE the controller
             _this._detachController(controller.uniqueId);
         });
+        this._scene.constantlyUpdateMeshUnderPointer = true;
         return true;
     };
     /**
@@ -173319,47 +173320,68 @@ var WebXRControllerPointerSelection = /** @class */ (function (_super) {
     };
     WebXRControllerPointerSelection.prototype._attachTrackedPointerRayMode = function (xrController) {
         var _this = this;
-        xrController.onMotionControllerInitObservable.add(function (motionController) {
-            if (_this._options.forceGazeMode) {
-                return _this._attachGazeMode(xrController);
-            }
-            var controllerData = _this._controllers[xrController.uniqueId];
-            if (_this._options.overrideButtonId) {
-                controllerData.selectionComponent = motionController.getComponent(_this._options.overrideButtonId);
-            }
-            if (!controllerData.selectionComponent) {
-                controllerData.selectionComponent = motionController.getMainComponent();
+        var controllerData = this._controllers[xrController.uniqueId];
+        if (this._options.forceGazeMode) {
+            return this._attachGazeMode(xrController);
+        }
+        controllerData.onFrameObserver = this._xrSessionManager.onXRFrameObservable.add(function () {
+            controllerData.laserPointer.isVisible = _this.displayLaserPointer;
+            controllerData.laserPointer.material.disableLighting = _this.disablePointerLighting;
+            controllerData.selectionMesh.material.disableLighting = _this.disableSelectionMeshLighting;
+            if (controllerData.pick) {
+                _this._scene.simulatePointerMove(controllerData.pick, { pointerId: controllerData.id });
             }
-            controllerData.onFrameObserver = _this._xrSessionManager.onXRFrameObservable.add(function () {
-                if (controllerData.selectionComponent && controllerData.selectionComponent.pressed) {
+        });
+        if (xrController.inputSource.gamepad) {
+            xrController.onMotionControllerInitObservable.add(function (motionController) {
+                if (_this._options.overrideButtonId) {
+                    controllerData.selectionComponent = motionController.getComponent(_this._options.overrideButtonId);
+                }
+                if (!controllerData.selectionComponent) {
+                    controllerData.selectionComponent = motionController.getMainComponent();
+                }
+                controllerData.onButtonChangedObserver = controllerData.selectionComponent.onButtonStateChangedObservable.add(function (component) {
+                    if (component.changes.pressed) {
+                        var pressed = component.changes.pressed.current;
+                        if (controllerData.pick) {
+                            if (pressed) {
+                                _this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
+                                controllerData.selectionMesh.material.emissiveColor = _this.selectionMeshPickedColor;
+                                controllerData.laserPointer.material.emissiveColor = _this.laserPointerPickedColor;
+                            }
+                            else {
+                                _this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
+                                controllerData.selectionMesh.material.emissiveColor = _this.selectionMeshDefaultColor;
+                                controllerData.laserPointer.material.emissiveColor = _this.lasterPointerDefaultColor;
+                            }
+                        }
+                    }
+                });
+            });
+        }
+        else {
+            // use the select and squeeze events
+            var selectStartListener = function (event) {
+                if (event.inputSource === controllerData.xrController.inputSource && controllerData.pick) {
+                    _this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
                     controllerData.selectionMesh.material.emissiveColor = _this.selectionMeshPickedColor;
                     controllerData.laserPointer.material.emissiveColor = _this.laserPointerPickedColor;
                 }
-                else {
+            };
+            var selectEndListener = function (event) {
+                if (event.inputSource === controllerData.xrController.inputSource && controllerData.pick) {
+                    _this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
                     controllerData.selectionMesh.material.emissiveColor = _this.selectionMeshDefaultColor;
                     controllerData.laserPointer.material.emissiveColor = _this.lasterPointerDefaultColor;
                 }
-                controllerData.laserPointer.isVisible = _this.displayLaserPointer;
-                controllerData.laserPointer.material.disableLighting = _this.disablePointerLighting;
-                controllerData.selectionMesh.material.disableLighting = _this.disableSelectionMeshLighting;
-                if (controllerData.pick) {
-                    _this._scene.simulatePointerMove(controllerData.pick, { pointerId: controllerData.id });
-                }
-            });
-            controllerData.onButtonChangedObserver = controllerData.selectionComponent.onButtonStateChangedObservable.add(function (component) {
-                if (component.changes.pressed) {
-                    var pressed = component.changes.pressed.current;
-                    if (controllerData.pick) {
-                        if (pressed) {
-                            _this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
-                        }
-                        else {
-                            _this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
-                        }
-                    }
-                }
-            });
-        });
+            };
+            controllerData.eventListeners = {
+                selectend: selectEndListener,
+                selectstart: selectStartListener
+            };
+            this._xrSessionManager.session.addEventListener('selectstart', selectStartListener);
+            this._xrSessionManager.session.addEventListener('selectend', selectEndListener);
+        }
     };
     WebXRControllerPointerSelection.prototype._convertNormalToDirectionOfRay = function (normal, ray) {
         if (normal) {
@@ -173371,6 +173393,7 @@ var WebXRControllerPointerSelection = /** @class */ (function (_super) {
         return normal;
     };
     WebXRControllerPointerSelection.prototype._detachController = function (xrControllerUniqueId) {
+        var _this = this;
         var controllerData = this._controllers[xrControllerUniqueId];
         if (!controllerData) {
             return;
@@ -173383,6 +173406,14 @@ var WebXRControllerPointerSelection = /** @class */ (function (_super) {
         if (controllerData.onFrameObserver) {
             this._xrSessionManager.onXRFrameObservable.remove(controllerData.onFrameObserver);
         }
+        if (controllerData.eventListeners) {
+            Object.keys(controllerData.eventListeners).forEach(function (eventName) {
+                var func = controllerData.eventListeners && controllerData.eventListeners[eventName];
+                if (func) {
+                    _this._xrSessionManager.session.removeEventListener(eventName, func);
+                }
+            });
+        }
         controllerData.selectionMesh.dispose();
         controllerData.laserPointer.dispose();
         // remove from the map

文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/babylon.max.js.map


+ 13 - 0
dist/preview release/babylon.module.d.ts

@@ -145469,6 +145469,19 @@ type XREye =
     | "left"
     | "right";
 
+type XREventType =
+    | "devicechange"
+    | "visibilitychange"
+    | "end"
+    | "inputsourceschange"
+    | "select"
+    | "selectstart"
+    | "selectend"
+    | "squeeze"
+    | "squeezestart"
+    | "squeezeend"
+    | "reset";
+
 interface XRSpace extends EventTarget {
 
 }

+ 13 - 0
dist/preview release/documentation.d.ts

@@ -71040,6 +71040,19 @@ type XREye =
     | "left"
     | "right";
 
+type XREventType =
+    | "devicechange"
+    | "visibilitychange"
+    | "end"
+    | "inputsourceschange"
+    | "select"
+    | "selectstart"
+    | "selectend"
+    | "squeeze"
+    | "squeezestart"
+    | "squeezeend"
+    | "reset";
+
 interface XRSpace extends EventTarget {
 
 }

+ 13 - 0
dist/preview release/viewer/babylon.module.d.ts

@@ -145469,6 +145469,19 @@ type XREye =
     | "left"
     | "right";
 
+type XREventType =
+    | "devicechange"
+    | "visibilitychange"
+    | "end"
+    | "inputsourceschange"
+    | "select"
+    | "selectstart"
+    | "selectend"
+    | "squeeze"
+    | "squeezestart"
+    | "squeezeend"
+    | "reset";
+
 interface XRSpace extends EventTarget {
 
 }

文件差异内容过多而无法显示
+ 4 - 4
dist/preview release/viewer/babylon.viewer.js


文件差异内容过多而无法显示
+ 3 - 3
dist/preview release/viewer/babylon.viewer.max.js


+ 1 - 0
dist/preview release/what's new.md

@@ -80,6 +80,7 @@
 - AR reference space type recommendation changed to 'unbounded' ([#7959](https://github.com/BabylonJS/Babylon.js/issues/7959)) ([RaananW](https://github.com/RaananW))
 - Teleportation plugin doesn't use the touched to finish teleportation ([#7916](https://github.com/BabylonJS/Babylon.js/issues/7916)) ([RaananW](https://github.com/RaananW))
 - Support for pointer selection and teleportation in right handed systems ([#7967](https://github.com/BabylonJS/Babylon.js/issues/7967)) ([RaananW](https://github.com/RaananW))
+- Pointer Selection feature now uses `selectstart` and `selectend` events when gamepad is not present ([#7989](https://github.com/BabylonJS/Babylon.js/issues/7989)) ([RaananW](https://github.com/RaananW))
 
 ### Collisions
 

+ 13 - 0
src/LibDeclarations/webxr.d.ts

@@ -35,6 +35,19 @@ type XREye =
     | "left"
     | "right";
 
+type XREventType =
+    | "devicechange"
+    | "visibilitychange"
+    | "end"
+    | "inputsourceschange"
+    | "select"
+    | "selectstart"
+    | "selectend"
+    | "squeeze"
+    | "squeezestart"
+    | "squeezeend"
+    | "reset";
+
 interface XRSpace extends EventTarget {
 
 }

+ 67 - 34
src/XR/features/WebXRControllerPointerSelection.ts

@@ -113,6 +113,8 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature {
             pick: Nullable<PickingInfo>;
             id: number;
             tmpRay: Ray;
+            // event support
+            eventListeners?: {[event in XREventType]?: ((event: XRInputSourceEvent) => void)};
         };
     } = {};
     private _scene: Scene;
@@ -196,6 +198,8 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature {
             this._detachController(controller.uniqueId);
         });
 
+        this._scene.constantlyUpdateMeshUnderPointer = true;
+
         return true;
     }
 
@@ -380,50 +384,71 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature {
     }
 
     private _attachTrackedPointerRayMode(xrController: WebXRInputSource) {
-        xrController.onMotionControllerInitObservable.add((motionController) => {
-            if (this._options.forceGazeMode) {
-                return this._attachGazeMode(xrController);
-            }
-
-            const controllerData = this._controllers[xrController.uniqueId];
+        const controllerData = this._controllers[xrController.uniqueId];
+        if (this._options.forceGazeMode) {
+            return this._attachGazeMode(xrController);
+        }
+        controllerData.onFrameObserver = this._xrSessionManager.onXRFrameObservable.add(() => {
+            controllerData.laserPointer.isVisible = this.displayLaserPointer;
+            (<StandardMaterial>controllerData.laserPointer.material).disableLighting = this.disablePointerLighting;
+            (<StandardMaterial>controllerData.selectionMesh.material).disableLighting = this.disableSelectionMeshLighting;
 
-            if (this._options.overrideButtonId) {
-                controllerData.selectionComponent = motionController.getComponent(this._options.overrideButtonId);
-            }
-            if (!controllerData.selectionComponent) {
-                controllerData.selectionComponent = motionController.getMainComponent();
+            if (controllerData.pick) {
+                this._scene.simulatePointerMove(controllerData.pick, { pointerId: controllerData.id });
             }
+        });
+        if (xrController.inputSource.gamepad) {
+            xrController.onMotionControllerInitObservable.add((motionController) => {
+                if (this._options.overrideButtonId) {
+                    controllerData.selectionComponent = motionController.getComponent(this._options.overrideButtonId);
+                }
+                if (!controllerData.selectionComponent) {
+                    controllerData.selectionComponent = motionController.getMainComponent();
+                }
 
-            controllerData.onFrameObserver = this._xrSessionManager.onXRFrameObservable.add(() => {
-                if (controllerData.selectionComponent && controllerData.selectionComponent.pressed) {
+                controllerData.onButtonChangedObserver = controllerData.selectionComponent.onButtonStateChangedObservable.add((component) => {
+                    if (component.changes.pressed) {
+                        const pressed = component.changes.pressed.current;
+                        if (controllerData.pick) {
+                            if (pressed) {
+                                this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
+                                (<StandardMaterial>controllerData.selectionMesh.material).emissiveColor = this.selectionMeshPickedColor;
+                                (<StandardMaterial>controllerData.laserPointer.material).emissiveColor = this.laserPointerPickedColor;
+                            } else {
+                                this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
+                                (<StandardMaterial>controllerData.selectionMesh.material).emissiveColor = this.selectionMeshDefaultColor;
+                        (<StandardMaterial>controllerData.laserPointer.material).emissiveColor = this.lasterPointerDefaultColor;
+                            }
+                        }
+                    }
+                });
+            });
+        } else {
+            // use the select and squeeze events
+            const selectStartListener = (event: XRInputSourceEvent) => {
+                if (event.inputSource === controllerData.xrController.inputSource && controllerData.pick) {
+                    this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
                     (<StandardMaterial>controllerData.selectionMesh.material).emissiveColor = this.selectionMeshPickedColor;
                     (<StandardMaterial>controllerData.laserPointer.material).emissiveColor = this.laserPointerPickedColor;
-                } else {
+                }
+            };
+
+            const selectEndListener = (event: XRInputSourceEvent) => {
+                if (event.inputSource === controllerData.xrController.inputSource && controllerData.pick) {
+                    this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
                     (<StandardMaterial>controllerData.selectionMesh.material).emissiveColor = this.selectionMeshDefaultColor;
                     (<StandardMaterial>controllerData.laserPointer.material).emissiveColor = this.lasterPointerDefaultColor;
                 }
-                controllerData.laserPointer.isVisible = this.displayLaserPointer;
-                (<StandardMaterial>controllerData.laserPointer.material).disableLighting = this.disablePointerLighting;
-                (<StandardMaterial>controllerData.selectionMesh.material).disableLighting = this.disableSelectionMeshLighting;
+            };
 
-                if (controllerData.pick) {
-                    this._scene.simulatePointerMove(controllerData.pick, { pointerId: controllerData.id });
-                }
-            });
+            controllerData.eventListeners = {
+                selectend: selectEndListener,
+                selectstart: selectStartListener
+            };
 
-            controllerData.onButtonChangedObserver = controllerData.selectionComponent.onButtonStateChangedObservable.add((component) => {
-                if (component.changes.pressed) {
-                    const pressed = component.changes.pressed.current;
-                    if (controllerData.pick) {
-                        if (pressed) {
-                            this._scene.simulatePointerDown(controllerData.pick, { pointerId: controllerData.id });
-                        } else {
-                            this._scene.simulatePointerUp(controllerData.pick, { pointerId: controllerData.id });
-                        }
-                    }
-                }
-            });
-        });
+            this._xrSessionManager.session.addEventListener('selectstart', selectStartListener);
+            this._xrSessionManager.session.addEventListener('selectend', selectEndListener);
+        }
     }
 
     private _convertNormalToDirectionOfRay(normal: Nullable<Vector3>, ray: Ray) {
@@ -447,6 +472,14 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature {
         if (controllerData.onFrameObserver) {
             this._xrSessionManager.onXRFrameObservable.remove(controllerData.onFrameObserver);
         }
+        if (controllerData.eventListeners) {
+            Object.keys(controllerData.eventListeners).forEach((eventName: string) => {
+                const func = controllerData.eventListeners && controllerData.eventListeners[eventName as XREventType];
+                if (func) {
+                    this._xrSessionManager.session.removeEventListener(eventName, func);
+                }
+            });
+        }
         controllerData.selectionMesh.dispose();
         controllerData.laserPointer.dispose();
         // remove from the map