forked from heremaps/harp.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapRenderingManager.ts
437 lines (393 loc) · 16 KB
/
MapRenderingManager.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (C) 2023- XYZ maps contributors
* Copyright (C) 2019-2021 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import {
IBloomEffect,
IOutlineEffect,
ISepiaEffect,
IVignetteEffect
} from "@xyzmaps/harp-datasource-protocol";
import { SepiaShader, VignetteShader } from "@xyzmaps/harp-materials";
import * as THREE from "three";
import { IPassManager } from "./IPassManager";
import { LowResRenderPass } from "./LowResRenderPass";
import { MSAARenderPass, MSAASampling } from "./MSAARenderPass";
import { OutlineEffect } from "./Outline";
import { RenderPass, ShaderPass } from "./Pass";
import { BloomPass } from "./UnrealBloomPass";
const DEFAULT_DYNAMIC_MSAA_SAMPLING_LEVEL = MSAASampling.Level_1;
const DEFAULT_STATIC_MSAA_SAMPLING_LEVEL = MSAASampling.Level_4;
/**
* Interface for the antialias settings passed when instantiating
* a {@link MapView}, and transferred to
* the {@link MapRenderingManager} instance.
*
* @remarks
* These parameters can be changed at runtime as opposed to
* the native WebGL antialiasing.
*/
export interface IMapAntialiasSettings {
/**
* Whether the MSAA is enabled or not.
*
* @default `false`
*/
msaaEnabled: boolean;
/**
* The sampling level to use for MSAA during continuous rendering.
*
* @default `MSAASampling.Level_1`
*/
dynamicMsaaSamplingLevel?: MSAASampling;
/**
* The sampling level to use for MSAA when the rendering stops.
*
* @default `MSAASampling.Level_4`
*/
staticMsaaSamplingLevel?: MSAASampling;
}
/**
* The `MapRenderingManager` class manages the map rendering (as opposed to text) by dispatching the
* {@link MapRenderingManager.render} call to a set of internal {@link Pass} instances.
*
* @remarks It provides an API to modify some of the rendering
* processes like the antialiasing behaviour at runtime.
*/
export interface IMapRenderingManager extends IPassManager {
/**
* Bloom effect parameters.
*/
bloom: IBloomEffect;
/**
* Outline effect parameters.
*/
outline: IOutlineEffect;
/**
* Vignette effect parameters.
*/
vignette: IVignetteEffect;
/**
* Sepia effect parameters.
*/
sepia: ISepiaEffect;
/**
* Set a `pixelRatio` for dynamic rendering (i.e. during animations). If a value is specified,
* the `LowResRenderPass` will be employed to used to render the scene into a lower resolution
* render target, which will then be rendered to the screen.
*/
lowResPixelRatio?: number;
/**
* The level of MSAA sampling while the user interacts. It should be a low level so that the
* MSAA does not impact the framerate.
*/
dynamicMsaaSamplingLevel: MSAASampling;
/**
* Enable or disable the MSAA. If disabled, `MapRenderingManager` will use the renderer provided
* in the {@link MapRenderingManager.render} method to render the scene.
*/
msaaEnabled: boolean;
/**
* The higher level of MSAA sampling for a last frame to render, when the camera is static. It
* can be a high level, providing high quality renders requiring few tens of seconds, since no
* frame is expected to immediately follow in the requestAnimationFrame. It is still limited by
* zooming, since zooming is not requestAnimationFrame-based and can lead to stuttering if the
* render time is too long, except on desktop Mac, where mouse interaction already implements
* some damping. Higher levels of sampling may lead to noticeable color banding, visible in
* areas with a slight color gradient, like large areas or the sky background.
*/
staticMsaaSamplingLevel: MSAASampling;
/**
* The method to call to render the map. This method depends on an `isStaticFrame` boolean that
* notifies the pass manager to switch to a higher level render quality for the last frame.
*
* @param renderer - The ThreeJS WebGLRenderer instance to render the map with.
* @param isStaticFrame - Whether the frame to render is static or dynamic. Selects level of
* antialiasing.
* @param time - Optional time argument provided by the requestAnimationFrame, to pass to
* sub-passes.
*/
render(
renderer: THREE.WebGLRenderer,
scene: THREE.Scene,
camera: THREE.PerspectiveCamera | THREE.OrthographicCamera,
isStaticFrame: boolean,
time?: number
): void;
/**
* Updating the outline rebuilds the outline materials of every outlined mesh.
*
* @param options - outline options from the {@link @xyzmaps/harp-datasource-protocol#Theme}.
*/
updateOutline(options: {
thickness: number;
color: string;
ghostExtrudedPolygons: boolean;
}): void;
}
/**
* The implementation of {@link IMapRenderingManager} to
* instantiate in {@link MapView} and manage the map
* rendering.
*/
export class MapRenderingManager implements IMapRenderingManager {
bloom = {
enabled: false,
strength: 1.5,
radius: 0.4,
threshold: 0.85
};
outline = {
enabled: false,
thickness: 0.005,
color: "#000000",
ghostExtrudedPolygons: false,
needsUpdate: false
};
vignette = {
enabled: false,
offset: 1.0,
darkness: 1.0
};
sepia = {
enabled: false,
amount: 0.5
};
private m_width: number = 1;
private m_height: number = 1;
private m_outlineEffect?: OutlineEffect;
private m_msaaPass: MSAARenderPass;
private readonly m_renderPass: RenderPass = new RenderPass();
private readonly m_target1: THREE.WebGLRenderTarget = new THREE.WebGLRenderTarget(1, 1);
private readonly m_target2: THREE.WebGLRenderTarget = new THREE.WebGLRenderTarget(1, 1);
private m_bloomPass?: BloomPass;
private m_sepiaPass: ShaderPass = new ShaderPass(SepiaShader);
private m_vignettePass: ShaderPass = new ShaderPass(VignetteShader);
private readonly m_readBuffer: THREE.WebGLRenderTarget;
private m_dynamicMsaaSamplingLevel: MSAASampling;
private m_staticMsaaSamplingLevel: MSAASampling;
private m_lowResPass: LowResRenderPass;
/**
* The constructor of `MapRenderingManager`.
*
* @param width - Width of the frame buffer.
* @param height - Height of the frame buffer.
* @param lowResPixelRatio - The `pixelRatio` determines the resolution of the internal
* `WebGLRenderTarget`. Values between 0.5 and `window.devicePixelRatio` can be tried to give
* good results. A value of `undefined` disables the low res render pass. The value should not
* be larger than`window.devicePixelRatio`.
* @param antialiasSetting - The object defining the demeanor of MSAA.
*/
constructor(
width: number,
height: number,
lowResPixelRatio: number | undefined,
antialiasSettings: IMapAntialiasSettings | undefined = { msaaEnabled: false }
) {
this.m_readBuffer = new THREE.WebGLRenderTarget(width, height);
this.m_msaaPass = new MSAARenderPass();
this.m_msaaPass.enabled =
antialiasSettings !== undefined ? antialiasSettings.msaaEnabled === true : false;
this.m_dynamicMsaaSamplingLevel =
antialiasSettings.dynamicMsaaSamplingLevel === undefined
? DEFAULT_DYNAMIC_MSAA_SAMPLING_LEVEL
: antialiasSettings.dynamicMsaaSamplingLevel;
this.m_staticMsaaSamplingLevel =
antialiasSettings.staticMsaaSamplingLevel === undefined
? DEFAULT_STATIC_MSAA_SAMPLING_LEVEL
: antialiasSettings.staticMsaaSamplingLevel;
this.m_lowResPass = new LowResRenderPass(lowResPixelRatio);
this.m_lowResPass.enabled = lowResPixelRatio !== undefined;
}
updateOutline(options: { thickness: number; color: string; ghostExtrudedPolygons: boolean }) {
this.outline.color = options.color;
this.outline.thickness = options.thickness;
this.outline.ghostExtrudedPolygons = options.ghostExtrudedPolygons;
this.outline.needsUpdate = true;
}
/**
* The method to call to render the map with the `MapRenderingManager` instance. It contains the
* chain of sub-passes that can transfer the write and read buffers, and other sheer rendering
* conditions as disabling AA when a high DPI device is in use.
*
* @param renderer - The ThreeJS WebGLRenderer instance to render the map with.
* @param scene - The ThreeJS Scene instance containing the map objects to render.
* @param camera - The ThreeJS Camera instance to render the scene through.
* @param isStaticFrame - Whether the frame to render is static or dynamic. Selects level of
* antialiasing.
*/
render(
renderer: THREE.WebGLRenderer,
scene: THREE.Scene,
camera: THREE.PerspectiveCamera | THREE.OrthographicCamera,
isStaticFrame: boolean
) {
const target = null;
if (!isStaticFrame && this.m_lowResPass.pixelRatio !== undefined) {
// Not designed to be combined with our own MSAA
this.m_lowResPass.renderToScreen = true;
this.m_lowResPass.render(renderer, scene, camera, target, this.m_readBuffer);
return;
}
const usePostEffects =
this.bloom.enabled ||
this.outline.enabled ||
this.vignette.enabled ||
this.sepia.enabled;
let activeTarget: null | THREE.WebGLRenderTarget = null;
// 1. If the bloom is enabled, clear the depth.
if (this.bloom.enabled || this.vignette.enabled || this.sepia.enabled) {
renderer.setRenderTarget(this.m_target1);
renderer.clearDepth();
}
// 2. Render the map.
if (this.m_msaaPass.enabled) {
// Use a higher MSAA sampling level for static rendering.
this.m_msaaPass.samplingLevel = isStaticFrame
? this.m_staticMsaaSamplingLevel
: this.m_dynamicMsaaSamplingLevel;
// MSAA is the only effect for the moment.
this.m_msaaPass.renderToScreen = !usePostEffects;
// Render to the specified target with the MSAA pass.
this.m_msaaPass.render(renderer, scene, camera, target, this.m_readBuffer);
} else {
if (this.bloom.enabled || this.vignette.enabled || this.sepia.enabled) {
activeTarget = this.m_target1;
this.m_renderPass.render(renderer, scene, camera, this.m_target1, null!);
} else if (!this.outline.enabled || (this.outline.enabled && !this.bloom.enabled)) {
renderer.render(scene, camera);
}
}
// 3. Apply effects
if (this.outline.enabled) {
if (this.m_outlineEffect === undefined) {
this.m_outlineEffect = new OutlineEffect(renderer);
}
if (this.outline.needsUpdate) {
this.m_outlineEffect.color = this.outline.color;
this.m_outlineEffect.thickness = this.outline.thickness;
this.m_outlineEffect.ghostExtrudedPolygons = this.outline.ghostExtrudedPolygons;
this.outline.needsUpdate = false;
}
const nextEffectEnabled =
this.bloom.enabled || this.vignette.enabled || this.sepia.enabled;
if (nextEffectEnabled) {
activeTarget = this.m_target1;
}
renderer.setRenderTarget(nextEffectEnabled ? activeTarget : null!);
this.m_outlineEffect.render(scene, camera);
}
if (this.bloom.enabled) {
if (this.m_bloomPass === undefined) {
this.m_bloomPass = new BloomPass(
new THREE.Vector2(this.m_width, this.m_height),
this.bloom.strength,
this.bloom.radius,
this.bloom.threshold
);
}
const nextEffectEnabled = this.vignette.enabled || this.sepia.enabled;
this.m_bloomPass.renderToScreen = !nextEffectEnabled;
this.m_bloomPass.radius = this.bloom.radius;
this.m_bloomPass.strength = this.bloom.strength;
this.m_bloomPass.threshold = this.bloom.threshold;
this.m_bloomPass.render(renderer, scene, camera, null!, activeTarget!);
} else if (this.m_bloomPass !== undefined) {
this.m_bloomPass.dispose();
this.m_bloomPass = undefined;
}
if (this.vignette.enabled) {
const oldTarget = activeTarget!;
const nextEffectEnabled = this.sepia.enabled;
this.m_vignettePass.uniforms.offset.value = this.vignette.offset;
this.m_vignettePass.uniforms.darkness.value = this.vignette.darkness;
this.m_vignettePass.renderToScreen = !nextEffectEnabled;
if (nextEffectEnabled) {
activeTarget = activeTarget === this.m_target1 ? this.m_target2 : this.m_target1;
}
this.m_vignettePass.render(renderer, scene, camera, activeTarget!, oldTarget);
}
if (this.sepia.enabled) {
this.m_sepiaPass.renderToScreen = true;
this.m_sepiaPass.uniforms.amount.value = this.sepia.amount;
this.m_sepiaPass.render(renderer, scene, camera, null!, activeTarget!);
}
}
/**
* The resize function to call on resize events to resize the render targets. It shall include
* the resize methods of all the sub-passes used in `MapRenderingManager`.
*
* @param width - New width to use.
* @param height - New height to use.
*/
setSize(width: number, height: number) {
this.m_readBuffer.setSize(width, height);
this.m_msaaPass.setSize(width, height);
if (this.m_bloomPass !== undefined) {
this.m_bloomPass.setSize(width, height);
}
this.m_lowResPass.setSize(width, height);
this.m_target1.setSize(width, height);
this.m_target2.setSize(width, height);
this.m_width = width;
this.m_height = height;
}
/**
* The `lowResPixelRatio` determines the resolution of the internal `WebGLRenderTarget`. Values
* between 0.5 and `window.devicePixelRatio` can be tried to give good results. A value of
* `undefined` disables the low res render pass. The value should not be larger than
* `window.devicePixelRatio`.
*/
get lowResPixelRatio(): number | undefined {
return this.m_lowResPass.pixelRatio;
}
set lowResPixelRatio(pixelRatio: number | undefined) {
this.m_lowResPass.pixelRatio = pixelRatio;
this.m_lowResPass.enabled = pixelRatio !== undefined;
}
/**
* Set the level of sampling while the user interacts.
*
* @param samplingLevel - The sampling level.
*/
set dynamicMsaaSamplingLevel(samplingLevel: MSAASampling) {
this.m_dynamicMsaaSamplingLevel = samplingLevel;
}
/**
* Return the sampling level defined during continuous rendering.
*/
get dynamicMsaaSamplingLevel(): MSAASampling {
return this.m_dynamicMsaaSamplingLevel;
}
/**
* Enable or disable the MSAA. If disabled, `MapRenderingManager` will use the renderer provided
* in the {@link MapRenderingManager.render} method to render the scene.
*
* @param value - If `true`, MSAA is enabled, disabled otherwise.
*/
set msaaEnabled(value: boolean) {
this.m_msaaPass.enabled = value;
}
/**
* Return whether the MSAA is enabled.
*/
get msaaEnabled(): boolean {
return this.m_msaaPass.enabled;
}
/**
* Set the sampling level for rendering static frames.
*
* @param samplingLevel - The sampling level.
*/
set staticMsaaSamplingLevel(samplingLevel: MSAASampling) {
this.m_staticMsaaSamplingLevel = samplingLevel;
}
/**
* Return the sampling level defined for rendering static frames.
*/
get staticMsaaSamplingLevel(): MSAASampling {
return this.m_staticMsaaSamplingLevel;
}
}