Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputManager: Modify Picking to only happen with necessary scenarios #13145

Merged
merged 11 commits into from
Nov 10, 2022
Next Next commit
lazy down
  • Loading branch information
PolygonalSun committed Oct 20, 2022
commit 195baa84e72e69ea38c37039904d1f6ed5c3fcc2
2 changes: 1 addition & 1 deletion packages/dev/core/src/Events/pointerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class PointerInfo extends PointerInfoBase {
/** @internal */
public _generatePickInfo(): void {
if (this._inputManager) {
this._pickInfo = this._inputManager._pickMove((this.event as IPointerEvent).pointerId);
this._pickInfo = this._inputManager._pick((this.event as IPointerEvent).pointerId, this.type);
this._inputManager._setRayOnPointerInfo(this._pickInfo, this.event);
this._inputManager = null;
}
Expand Down
68 changes: 43 additions & 25 deletions packages/dev/core/src/Inputs/scene.inputManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,30 @@ export class InputManager {
}

/** @internal */
public _pickMove(pointerId: number): Nullable<PickingInfo> {
public _pick(pointerId: number, type: PointerEventTypes): Nullable<PickingInfo> {
const scene = this._scene;
const pickResult = scene.pick(
this._unTranslatedPointerX,
this._unTranslatedPointerY,
scene.pointerMovePredicate,
false,
scene.cameraToUseForPointers,
scene.pointerMoveTrianglePredicate
);
let pickResult: Nullable<PickingInfo> = null;

this._setCursorAndPointerOverMesh(pickResult, pointerId, scene);
switch (type) {
case PointerEventTypes.POINTERDOWN:
pickResult = scene.pick(this._unTranslatedPointerX, this._unTranslatedPointerY, scene.pointerDownPredicate, false, scene.cameraToUseForPointers);
if (pickResult?.pickedMesh) {
this._pickedDownMesh = pickResult?.pickedMesh;
}
break;
case PointerEventTypes.POINTERMOVE:
pickResult = scene.pick(
this._unTranslatedPointerX,
this._unTranslatedPointerY,
scene.pointerMovePredicate,
false,
scene.cameraToUseForPointers,
scene.pointerMoveTrianglePredicate
);

this._setCursorAndPointerOverMesh(pickResult, pointerId, scene);
break;
}

return pickResult;
}
Expand Down Expand Up @@ -336,7 +348,7 @@ export class InputManager {

private _processPointerDown(pickResult: Nullable<PickingInfo>, evt: IPointerEvent): void {
const scene = this._scene;
if (pickResult && pickResult.hit && pickResult.pickedMesh) {
if (scene._registeredActions > 0 && pickResult?.pickedMesh) {
this._pickedDownMesh = pickResult.pickedMesh;
const actionManager = pickResult.pickedMesh._getActionManagerForTrigger();
if (actionManager) {
Expand Down Expand Up @@ -388,18 +400,24 @@ export class InputManager {
}
}

if (pickResult) {
const type = PointerEventTypes.POINTERDOWN;
let pointerInfo: PointerInfo;
const type = PointerEventTypes.POINTERDOWN;

if (scene.onPointerDown) {
scene.onPointerDown(evt, pickResult, type);
}
if (scene.onPointerDown) {
const pr = pickResult ? pickResult : this._pick(evt.pointerId, type);
// We know that _pick will return a PickingInfo because _internalPick always returns a PickingInfo
scene.onPointerDown(evt, pr!, type);
}

if (scene.onPointerObservable.hasObservers()) {
const pi = new PointerInfo(type, evt, pickResult);
this._setRayOnPointerInfo(pickResult, evt);
scene.onPointerObservable.notifyObservers(pi, type);
}
if (pickResult) {
pointerInfo = new PointerInfo(type, evt, pickResult);
this._setRayOnPointerInfo(pickResult, evt);
} else {
pointerInfo = new PointerInfo(type, evt, null, this);
}

if (scene.onPointerObservable.hasObservers()) {
scene.onPointerObservable.notifyObservers(pointerInfo, type);
}
}

Expand Down Expand Up @@ -725,7 +743,7 @@ export class InputManager {
(!scene.cameraToUseForPointers || (scene.cameraToUseForPointers.layerMask & mesh.layerMask) !== 0);
}

const pickResult = scene._registeredActions > 0 ? this._pickMove((evt as IPointerEvent).pointerId) : null;
const pickResult = scene._registeredActions > 0 ? this._pick((evt as IPointerEvent).pointerId, PointerEventTypes.POINTERMOVE) : null;
this._processPointerMove(pickResult, evt as IPointerEvent);
};

Expand Down Expand Up @@ -775,11 +793,11 @@ export class InputManager {

// Meshes
this._pickedDownMesh = null;
let pickResult;
if (scene.skipPointerDownPicking || (scene._registeredActions === 0 && !scene.onPointerObservable.hasObservers())) {
let pickResult = null;
if (scene.skipPointerDownPicking) {
pickResult = new PickingInfo();
} else {
pickResult = scene.pick(this._unTranslatedPointerX, this._unTranslatedPointerY, scene.pointerDownPredicate, false, scene.cameraToUseForPointers);
pickResult = scene.onPointerPick || scene._registeredActions > 0 ? this._pick((evt as IPointerEvent).pointerId, PointerEventTypes.POINTERDOWN) : null;
}

this._processPointerDown(pickResult, evt);
Expand Down
2 changes: 1 addition & 1 deletion packages/dev/core/src/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
/** Callback called when a pointer up is detected */
public onPointerUp: (evt: IPointerEvent, pickInfo: Nullable<PickingInfo>, type: PointerEventTypes) => void;
/** Callback called when a pointer pick is detected */
public onPointerPick: (evt: IPointerEvent, pickInfo: PickingInfo) => void;
public onPointerPick?: (evt: IPointerEvent, pickInfo: PickingInfo) => void;

/**
* Gets or sets a predicate used to select candidate faces for a pointer move event
Expand Down