-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
post-effect-queue.js
391 lines (323 loc) · 12.4 KB
/
post-effect-queue.js
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
import { ADDRESS_CLAMP_TO_EDGE, FILTER_NEAREST, PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F, PIXELFORMAT_RGBA8 } from '../../../platform/graphics/constants.js';
import { DebugGraphics } from '../../../platform/graphics/debug-graphics.js';
import { RenderTarget } from '../../../platform/graphics/render-target.js';
import { Texture } from '../../../platform/graphics/texture.js';
import { LAYERID_DEPTH } from '../../../scene/constants.js';
class PostEffectEntry {
constructor(effect, inputTarget) {
this.effect = effect;
this.inputTarget = inputTarget;
this.outputTarget = null;
this.name = effect.constructor.name;
}
}
/**
* Used to manage multiple post effects for a camera.
*
* @category Graphics
*/
class PostEffectQueue {
/**
* Create a new PostEffectQueue instance.
*
* @param {import('../../app-base.js').AppBase} app - The application.
* @param {import('./component.js').CameraComponent} camera - The camera component.
*/
constructor(app, camera) {
this.app = app;
this.camera = camera;
/**
* Render target where the postprocessed image needs to be rendered to. Defaults to null
* which is main framebuffer.
*
* @type {RenderTarget}
* @ignore
*/
this.destinationRenderTarget = null;
/**
* All of the post effects in the queue.
*
* @type {PostEffectEntry[]}
* @ignore
*/
this.effects = [];
/**
* If the queue is enabled it will render all of its effects, otherwise it will not render
* anything.
*
* @type {boolean}
* @ignore
*/
this.enabled = false;
// legacy
this.depthTarget = null;
camera.on('set:rect', this.onCameraRectChanged, this);
}
/**
* Allocate a color buffer texture.
*
* @param {number} format - The format of the color buffer.
* @param {string} name - The name of the color buffer.
* @returns {Texture} The color buffer texture.
* @private
*/
_allocateColorBuffer(format, name) {
const rect = this.camera.rect;
const renderTarget = this.destinationRenderTarget;
const device = this.app.graphicsDevice;
const width = Math.floor(rect.z * (renderTarget?.width ?? device.width));
const height = Math.floor(rect.w * (renderTarget?.height ?? device.height));
const colorBuffer = new Texture(device, {
name: name,
format: format,
width: width,
height: height,
mipmaps: false,
minFilter: FILTER_NEAREST,
magFilter: FILTER_NEAREST,
addressU: ADDRESS_CLAMP_TO_EDGE,
addressV: ADDRESS_CLAMP_TO_EDGE
});
return colorBuffer;
}
/**
* Creates a render target with the dimensions of the canvas, with an optional depth buffer.
*
* @param {boolean} useDepth - Set to true to create a render target with a depth buffer.
* @param {boolean} hdr - Use HDR render target format.
* @returns {RenderTarget} The render target.
* @private
*/
_createOffscreenTarget(useDepth, hdr) {
const device = this.app.graphicsDevice;
const format = hdr && device.getRenderableHdrFormat([PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F], true) || PIXELFORMAT_RGBA8;
const name = this.camera.entity.name + '-posteffect-' + this.effects.length;
const colorBuffer = this._allocateColorBuffer(format, name);
return new RenderTarget({
colorBuffer: colorBuffer,
depth: useDepth,
stencil: useDepth && this.app.graphicsDevice.supportsStencil,
samples: useDepth ? device.samples : 1
});
}
_resizeOffscreenTarget(rt) {
const format = rt.colorBuffer.format;
const name = rt.colorBuffer.name;
rt.destroyFrameBuffers();
rt.destroyTextureBuffers();
rt._colorBuffer = this._allocateColorBuffer(format, name);
rt._colorBuffers = [rt._colorBuffer];
}
_destroyOffscreenTarget(rt) {
rt.destroyTextureBuffers();
rt.destroy();
}
/**
* Adds a post effect to the queue. If the queue is disabled adding a post effect will
* automatically enable the queue.
*
* @param {import('../../../scene/graphics/post-effect.js').PostEffect} effect - The post
* effect to add to the queue.
*/
addEffect(effect) {
// first rendering of the scene requires depth buffer
const effects = this.effects;
const isFirstEffect = effects.length === 0;
const inputTarget = this._createOffscreenTarget(isFirstEffect, effect.hdr);
const newEntry = new PostEffectEntry(effect, inputTarget);
effects.push(newEntry);
this._sourceTarget = newEntry.inputTarget;
// connect the effect with the previous effect if one exists
if (effects.length > 1) {
effects[effects.length - 2].outputTarget = newEntry.inputTarget;
}
// Request depthmap if needed
this._newPostEffect = effect;
if (effect.needsDepthBuffer) {
this._requestDepthMap();
}
this.enable();
this._newPostEffect = undefined;
}
/**
* Removes a post effect from the queue. If the queue becomes empty it will be disabled
* automatically.
*
* @param {import('../../../scene/graphics/post-effect.js').PostEffect} effect - The post effect
* to remove.
*/
removeEffect(effect) {
// find index of effect
let index = -1;
for (let i = 0, len = this.effects.length; i < len; i++) {
if (this.effects[i].effect === effect) {
index = i;
break;
}
}
if (index >= 0) {
if (index > 0) {
// connect the previous effect with the effect after the one we're about to remove
this.effects[index - 1].outputTarget = (index + 1) < this.effects.length ?
this.effects[index + 1].inputTarget :
null;
} else {
if (this.effects.length > 1) {
// if we removed the first effect then make sure that
// the input render target of the effect that will now become the first one
// has a depth buffer
if (!this.effects[1].inputTarget._depth) {
this._destroyOffscreenTarget(this.effects[1].inputTarget);
this.effects[1].inputTarget = this._createOffscreenTarget(true, this.effects[1].hdr);
this._sourceTarget = this.effects[1].inputTarget;
}
this.camera.renderTarget = this.effects[1].inputTarget;
}
}
// release memory for removed effect
this._destroyOffscreenTarget(this.effects[index].inputTarget);
this.effects.splice(index, 1);
}
if (this.enabled) {
if (effect.needsDepthBuffer) {
this._releaseDepthMap();
}
}
if (this.effects.length === 0) {
this.disable();
}
}
_requestDepthMaps() {
for (let i = 0, len = this.effects.length; i < len; i++) {
const effect = this.effects[i].effect;
if (this._newPostEffect === effect)
continue;
if (effect.needsDepthBuffer) {
this._requestDepthMap();
}
}
}
_releaseDepthMaps() {
for (let i = 0, len = this.effects.length; i < len; i++) {
const effect = this.effects[i].effect;
if (effect.needsDepthBuffer) {
this._releaseDepthMap();
}
}
}
_requestDepthMap() {
const depthLayer = this.app.scene.layers.getLayerById(LAYERID_DEPTH);
if (depthLayer) {
depthLayer.incrementCounter();
this.camera.requestSceneDepthMap(true);
}
}
_releaseDepthMap() {
const depthLayer = this.app.scene.layers.getLayerById(LAYERID_DEPTH);
if (depthLayer) {
depthLayer.decrementCounter();
this.camera.requestSceneDepthMap(false);
}
}
/**
* Removes all the effects from the queue and disables it.
*/
destroy() {
// release memory for all effects
for (let i = 0, len = this.effects.length; i < len; i++) {
this.effects[i].inputTarget.destroy();
}
this.effects.length = 0;
this.disable();
}
/**
* Enables the queue and all of its effects. If there are no effects then the queue will not be
* enabled.
*/
enable() {
if (!this.enabled && this.effects.length) {
this.enabled = true;
this._requestDepthMaps();
this.app.graphicsDevice.on('resizecanvas', this._onCanvasResized, this);
// original camera's render target is where the final output needs to go
this.destinationRenderTarget = this.camera.renderTarget;
// camera renders to the first effect's render target
this.camera.renderTarget = this.effects[0].inputTarget;
// callback when postprocessing takes place
this.camera.onPostprocessing = () => {
if (this.enabled) {
let rect = null;
const len = this.effects.length;
if (len) {
for (let i = 0; i < len; i++) {
const fx = this.effects[i];
let destTarget = fx.outputTarget;
// last effect
if (i === len - 1) {
rect = this.camera.rect;
// if camera originally rendered to a render target, render last effect to it
if (this.destinationRenderTarget) {
destTarget = this.destinationRenderTarget;
}
}
DebugGraphics.pushGpuMarker(this.app.graphicsDevice, fx.name);
fx.effect.render(fx.inputTarget, destTarget, rect);
DebugGraphics.popGpuMarker(this.app.graphicsDevice);
}
}
}
};
}
}
/**
* Disables the queue and all of its effects.
*/
disable() {
if (this.enabled) {
this.enabled = false;
this.app.graphicsDevice.off('resizecanvas', this._onCanvasResized, this);
this._releaseDepthMaps();
this._destroyOffscreenTarget(this._sourceTarget);
this.camera.renderTarget = null;
this.camera.onPostprocessing = null;
}
}
/**
* Handler called when the application's canvas element is resized.
*
* @param {number} width - The new width of the canvas.
* @param {number} height - The new height of the canvas.
* @private
*/
_onCanvasResized(width, height) {
const rect = this.camera.rect;
const renderTarget = this.destinationRenderTarget;
width = renderTarget?.width ?? width;
height = renderTarget?.height ?? height;
this.camera.camera.aspectRatio = (width * rect.z) / (height * rect.w);
this.resizeRenderTargets();
}
resizeRenderTargets() {
const device = this.app.graphicsDevice;
const renderTarget = this.destinationRenderTarget;
const width = renderTarget?.width ?? device.width;
const height = renderTarget?.height ?? device.height;
const rect = this.camera.rect;
const desiredWidth = Math.floor(rect.z * width);
const desiredHeight = Math.floor(rect.w * height);
const effects = this.effects;
for (let i = 0, len = effects.length; i < len; i++) {
const fx = effects[i];
if (fx.inputTarget.width !== desiredWidth ||
fx.inputTarget.height !== desiredHeight) {
this._resizeOffscreenTarget(fx.inputTarget);
}
}
}
onCameraRectChanged(name, oldValue, newValue) {
if (this.enabled) {
this.resizeRenderTargets();
}
}
}
export { PostEffectQueue };