three-globe

repository·master·Indexed 23 days ago

https://github.com/vasturiano/three-globe

A WebGL-based visualization tool built on ThreeJS for rendering complex data layers on a spherical globe projection. It supports various data layers including points, arcs, polygons, paths, heatmaps, hex bin maps, hexed polygons, and tiles. The library provides a reusable 3D object that can be integrated into a standard Three.js scene, with standalone (globe.gl) and React Three Fiber (r3f-globe) bindings available.

Tokens
6.3K
Snippets
2
Records
33
Agent score
76%

What's inside three-globe

  1. Overview of ThreeJS Globe Visualization

    master

    Three-globe is a ThreeJS WebGL class designed to represent data visualization layers on a globe using a spherical projection. It is inspired by Google's WebGL Globe and allows developers to layer various types of data (points, arcs, polygons, etc.) onto a 3D globe.

    For different environments, you can use the standalone version (globe.gl) or the React Three Fiber bindings (r3f-globe).

  2. Quick start with Three-globe

    master

    To use three-globe, you can import it as a module or include it via a CDN script tag. Once initialized, you can configure the globe with images and data, then add the resulting object to a standard Three.js scene.

    Module Import

    import ThreeGlobe from 'three-globe';

    Script Tag

    <script src="//cdn.jsdelivr.net/npm/three-globe"></script>

    Basic Usage

    const myGlobe = new ThreeGlobe()
      .globeImageUrl(myImageUrl)
      .pointsData(myData);
    
    const myScene = new THREE.Scene();
    myScene.add(myGlobe);
    import ThreeGlobe from 'three-globe';
    
    // or using a script tag
    // <script src="//cdn.jsdelivr.net/npm/three-globe"></script>
    
    const myGlobe = new ThreeGlobe()
      .globeImageUrl(myImageUrl)
      .pointsData(myData);
    
    const myScene = new THREE.Scene();
    myScene.add(myGlobe);
  3. How data accessors work in ThreeGlobe

    master

    Most layer methods in ThreeGlobe use an Accessor pattern. This allows you to pass data to the globe in three ways:

    1. A direct value: e.g., pointRadius(5) sets all points to radius 5.
    2. A string key: e.g., pointRadius('radius') tells the globe to look for a property named radius on each object in your data array.
    3. A callback function: e.g., pointRadius(d => d.customRadius) allows for custom logic per data object.

    This pattern is used across all layers (Points, Arcs, Polygons, etc.) to map your raw data to visual properties like latitude, longitude, color, altitude, and radius.

  4. Initialize ThreeGlobe with ConfigOptions

    master

    To create a new globe instance, instantiate the ThreeGlobe class. You can optionally pass a ConfigOptions object to the constructor.

    ConfigOptions properties:

    • waitForGlobeReady (boolean): Whether to wait for the globe to be ready.
    • animateIn (boolean): Whether to animate the initial appearance of the globe.
  5. Configure the Polygons Layer

    master

    The Polygons Layer represents shapes as extruded cones from the globe surface. Use polygonsData to provide the list of polygon objects. Each polygon can be customized using the following methods:

    • polygonGeoJsonGeometry([str | fn]): Accessor for the GeoJson geometry (type and coordinates required). Supports Polygon or MultiPolygon.
    • polygonCapColor([str | fn]): Color of the top surface.
    • polygonCapMaterial([material, str | fn]): ThreeJS material for the top surface. Takes precedence over polygonCapColor.
    • polygonSideColor([str | fn]): Color of the cone sides.
    • polygonSideMaterial([material, str | fn]): ThreeJS material for the cone sides. Takes precedence over polygonSideColor.
    • polygonStrokeColor([str | fn]): Color of the perimeter stroke. Falsy values disable stroking.
    • polygonAltitude([num, str | fn]): Altitude of the cone in globe radius units (0 = flat, 1 = globe radius).
    • polygonCapCurvatureResolution([num, str | fn]): Resolution (in angular degrees) for the cap surface curvature. Higher values increase detail but decrease performance.
    • polygonsTransitionDuration([num]): Duration (ms) for animating altitude changes.
  6. Configure the Points Layer

    master

    The Points Layer represents data as cylindrical 3D objects rising from the globe surface.

    Key methods include:

    • pointsData(array): The list of data points to render.
    • pointLat(accessor): Accessor for the cylinder's center latitude.
    • pointLng(accessor): Accessor for the cylinder's center longitude.
    • pointColor(accessor): Accessor for the cylinder color.
    • pointAltitude(accessor): Accessor for the cylinder's altitude (0 = flat, 1 = globe radius).
    • pointRadius(accessor): Accessor for the cylinder's radius in angular degrees.
    • pointsMerge(boolean): If true, merges all point meshes into a single ThreeJS object for better performance.
    • pointsTransitionDuration(ms): Duration for animating geometry changes. Only works if pointsMerge is disabled.
  7. Configure the Globe Layer

    master

    The Globe Layer controls the appearance and properties of the sphere itself.

    Key methods include:

    • globeImageUrl(url): Sets the URL for the equirectangular projection image wrapping the globe. If null, the globe is a black sphere.
    • bumpImageUrl(url): Sets the URL for a bump map to represent terrain (requires equirectangular projection).
    • showGlobe(boolean): Toggles the visibility of the globe surface.
    • showGraticules(boolean): Toggles a latitude/longitude grid (every 10 degrees).
    • showAtmosphere(boolean): Toggles the atmospheric halo.
    • atmosphereColor(color): Sets the color of the atmosphere.
    • atmosphereAltitude(altitude): Sets the max altitude of the atmosphere in globe radius units.
    • globeMaterial(material): Allows passing a custom ThreeJS material for advanced styling.
    • onGlobeReady(fn): A callback invoked once the globe is initialized and visible.
    • globeTileEngineUrl(fn): Defines a function for a slippy map tile engine. The function receives (x, y, l) and must return a URL string.
  8. Configure the 3D Objects Layer

    master

    The 3D Objects Layer allows rendering custom ThreeJS Object3d instances at specific locations.

    • objectsData([array]): The list of custom 3D objects.
    • objectLat([num, str, or fn]): Accessor for latitude (default: 'lat').
    • objectLng([num, str, or fn]): Accessor for longitude (default: 'lng').
    • objectAltitude([num, str, or fn]): Accessor for altitude in globe radius units (default: 0.01).
    • objectRotation([{[x], [y], [z]}, str, or fn]): Accessor for rotation in degrees. Supports partial {x, y, z} objects. Rotation order is X->Y->Z.
    • objectFacesSurface([boolean, str, or fn]): Whether to rotate the object to face the globe surface (default: true).
    • objectThreeObject([Object3d, str, or fn]): Accessor to define the custom 3D object (default: a yellow sphere).
  9. Configure the Hexed Polygons Layer

    master

    The Hexed Polygons layer represents GeoJSON shapes (Polygon or MultiPolygon) as a collection of hexagons.

    Key configuration options:

    • hexPolygonsData: Array of polygon shapes.
    • hexPolygonGeoJsonGeometry: Accessor for the GeoJSON geometry (must contain type and coordinates).
    • hexPolygonResolution: H3 resolution (0-15) for the hexagons.
    • hexPolygonUseDots: If true, represents polygon points as circular dots instead of hexagons.
    • hexPolygonDotResolution: Resolution for circular dots (number of segments) if hexPolygonUseDots is enabled.
    • hexPolygonColor: Accessor for the color of the hexagons.
    • hexPolygonAltitude: Altitude in globe radius units.
    • hexPolygonsTransitionDuration: Duration (ms) for animating altitude and margin changes.
  10. Use Coordinate Utilities

    master

    Three-globe provides utility methods for converting between spherical (geographic) and cartesian (3D spatial) coordinates:

    • getGlobeRadius(): Returns the cartesian distance of a globe radius in absolute spatial units.
    • getCoords(lat, lng, [altitude]): Translates spherical coordinates (latitude, longitude, and optional altitude in globe radius units) to {x, y, z} cartesian spatial coordinates.
    • toGeoCoords({x, y, z}): Translates cartesian {x, y, z} coordinates to {lat, lng, altitude} spherical coordinates (altitude is in globe radius units).
  11. Configure the Tiles Layer

    master

    The Tiles layer displays spherical surface segments (tiles) that can be styled individually.

    Key configuration options:

    • tilesData: Array of tile objects.
    • tileLat / tileLng: Accessors for the segment's centroid coordinates.
    • tileWidth / tileHeight: Longitudinal width and latitudinal height in angular degrees.
    • tileAltitude: Altitude in globe radius units.
    • tileMaterial: Accessor for a ThreeJS Material to style the surface.
    • tileUseGlobeProjection: If true, shapes the segment to the globe's projection; if false, shapes it as if on the equator.
    • tileCurvatureResolution: Resolution (angular degrees) for surface curvature approximation.
    • tilesTransitionDuration: Duration (ms) for animating geometry changes.
  12. Configure the Hex Bin Map Layer

    master

    The Hex Bin Map layer aggregates points into hexagonal 3D prisms using H3 geographic binning. You can control the resolution, appearance, and animation of these hexagons.

    Key configuration options:

    • hexBinPointsData: The array of points to aggregate.
    • hexBinResolution: H3 resolution (0-15). Higher values create smaller hexagons.
    • hexBinPointLat / hexBinPointLng: Accessors for point coordinates.
    • hexBinPointWeight: Accessor for point weight; weights are summed to determine hexagon altitude.
    • hexAltitude: Altitude in globe radius units. Supports an accessor function: ({ sumWeight }) => ....
    • hexMargin: Radial margin (0 to 1) to create gaps between hexagons.
    • hexTopColor / hexSideColor: Accessor functions for coloring the top and sides of the hexagons.
    • hexBinMerge: Set to true to merge all meshes into one ThreeJS object for better performance.
    • hexTransitionDuration: Duration (ms) for animating geometry changes. Note: This only works if hexBinMerge is false.