PolylineMeshDesc
The PolylineMeshDesc class draws a single polyline from a list of positions, without a source or a layer. It uses the same geometry and material as a vector layer’s polyline, and update() rebuilds it from new positions in place. This makes it suited to lines that change often, for example a route being drawn or edited. Once a line is final, toGeoJSON() hands it to a geojson source.
By default the line is clamped to the ground: it follows the terrain under it and is hidden by geometry in front of the ground. With clampToGround: false, it is drawn at height.
The line is lit, so it renders black when the scene has no light. Add a light, or set lit: false on the mesh config to draw it in its plain color.
The base class transform properties (geodetic, matrix, matrixWorld, position, rotation, scale) work as for any other mesh: they map the positions from the mesh’s local space to the world. Geographic positions are converted to ECEF first, so without a transform they are drawn where they are. The transform is applied when the geometry is built, so changing it with update() rebuilds the line. visible, lit and pickable are available. See MeshDesc for details.
For example, geodetic places cartesian points in a west-up-north frame at a geographic position:
{ polyline: { // 500 m to the north, in west-up-north meters points: [ { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 500 }, ], }, geodetic: { lng: 139.76, lat: 35.68, height: 0 },}Positions
Section titled “Positions”Set exactly one of positions, points or geojson. Setting one in update() clears the others. Passing the same array again rebuilds the line, so an array mutated in place can be re-submitted as is.
A mesh draws one line. Add one mesh per part of a multi-line.
A line is empty when fewer than two of its positions are apart on the ground, for example [], a single position, or positions that differ only in height or lie within 1 mm of each other. An empty line throws no error: the mesh stays in the view but draws and picks nothing until update() gives it a line again.
positions
Section titled “positions”Type: LatLngHeight[]
Description: Geographic positions: lng and lat in degrees, height in meters.
Default: undefined
Example:
{ polyline: { positions: [ { lng: 139.76, lat: 35.68, height: 0 }, { lng: 139.77, lat: 35.685, height: 0 }, { lng: 139.78, lat: 35.68, height: 0 }, ], }}points
Section titled “points”Type: XYZ[]
Description: Cartesian positions in meters in the mesh’s local space, which is ECEF without a transform.
Default: undefined
Example:
{ polyline: { points: [ { x: -3959000, y: 3352000, z: 3697000 }, { x: -3960000, y: 3351000, z: 3697000 }, ], }}geojson
Section titled “geojson”Type: LineString | Polygon (GeoJSON geometry)
Description: A GeoJSON LineString, with coordinates as [lng, lat, height?] in degrees and meters.
Default: undefined
Example:
{ polyline: { geojson: { type: "LineString", coordinates: [ [139.76, 35.68], [139.78, 35.68], ], }, }}Type: boolean
Description: Joins a repeated first position as a seam instead of drawing two end caps, so a closed outline has no visible start. Repeat the first position at the end of the positions to close the line.
Default: false
Example:
{ polyline: { positions: ring, // the first position repeated at the end ring: true, }}Properties
Section titled “Properties”clampToGround
Section titled “clampToGround”Type: boolean
Description: Draws the line on the terrain under it, following the terrain as tiles load. A clamped line ignores height. Can be changed with update().
Default: true
Example:
{ polyline: { clampToGround: false, }}useGroundNormals
Section titled “useGroundNormals”Type: boolean
Description: Shades a clamped line with the terrain’s normal instead of its own, so the line takes the terrain’s light and shade. It costs a full-screen copy of the terrain normals each frame. Enable it only where the terrain writes normals (quantized-mesh with requestVertexNormals, or a raster-dem terrain with a hillshade layer): elsewhere the line is shaded with an undefined normal.
Default: false
Example:
{ polyline: { clampToGround: true, useGroundNormals: true, }}height
Section titled “height”Type: number
Description: Height of the line in meters, relative to the ellipsoid. Applies when clampToGround is false.
Default: 0
Example:
{ polyline: { clampToGround: false, height: 100, }}Type: number
Description: Width of the line in pixels.
Default: 1
Example:
{ polyline: { width: 4, }}maxWidth
Section titled “maxWidth”Type: number
Description: Upper limit of the rendered width in meters. From far away a pixel covers many meters, so the default limit makes the line thinner than width at high altitude. Raise it to keep the line at width pixels from far away.
Default: 1000
Example:
{ polyline: { width: 3, maxWidth: 100000, }}dashArray
Section titled “dashArray”Type: number[]
Description: Lengths of the alternating dashes and gaps of the line, in multiples of the line width. The pattern repeats along the line with a period equal to the sum of the entries, following the line-dasharray property of the MapLibre Style Spec. An array with an odd number of entries therefore ends in a dash that joins the first one. An empty array draws a solid line.
The pattern keeps its place on the line while the camera moves, and a dash is drawn up to twice as long as specified, depending on the camera distance. The pattern starts in the middle of its longest gap. Picking treats the gaps as part of the line.
Default: []
Example:
{ polyline: { width: 4, // 12px dashes separated by 8px gaps dashArray: [3, 2], }}Type: "butt" | "round" | "square"
Description: Shape of the ends of the line and of the dashes of its dashArray pattern. "round" adds a half circle, half a line width in radius, beyond both ends of the line and of every dash. Each dash is therefore one line width longer than its dashArray entry, and a dash of length 0 is drawn as a dot. "square" extends both ends of the line by half a line width and leaves the ends of the dashes square. A closed ring has no ends.
Default: "butt"
Example:
{ polyline: { // A dotted line dashArray: [0, 2], cap: "round", }}Type: Color
Description: Specifies the line color as a Color instance.
Default: White (0xffffff)
Example:
import { Color } from "@navaramap/three";
{ polyline: { color: new Color().setHex(0xff6b2c), }}gradient
Section titled “gradient”Type: LineGradientStop[]
Description: Colors the line along its length instead of with color, following the line-gradient property of the MapLibre Style Spec. Each stop gives a color and an opacity at a progress along the line, from 0 at its first position to 1 at its last, measured by lineLength. Between two stops the color and opacity change linearly. Before the first stop and after the last they keep that stop’s values, so the caps take the color of their end. Two stops at the same progress make a hard change.
A stop’s opacity multiplies opacity, and a stop below 1 makes the line blend without transparent. As in MapLibre, the gradient also colors a dashArray pattern: each dash takes the color at its place on the line. Where the opacity reaches 0, the line draws nothing and hides nothing behind it, but it still picks. With ring, the progress jumps from 1 back to 0 at the seam. As in MapLibre, the gradient is drawn from a ramp of 256 colors over the whole line, or up to 4096 when stops lie close together, so a stop lands on the line to within one color of the ramp: within about 12 m on a 50 km line. The stops must be in ascending progress, and [] removes the gradient.
The progress here is measured in meters by lineLength. The gradient of a polyline layer material measures it as MapLibre does, by length in Web Mercator, so the same stops can fall at other places on a long line, most of all one running north and south.
To place a stop at a distance in meters, divide the distance by lineLength, as in the second example below. The stops keep their progress when the length changes, so recompute such a stop after each change that can change it: new positions in update(), and a change of the transform, including the terrain height under a geodetic placement with heightReference: "terrain".
| Field | Type | Description |
|---|---|---|
progress | number | Where the stop lies along the line, from 0 to 1 |
color | Color | Color of the line at the stop |
opacity | number | undefined | Opacity of the line at the stop, from 0 to 1. Defaults to 1 |
Default: undefined
Example:
import { Color } from "@navaramap/three";
const pink = new Color().setHex(0xff4f9a);
{ polyline: { width: 10, // Fades in over the first 2 % of the line and out over the last 2 % gradient: [ { progress: 0, color: pink, opacity: 0 }, { progress: 0.02, color: pink }, { progress: 0.98, color: pink }, { progress: 1, color: pink, opacity: 0 }, ], }}// Fade the line in over its first 40 m, after new positionsline.update({ polyline: { positions } });line.update({ polyline: { gradient: [ { progress: 0, color: pink, opacity: 0 }, { progress: Math.min(40 / line.ref.lineLength, 1), color: pink }, ], },});opacity
Section titled “opacity”Type: number
Description: Opacity from 0.0 (fully transparent) to 1.0 (fully opaque). Requires transparent to be true.
Default: 1
Example:
{ polyline: { transparent: true, opacity: 0.5, }}transparent
Section titled “transparent”Type: boolean
Description: Enables alpha blending, which opacity needs.
Default: false
Example:
{ polyline: { transparent: true, }}depthWrite
Section titled “depthWrite”Type: boolean
Description: Enables writing to the depth buffer. Set it to false for a transparent line to avoid depth sorting artifacts.
Default: true
Example:
{ polyline: { depthWrite: false, }}castShadow
Section titled “castShadow”Type: boolean
Description: Specifies whether the line casts shadows. A clamped line casts no shadow.
Default: false
Example:
{ polyline: { clampToGround: false, castShadow: true, }}receiveShadow
Section titled “receiveShadow”Type: boolean
Description: Specifies whether the line receives shadows. A clamped line receives the directional light’s shadows at the ground under each pixel.
Default: false
Example:
{ polyline: { receiveShadow: true, }}emissiveColor
Section titled “emissiveColor”Type: Color
Description: Specifies the emissive color as a Color instance, used with emissiveIntensity.
Default: undefined
Example:
import { Color } from "@navaramap/three";
{ polyline: { emissiveColor: new Color().setHex(0xffaa00), emissiveIntensity: 1, }}emissiveIntensity
Section titled “emissiveIntensity”Type: number
Description: Specifies the emissive intensity.
Default: 0
Example:
{ polyline: { emissiveIntensity: 1, }}effectIds
Section titled “effectIds”Type: string[]
Description: Specifies an array of selective effect descriptor IDs to apply to this line.
Default: undefined
Example:
{ polyline: { effectIds: ["bloom-effect"], }}The layer-only options tiled, geodesic, geometryTypes, show and lit of PolylineMaterial are not available, and gradient takes Color stops whose progress is measured by lineLength. Use the mesh config’s visible and lit instead of show and lit.
Methods and Accessors
Section titled “Methods and Accessors”toGeoJSON()
Section titled “toGeoJSON()”Returns the line as a GeoJSON LineString, for example to add it to a geojson source once editing is done. An empty line returns no coordinates.
Syntax:
toGeoJSON(): LineStringReturns:
A GeoJSON LineString whose coordinates are [lng, lat, height]. The coordinates are the positions as drawn, with the transform applied.
Example:
// Replace the edited mesh with a feature of a vector layerconst source = view.addSource({ type: "geojson", data: line.ref.toGeoJSON(),});view.addLayer({ type: "vector", source, polyline: { color: new Color().setHex(0xff6b2c), width: 4, clampToGround: true, },});line.delete();batchId
Section titled “batchId”Type: number | undefined
Description: The batch ID assigned when pickable is true. Compare it with the picked feature to tell which line was clicked. See Picking.
lineLength
Section titled “lineLength”Type: number
Description: Length of the line in meters, which the gradient progress is measured by. It is the sum of the straight segments between the positions placed on the ellipsoid at height 0, after a segment longer than about 10 km is split along the geodesic, so neither the positions’ heights nor the terrain count. New positions and a change of the transform change it (see gradient), and it is 0 for an empty line.
Usage Examples
Section titled “Usage Examples”Clamped Line
Section titled “Clamped Line”import ThreeView, { Color } from "@navaramap/three";import type { PolylineMeshDesc } from "@navaramap/three-default-descs";import { DefaultPlugin, type DefaultDescriptions,} from "@navaramap/three-default-plugin";
const view = new ThreeView<DefaultDescriptions>();view.addPlugin(new DefaultPlugin());await view.init();
const line = view.addMesh<PolylineMeshDesc>({ polyline: { positions: [ { lng: 138.72, lat: 35.36, height: 0 }, { lng: 138.74, lat: 35.37, height: 0 }, { lng: 138.76, lat: 35.36, height: 0 }, ], color: new Color().setHex(0xff6b2c), width: 4, }, // Plain color without lights in the scene lit: false, pickable: true,});
// Append a point: the line is rebuilt from the new positionsline.update({ polyline: { positions: [ { lng: 138.72, lat: 35.36, height: 0 }, { lng: 138.74, lat: 35.37, height: 0 }, { lng: 138.76, lat: 35.36, height: 0 }, { lng: 138.78, lat: 35.37, height: 0 }, ], },});Closed Outline of a Polygon
Section titled “Closed Outline of a Polygon”import ThreeView, { Color, type LatLngHeight } from "@navaramap/three";import type { PolygonMeshDesc, PolylineMeshDesc,} from "@navaramap/three-default-descs";import { DefaultPlugin, type DefaultDescriptions,} from "@navaramap/three-default-plugin";
const view = new ThreeView<DefaultDescriptions>();view.addPlugin(new DefaultPlugin());await view.init();
view.addLight({ ambient: { intensity: 1 } });
const ring: LatLngHeight[] = [ { lng: 138.72, lat: 35.36, height: 0 }, { lng: 138.75, lat: 35.36, height: 0 }, { lng: 138.75, lat: 35.38, height: 0 }, { lng: 138.72, lat: 35.36, height: 0 },];
view.addMesh<PolygonMeshDesc>({ polygon: { positions: ring, color: new Color().setHex(0x0091ff), transparent: true, opacity: 0.5, },});view.addMesh<PolylineMeshDesc>({ polyline: { positions: ring, ring: true, color: new Color().setHex(0xffffff), width: 3, },});