@aibee/crc-bmap 0.0.66 → 0.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bmap.esm.js CHANGED
@@ -44,7 +44,7 @@ var __async = (__this, __arguments, generator) => {
44
44
  };
45
45
 
46
46
  // src/bmap.ts
47
- import { EventDispatcher as EventDispatcher7, Vector3 as Vector312 } from "three";
47
+ import { EventDispatcher as EventDispatcher7, Vector3 as Vector313 } from "three";
48
48
 
49
49
  // src/utils/timer.ts
50
50
  var Timer = class {
@@ -303,6 +303,7 @@ function getLongestSideDir(cds) {
303
303
  const point_1 = new Vector3(cds[i][0], cds[i][1], 0);
304
304
  const distance = point_1.distanceTo(point_0);
305
305
  if (distance > maxDistance) {
306
+ maxDistance = distance;
306
307
  dir = point_1.clone().sub(point_0).normalize();
307
308
  }
308
309
  }
@@ -420,18 +421,22 @@ var loader = null;
420
421
  var modelMap = /* @__PURE__ */ new Map();
421
422
  function loadModel(url) {
422
423
  if (modelMap.has(url)) {
423
- return modelMap.get(url);
424
+ const gltf = modelMap.get(url).then((gltf2) => {
425
+ gltf2.scene = gltf2.scene.clone();
426
+ return gltf2;
427
+ });
424
428
  }
425
429
  if (!loader) {
426
430
  loader = createLoader();
427
431
  }
428
432
  const p = new Promise((resolve, reject) => {
429
- loader.load(url, (gltf) => {
430
- resolve(gltf);
431
- }, void 0, reject);
433
+ loader.load(url, resolve, void 0, reject);
432
434
  });
433
435
  modelMap.set(url, p);
434
- return p;
436
+ return p.then((gltf) => {
437
+ gltf.scene = gltf.scene.clone();
438
+ return gltf;
439
+ });
435
440
  }
436
441
  function disposeLoader() {
437
442
  loader = null;
@@ -442,10 +447,10 @@ function disposeLoader() {
442
447
  import {
443
448
  EventDispatcher as EventDispatcher6,
444
449
  Box2,
445
- Vector3 as Vector311,
450
+ Vector3 as Vector312,
446
451
  Vector2 as Vector23,
447
452
  Raycaster as Raycaster3,
448
- Box3 as Box37,
453
+ Box3 as Box38,
449
454
  Color as Color4,
450
455
  AmbientLight as AmbientLight2
451
456
  } from "three";
@@ -1388,6 +1393,7 @@ var Model = class extends Object3D7 {
1388
1393
  this.options = options;
1389
1394
  __publicField(this, "overlay", null);
1390
1395
  __publicField(this, "model", null);
1396
+ this.position.copy(options.position || new Vector37(0, 0, 0));
1391
1397
  this.loadModel();
1392
1398
  }
1393
1399
  loadModel() {
@@ -1401,17 +1407,25 @@ var Model = class extends Object3D7 {
1401
1407
  });
1402
1408
  }
1403
1409
  initOverlay() {
1410
+ var _a, _b;
1404
1411
  if (!this.options.icon) {
1405
1412
  return;
1406
1413
  }
1407
1414
  if (this.model) {
1408
- const box = new Box34().setFromObject(this.model.scene);
1409
- const center2 = box.getCenter(new Vector37());
1410
- const overlay = new Overlay(this.context, { autoUpdate: true });
1411
- overlay.position = center2;
1415
+ const w = ((_a = this.options.icon_size) == null ? void 0 : _a[0]) || 14;
1416
+ const h = ((_b = this.options.icon_size) == null ? void 0 : _b[1]) || 14;
1417
+ const box = new Box34().setFromObject(this);
1418
+ const overlay = new Overlay(this.context, { autoUpdate: false });
1419
+ overlay.addEventListener("update-position", ({ x, y, height }) => {
1420
+ overlay.div.style.transform = `translate3d(${x - w / 2}px, ${-height + y - h}px, 0)`;
1421
+ });
1412
1422
  const img = document.createElement("img");
1413
1423
  img.src = this.options.icon;
1424
+ img.style.width = `${w}px`;
1425
+ img.style.height = `${h}px`;
1414
1426
  overlay.div.appendChild(img);
1427
+ const center2 = box.getCenter(new Vector37());
1428
+ overlay.position = center2;
1415
1429
  this.overlay = overlay;
1416
1430
  }
1417
1431
  }
@@ -2284,6 +2298,92 @@ var MaterialFactory = class {
2284
2298
  }
2285
2299
  };
2286
2300
 
2301
+ // src/utils/camera-bound.ts
2302
+ import { Box3 as Box37, Vector3 as Vector311 } from "three";
2303
+ var CameraBound = class {
2304
+ constructor(context) {
2305
+ this.context = context;
2306
+ __publicField(this, "prevCamera", {
2307
+ position: new Vector311(),
2308
+ zoom: 1,
2309
+ target: new Vector311()
2310
+ });
2311
+ __publicField(this, "enable", true);
2312
+ __publicField(this, "onCameraChange", () => {
2313
+ const bound = this.getCurFloorScreenPosition();
2314
+ if (bound) {
2315
+ const { leftBottom, rightTop } = bound;
2316
+ const isInBound = this.checkDistanceToScreenEdge(leftBottom, rightTop);
2317
+ if (isInBound) {
2318
+ this.changePrevCamera();
2319
+ } else {
2320
+ this.backToPrevCamera();
2321
+ }
2322
+ }
2323
+ });
2324
+ this.registryEvent();
2325
+ this.changePrevCamera();
2326
+ }
2327
+ setEnable(enable) {
2328
+ this.enable = enable;
2329
+ if (enable) {
2330
+ this.registryEvent();
2331
+ } else {
2332
+ this.unRegistryEvent();
2333
+ }
2334
+ }
2335
+ changePrevCamera() {
2336
+ this.prevCamera = {
2337
+ position: this.context.camera.position.clone(),
2338
+ zoom: this.context.camera.zoom,
2339
+ target: this.context.control.target.clone()
2340
+ };
2341
+ }
2342
+ backToPrevCamera() {
2343
+ this.setEnable(false);
2344
+ this.context.camera.position.copy(this.prevCamera.position);
2345
+ this.context.camera.zoom = this.prevCamera.zoom;
2346
+ this.context.control.target.copy(this.prevCamera.target);
2347
+ this.context.control.update();
2348
+ this.setEnable(true);
2349
+ }
2350
+ registryEvent() {
2351
+ this.context.addEventListener("control-change", this.onCameraChange);
2352
+ }
2353
+ unRegistryEvent() {
2354
+ this.context.removeEventListener("control-change", this.onCameraChange);
2355
+ }
2356
+ getCurFloorScreenPosition() {
2357
+ if (!this.context.currentFloor) {
2358
+ return null;
2359
+ }
2360
+ const box = new Box37().setFromObject(this.context.currentFloor.groundUpper);
2361
+ const { camera, container: { clientWidth: w, clientHeight: h } } = this.context;
2362
+ const { min, max } = box;
2363
+ const leftBottom = vector3ToDevice(min, camera, w, h);
2364
+ const rightTop = vector3ToDevice(max, camera, w, h);
2365
+ return { leftBottom, rightTop };
2366
+ }
2367
+ /**
2368
+ * 检测地图是不是在显示范围
2369
+ * @param leftBottom
2370
+ * @param rightTop
2371
+ * @returns { boolean }
2372
+ */
2373
+ checkDistanceToScreenEdge(leftBottom, rightTop) {
2374
+ const { width, height } = this.context.clientSize;
2375
+ const [pt, pr, pb, pl] = this.context.config.cameraBound.padding;
2376
+ const left = leftBottom.x;
2377
+ const right = width - rightTop.x;
2378
+ const top = rightTop.y;
2379
+ const bottom = height - leftBottom.y;
2380
+ return left < pl && right < pr && top < pt && bottom < pb;
2381
+ }
2382
+ dispose() {
2383
+ this.unRegistryEvent();
2384
+ }
2385
+ };
2386
+
2287
2387
  // src/context.ts
2288
2388
  var Context = class extends EventDispatcher6 {
2289
2389
  constructor(container, config) {
@@ -2304,7 +2404,7 @@ var Context = class extends EventDispatcher6 {
2304
2404
  __publicField(this, "basicRatio");
2305
2405
  // zoom=1的时候,100M对应的像素个数
2306
2406
  __publicField(this, "materialFactory");
2307
- __publicField(this, "observe", null);
2407
+ __publicField(this, "cameraBound");
2308
2408
  __publicField(this, "clientSize", {
2309
2409
  width: 0,
2310
2410
  height: 0
@@ -2387,11 +2487,12 @@ var Context = class extends EventDispatcher6 {
2387
2487
  this.dispatchEvent({ type: "change-ratio", px: (this.basicRatio || 0) * this.camera.zoom });
2388
2488
  this.dispatchEvent({ type: "control-change" });
2389
2489
  });
2490
+ this.cameraBound = new CameraBound(this);
2390
2491
  }
2391
2492
  /**
2392
2493
  * 获取两个点之间的像素数
2393
2494
  */
2394
- getRatio(point1 = new Vector311(0, 0, 0), point22 = new Vector311(100, 0, 0)) {
2495
+ getRatio(point1 = new Vector312(0, 0, 0), point22 = new Vector312(100, 0, 0)) {
2395
2496
  const { clientWidth, clientHeight } = this.container;
2396
2497
  const device1 = vector3ToDevice(point1, this.camera, clientWidth, clientHeight);
2397
2498
  const device2 = vector3ToDevice(point22, this.camera, clientWidth, clientHeight);
@@ -2531,7 +2632,7 @@ var Context = class extends EventDispatcher6 {
2531
2632
  );
2532
2633
  }
2533
2634
  getCameraLookAt() {
2534
- return new Vector311().subVectors(this.control.target, this.camera.position);
2635
+ return new Vector312().subVectors(this.control.target, this.camera.position);
2535
2636
  }
2536
2637
  /**
2537
2638
  * 按照一个中心点设置相机的放大缩小
@@ -2590,7 +2691,7 @@ var Context = class extends EventDispatcher6 {
2590
2691
  if (force2DView) {
2591
2692
  this.setPolarAngle(0, 0);
2592
2693
  }
2593
- const boundingBox = new Box37().setFromObject(object);
2694
+ const boundingBox = new Box38().setFromObject(object);
2594
2695
  this.setPolarAngle(polar, 0);
2595
2696
  const { max, min } = boundingBox;
2596
2697
  const max2d = vector3ToDevice(max, this.camera, width, height);
@@ -2603,7 +2704,7 @@ var Context = class extends EventDispatcher6 {
2603
2704
  const xScale = (width - right - left) / size.x;
2604
2705
  const yScale = (height - top - bottom) / size.y;
2605
2706
  const scale = Math.min(xScale, yScale);
2606
- const center2 = new Vector311((max.x + min.x) / 2, (max.y + min.y) / 2, max.z);
2707
+ const center2 = new Vector312((max.x + min.x) / 2, (max.y + min.y) / 2, max.z);
2607
2708
  return this.setZoom(scale * this.camera.zoom, center2, duration);
2608
2709
  }
2609
2710
  fitCameraToGround(padding = [20, 20, 20, 20], duration = 500, force2DView = true) {
@@ -2649,6 +2750,7 @@ var Context = class extends EventDispatcher6 {
2649
2750
  this.tweenGroup.update();
2650
2751
  }
2651
2752
  dispose() {
2753
+ this.cameraBound.dispose();
2652
2754
  this.selection.dispose();
2653
2755
  this.hoverHelper.dispose();
2654
2756
  this.tweenGroup.getAll().forEach((item) => item.stop());
@@ -2723,6 +2825,9 @@ var defaultConfig = {
2723
2825
  },
2724
2826
  graphic: {
2725
2827
  fillOpacity: 1
2828
+ },
2829
+ cameraBound: {
2830
+ padding: [150, 150, 150, 150]
2726
2831
  }
2727
2832
  };
2728
2833
  function getConfig(config) {
@@ -2787,6 +2892,7 @@ var BMap = class extends EventDispatcher7 {
2787
2892
  }
2788
2893
  });
2789
2894
  __publicField(this, "resize", () => {
2895
+ this.context.cameraBound.setEnable(false);
2790
2896
  this.context.onWindowResize();
2791
2897
  const azimuthal = this.context.control.getAzimuthalAngle();
2792
2898
  const zoom = this.context.camera.zoom;
@@ -2801,6 +2907,7 @@ var BMap = class extends EventDispatcher7 {
2801
2907
  this.context.camera.zoom = zoom;
2802
2908
  this.context.control.addEventListener("change", this.onControlChange);
2803
2909
  this.context.setAzimuthalAngle(azimuthal, 0);
2910
+ this.context.cameraBound.setEnable(true);
2804
2911
  });
2805
2912
  this.config = getConfig(config);
2806
2913
  this.context = new Context(container, this.config);
@@ -2940,6 +3047,7 @@ var BMap = class extends EventDispatcher7 {
2940
3047
  this.currentBuildGround = this.buildingGroundMap.get(buildingKey) || null;
2941
3048
  const createdFloor = this.createFloor(curFloorData);
2942
3049
  if (createdFloor) {
3050
+ this.context.cameraBound.setEnable(false);
2943
3051
  this.context.switchFloor(createdFloor.curFloor);
2944
3052
  this.context.control.minZoom = 0;
2945
3053
  this.context.control.maxZoom = Infinity;
@@ -2954,6 +3062,7 @@ var BMap = class extends EventDispatcher7 {
2954
3062
  this.context.fitCameraToGround([80, 20, 80, 20], 0, false);
2955
3063
  }
2956
3064
  this.onControlChange();
3065
+ this.context.cameraBound.setEnable(true);
2957
3066
  } else {
2958
3067
  console.warn("[switchFloor error] [" + floor + "] \u697C\u5C42\u6CA1\u6709\u6570\u636E");
2959
3068
  }
@@ -2962,24 +3071,19 @@ var BMap = class extends EventDispatcher7 {
2962
3071
  }
2963
3072
  }
2964
3073
  // 扶梯
2965
- addStaircaseModel(graphic, icon, modelUrl) {
3074
+ addModel(graphic, options) {
2966
3075
  var _a;
2967
3076
  if (graphic.options.geometry.type === "polygon") {
2968
- const dir = getLongestSideDir(graphic.options.geometry.cds[0]);
2969
- const model = (_a = this.context.currentFloor) == null ? void 0 : _a.addModel({ icon, modelUrl });
3077
+ const model = (_a = this.context.currentFloor) == null ? void 0 : _a.addModel(__spreadProps(__spreadValues({}, options), {
3078
+ position: graphic.getPosition().setZ(0.1)
3079
+ }));
2970
3080
  if (model) {
2971
- model.position.copy(graphic.getPosition().setZ(0.1));
2972
- const angleY = dir.angleTo(new Vector312(0, 1, 0));
3081
+ const dir = getLongestSideDir(graphic.options.geometry.cds[0]);
3082
+ const angleY = dir.angleTo(new Vector313(0, 1, 0));
2973
3083
  model.rotateZ(angleY);
2974
3084
  }
2975
3085
  }
2976
3086
  }
2977
- // 电梯
2978
- addLiftModel(graphic) {
2979
- }
2980
- // 楼梯
2981
- addStairwayModel(graphic) {
2982
- }
2983
3087
  addHeatmap(data) {
2984
3088
  var _a;
2985
3089
  return (_a = this.context.currentFloor) == null ? void 0 : _a.addHeatmap(data);