Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/render/painter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
isRenderingGlobe: boolean;
};

type RenderHookPhase =
| 'beforeOpaque'
| 'afterOpaque'
| 'beforeTranslucent'
| 'afterTranslucent';

type RenderHook = (painter: Painter, renderOptions: RenderOptions) => void;

/**
* @internal
* Initialize a new painter object.
Expand Down Expand Up @@ -135,6 +143,8 @@
// every time the camera-matrix changes the terrain-facilitators will be redrawn.
terrainFacilitator: {dirty: boolean; matrix: mat4; renderTime: number};

_renderHooks: Map<RenderHookPhase, RenderHook[]> = new Map();

constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, transform: IReadonlyTransform) {
this.context = new Context(gl);
this.transform = transform;
Expand Down Expand Up @@ -560,6 +570,7 @@
this._showOverdrawInspector = options.showOverdrawInspector;
this.depthRangeFor3D = [0, 1 - ((style._order.length + 2) * this.numSublayers * this.depthEpsilon)];

this._runRenderHooks('beforeOpaque',renderOptions);
// Opaque pass ===============================================
// Draw opaque layers top-to-bottom first.
if (!this.renderToTexture) {
Expand All @@ -575,6 +586,9 @@
}
}

this._runRenderHooks('afterOpaque',renderOptions);
this._runRenderHooks('beforeTranslucent',renderOptions);

// Translucent pass ===============================================
// Draw all other layers bottom-to-top.
this.renderPass = 'translucent';
Expand Down Expand Up @@ -605,6 +619,8 @@
this.renderLayer(this, tileManager, layer, coords, renderOptions);
}

this._runRenderHooks('afterTranslucent',renderOptions);

// Render atmosphere, only for Globe projection
if (renderOptions.isRenderingGlobe) {
drawAtmosphere(this, this.style.sky, this.style.light);
Expand Down Expand Up @@ -843,4 +859,30 @@
const {drawingBufferWidth, drawingBufferHeight} = this.context.gl;
return this.width !== drawingBufferWidth || this.height !== drawingBufferHeight;
}

/**
*
* @param phase

Check warning on line 865 in src/render/painter.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
*/
_runRenderHooks(phase: RenderHookPhase,renderOptions: RenderOptions) {
const hooks = this._renderHooks.get(phase);
if (!hooks) return;
for (const hook of hooks) {
hook(this,renderOptions);
}
}

/**
*
* @param phase

Check warning on line 877 in src/render/painter.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
* @param hook

Check warning on line 878 in src/render/painter.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
*/
addRenderHook(phase: RenderHookPhase, hook: RenderHook) {
let list = this._renderHooks.get(phase);
if (!list) {
list = [];
this._renderHooks.set(phase, list);
}
list.push(hook);
}
}