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

Implement Lazy Picking for POINTERMOVE #13044

Merged
merged 29 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bf65e33
Lazy Pick on Move
PolygonalSun Sep 8, 2022
07785a7
Change to account for undefined
PolygonalSun Sep 8, 2022
757a531
cameras using DSM
PolygonalSun Sep 8, 2022
5c4cd98
Check for reasons to pick before picking
PolygonalSun Sep 8, 2022
ec05176
Addressed some feedback (missing meshUnderPointer)
PolygonalSun Sep 15, 2022
2fbd9db
Modified get meshUnderPointer
PolygonalSun Sep 16, 2022
166a60c
Add frame awareness picking for move
PolygonalSun Sep 17, 2022
a81875c
Merge branch 'master' into input-perf-test
PolygonalSun Sep 20, 2022
fa70f57
Caching Test
PolygonalSun Sep 27, 2022
d7d0986
Merge branch 'master' into input-perf-test
PolygonalSun Sep 27, 2022
b4db311
format
PolygonalSun Sep 27, 2022
0502afa
Merge branch 'input-perf-test' of https://github.com/PolygonalSun/Bab…
PolygonalSun Sep 27, 2022
fdf4934
Merge branch 'master' into input-perf-test
PolygonalSun Sep 28, 2022
568d43a
import fix
PolygonalSun Sep 28, 2022
5dd409d
Removed test code
PolygonalSun Sep 28, 2022
85c814f
PR Feedback
PolygonalSun Oct 6, 2022
0ccc7a2
fixed import
PolygonalSun Oct 6, 2022
a0ffe9c
removed gesture recognizer and reverted inputs
PolygonalSun Oct 7, 2022
51ee8cd
Revert videoDome because it uses picking info
PolygonalSun Oct 7, 2022
a9801ee
formatting
PolygonalSun Oct 7, 2022
3aab516
Changed to just MOVE Lazy Picking
PolygonalSun Oct 12, 2022
ee240e5
Format
PolygonalSun Oct 12, 2022
a3e5370
Removed comment
PolygonalSun Oct 12, 2022
f032d99
PR Feedback
PolygonalSun Oct 14, 2022
4a29d5a
Re-added meshunderpointer code back
PolygonalSun Oct 14, 2022
7b42a02
More feedback
PolygonalSun Oct 14, 2022
4d5dc2c
Comment
PolygonalSun Oct 14, 2022
eba0051
PR Feedback part 1
PolygonalSun Oct 17, 2022
dc3c171
comment
PolygonalSun Oct 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed to just MOVE Lazy Picking
  • Loading branch information
PolygonalSun committed Oct 12, 2022
commit 3aab516db1402d14086c975630ef454103aa8667
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export abstract class BaseCameraMouseWheelInput implements ICameraInput<Camera>
}
};

this._observer = this.camera._addPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
this._observer = this.camera.getScene().onPointerObservable.add(this._wheel, PointerEventTypes.POINTERWHEEL);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/dev/core/src/Cameras/Inputs/BaseCameraPointersInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
}
};

this._observer = this.camera._addPointerObserver(
this._pointerInput,
PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE | PointerEventTypes.POINTERDOUBLETAP
);
this._observer = this.camera
.getScene()
.onPointerObservable.add(
this._pointerInput,
PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE | PointerEventTypes.POINTERDOUBLETAP
);

this._onLostFocus = () => {
this._pointA = this._pointB = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
}
};

this._observer = this.camera._addPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
this._observer = this.camera.getScene().onPointerObservable.add(this._wheel, PointerEventTypes.POINTERWHEEL)

