forked from maplibre/maplibre-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_custom.ts
63 lines (48 loc) · 2.32 KB
/
draw_custom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {DepthMode} from '../gl/depth_mode';
import {StencilMode} from '../gl/stencil_mode';
import type {Painter} from './painter';
import type {SourceCache} from '../source/source_cache';
import type {CustomRenderMethodInput, CustomStyleLayer} from '../style/style_layer/custom_style_layer';
export function drawCustom(painter: Painter, sourceCache: SourceCache, layer: CustomStyleLayer) {
const context = painter.context;
const implementation = layer.implementation;
const projection = painter.style.projection;
const transform = painter.transform;
const projectionData = transform.getProjectionDataForCustomLayer();
const customLayerArgs: CustomRenderMethodInput = {
farZ: transform.farZ,
nearZ: transform.nearZ,
fov: transform.fov * Math.PI / 180, // fov converted to radians
modelViewProjectionMatrix: transform.modelViewProjectionMatrix,
projectionMatrix: transform.projectionMatrix,
shaderData: {
variantName: projection.shaderVariantName,
vertexShaderPrelude: `const float PI = 3.141592653589793;\nuniform mat4 u_projection_matrix;\n${projection.shaderPreludeCode.vertexSource}`,
define: projection.shaderDefine,
},
defaultProjectionData: projectionData,
};
const renderingMode = implementation.renderingMode ? implementation.renderingMode : '2d';
if (painter.renderPass === 'offscreen') {
const prerender = implementation.prerender;
if (prerender) {
painter.setCustomLayerDefaults();
context.setColorMode(painter.colorModeForRenderPass());
prerender.call(implementation, context.gl, customLayerArgs);
context.setDirty();
painter.setBaseState();
}
} else if (painter.renderPass === 'translucent') {
painter.setCustomLayerDefaults();
context.setColorMode(painter.colorModeForRenderPass());
context.setStencilMode(StencilMode.disabled);
const depthMode = renderingMode === '3d' ?
painter.getDepthModeFor3D() :
painter.getDepthModeForSublayer(0, DepthMode.ReadOnly);
context.setDepthMode(depthMode);
implementation.render(context.gl, customLayerArgs);
context.setDirty();
painter.setBaseState();
context.bindFramebuffer.set(null);
}
}