WebGLRenderer interface

Interface for rendering WebGL graphics in a WebGLLayer.

Properties

renderingMode

Either "2d" or "3d". Defaults to "2d".

Methods

onAdd(Map, WebGLRenderingContext)

Optional method called when the layer has been added to the Map. This gives the layer a chance to initialize gl resources and register event listeners.

onRemove(Map, WebGLRenderingContext)

Optional method called when the layer has been removed from the Map. This gives the layer a chance to clean up gl resources and event listeners.

prerender(WebGLRenderingContext, number[])

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.

render(WebGLRenderingContext, number[])

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).

Property Details

renderingMode

Either "2d" or "3d". Defaults to "2d".

renderingMode?: "2d" | "3d"

Property Value

"2d" | "3d"

Method Details

onAdd(Map, WebGLRenderingContext)

Optional method called when the layer has been added to the Map. This gives the layer a chance to initialize gl resources and register event listeners.

function onAdd(map: Map, gl: WebGLRenderingContext)

Parameters

map
Map

The Map this WebGL layer was just added to.

gl

WebGLRenderingContext

The gl context for the map.

onRemove(Map, WebGLRenderingContext)

Optional method called when the layer has been removed from the Map. This gives the layer a chance to clean up gl resources and event listeners.

function onRemove(map: Map, gl: WebGLRenderingContext)

Parameters

map
Map

The Map this WebGL layer was just added to.

gl

WebGLRenderingContext

The gl context for the map.

prerender(WebGLRenderingContext, number[])

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.

function prerender(gl: WebGLRenderingContext, matrix: number[])

Parameters

gl

WebGLRenderingContext

The map's gl context.

matrix

number[]

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 .fromLatLng can be used to project a LngLat to a mercator coordinate.

render(WebGLRenderingContext, number[])

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).

function render(gl: WebGLRenderingContext, matrix: number[])

Parameters

gl

WebGLRenderingContext

The map's gl context.

matrix

number[]

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 .fromLatLng can be used to project a LngLat to a mercator coordinate.