UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

56 lines (55 loc) 2.1 kB
"use strict"; import { Triangle, Vector3 } from "three"; import { expandTriangle } from "../../../math/_Module"; import { CoreGeometryBuilderMesh } from "../../../geometry/modules/three/builders/Mesh"; import { CoreMapboxUtils } from "../Utils"; import { pointsFromObject } from "../../../geometry/entities/point/CorePointUtils"; const _points = []; export class MapboxPlaneFrustumController { constructor() { this._triangle_a = new Triangle(); this._triangle_b = new Triangle(); this._point_pos = new Vector3(); } deleteOutOfView(map, object, plane_dimensions, segments_counts) { const near_lng_lat_pts = CoreMapboxUtils.verticalNearLngLatPoints(map); const far_lng_lat_pts = CoreMapboxUtils.verticalFarLngLatPoints(map); if (!near_lng_lat_pts || !far_lng_lat_pts) { return; } let delete_out_of_view_bound_pts = near_lng_lat_pts.concat(far_lng_lat_pts); const delete_out_of_view_margin = Math.max( plane_dimensions.x / segments_counts.x, plane_dimensions.y / segments_counts.y ); return this._deleteOutOfView( object, delete_out_of_view_bound_pts, delete_out_of_view_margin * 2 // *2 just to be safe ); } _deleteOutOfView(object, bound_pts, margin) { pointsFromObject(object, _points); const positions = bound_pts.map((bound_pt) => new Vector3(bound_pt.lng, 0, bound_pt.lat)); this._triangle_a.a.copy(positions[0]); this._triangle_a.b.copy(positions[1]); this._triangle_a.c.copy(positions[2]); this._triangle_b.a.copy(positions[2]); this._triangle_b.b.copy(positions[3]); this._triangle_b.c.copy(positions[1]); expandTriangle(this._triangle_a, margin); expandTriangle(this._triangle_b, margin); const keptPoints = []; for (const point of _points) { point.position(this._point_pos); if ( // this._triangle_a.containsPoint(this._point_pos) || this._triangle_b.containsPoint(this._point_pos) ) { keptPoints.push(point); } } return new CoreGeometryBuilderMesh().fromPoints(object, keptPoints); } }