forked from donmccurdy/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPass.js
More file actions
392 lines (279 loc) · 7.77 KB
/
Pass.js
File metadata and controls
392 lines (279 loc) · 7.77 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
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
import { BasicDepthPacking, BufferAttribute, BufferGeometry, Camera, Mesh, Scene } from "three";
const dummyCamera = new Camera();
let geometry = null;
/**
* Returns a shared fullscreen triangle.
*
* The screen size is 2x2 units (NDC). A triangle needs to be 4x4 units to fill the screen.
*
* @private
* @return {BufferGeometry} The fullscreen geometry.
*/
function getFullscreenTriangle() {
if(geometry === null) {
const vertices = new Float32Array([-1, -1, 0, 3, -1, 0, -1, 3, 0]);
const uvs = new Float32Array([0, 0, 2, 0, 0, 2]);
geometry = new BufferGeometry();
// Added for backward compatibility (setAttribute was added in three r110).
if(geometry.setAttribute !== undefined) {
geometry.setAttribute("position", new BufferAttribute(vertices, 3));
geometry.setAttribute("uv", new BufferAttribute(uvs, 2));
} else {
geometry.addAttribute("position", new BufferAttribute(vertices, 3));
geometry.addAttribute("uv", new BufferAttribute(uvs, 2));
}
}
return geometry;
}
/**
* An abstract pass.
*
* Fullscreen passes use a shared fullscreen triangle:
* https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in-full-screen-passes/
*
* @implements {Initializable}
* @implements {Resizable}
* @implements {Disposable}
*/
export class Pass {
/**
* Constructs a new pass.
*
* @param {String} [name] - The name of this pass. Does not have to be unique.
* @param {Scene} [scene] - The scene to render. The default scene contains a single mesh that fills the screen.
* @param {Camera} [camera] - A camera. Fullscreen effect passes don't require a camera.
*/
constructor(name = "Pass", scene = new Scene(), camera = dummyCamera) {
/**
* The name of this pass.
*
* @type {String}
*/
this.name = name;
/**
* The renderer.
*
* @deprecated
* @type {WebGLRenderer}
* @protected
*/
this.renderer = null;
/**
* The scene to render.
*
* @type {Scene}
* @protected
*/
this.scene = scene;
/**
* The camera.
*
* @type {Camera}
* @protected
*/
this.camera = camera;
/**
* A mesh that fills the screen.
*
* @type {Mesh}
* @private
*/
this.screen = null;
/**
* Indicates whether this pass should render to texture.
*
* @type {Boolean}
* @private
*/
this.rtt = true;
/**
* Only relevant for subclassing.
*
* Indicates whether the {@link EffectComposer} should swap the frame buffers after this pass has finished
* rendering. Set this to `false` if this pass doesn't render to the output buffer or the screen. Otherwise, the
* contents of the input buffer will be lost.
*
* @type {Boolean}
*/
this.needsSwap = true;
/**
* Only relevant for subclassing.
*
* Indicates whether the {@link EffectComposer} should prepare a depth texture for this pass.
* Set this to `true` if this pass relies on depth information from a preceding {@link RenderPass}.
*
* @type {Boolean}
*/
this.needsDepthTexture = false;
/**
* Indicates whether this pass is enabled.
*
* @type {Boolean}
*/
this.enabled = true;
}
/**
* Indicates whether this pass should render to screen.
*
* @type {Boolean}
*/
get renderToScreen() {
return !this.rtt;
}
/**
* Sets the render to screen flag.
*
* If this flag is changed, the fullscreen material will be updated as well.
*
* @type {Boolean}
*/
set renderToScreen(value) {
if(this.rtt === value) {
const material = this.getFullscreenMaterial();
if(material !== null) {
material.needsUpdate = true;
}
this.rtt = !value;
}
}
/**
* Sets the renderer
*
* @deprecated
* @param {WebGLRenderer} renderer - The renderer.
*/
setRenderer(renderer) {
this.renderer = renderer;
}
/**
* Indicates whether this pass is enabled.
*
* @deprecated Use enabled instead.
* @return {Boolean} Whether this pass is enabled.
*/
isEnabled() {
return this.enabled;
}
/**
* Enables or disables this pass.
*
* @deprecated Use enabled instead.
* @param {Boolean} value - Whether the pass should be enabled.
*/
setEnabled(value) {
this.enabled = value;
}
/**
* The fullscreen material.
*
* @type {Material}
*/
get fullscreenMaterial() {
return (this.screen !== null) ? this.screen.material : null;
}
set fullscreenMaterial(value) {
let screen = this.screen;
if(screen !== null) {
screen.material = value;
} else {
screen = new Mesh(getFullscreenTriangle(), value);
screen.frustumCulled = false;
if(this.scene === null) {
this.scene = new Scene();
}
this.scene.add(screen);
this.screen = screen;
}
}
/**
* Returns the current fullscreen material.
*
* @deprecated Use fullscreenMaterial instead.
* @return {Material} The current fullscreen material, or null if there is none.
*/
getFullscreenMaterial() {
return this.fullscreenMaterial;
}
/**
* Sets the fullscreen material.
*
* @deprecated Use fullscreenMaterial instead.
* @protected
* @param {Material} value - A fullscreen material.
*/
setFullscreenMaterial(value) {
this.fullscreenMaterial = value;
}
/**
* Returns the current depth texture.
*
* @return {Texture} The current depth texture, or null if there is none.
*/
getDepthTexture() {
return null;
}
/**
* Sets the depth texture.
*
* This method will be called automatically by the {@link EffectComposer}.
* You may override this method if your pass relies on the depth information of a preceding {@link RenderPass}.
*
* @param {Texture} depthTexture - A depth texture.
* @param {DepthPackingStrategy} [depthPacking=BasicDepthPacking] - The depth packing.
*/
setDepthTexture(depthTexture, depthPacking = BasicDepthPacking) {}
/**
* Renders this pass.
*
* This is an abstract method that must be overridden.
*
* @abstract
* @throws {Error} An error is thrown if the method is not overridden.
* @param {WebGLRenderer} renderer - The renderer.
* @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
* @param {WebGLRenderTarget} outputBuffer - A frame buffer that serves as the output render target unless this pass renders to screen.
* @param {Number} [deltaTime] - The time between the last frame and the current one in seconds.
* @param {Boolean} [stencilTest] - Indicates whether a stencil mask is active.
*/
render(renderer, inputBuffer, outputBuffer, deltaTime, stencilTest) {
throw new Error("Render method not implemented!");
}
/**
* Sets the size.
*
* You may override this method if you want to be informed about the size of the backbuffer/canvas.
* This method is called before {@link initialize} and every time the size of the {@link EffectComposer} changes.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {}
/**
* Performs initialization tasks.
*
* This method is called when this pass is added to an {@link EffectComposer}.
*
* @param {WebGLRenderer} renderer - The renderer.
* @param {Boolean} alpha - Whether the renderer uses the alpha channel or not.
* @param {Number} frameBufferType - The type of the main frame buffers.
*/
initialize(renderer, alpha, frameBufferType) {}
/**
* Performs a shallow search for disposable properties and deletes them.
*
* The {@link EffectComposer} calls this method when it is being destroyed. You can use it independently to free
* memory when you're certain that you don't need this pass anymore.
*/
dispose() {
for(const key of Object.keys(this)) {
const property = this[key];
if(property !== null && typeof property.dispose === "function") {
if(property instanceof Scene || property === this.renderer) {
continue;
}
/** @ignore */
this[key].dispose();
}
}
}
}