Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vladyslav-tk committed Jan 8, 2025
1 parent 90b8320 commit c912fdc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
72 changes: 47 additions & 25 deletions src/plugins/cesium/ngv-plugin-cesium-model-interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
DataSourceCollection,
Cartesian2,
Cesium3DTileset,
Event,
} from '@cesium/engine';
import {
Model,
Expand Down Expand Up @@ -100,6 +101,9 @@ export class NgvPluginCesiumModelInteract extends LitElement {
private movePlane: Plane | undefined;
private grabType: GrabType;
private hoveredEdge: Entity | undefined;
private cameraMoving = false;
private unlistenMoveStart: Event.RemoveCallback;
private unlistenMoveEnd: Event.RemoveCallback;

initEvents(): void {
this.eventHandler = new ScreenSpaceEventHandler(this.viewer.canvas);
Expand Down Expand Up @@ -146,6 +150,12 @@ export class NgvPluginCesiumModelInteract extends LitElement {
applyClippingTo3dTileset(tileset, this.models);
},
);
this.unlistenMoveStart = this.viewer.camera.moveStart.addEventListener(
() => (this.cameraMoving = true),
);
this.unlistenMoveEnd = this.viewer.camera.moveEnd.addEventListener(
() => (this.cameraMoving = false),
);
}

removeEvents(): void {
Expand All @@ -156,6 +166,8 @@ export class NgvPluginCesiumModelInteract extends LitElement {
this.eventHandler.destroy();
this.eventHandler = null;
}
if (this.unlistenMoveStart) this.unlistenMoveStart();
if (this.unlistenMoveEnd) this.unlistenMoveEnd();
}

onPrimitivesChanged(): void {
Expand Down Expand Up @@ -232,6 +244,7 @@ export class NgvPluginCesiumModelInteract extends LitElement {
}

onMouseMove(evt: ScreenSpaceEventHandler.MotionEvent): void {
if (this.cameraMoving) return;
if (this.grabType && this.chosenModel) {
const endPosition = this.viewer.scene.pickPosition(evt.endPosition);
if (!endPosition) return;
Expand Down Expand Up @@ -295,44 +308,64 @@ export class NgvPluginCesiumModelInteract extends LitElement {
}

updateCursor(position: Cartesian2): void {
const model: Model | undefined = this.pickModel(position);
const isSidePlane = !!this.pickEntity(position, this.sidePlanesDataSource);
const isTopPlane = !!this.pickEntity(
position,
this.topDownPlanesDataSource,
);
const edgeEntity = this.pickEntity(position, this.edgeLinesDataSource);
const isCorner = !!this.pickEntity(position, this.cornerPointsDataSource);
const obj: {id: Entity | undefined; primitive: Model | undefined} = <
{id: Entity | undefined; primitive: Model | undefined}
>this.viewer.scene.pick(position);
if (!obj) return;

const pickedEntity =
this.chosenModel && obj?.id && obj.id instanceof Entity
? obj?.id
: undefined;
const model =
!pickedEntity &&
obj?.primitive &&
this.primitiveCollection.contains(obj.primitive)
? obj.primitive
: undefined;
if (!pickedEntity && !model) return;

const isEdge =
pickedEntity && this.edgeLinesDataSource.entities.contains(pickedEntity);
if (model && !this.chosenModel) {
if (this.cursor !== 'pointer') {
this.viewer.canvas.style.cursor = this.cursor = 'pointer';
}
} else if (isSidePlane) {
} else if (
pickedEntity &&
this.sidePlanesDataSource.entities.contains(pickedEntity)
) {
if (this.cursor !== 'move') {
this.viewer.canvas.style.cursor = this.cursor = 'move';
}
} else if (isTopPlane) {
} else if (
pickedEntity &&
this.topDownPlanesDataSource.entities.contains(pickedEntity)
) {
if (this.cursor !== 'ns-resize') {
this.viewer.canvas.style.cursor = this.cursor = 'ns-resize';
}
} else if (isCorner) {
} else if (
pickedEntity &&
this.cornerPointsDataSource.entities.contains(pickedEntity)
) {
if (this.cursor !== 'nesw-resize') {
this.viewer.canvas.style.cursor = this.cursor = 'nesw-resize';
}
} else if (edgeEntity) {
} else if (isEdge) {
if (this.cursor !== 'ew-resize') {
this.viewer.canvas.style.cursor = this.cursor = 'ew-resize';
}
if (!this.hoveredEdge) {
this.hoveredEdge = edgeEntity;
this.hoveredEdge = pickedEntity;
this.hoveredEdge.polyline.material = new ColorMaterialProperty(
Color.WHITE.withAlpha(0.9),
);
}
} else if (this.cursor !== 'default') {
this.viewer.canvas.style.cursor = this.cursor = 'default';
}
if (this.hoveredEdge && !edgeEntity) {
if (this.hoveredEdge && !isEdge) {
this.hoveredEdge.polyline.material = new ColorMaterialProperty(
Color.WHITE.withAlpha(0.3),
);
Expand All @@ -350,17 +383,6 @@ export class NgvPluginCesiumModelInteract extends LitElement {
: undefined;
}

pickEntity(position: Cartesian2, dataSource: DataSource): Entity | undefined {
const obj: {id: Entity | undefined} = <{id: Entity | undefined}>(
this.viewer.scene.pick(position)
);
return obj?.id &&
obj.id instanceof Entity &&
dataSource.entities.contains(obj.id)
? obj?.id
: undefined;
}

pickGrabType(position: Cartesian2): GrabType | undefined {
const pickedObject: {id: Entity | undefined} = <{id: Entity | undefined}>(
this.viewer.scene.pick(position)
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/cesium/ngv-plugin-cesium-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export class NgvPluginCesiumUpload extends LitElement {
}

onMouseMove(event: ScreenSpaceEventHandler.MotionEvent): void {
if (!event.endPosition) return;
const position = this.viewer.scene.pickPosition(event.endPosition);
if (!position) return;
const cart = Cartographic.fromCartesian(
position,
this.viewer.scene.ellipsoid,
Expand Down

0 comments on commit c912fdc

Please sign in to comment.