@reearth/core 0.0.7-alpha.17 → 0.0.7-alpha.19

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/dist/index.d.ts CHANGED
@@ -608,6 +608,7 @@ export declare type EngineRef = {
608
608
  unselectFeatures: (layerId: string, featureId: string[]) => void;
609
609
  pickManyFromViewport: (windowPosition: [x: number, y: number], windowWidth: number, windowHeight: number, condition?: (f: PickedFeature) => boolean) => PickedFeature[] | undefined;
610
610
  calcRectangleControlPoint: (p1: Position3d, p2: Position3d, p3: Position3d) => [p1: Position3d, p2: Position3d, p3: Position3d];
611
+ getCredits: () => Credit[] | undefined;
611
612
  } & MouseEventHandles;
612
613
 
613
614
  export declare const engines: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reearth/core",
3
- "version": "0.0.7-alpha.17",
3
+ "version": "0.0.7-alpha.19",
4
4
  "author": "Re:Earth contributors <community@reearth.io>",
5
5
  "license": "Apache-2.0",
6
6
  "description": "A library that abstracts a map engine as one common API.",
package/src/Map/ref.ts CHANGED
@@ -91,6 +91,7 @@ const engineRefKeys: FunctionKeys<EngineRef> = {
91
91
  bringToFront: 1,
92
92
  sendToBack: 1,
93
93
  calcRectangleControlPoint: 1,
94
+ getCredits: 1,
94
95
  };
95
96
 
96
97
  const layersRefKeys: FunctionKeys<LayersRef> = {
@@ -211,6 +211,7 @@ export type EngineRef = {
211
211
  p2: Position3d,
212
212
  p3: Position3d,
213
213
  ) => [p1: Position3d, p2: Position3d, p3: Position3d];
214
+ getCredits: () => Credit[] | undefined;
214
215
  } & MouseEventHandles;
215
216
 
