Maps SDK for Web
ICustomLayer
Custom layers allow a user to render directly into the map's GL context using the map's camera. These layers can be added between any regular layers using map.addLayer. Custom layers must have a unique id and must have the type of "custom". They must implement render and may implement prerender, onAdd and onRemove. They can trigger rendering using map.addLayer and they should appropriately handle webglcontextlost map event and webglcontextrestored map event.
The renderingMode property controls whether the layer is treated as a "2d" or "3d" map layer. Use:
- "renderingMode": "3d" to use the depth buffer and share it with other layers
- "renderingMode": "2d" to add a layer with no depth. If you need to use the depth buffer for a "2d" layer you must use an offscreen framebuffer and CustomLayerInterface#prerender
Properties
id: String
renderingMode: String
"2d"
or "3d"
. Defaults to "2d"
.
type: String
"custom"
.
Methods
onAdd([map], [gl])
Optional method called when the layer has been added to the Map with map.addLayer. This gives the layer a chance to initialize gl resources and register event listeners.
Parameters
Name | Description |
---|---|
map
Maps.Map default:None |
The Map this custom layer was just added to. |
gl
WebGLRenderingContext default:None |
The gl context for the map. |
onRemove([map], [gl])
Optional method called when the layer has been removed from the Map with map.removeLayer. This gives the layer a chance to clean up gl resources and event listeners.
Parameters
Name | Description |
---|---|
map
Maps.Map default:None |
The Map this custom layer was just removed from. |
gl
WebGLRenderingContext default:None |
The gl context for the map. |
prerender([gl], [matrix])
Optional method called during a render frame to allow a layer to prepare resources or render into a texture.
The layer cannot make any assumptions about the current GL state and must bind a framebuffer before rendering.
Parameters
Name | Description |
---|---|
gl
WebGLRenderingContext default:None |
The gl context for the map. |
matrix
Array default:None |
The map's camera matrix. It projects spherical mercator coordinates to gl coordinates.
The mercator coordinate (0, 0) represents the top left corner of the mercator world and (1, 1) represents
the bottom right corner. When the renderingMode is "3d" , the z coordinate is conformal.
A box with identical x, y, and z lengths in mercator units would be rendered as a cube.
MercatorCoordinate method fromLngLat
can be used to project a LngLat to a mercator coordinate.
|
render([gl], [matrix])
Called during a render frame allowing the layer to draw into the GL context. The layer can assume blending and depth state is set to allow the layer to properly blend and clip other layers. The layer cannot make any other assumptions about the current GL state. If the layer needs to render to a texture, it should implement the prerender method to do this and only use the render method for drawing directly into the main framebuffer.
The blend function is set to gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
.
This expects colors to be provided in premultiplied alpha form where the r, g and b values are already multiplied
by the a value. If you are unable to provide colors in premultiplied form you may want to change the blend function
to gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
.
The layer cannot make any assumptions about the current GL state and must bind a framebuffer before rendering.
Parameters
Name | Description |
---|---|
gl
WebGLRenderingContext default:None |
The gl context for the map. |
matrix
Array default:None |
The map's camera matrix. It projects spherical mercator coordinates to gl coordinates.
The mercator coordinate (0, 0) represents the top left corner of the mercator world and (1, 1) represents
the bottom right corner. When the renderingMode is "3d" , the z coordinate is conformal.
A box with identical x, y, and z lengths in mercator units would be rendered as a cube.
MercatorCoordinate method fromLngLat
can be used to project a LngLat to a mercator coordinate.
|