Skip to content

Commit

Permalink
Merge pull request #404 from xeokit/fix-excess-pick-events
Browse files Browse the repository at this point in the history
CameraControl picking optimizations and fixes
  • Loading branch information
xeolabs authored Aug 11, 2020
2 parents d5ea297 + 5c11777 commit d611642
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 208 deletions.
4 changes: 1 addition & 3 deletions src/viewer/scene/CameraControl/CameraControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {KeyboardPanRotateDollyHandler} from "./lib/handlers/KeyboardPanRotateDol
import {CameraUpdater} from "./lib/CameraUpdater.js";
import {MouseMiscHandler} from "./lib/handlers/MouseMiscHandler.js";
import {TouchPanRotateAndDollyHandler} from "./lib/handlers/TouchPanRotateAndDollyHandler.js";
import {TouchPickHandler} from "./lib/handlers/TouchPickHandler.js";
import {utils} from "../utils.js";

/**
Expand Down Expand Up @@ -673,7 +672,7 @@ class CameraControl extends Component {
activeTouches: [],
tapStartPos: new Float32Array(2),
tapStartTime: -1,
lastTapTime: -1,
lastTapTime: -1
};

// Updates for CameraUpdater to process on next Scene "tick" event
Expand Down Expand Up @@ -709,7 +708,6 @@ class CameraControl extends Component {
new MousePanRotateDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new KeyboardAxisViewHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new MousePickHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new TouchPickHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new KeyboardPanRotateDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates)
];

Expand Down
16 changes: 9 additions & 7 deletions src/viewer/scene/CameraControl/lib/CameraUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CameraUpdater {

this._scene = scene;
const camera = scene.camera;
const pickController = controllers.pickController;
const pivotController = controllers.pivotController;
const panController = controllers.panController;

Expand Down Expand Up @@ -71,14 +72,13 @@ class CameraUpdater {

if (updates.rotateDeltaY === 0 && updates.rotateDeltaX === 0) {

const pickResult = this._scene.pick({
pickSurface: true,
pickSurfaceNormal: false,
canvasPos: states.pointerCanvasPos
});
pickController.pickCursorPos = states.pointerCanvasPos;
pickController.schedulePickSurface = true;

if (pickResult && pickResult.worldPos) {
const worldPos = pickResult.worldPos;
pickController.update();

if (pickController.pickResult && pickController.pickResult.worldPos) {
const worldPos = pickController.pickResult.worldPos;
pivotController.setPivotPos(worldPos);
pivotController.hidePivot();
} else {
Expand Down Expand Up @@ -289,6 +289,8 @@ class CameraUpdater {
updates.dollyDelta *= configs.dollyInertia;
}

pickController.fireEvents();

document.body.style.cursor = cursorType;
});
}
Expand Down
73 changes: 64 additions & 9 deletions src/viewer/scene/CameraControl/lib/controllers/PickController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {math} from "../../../math/math.js";
import {PickResult} from "../../../webgl/PickResult.js";

/**
*
Expand Down Expand Up @@ -56,9 +57,7 @@ class PickController {

this._lastPickedEntityId = null;

this._onTick = this._scene.on("tick", () => {
this.update()
});
this._needFireEvents = false;
}

/**
Expand All @@ -76,32 +75,87 @@ class PickController {

this.picked = false;
this.pickedSurface = false;
this._needFireEvents = false;

const hasHoverSurfaceSubs = this._cameraControl.hasSubs("hoverSurface");

if (this.schedulePickSurface) {
if (this.pickResult && this.pickResult.worldPos) {
const pickResultCanvasPos = this.pickResult.canvasPos;
if (pickResultCanvasPos[0] === this.pickCursorPos[0] && pickResultCanvasPos[1] === this.pickCursorPos[1]) {
this.picked = true;
this.pickedSurface = true;
this._needFireEvents = hasHoverSurfaceSubs;
this.schedulePickEntity = false;
this.schedulePickSurface = false;
return;
}
}
}

if (this.schedulePickEntity) {
if (this.pickResult) {
const pickResultCanvasPos = this.pickResult.canvasPos;
if (pickResultCanvasPos[0] === this.pickCursorPos[0] && pickResultCanvasPos[1] === this.pickCursorPos[1]) {
this.picked = true;
this.pickedSurface = false;
this._needFireEvents = false;
this.schedulePickEntity = false;
this.schedulePickSurface = false;
return;
}
}
}

if (this.schedulePickSurface) {

if (this.schedulePickSurface || this._cameraControl.hasSubs("hoverSurface")) {
this.pickResult = this._scene.pick({
pickSurface: true,
pickSurfaceNormal: true,
canvasPos: this.pickCursorPos
});

if (this.pickResult) {
this.picked = true;
this.pickedSurface = true;
this._needFireEvents = true;
}

} else { // schedulePickEntity == true

this.pickResult = this._scene.pick({
canvasPos: this.pickCursorPos
});

if (this.pickResult) {
this.picked = true;
this.pickedSurface = false;
this._needFireEvents = true;
}
}

if (this.pickResult) {
this.schedulePickEntity = false;
this.schedulePickSurface = false;
}

fireEvents() {

if (!this._needFireEvents) {
return;
}

this.picked = true;
if (this.picked && this.pickResult) {

const pickedEntityId = this.pickResult.entity.id;

if (this._lastPickedEntityId !== pickedEntityId) {
if (this._lastPickedEntityId !== undefined) {

if (this._lastPickedEntityId !== undefined) {
this._cameraControl.fire("hoverOut", {
entity: this._scene.objects[this._lastPickedEntityId]
}, true);
}

this._cameraControl.fire("hoverEnter", this.pickResult, true);
this._lastPickedEntityId = pickedEntityId;
}
Expand All @@ -127,8 +181,9 @@ class PickController {
}, true);
}

this.schedulePickEntity = false;
this.schedulePickSurface = false;
this.pickResult = null;

this._needFireEvents = false;
}

destroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,9 @@ class MousePickHandler {

if (this._clicks === 1) { // First click

const pointerCanvasPos = states.pointerCanvasPos.slice();

this._timeout = setTimeout(() => {

pickController.pickCursorPos = pointerCanvasPos;
pickController.pickCursorPos = states.pointerCanvasPos;
pickController.schedulePickEntity = configs.doublePickFlyTo;
pickController.schedulePickSurface = pickedSurfaceSubs;
pickController.update();
Expand Down
186 changes: 0 additions & 186 deletions src/viewer/scene/CameraControl/lib/handlers/TouchPickHandler.js

This file was deleted.

0 comments on commit d611642

Please sign in to comment.