forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSAOEffect.js
More file actions
622 lines (442 loc) · 14.5 KB
/
SSAOEffect.js
File metadata and controls
622 lines (442 loc) · 14.5 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import { BasicDepthPacking, Color, RepeatWrapping, RGBAFormat, Uniform, WebGLRenderTarget } from "three";
import { Resolution } from "../core/index.js";
import { BlendFunction, EffectAttribute } from "../enums/index.js";
import { NoiseTexture } from "../textures/index.js";
import { SSAOMaterial } from "../materials/index.js";
import { DepthDownsamplingPass, ShaderPass } from "../passes/index.js";
import { Effect } from "./Effect.js";
import fragmentShader from "./glsl/ssao.frag";
const NOISE_TEXTURE_SIZE = 64;
/**
* A Screen Space Ambient Occlusion effect.
*
* Based on "Scalable Ambient Obscurance" by Morgan McGuire et al.
* and "Depth-aware upsampling experiments" by Eleni Maria Stea:
* https://research.nvidia.com/publication/scalable-ambient-obscurance
* https://eleni.mutantstargoat.com/hikiko/on-depth-aware-upsampling
*
* The view position calculation is based on a shader by Norbert Nopper:
* https://github.com/McNopper/OpenGL/blob/master/Example28/shader/ssao.frag.glsl
*/
export class SSAOEffect extends Effect {
/**
* Constructs a new SSAO effect.
*
* @todo Move normalBuffer to options.
* @param {Camera} [camera] - The main camera.
* @param {Texture} [normalBuffer] - A texture that contains the scene normals.
* @param {Object} [options] - The options.
* @param {BlendFunction} [options.blendFunction=BlendFunction.MULTIPLY] - The blend function of this effect.
* @param {Boolean} [options.distanceScaling=true] - Deprecated.
* @param {Boolean} [options.depthAwareUpsampling=true] - Enables or disables depth-aware upsampling. Has no effect if WebGL 2 is not supported.
* @param {Texture} [options.normalDepthBuffer=null] - Deprecated.
* @param {Number} [options.samples=9] - The amount of samples per pixel. Should not be a multiple of the ring count.
* @param {Number} [options.rings=7] - The amount of spiral turns in the occlusion sampling pattern. Should be a prime number.
* @param {Number} [options.worldDistanceThreshold] - The world distance threshold at which the occlusion effect starts to fade out.
* @param {Number} [options.worldDistanceFalloff] - The world distance falloff. Influences the smoothness of the occlusion cutoff.
* @param {Number} [options.worldProximityThreshold] - The world proximity threshold at which the occlusion starts to fade out.
* @param {Number} [options.worldProximityFalloff] - The world proximity falloff. Influences the smoothness of the proximity cutoff.
* @param {Number} [options.distanceThreshold=0.97] - Deprecated.
* @param {Number} [options.distanceFalloff=0.03] - Deprecated.
* @param {Number} [options.rangeThreshold=0.0005] - Deprecated.
* @param {Number} [options.rangeFalloff=0.001] - Deprecated.
* @param {Number} [options.minRadiusScale=0.1] - The minimum radius scale.
* @param {Number} [options.luminanceInfluence=0.7] - Determines how much the luminance of the scene influences the ambient occlusion.
* @param {Number} [options.radius=0.1825] - The occlusion sampling radius, expressed as a scale relative to the resolution. Range [1e-6, 1.0].
* @param {Number} [options.intensity=1.0] - The intensity of the ambient occlusion.
* @param {Number} [options.bias=0.025] - An occlusion bias. Eliminates artifacts caused by depth discontinuities.
* @param {Number} [options.fade=0.01] - Influences the smoothness of the shadows. A lower value results in higher contrast.
* @param {Color} [options.color=null] - The color of the ambient occlusion.
* @param {Number} [options.resolutionScale=1.0] - The resolution scale.
* @param {Number} [options.resolutionX=Resolution.AUTO_SIZE] - The horizontal resolution.
* @param {Number} [options.resolutionY=Resolution.AUTO_SIZE] - The vertical resolution.
* @param {Number} [options.width=Resolution.AUTO_SIZE] - Deprecated. Use resolutionX instead.
* @param {Number} [options.height=Resolution.AUTO_SIZE] - Deprecated. Use resolutionY instead.
*/
constructor(camera, normalBuffer, {
blendFunction = BlendFunction.MULTIPLY,
samples = 9,
rings = 7,
normalDepthBuffer = null,
depthAwareUpsampling = true,
worldDistanceThreshold,
worldDistanceFalloff,
worldProximityThreshold,
worldProximityFalloff,
distanceThreshold = 0.97,
distanceFalloff = 0.03,
rangeThreshold = 0.0005,
rangeFalloff = 0.001,
minRadiusScale = 0.1,
luminanceInfluence = 0.7,
radius = 0.1825,
intensity = 1.0,
bias = 0.025,
fade = 0.01,
color = null,
resolutionScale = 1.0,
width = Resolution.AUTO_SIZE,
height = Resolution.AUTO_SIZE,
resolutionX = width,
resolutionY = height
} = {}) {
super("SSAOEffect", fragmentShader, {
blendFunction,
attributes: EffectAttribute.DEPTH,
defines: new Map([
["THRESHOLD", "0.997"]
]),
uniforms: new Map([
["aoBuffer", new Uniform(null)],
["normalDepthBuffer", new Uniform(normalDepthBuffer)],
["luminanceInfluence", new Uniform(luminanceInfluence)],
["color", new Uniform(null)],
["intensity", new Uniform(intensity)],
["scale", new Uniform(0.0)] // Unused.
])
});
/**
* A render target.
*
* @type {WebGLRenderTarget}
* @private
*/
this.renderTarget = new WebGLRenderTarget(1, 1, { depthBuffer: false });
this.renderTarget.texture.name = "AO.Target";
this.uniforms.get("aoBuffer").value = this.renderTarget.texture;
/**
* The resolution.
*
* @type {Resolution}
*/
const resolution = this.resolution = new Resolution(this, resolutionX, resolutionY, resolutionScale);
resolution.addEventListener("change", (e) => this.setSize(resolution.baseWidth, resolution.baseHeight));
/**
* The main camera.
*
* @type {Camera}
* @private
*/
this.camera = camera;
/**
* A depth downsampling pass.
*
* @type {DepthDownsamplingPass}
* @private
*/
this.depthDownsamplingPass = new DepthDownsamplingPass({ normalBuffer, resolutionScale });
this.depthDownsamplingPass.enabled = (normalDepthBuffer === null);
/**
* An SSAO pass.
*
* @type {ShaderPass}
* @private
*/
this.ssaoPass = new ShaderPass(new SSAOMaterial(camera));
const noiseTexture = new NoiseTexture(NOISE_TEXTURE_SIZE, NOISE_TEXTURE_SIZE, RGBAFormat);
noiseTexture.wrapS = noiseTexture.wrapT = RepeatWrapping;
const ssaoMaterial = this.ssaoMaterial;
ssaoMaterial.normalBuffer = normalBuffer;
ssaoMaterial.noiseTexture = noiseTexture;
ssaoMaterial.minRadiusScale = minRadiusScale;
ssaoMaterial.samples = samples;
ssaoMaterial.radius = radius;
ssaoMaterial.rings = rings;
ssaoMaterial.fade = fade;
ssaoMaterial.bias = bias;
ssaoMaterial.distanceThreshold = distanceThreshold;
ssaoMaterial.distanceFalloff = distanceFalloff;
ssaoMaterial.proximityThreshold = rangeThreshold;
ssaoMaterial.proximityFalloff = rangeFalloff;
if(worldDistanceThreshold !== undefined) {
ssaoMaterial.worldDistanceThreshold = worldDistanceThreshold;
}
if(worldDistanceFalloff !== undefined) {
ssaoMaterial.worldDistanceFalloff = worldDistanceFalloff;
}
if(worldProximityThreshold !== undefined) {
ssaoMaterial.worldProximityThreshold = worldProximityThreshold;
}
if(worldProximityFalloff !== undefined) {
ssaoMaterial.worldProximityFalloff = worldProximityFalloff;
}
if(normalDepthBuffer !== null) {
this.ssaoMaterial.normalDepthBuffer = normalDepthBuffer;
this.defines.set("NORMAL_DEPTH", "1");
}
this.depthAwareUpsampling = depthAwareUpsampling;
this.color = color;
}
set mainCamera(value) {
this.camera = value;
this.ssaoMaterial.copyCameraSettings(value);
}
/**
* Sets the normal buffer.
*
* @type {Texture}
*/
get normalBuffer() {
return this.ssaoMaterial.normalBuffer;
}
set normalBuffer(value) {
this.ssaoMaterial.normalBuffer = value;
this.depthDownsamplingPass.fullscreenMaterial.normalBuffer = value;
}
/**
* Returns the resolution settings.
*
* @deprecated Use resolution instead.
* @return {Resolution} The resolution.
*/
getResolution() {
return this.resolution;
}
/**
* The SSAO material.
*
* @type {SSAOMaterial}
*/
get ssaoMaterial() {
return this.ssaoPass.fullscreenMaterial;
}
/**
* Returns the SSAO material.
*
* @deprecated Use ssaoMaterial instead.
* @return {SSAOMaterial} The material.
*/
getSSAOMaterial() {
return this.ssaoMaterial;
}
/**
* The amount of occlusion samples per pixel.
*
* @type {Number}
* @deprecated Use ssaoMaterial.samples instead.
*/
get samples() {
return this.ssaoMaterial.samples;
}
set samples(value) {
this.ssaoMaterial.samples = value;
}
/**
* The amount of spiral turns in the occlusion sampling pattern.
*
* @type {Number}
* @deprecated Use ssaoMaterial.rings instead.
*/
get rings() {
return this.ssaoMaterial.rings;
}
set rings(value) {
this.ssaoMaterial.rings = value;
}
/**
* The occlusion sampling radius.
*
* @type {Number}
* @deprecated Use ssaoMaterial.radius instead.
*/
get radius() {
return this.ssaoMaterial.radius;
}
set radius(value) {
this.ssaoMaterial.radius = value;
}
/**
* Indicates whether depth-aware upsampling is enabled.
*
* @type {Boolean}
*/
get depthAwareUpsampling() {
return this.defines.has("DEPTH_AWARE_UPSAMPLING");
}
set depthAwareUpsampling(value) {
if(this.depthAwareUpsampling !== value) {
if(value) {
this.defines.set("DEPTH_AWARE_UPSAMPLING", "1");
} else {
this.defines.delete("DEPTH_AWARE_UPSAMPLING");
}
this.setChanged();
}
}
/**
* Indicates whether depth-aware upsampling is enabled.
*
* @deprecated Use depthAwareUpsampling instead.
* @return {Boolean} Whether depth-aware upsampling is enabled.
*/
isDepthAwareUpsamplingEnabled() {
return this.depthAwareUpsampling;
}
/**
* Enables or disables depth-aware upsampling.
*
* @deprecated Use depthAwareUpsampling instead.
* @param {Boolean} value - Whether depth-aware upsampling should be enabled.
*/
setDepthAwareUpsamplingEnabled(value) {
this.depthAwareUpsampling = value;
}
/**
* Indicates whether distance-based radius scaling is enabled.
*
* @type {Boolean}
* @deprecated
*/
get distanceScaling() { return true; }
set distanceScaling(value) {}
/**
* The color of the ambient occlusion. Set to `null` to disable.
*
* @type {Color}
*/
get color() {
return this.uniforms.get("color").value;
}
set color(value) {
const uniforms = this.uniforms;
const defines = this.defines;
if(value !== null) {
if(defines.has("COLORIZE")) {
uniforms.get("color").value.set(value);
} else {
defines.set("COLORIZE", "1");
uniforms.get("color").value = new Color(value);
this.setChanged();
}
} else if(defines.has("COLORIZE")) {
defines.delete("COLORIZE");
uniforms.get("color").value = null;
this.setChanged();
}
}
/**
* The luminance influence factor. Range: [0.0, 1.0].
*
* @type {Boolean}
*/
get luminanceInfluence() {
return this.uniforms.get("luminanceInfluence").value;
}
set luminanceInfluence(value) {
this.uniforms.get("luminanceInfluence").value = value;
}
/**
* The intensity.
*
* @type {Number}
*/
get intensity() {
return this.uniforms.get("intensity").value;
}
set intensity(value) {
this.uniforms.get("intensity").value = value;
}
/**
* Returns the color of the ambient occlusion.
*
* @deprecated Use color instead.
* @return {Color} The color.
*/
getColor() {
return this.color;
}
/**
* Sets the color of the ambient occlusion. Set to `null` to disable colorization.
*
* @deprecated Use color instead.
* @param {Color} value - The color.
*/
setColor(value) {
this.color = value;
}
/**
* Sets the occlusion distance cutoff.
*
* @deprecated Use ssaoMaterial instead.
* @param {Number} threshold - The distance threshold. Range [0.0, 1.0].
* @param {Number} falloff - The falloff. Range [0.0, 1.0].
*/
setDistanceCutoff(threshold, falloff) {
this.ssaoMaterial.distanceThreshold = threshold;
this.ssaoMaterial.distanceFalloff = falloff;
}
/**
* Sets the occlusion proximity cutoff.
*
* @deprecated Use ssaoMaterial instead.
* @param {Number} threshold - The proximity threshold. Range [0.0, 1.0].
* @param {Number} falloff - The falloff. Range [0.0, 1.0].
*/
setProximityCutoff(threshold, falloff) {
this.ssaoMaterial.proximityThreshold = threshold;
this.ssaoMaterial.proximityFalloff = falloff;
}
/**
* Sets the depth texture.
*
* @param {Texture} depthTexture - A depth texture.
* @param {DepthPackingStrategies} [depthPacking=BasicDepthPacking] - The depth packing.
*/
setDepthTexture(depthTexture, depthPacking = BasicDepthPacking) {
this.depthDownsamplingPass.setDepthTexture(depthTexture, depthPacking);
this.ssaoMaterial.depthBuffer = depthTexture;
this.ssaoMaterial.depthPacking = depthPacking;
}
/**
* Updates this effect.
*
* @param {WebGLRenderer} renderer - The renderer.
* @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
* @param {Number} [deltaTime] - The time between the last frame and the current one in seconds.
*/
update(renderer, inputBuffer, deltaTime) {
const renderTarget = this.renderTarget;
if(this.depthDownsamplingPass.enabled) {
this.depthDownsamplingPass.render(renderer);
}
this.ssaoPass.render(renderer, null, renderTarget);
}
/**
* Sets the size.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
const resolution = this.resolution;
resolution.setBaseSize(width, height);
const w = resolution.width, h = resolution.height;
this.ssaoMaterial.copyCameraSettings(this.camera);
this.ssaoMaterial.setSize(w, h);
this.renderTarget.setSize(w, h);
this.depthDownsamplingPass.resolution.scale = resolution.scale;
this.depthDownsamplingPass.setSize(width, height);
}
/**
* Performs initialization tasks.
*
* @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) {
try {
let normalDepthBuffer = this.uniforms.get("normalDepthBuffer").value;
if(normalDepthBuffer === null) {
this.depthDownsamplingPass.initialize(renderer, alpha, frameBufferType);
normalDepthBuffer = this.depthDownsamplingPass.texture;
this.uniforms.get("normalDepthBuffer").value = normalDepthBuffer;
this.ssaoMaterial.normalDepthBuffer = normalDepthBuffer;
this.defines.set("NORMAL_DEPTH", "1");
}
} catch(e) {
// Not supported.
this.depthDownsamplingPass.enabled = false;
}
}
}