216
217
  export type EngineProps = {
@@ -448,7 +448,7 @@ export const useHooks = ({
448
448
  }) => {
449
449
  const { viewer } = useCesium();
450
450
  const tilesetRef = useRef<Cesium3DTilesetType>();
451
- const { onLayerLoad } = useContext();
451
+ const { onLayerLoad, updateCredits } = useContext();
452
452
  const layerIdRef = useRef(layer?.id);
453
453
  layerIdRef.current = layer?.id;
454
454
 
@@ -500,8 +500,9 @@ export const useHooks = ({
500
500
  return prevPlanes.current;
501
501
  }, [_planes]);
502
502
  const clipDirection = direction === "inside" ? -1 : 1;
503
+
503
504
  // Create immutable object
504
- const [clippingPlanes] = useState(
505
+ const [clippingPlanes, setClippingPlanes] = useState<CesiumClippingPlaneCollection>(
505
506
  () =>
506
507
  new CesiumClippingPlaneCollection({
507
508
  planes: planes?.map(
@@ -516,6 +517,27 @@ export const useHooks = ({
516
517
  edgeColor: toColor(edgeColor),
517
518
  }),
518
519
  );
520
+ // Initialize clipping planes
521
+ // This is workaround to reinitialize ClippingPlanes in strict mode.
522
+ useEffect(
523
+ () => () => {
524
+ setClippingPlanes(
525
+ new CesiumClippingPlaneCollection({
526
+ planes: planes?.map(
527
+ plane =>
528
+ new ClippingPlane(
529
+ new Cartesian3(plane.normal?.x, plane.normal?.y, plane.normal?.z),
530
+ (plane.distance || 0) * clipDirection,
531
+ ),
532
+ ),
533
+ unionClippingRegions: direction === "outside",
534
+ edgeWidth: edgeWidth,
535
+ edgeColor: toColor(edgeColor),
536
+ }),
537
+ );
538
+ },
539
+ [], // eslint-disable-line react-hooks/exhaustive-deps
540
+ );
519
541
 
520
542
  const { drawClippingEnabled, drawClippingEdgeProps } = useDrawClipping({
521
543
  ...experimental_clipping?.draw,
@@ -779,6 +801,13 @@ export const useHooks = ({
779
801
  [onLayerFetch, onLayerLoad],
780
802
  );
781
803
 
804
+ useEffect(() => {
805
+ updateCredits?.();
806
+ return () => {
807
+ updateCredits?.();
808
+ };
809
+ }, [type, updateCredits]);
810
+
782
811
  return {
783
812
  tilesetUrl,
784
813
  ref,
@@ -26,6 +26,7 @@ export type Context = {
26
26
  position: [x: number, y: number, z: number],
27
27
  ) => [x: number, y: number] | undefined;
28
28
  isPositionVisible?: (position: [x: number, y: number, z: number]) => boolean;
29
+ updateCredits?: () => void;
29
30
  };
30
31
 
31
32
  export const context = createContext<Context>({});
@@ -35,6 +35,8 @@ import {
35
35
  Plane,
36
36
  CameraEventType,
37
37
  KeyboardEventModifier,
38
+ CreditDisplay,
39
+ Credit as CesiumCredit,
38
40
  } from "cesium";
39
41
  import { MutableRefObject, useMemo } from "react";
40
42
 
@@ -898,3 +900,35 @@ export function getExtrudedHeight(
898
900
  }
899
901
  return;
900
902
  }
903
+
904
+ export function getCredits(viewer: Viewer) {
905
+ if (!viewer) return;
906
+ const creditDisplay = viewer.creditDisplay as
907
+ | (CreditDisplay & {
908
+ _currentFrameCredits: {
909
+ lightboxCredits: { _array: { credit?: CesiumCredit }[] };
910
+ screenCredits: { _array: { credit?: CesiumCredit }[] };
911
+ };
912
+ _currentCesiumCredit: CesiumCredit;
913
+ })
914
+ | undefined;
915
+
916
+ if (!creditDisplay) return;
917
+
918
+ const { lightboxCredits, screenCredits } = creditDisplay?._currentFrameCredits || {};
919
+ const cesiumCredits = creditDisplay._currentCesiumCredit;
920
+
921
+ const credits: {
922
+ html?: string;
923
+ }[] = [
924
+ ...(cesiumCredits?.html ? [{ html: cesiumCredits.html }] : []),
925
+ ...Array.from(lightboxCredits?._array ?? []).map(c => ({
926
+ html: c?.credit?.html,
927
+ })),
928
+ ...Array.from(screenCredits?._array ?? []).map(c => ({
929
+ html: c?.credit?.html,
930
+ })),
931
+ ];
932
+
933
+ return credits;
934
+ }
@@ -32,6 +32,7 @@ import {
32
32
  cartesianToLatLngHeight,
33
33
  getExtrudedHeight,
34
34
  getOverriddenScreenSpaceCameraOptions,
35
+ getCredits,
35
36
  } from "../common";
36
37
  import { attachTag, getTag } from "../Feature";
37
38
  import { PickedFeature, pickManyFromViewportAsFeature } from "../pickMany";
@@ -974,6 +975,11 @@ export default function useEngineRef(
974
975
  const p5 = [pp5.x, pp5.y, pp5.z] as Position3d;
975
976
  return [p1, p2, p5];
976
977
  },
978
+ getCredits: () => {
979
+ const viewer = cesium.current?.cesiumElement;
980
+ if (!viewer || viewer.isDestroyed()) return;
981
+ return getCredits(viewer);
982
+ },
977
983
  };
978
984
  }, [cesium]);
979
985
 
@@ -12,8 +12,6 @@ import {
12
12
  GroundPrimitive,
13
13
  ShadowMap,
14
14
  ImageryLayer,
15
- CreditDisplay,
16
- Credit as CesiumCredit,
17
15
  } from "cesium";
18
16
  import { MutableRefObject, RefObject, useCallback, useEffect, useMemo, useRef } from "react";
19
17
  import type { CesiumComponentRef, CesiumMovementEvent, RootEventTarget } from "resium";
@@ -40,7 +38,7 @@ import {
40
38
  import { TimelineManagerRef } from "../../Map/useTimelineManager";
41
39
  import { FEATURE_FLAGS } from "../../Visualizer/featureFlags";
42
40
 
43
- import { isSelectable } from "./common";
41
+ import { getCredits, isSelectable } from "./common";
44
42
  import { getTag, type Context as FeatureContext } from "./Feature";
45
43
  import { arrayToCartecian3 } from "./helpers/sphericalHaromic";
46
44
  import useCamera from "./hooks/useCamera";
@@ -653,6 +651,22 @@ export default ({
653
651
  viewer.scene.requestRender();
654
652
  }, []);
655
653
 
654
+ const onCreditsUpdateRef = useRef(onCreditsUpdate);
655
+ onCreditsUpdateRef.current = onCreditsUpdate;
656
+ const updateCredits = useCallback(() => {
657
+ if (!onCreditsUpdateRef.current) return;
658
+ // currently we don't have a proper way to get the credits update event
659
+ // wait for 3 seconds to get latest credits
660
+ // some internal property is been used here.
661
+ setTimeout(() => {
662
+ if (!onCreditsUpdateRef.current) return;
663
+ const viewer = cesium.current?.cesiumElement;
664
+ if (!viewer || viewer.isDestroyed()) return;
665
+ const credits: Credit[] = getCredits(viewer) ?? [];
666
+ onCreditsUpdateRef.current(credits);
667
+ }, 3000);
668
+ }, []);
669
+
656
670
  const context = useMemo<FeatureContext>(
657
671
  () => ({
658
672
  selectionReason,
@@ -667,8 +681,17 @@ export default ({
667
681
  toXYZ: engineAPI.toXYZ,
668
682
  toWindowPosition: engineAPI.toWindowPosition,
669
683
  isPositionVisible: engineAPI.isPositionVisible,
684
+ updateCredits,
670
685
  }),
671
- [selectionReason, engineAPI, onLayerEdit, onLayerVisibility, onLayerLoad, timelineManagerRef],
686
+ [
687
+ selectionReason,
688
+ engineAPI,
689
+ onLayerEdit,
690
+ onLayerVisibility,
691
+ onLayerLoad,
692
+ timelineManagerRef,
693
+ updateCredits,
694
+ ],
672
695
  );
673
696
 
674
697
  useEffect(() => {
@@ -733,41 +756,6 @@ export default ({
733
756
  unmountCamera?.();
734
757
  }, [unmountCamera]);
735
758
 
736
- const updateCredits = useCallback(() => {
737
- if (!onCreditsUpdate) return;
738
- // currently we don't have a proper way to get the credits update event
739
- // wait for 3 seconds to get latest credits
740
- // some internal property is been used here.
741
- setTimeout(() => {
742
- const creditDisplay = cesium.current?.cesiumElement?.creditDisplay as
743
- | (CreditDisplay & {
744
- _currentFrameCredits: {
745
- lightboxCredits: { _array: { credit?: CesiumCredit }[] };
746
- screenCredits: { _array: { credit?: CesiumCredit }[] };
747
- };
748
- _currentCesiumCredit: CesiumCredit;
749
- })
750
- | undefined;
751
-
752
- if (!creditDisplay) return;
753
-
754
- const { lightboxCredits, screenCredits } = creditDisplay?._currentFrameCredits || {};
755
- const cesiumCredits = creditDisplay._currentCesiumCredit;
756
-
757
- const credits: Credit[] = [
758
- ...(cesiumCredits?.html ? [{ html: cesiumCredits.html }] : []),
759
- ...Array.from(lightboxCredits?._array ?? []).map(c => ({
760
- html: c?.credit?.html,
761
- })),
762
- ...Array.from(screenCredits?._array ?? []).map(c => ({
763
- html: c?.credit?.html,
764
- })),
765
- ];
766
-
767
- onCreditsUpdate(credits);
768
- }, 3000);
769
- }, [onCreditsUpdate]);
770
-
771
759
  const handleTilesChange = useCallback(() => {
772
760
  updateCredits();
773
761
  }, [updateCredits]);