Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
HARP-13901: Move raycasterFromScreenPoint to PickHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
atomicsulfate committed Jan 29, 2021
1 parent 2a0a3af commit 5cbe5c6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
5 changes: 2 additions & 3 deletions @here/harp-mapview/lib/MapObjectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import { GeometryKind, MapEnv, Pickability, Technique } from "@here/harp-datasource-protocol";
import { GeometryKind, Pickability, Technique } from "@here/harp-datasource-protocol";
import * as THREE from "three";

import { DataSource } from "./DataSource";
Expand Down Expand Up @@ -135,9 +135,8 @@ export class MapObjectAdapter {

/**
* Whether underlying `THREE.Object3D` should be pickable by {@link PickHandler}.
* @param env - Property lookup environment.
*/
isPickable(env: MapEnv) {
isPickable() {
// An object is pickable only if it's visible and Pickabilty.onlyVisible or
// Pickabililty.all set.
return (
Expand Down
32 changes: 5 additions & 27 deletions @here/harp-mapview/lib/MapView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import { MapViewFog } from "./MapViewFog";
import { MapViewTaskScheduler } from "./MapViewTaskScheduler";
import { MapViewThemeManager } from "./MapViewThemeManager";
import { PickHandler, PickResult } from "./PickHandler";
import { PickingRaycaster } from "./PickingRaycaster";
import { PoiManager } from "./poi/PoiManager";
import { PoiTableManager } from "./poi/PoiTableManager";
import { PolarTileDataSource } from "./PolarTileDataSource";
Expand Down Expand Up @@ -870,7 +869,6 @@ export class MapView extends EventDispatcher {

// gestures
private readonly m_raycaster = new THREE.Raycaster();
private readonly m_pickingRaycaster: PickingRaycaster;
private readonly m_plane = new THREE.Plane(new THREE.Vector3(0, 0, 1));
private readonly m_sphere = new THREE.Sphere(undefined, EarthConstants.EQUATORIAL_RADIUS);

Expand Down Expand Up @@ -1008,11 +1006,6 @@ export class MapView extends EventDispatcher {
this.m_politicalView = this.m_options.politicalView;

this.handleRequestAnimationFrame = this.renderLoop.bind(this);
this.m_pickHandler = new PickHandler(
this,
this.m_rteCamera,
this.m_options.enablePickTechnique === true
);

if (this.m_options.tileWrappingEnabled !== undefined) {
this.m_tileWrappingEnabled = this.m_options.tileWrappingEnabled;
Expand Down Expand Up @@ -1088,7 +1081,11 @@ export class MapView extends EventDispatcher {
// setup camera with initial position
this.setupCamera();

this.m_pickingRaycaster = new PickingRaycaster(width, height, this.m_env);
this.m_pickHandler = new PickHandler(
this,
this.m_rteCamera,
this.m_options.enablePickTechnique === true
);

this.m_movementDetector = new CameraMovementDetector(
this.m_options.movementThrottleTimeout,
Expand Down Expand Up @@ -2570,25 +2567,6 @@ export class MapView extends EventDispatcher {
return p;
}

/**
* Returns a ray caster using the supplied screen positions.
*
* @param x - The X position in css/client coordinates (without applied display ratio).
* @param y - The Y position in css/client coordinates (without applied display ratio).
*
* @alpha
*
* @return Raycaster with origin at the camera and direction based on the supplied x / y screen
* points.
*/
raycasterFromScreenPoint(x: number, y: number): THREE.Raycaster {
this.m_pickingRaycaster.setFromCamera(
this.getNormalizedScreenCoordinates(x, y),
this.m_rteCamera
);
return this.m_pickingRaycaster;
}

getWorldPositionAt(x: number, y: number, fallback: true): THREE.Vector3;
getWorldPositionAt(x: number, y: number, fallback?: boolean): THREE.Vector3 | null;

Expand Down
29 changes: 27 additions & 2 deletions @here/harp-mapview/lib/PickHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as THREE from "three";
import { IntersectParams } from "./IntersectParams";
import { MapView } from "./MapView";
import { MapViewPoints } from "./MapViewPoints";
import { PickingRaycaster } from "./PickingRaycaster";
import { PickListener } from "./PickListener";
import { Tile, TileFeatureData } from "./Tile";
import { MapViewUtils } from "./Utils";
Expand Down Expand Up @@ -145,11 +146,16 @@ function intersectDependentObjects(
* @internal
*/
export class PickHandler {
private readonly m_pickingRaycaster: PickingRaycaster;

constructor(
readonly mapView: MapView,
readonly camera: THREE.Camera,
public enablePickTechnique = false
) {}
) {
const { width, height } = mapView.renderer.getSize(new THREE.Vector2());
this.m_pickingRaycaster = new PickingRaycaster(width, height);
}

/**
* Does a raycast on all objects in the scene; useful for picking.
Expand Down Expand Up @@ -216,6 +222,25 @@ export class PickHandler {
return pickListener.results;
}

/**
* Returns a ray caster using the supplied screen positions.
*
* @param x - The X position in css/client coordinates (without applied display ratio).
* @param y - The Y position in css/client coordinates (without applied display ratio).
*
* @alpha
*
* @return Raycaster with origin at the camera and direction based on the supplied x / y screen
* points.
*/
raycasterFromScreenPoint(x: number, y: number): THREE.Raycaster {
this.m_pickingRaycaster.setFromCamera(
this.mapView.getNormalizedScreenCoordinates(x, y),
this.camera
);
return this.m_pickingRaycaster;
}

private createResult(intersection: THREE.Intersection): PickResult {
const pickResult: PickResult = {
type: PickObjectType.Unspecified,
Expand Down Expand Up @@ -352,7 +377,7 @@ export class PickHandler {

private createRaycaster(x: number, y: number): THREE.Raycaster {
const camera = this.mapView.camera;
const rayCaster = this.mapView.raycasterFromScreenPoint(x, y);
const rayCaster = this.raycasterFromScreenPoint(x, y);

// A threshold must be set for picking of line and line segments, indicating the maximum
// distance in world units from the ray to a line to consider it as picked. Use the world
Expand Down
13 changes: 5 additions & 8 deletions @here/harp-mapview/lib/PickingRaycaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { MapEnv } from "@here/harp-datasource-protocol";
import * as THREE from "three";

import { MapObjectAdapter } from "./MapObjectAdapter";

function intersectObject(
object: THREE.Object3D,
raycaster: PickingRaycaster,
env: MapEnv,
intersects: THREE.Intersection[],
recursive?: boolean
) {
if (object.layers.test(raycaster.layers) && object.visible) {
const mapObjectAdapter = MapObjectAdapter.get(object);
if (!mapObjectAdapter || mapObjectAdapter.isPickable(env)) {
if (!mapObjectAdapter || mapObjectAdapter.isPickable()) {
object.raycast(raycaster, intersects);
}
}

if (recursive === true) {
for (const child of object.children) {
intersectObject(child, raycaster, env, intersects, true);
intersectObject(child, raycaster, intersects, true);
}
}
}
Expand All @@ -43,9 +41,8 @@ export class PickingRaycaster extends THREE.Raycaster {
*
* @param width - the canvas width.
* @param height - the canvas height.
* @param m_env - the view enviroment.
*/
constructor(public width: number, public height: number, private readonly m_env: MapEnv) {
constructor(public width: number, public height: number) {
super();
}

Expand All @@ -58,7 +55,7 @@ export class PickingRaycaster extends THREE.Raycaster {
): THREE.Intersection[] {
const intersects: THREE.Intersection[] = optionalTarget ?? [];

intersectObject(object, this, this.m_env, intersects, recursive);
intersectObject(object, this, intersects, recursive);

return intersects;
}
Expand All @@ -73,7 +70,7 @@ export class PickingRaycaster extends THREE.Raycaster {
const intersects: THREE.Intersection[] = optionalTarget ?? [];

for (const object of objects) {
intersectObject(object, this, this.m_env, intersects, recursive);
intersectObject(object, this, intersects, recursive);
}

return intersects;
Expand Down

0 comments on commit 5cbe5c6

Please sign in to comment.