if (this.zoomToMouseLocation) {
this._inertialPanning.setAll(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class FlyCameraMouseInput implements ICameraInput<FlyCamera> {
noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
this._noPreventDefault = noPreventDefault;

this._observer = this.camera._addPointerObserver((p: any) => {
this._observer = this.camera.getScene().onPointerObservable.add((p: any) => {
this._pointerInput(p);
}, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class FollowCameraMouseWheelInput implements ICameraInput<FollowCamera> {
}
};

this._observer = this.camera._addPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
this._observer = this.camera.getScene().onPointerObservable.add(this._wheel, PointerEventTypes.POINTERWHEEL);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/dev/core/src/Cameras/Inputs/freeCameraMouseInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
}
};

this._observer = this.camera._addPointerObserver(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
this._observer = this.camera
.getScene()
.onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);

if (element) {
this._contextMenuBind = this.onContextMenu.bind(this);
Expand Down
4 changes: 3 additions & 1 deletion packages/dev/core/src/Cameras/Inputs/freeCameraTouchInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export class FreeCameraTouchInput implements ICameraInput<FreeCamera> {
};
}

this._observer = this.camera._addPointerObserver(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
this._observer = this.camera
.getScene()
.onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);

if (this._onLostFocus) {
const engine = this.camera.getEngine();
Expand Down
11 changes: 0 additions & 11 deletions packages/dev/core/src/Cameras/camera.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { serialize, SerializationHelper, serializeAsVector3 } from "../Misc/decorators";
import { SmartArray } from "../Misc/smartArray";
import { Tools } from "../Misc/tools";
import type { Observer } from "../Misc/observable";
import { Observable } from "../Misc/observable";
import type { Nullable } from "../types";
import type { CameraInputsManager } from "./cameraInputsManager";
Expand All @@ -18,18 +17,13 @@ import { Viewport } from "../Maths/math.viewport";
import { Frustum } from "../Maths/math.frustum";
import type { Plane } from "../Maths/math.plane";
import { Constants } from "../Engines/constants";
import type { PointerInfo } from "../Events/pointerEvents";

declare type PostProcess = import("../PostProcesses/postProcess").PostProcess;
declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
declare type FreeCamera = import("./freeCamera").FreeCamera;
declare type TargetCamera = import("./targetCamera").TargetCamera;
declare type Ray = import("../Culling/ray").Ray;

const forCameraState = {
forCamera: true,
};

/**
* This is the base class of all the camera used in the application.
* @see https://doc.babylonjs.com/features/cameras
Expand Down Expand Up @@ -638,11 +632,6 @@ export class Camera extends Node {
return check;
}

/** @internal */
public _addPointerObserver(callback: any, mask?: number): Nullable<Observer<PointerInfo>> {
return this._scene.onPointerObservable.add(callback, mask, false, null, false, forCameraState);
}

/**
* Attach the input controls to a specific dom element to get the input from.
* @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
Expand Down
1 change: 1 addition & 0 deletions packages/dev/core/src/Cameras/flyCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class FlyCamera extends TargetCamera {
*/
public detachControl(): void {
this.inputs.detachElement();

this.cameraDirection = new Vector3(0, 0, 0);
}

Expand Down
1 change: 1 addition & 0 deletions packages/dev/core/src/Cameras/freeCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export class FreeCamera extends TargetCamera {
*/
public detachControl(): void {
this.inputs.detachElement();

this.cameraDirection = new Vector3(0, 0, 0);
this.cameraRotation = new Vector2(0, 0);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/dev/core/src/Events/pointerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ export class PointerInfo extends PointerInfoBase {
*/
public get pickInfo(): Nullable<PickingInfo> {
if (this._inputManager) {
this._pickInfo = this._inputManager._pickMove((this.event as IPointerEvent).pointerId);
// Store InputManager locally so that we can null out the object ref and avoid circular reference
const inputManager = this._inputManager;
this._inputManager = null;
this._pickInfo = inputManager._pickMove((this.event as IPointerEvent).pointerId);
inputManager._setRayOnPointerInfo(this);
}

return this._pickInfo;
Expand Down
78 changes: 33 additions & 45 deletions packages/dev/core/src/Inputs/scene.inputManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export class InputManager {
private _meshUnderPointerId: { [pointerId: number]: Nullable<AbstractMesh> } = {};
private _movePointerInfo: Nullable<PointerInfo> = null;
private _clearCachedPickInfo: Nullable<Observer<Scene>> = null;
private _shouldPick = false;

// Keyboard
private _onKeyDown: (evt: IKeyboardEvent) => void;
Expand Down Expand Up @@ -197,14 +196,6 @@ export class InputManager {
this._pointerY = value;
}

private _computeShouldPick() {
const observers = this._scene.onPointerObservable.observers;

this._shouldPick = observers.some((observer) => {
return !observer.customState?.forCamera;
});
}

private _updatePointerPosition(evt: IPointerEvent): void {
const canvasRect = this._scene.getEngine().getInputElementClientRect();

Expand Down Expand Up @@ -251,14 +242,18 @@ export class InputManager {
if (!pickResult) {
this._movePointerInfo = pi;
}
if (this._shouldPick) {
else {
this._setRayOnPointerInfo(pi);
}
scene.onPointerObservable.notifyObservers(pi, type);
if (scene.onPointerObservable.hasObservers()) {
//this._setRayOnPointerInfo(pi);
scene.onPointerObservable.notifyObservers(pi, type);
}
}

// Pointers handling
private _setRayOnPointerInfo(pointerInfo: PointerInfo) {
/** @internal */
public _setRayOnPointerInfo(pointerInfo: PointerInfo) {
const scene = this._scene;
if (pointerInfo.pickInfo && scene._pickingAvailable) {
if (!pointerInfo.pickInfo.ray) {
Expand Down Expand Up @@ -302,6 +297,7 @@ export class InputManager {
);

this._setCursorAndPointerOverMesh(pickResult, pointerId, scene);

return pickResult;
}

Expand Down Expand Up @@ -417,12 +413,11 @@ export class InputManager {
scene.onPointerDown(evt, pickResult, type);
}

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

scene.onPointerObservable.notifyObservers(pi, type);
}
}

Expand Down Expand Up @@ -470,7 +465,7 @@ export class InputManager {
if (scene.onPointerPick) {
scene.onPointerPick(evt, pickResult);
}
if (clickInfo.singleClick && !clickInfo.ignore && this._shouldPick) {
if (clickInfo.singleClick && !clickInfo.ignore && scene.onPointerObservable.hasObservers()) {
const type = PointerEventTypes.POINTERPICK;
const pi = new PointerInfo(type, evt, pickResult);
this._setRayOnPointerInfo(pi);
Expand Down Expand Up @@ -506,28 +501,28 @@ export class InputManager {
}

let type = 0;
if (!clickInfo.ignore && !clickInfo.hasSwiped) {
if (clickInfo.singleClick && scene.onPointerObservable.hasSpecificMask(PointerEventTypes.POINTERTAP)) {
type = PointerEventTypes.POINTERTAP;
} else if (clickInfo.doubleClick && scene.onPointerObservable.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP)) {
type = PointerEventTypes.POINTERDOUBLETAP;
if (scene.onPointerObservable.hasObservers()) {
if (!clickInfo.ignore && !clickInfo.hasSwiped) {
if (clickInfo.singleClick && scene.onPointerObservable.hasSpecificMask(PointerEventTypes.POINTERTAP)) {
type = PointerEventTypes.POINTERTAP;
} else if (clickInfo.doubleClick && scene.onPointerObservable.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP)) {
type = PointerEventTypes.POINTERDOUBLETAP;
}
if (type) {
const pi = new PointerInfo(type, evt, pickResult);
this._setRayOnPointerInfo(pi);
scene.onPointerObservable.notifyObservers(pi, type);
}
}
if (type) {

if (!clickInfo.ignore) {
type = PointerEventTypes.POINTERUP;
const pi = new PointerInfo(type, evt, pickResult);
this._setRayOnPointerInfo(pi);
scene.onPointerObservable.notifyObservers(pi, type);
}
}

if (!clickInfo.ignore) {
type = PointerEventTypes.POINTERUP;
const pi = new PointerInfo(type, evt, pickResult);
if (this._shouldPick) {
this._setRayOnPointerInfo(pi);
}
scene.onPointerObservable.notifyObservers(pi, type);
}

if (scene.onPointerUp && !clickInfo.ignore) {
scene.onPointerUp(evt, pickResult, type);
}
Expand All @@ -553,13 +548,6 @@ export class InputManager {
const scene = this._scene;
const engine = scene.getEngine();

scene.onPointerObservable.onObserverAdded = () => {
this._computeShouldPick();
};
scene.onPointerObservable.onObserverRemoved = () => {
this._computeShouldPick();
};

if (!elementToAttachTo) {
elementToAttachTo = engine.getInputElement();
}
Expand All @@ -577,7 +565,7 @@ export class InputManager {
this._initActionManager = (act: Nullable<AbstractActionManager>): Nullable<AbstractActionManager> => {
if (!this._meshPickProceed) {
const pickResult =
scene.skipPointerUpPicking || (scene._registeredActions === 0 && !this._shouldPick)
scene.skipPointerUpPicking || (scene._registeredActions === 0 && !scene.onPointerObservable.hasObservers())
? null
: scene.pick(this._unTranslatedPointerX, this._unTranslatedPointerY, scene.pointerUpPredicate, false, scene.cameraToUseForPointers);
this._currentPickResult = pickResult;
Expand Down Expand Up @@ -806,7 +794,7 @@ export class InputManager {
// Meshes
this._pickedDownMesh = null;
let pickResult;
if (scene.skipPointerDownPicking || (scene._registeredActions === 0 && !this._shouldPick)) {
if (scene.skipPointerDownPicking || (scene._registeredActions === 0 && !scene.onPointerObservable.hasObservers())) {
pickResult = new PickingInfo();
} else {
pickResult = scene.pick(this._unTranslatedPointerX, this._unTranslatedPointerY, scene.pointerDownPredicate, false, scene.cameraToUseForPointers);
Expand Down Expand Up @@ -839,7 +827,7 @@ export class InputManager {

this._initClickEvent(scene.onPrePointerObservable, scene.onPointerObservable, evt, (clickInfo: _ClickInfo, pickResult: Nullable<PickingInfo>) => {
// PreObservable support
if (this._shouldPick) {
if (scene.onPrePointerObservable.hasObservers()) {
if (!clickInfo.ignore) {
if (!clickInfo.hasSwiped) {
if (clickInfo.singleClick && scene.onPrePointerObservable.hasSpecificMask(PointerEventTypes.POINTERTAP)) {
Expand Down Expand Up @@ -877,7 +865,7 @@ export class InputManager {
}

// Meshes
if (!this._meshPickProceed && ((AbstractActionManager && AbstractActionManager.HasTriggers) || this._shouldPick)) {
if (!this._meshPickProceed && ((AbstractActionManager && AbstractActionManager.HasTriggers) || scene.onPointerObservable.hasObservers())) {
this._initActionManager(null, clickInfo);
}
if (!pickResult) {
Expand All @@ -892,7 +880,7 @@ export class InputManager {

this._onKeyDown = (evt: IKeyboardEvent) => {
const type = KeyboardEventTypes.KEYDOWN;
if (scene.onKeyboardObservable.hasObservers()) {
if (scene.onPreKeyboardObservable.hasObservers()) {
const pi = new KeyboardInfoPre(type, evt);
scene.onPreKeyboardObservable.notifyObservers(pi, type);
if (pi.skipOnKeyboardObservable) {
Expand All @@ -912,7 +900,7 @@ export class InputManager {

this._onKeyUp = (evt: IKeyboardEvent) => {
const type = KeyboardEventTypes.KEYUP;
if (this._shouldPick) {
if (scene.onPreKeyboardObservable.hasObservers()) {
const pi = new KeyboardInfoPre(type, evt);
scene.onPreKeyboardObservable.notifyObservers(pi, type);
if (pi.skipOnKeyboardObservable) {
Expand Down
Loading