This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Cesium Ion terrain (#331)
* custom-terrain-url * update * Bug fix * Revert "Bug fix" This reverts commit 7b773b4. * fix: remove get-pixels dependency and used CanvasAPI for PNG conversion to multidimensional array * remove comments and logging statements * update * fix * remove get-pixels * relocated the formatting helper statements * fix * remove gsi terrain * refactor Co-authored-by: nina992 <nouralali992@gmail.com> Co-authored-by: rot1024 <aayhrot@gmail.com> Co-authored-by: Piyush Chauhan <piyushchauhan1004@gmail.com>
- Loading branch information
1 parent
bd9dc50
commit e0b99a6
Showing
7 changed files
with
142 additions
and
91 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/components/molecules/Visualizer/Engine/Cesium/core/Globe.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
ArcGISTiledElevationTerrainProvider, | ||
CesiumTerrainProvider, | ||
EllipsoidTerrainProvider, | ||
IonResource, | ||
TerrainProvider, | ||
} from "cesium"; | ||
import { pick } from "lodash-es"; | ||
import { useMemo } from "react"; | ||
import { Globe as CesiumGlobe } from "resium"; | ||
|
||
import { objKeys } from "@reearth/util/util"; | ||
|
||
import type { SceneProperty, TerrainProperty } from "../../ref"; | ||
|
||
export type Props = { | ||
property?: SceneProperty; | ||
cesiumIonAccessToken?: string; | ||
}; | ||
|
||
export default function Globe({ property, cesiumIonAccessToken }: Props): JSX.Element | null { | ||
const terrainProperty = useMemo( | ||
(): TerrainProperty => ({ | ||
...property?.terrain, | ||
...pick(property?.default, terrainPropertyKeys), | ||
}), | ||
[property?.terrain, property?.default], | ||
); | ||
|
||
const terrainProvider = useMemo((): TerrainProvider | undefined => { | ||
const opts = { | ||
terrain: terrainProperty?.terrain, | ||
terrainType: terrainProperty?.terrainType, | ||
terrainCesiumIonAccessToken: | ||
terrainProperty?.terrainCesiumIonAccessToken || cesiumIonAccessToken, | ||
terrainCesiumIonAsset: terrainProperty?.terrainCesiumIonAsset, | ||
terrainCesiumIonUrl: terrainProperty?.terrainCesiumIonUrl, | ||
}; | ||
const provider = opts.terrain ? terrainProviders[opts.terrainType || "cesium"] : undefined; | ||
return (typeof provider === "function" ? provider(opts) : provider) ?? defaultTerrainProvider; | ||
}, [ | ||
terrainProperty?.terrain, | ||
terrainProperty?.terrainType, | ||
terrainProperty?.terrainCesiumIonAccessToken, | ||
terrainProperty?.terrainCesiumIonAsset, | ||
terrainProperty?.terrainCesiumIonUrl, | ||
cesiumIonAccessToken, | ||
]); | ||
|
||
return ( | ||
<CesiumGlobe | ||
enableLighting={!!property?.atmosphere?.enable_lighting} | ||
showGroundAtmosphere={property?.atmosphere?.ground_atmosphere ?? true} | ||
atmosphereSaturationShift={property?.atmosphere?.surturation_shift} | ||
atmosphereHueShift={property?.atmosphere?.hue_shift} | ||
atmosphereBrightnessShift={property?.atmosphere?.brightness_shift} | ||
terrainProvider={terrainProvider} | ||
depthTestAgainstTerrain={!!terrainProperty.depthTestAgainstTerrain} | ||
terrainExaggerationRelativeHeight={terrainProperty.terrainExaggerationRelativeHeight} | ||
terrainExaggeration={terrainProperty.terrainExaggeration} | ||
/> | ||
); | ||
} | ||
|
||
const terrainPropertyKeys = objKeys<TerrainProperty>({ | ||
terrain: 0, | ||
terrainType: 0, | ||
terrainExaggeration: 0, | ||
terrainExaggerationRelativeHeight: 0, | ||
depthTestAgainstTerrain: 0, | ||
terrainCesiumIonAsset: 0, | ||
terrainCesiumIonAccessToken: 0, | ||
terrainCesiumIonUrl: 0, | ||
terrainUrl: 0, | ||
}); | ||
|
||
const defaultTerrainProvider = new EllipsoidTerrainProvider(); | ||
|
||
const terrainProviders: { | ||
[k in NonNullable<TerrainProperty["terrainType"]>]: | ||
| TerrainProvider | ||
| (( | ||
opts: Pick< | ||
TerrainProperty, | ||
"terrainCesiumIonAccessToken" | "terrainCesiumIonAsset" | "terrainCesiumIonUrl" | ||
>, | ||
) => TerrainProvider | null); | ||
} = { | ||
cesium: ({ terrainCesiumIonAccessToken }) => | ||
// https://github.com/CesiumGS/cesium/blob/main/Source/Core/createWorldTerrain.js | ||
new CesiumTerrainProvider({ | ||
url: IonResource.fromAssetId(1, { | ||
accessToken: terrainCesiumIonAccessToken, | ||
}), | ||
requestVertexNormals: false, | ||
requestWaterMask: false, | ||
}), | ||
arcgis: () => | ||
new ArcGISTiledElevationTerrainProvider({ | ||
url: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer", | ||
}), | ||
cesiumion: ({ terrainCesiumIonAccessToken, terrainCesiumIonAsset, terrainCesiumIonUrl }) => | ||
terrainCesiumIonAsset | ||
? new CesiumTerrainProvider({ | ||
url: | ||
terrainCesiumIonUrl || | ||
IonResource.fromAssetId(parseInt(terrainCesiumIonAsset, 10), { | ||
accessToken: terrainCesiumIonAccessToken, | ||
}), | ||
requestVertexNormals: true, | ||
}) | ||
: null, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/components/molecules/Visualizer/Engine/Cesium/terrain.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters