-
Notifications
You must be signed in to change notification settings - Fork 658
Expand file tree
/
Copy pathLayerService.ts
More file actions
308 lines (266 loc) · 7.96 KB
/
Copy pathLayerService.ts
File metadata and controls
308 lines (266 loc) · 7.96 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import type { DebouncedFunc } from '@antv/l7-utils';
import { lodashUtil, rgb2arr } from '@antv/l7-utils';
import { EventEmitter } from 'eventemitter3';
import type { L7Container } from '../../inversify.config';
import Clock from '../../utils/clock';
import type { ILayer, ILayerService, LayerServiceEvent } from './ILayerService';
import { MaskOperation, StencilType } from './ILayerService';
const { throttle } = lodashUtil;
export default class LayerService extends EventEmitter<LayerServiceEvent> implements ILayerService {
// pickedLayerId 参数用于指定当前存在被选中的 layer
public pickedLayerId: number = -1;
public clock = new Clock();
public alreadyInRendering: boolean = false;
private layers: ILayer[] = [];
private layerList: ILayer[] = [];
private layerRenderID: number;
private sceneInited: boolean = false;
private animateInstanceCount: number = 0;
// TODO: 是否开启 shader 中的颜色拾取计算
private shaderPicking: boolean = true;
private enableRender: boolean = true;
private get renderService() {
return this.container.rendererService;
}
private get mapService() {
return this.container.mapService;
}
private get debugService() {
return this.container.debugService;
}
constructor(private container: L7Container) {
super();
}
public reRender: DebouncedFunc<() => void> = throttle(() => {
this.renderLayers();
}, 32);
public throttleRenderLayers: DebouncedFunc<() => void> = throttle(() => {
this.renderLayers();
}, 16);
public needPick(type: string): boolean {
this.updateLayerRenderList();
return this.layerList.some((layer) => layer.needPick(type));
}
public add(layer: ILayer) {
this.layers.push(layer);
if (this.sceneInited) {
layer.init().then(() => {
this.renderLayers();
});
}
}
public addMask(mask: ILayer) {
if (this.sceneInited) {
mask.init().then(() => {
this.renderLayers();
});
}
}
public async initLayers() {
this.sceneInited = true;
this.layers.forEach(async (layer) => {
if (!layer.startInit) {
await layer.init();
this.updateLayerRenderList();
}
});
}
public getSceneInited() {
return this.sceneInited;
}
public getRenderList(): ILayer[] {
return this.layerList;
}
public getLayers(): ILayer[] {
return this.layers;
}
public getLayer(id: string): ILayer | undefined {
return this.layers.find((layer) => layer.id === id);
}
public getLayerByName(name: string): ILayer | undefined {
return this.layers.find((layer) => layer.name === name);
}
public async remove(layer: ILayer, parentLayer?: ILayer): Promise<void> {
if (parentLayer) {
if (!parentLayer.layerChildren) {
parentLayer.layerChildren = [];
}
const index = parentLayer.layerChildren.findIndex((item) => item.id === layer.id);
if (index > -1) {
parentLayer.layerChildren.splice(index, 1);
}
} else {
const index = this.layers.findIndex((item) => item.id === layer.id);
if (index > -1) {
this.layers.splice(index, 1);
}
}
layer.destroy();
this.reRender();
this.emit('layerChange', this.layers);
}
public async removeAllLayers(): Promise<void> {
this.destroy();
this.reRender();
}
public setEnableRender(flag: boolean) {
this.enableRender = flag;
}
public async renderLayers() {
if (this.alreadyInRendering || !this.enableRender) {
return;
}
this.updateLayerRenderList();
const renderUid = this.debugService.generateRenderUid();
this.debugService.renderStart(renderUid);
this.alreadyInRendering = true;
this.clear();
for (const layer of this.layerList) {
layer.prerender();
}
// The main render pass, all layers in a whole.
this.renderService.beginFrame();
for (const layer of this.layerList) {
const { enableMask } = layer.getLayerConfig();
if (layer.masks.filter((m) => m.inited).length > 0 && enableMask) {
// 清除上一次的模版缓存
this.renderMask(layer.masks);
}
if (layer.getLayerConfig().enableMultiPassRenderer) {
// multiPassRender 不是同步渲染完成的
await layer.renderMultiPass();
} else {
layer.render();
}
}
this.renderService.endFrame();
this.debugService.renderEnd(renderUid);
this.alreadyInRendering = false;
}
public renderMask(masks: ILayer[]) {
let maskIndex = 0;
this.renderService.clear({
stencil: 0,
depth: 1,
framebuffer: null,
});
const stencilType = masks.length > 1 ? StencilType.MULTIPLE : StencilType.SINGLE;
for (const layer of masks) {
// 清除上一次的模版缓存
layer.render({ isStencil: true, stencilType, stencilIndex: maskIndex++ });
}
}
public async beforeRenderData(layer: ILayer) {
const flag = await layer.hooks.beforeRenderData.promise();
if (flag) {
this.renderLayers();
}
}
public renderTileLayerMask(layer: ILayer) {
let maskindex = 0;
const { enableMask = true } = layer.getLayerConfig();
let maskCount = layer.tileMask ? 1 : 0; // 瓦片裁剪 线图层或者面图层
const masklayers = layer.masks.filter((m) => m.inited);
maskCount = maskCount + (enableMask ? masklayers.length : 1);
const stencilType = maskCount > 1 ? StencilType.MULTIPLE : StencilType.SINGLE;
// 兼容MaskLayer MaskLayer的掩模不能clear
if (layer.tileMask || (masklayers.length && enableMask)) {
this.renderService.clear({
stencil: 0,
depth: 1,
framebuffer: null,
});
}
if (masklayers.length && enableMask) {
for (const mask of masklayers) {
mask.render({
isStencil: true,
stencilType,
stencilIndex: maskindex++,
});
}
}
// // 瓦片裁剪
if (layer.tileMask) {
layer.tileMask.render({
isStencil: true,
stencilType,
stencilIndex: maskindex++,
stencilOperation: MaskOperation.OR,
});
}
}
// 瓦片图层渲染
public async renderTileLayer(layer: ILayer) {
this.renderTileLayerMask(layer);
if (layer.getLayerConfig().enableMultiPassRenderer) {
// multiPassRender 不是同步渲染完成的
await layer.renderMultiPass();
} else {
await layer.render();
}
}
public updateLayerRenderList() {
// Tip: 每次更新都是从 layers 重新构建
this.layerList = [];
this.layers
.filter((layer) => layer.inited)
.filter((layer) => layer.isVisible())
.sort((pre: ILayer, next: ILayer) => {
// 根据 zIndex 对渲染顺序进行排序
return pre.zIndex - next.zIndex;
})
.forEach((layer) => {
this.layerList.push(layer);
});
}
public destroy() {
this.layers.forEach((layer) => {
layer.destroy();
});
this.layers = [];
this.layerList = [];
this.emit('layerChange', this.layers);
}
public startAnimate() {
if (this.animateInstanceCount++ === 0) {
this.clock.start();
this.runRender();
}
}
public stopAnimate() {
if (--this.animateInstanceCount === 0) {
this.stopRender();
this.clock.stop();
}
}
public getOESTextureFloat() {
return this.renderService.extensionObject.OES_texture_float;
}
// 控制着色器颜色拾取计算
public enableShaderPick() {
this.shaderPicking = true;
}
public disableShaderPick() {
this.shaderPicking = false;
}
public getShaderPickStat() {
return this.shaderPicking;
}
public clear() {
const color = rgb2arr(this.mapService.bgColor) as [number, number, number, number];
this.renderService.clear({
color,
depth: 1,
stencil: 0,
framebuffer: null,
});
}
private runRender() {
this.renderLayers();
this.layerRenderID = window.requestAnimationFrame(this.runRender.bind(this));
}
private stopRender() {
window.cancelAnimationFrame(this.layerRenderID);
}
}