forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutlineEffect.js
More file actions
823 lines (578 loc) · 15.7 KB
/
OutlineEffect.js
File metadata and controls
823 lines (578 loc) · 15.7 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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import { Color, RepeatWrapping, Uniform, UnsignedByteType, WebGLRenderTarget } from "three";
import { Resolution, Selection } from "../core/index.js";
import { BlendFunction, KernelSize } from "../enums/index.js";
import { DepthComparisonMaterial, OutlineMaterial } from "../materials/index.js";
import { KawaseBlurPass, ClearPass, DepthPass, RenderPass, ShaderPass } from "../passes/index.js";
import { Effect } from "./Effect.js";
import fragmentShader from "./glsl/outline.frag";
import vertexShader from "./glsl/outline.vert";
/**
* An outline effect.
*/
export class OutlineEffect extends Effect {
/**
* Constructs a new outline effect.
*
* @param {Scene} scene - The main scene.
* @param {Camera} camera - The main camera.
* @param {Object} [options] - The options.
* @param {BlendFunction} [options.blendFunction=BlendFunction.SCREEN] - The blend function. Use `BlendFunction.ALPHA` for dark outlines.
* @param {Texture} [options.patternTexture=null] - A pattern texture.
* @param {Number} [options.patternScale=1.0] - The pattern scale.
* @param {Number} [options.edgeStrength=1.0] - The edge strength.
* @param {Number} [options.pulseSpeed=0.0] - The pulse speed. A value of zero disables the pulse effect.
* @param {Number} [options.visibleEdgeColor=0xffffff] - The color of visible edges.
* @param {Number} [options.hiddenEdgeColor=0x22090a] - The color of hidden edges.
* @param {KernelSize} [options.kernelSize=KernelSize.VERY_SMALL] - The blur kernel size.
* @param {Boolean} [options.blur=false] - Whether the outline should be blurred.
* @param {Boolean} [options.xRay=true] - Whether occluded parts of selected objects should be visible.
* @param {Number} [options.multisampling=0] - The number of samples used for multisample antialiasing. Requires WebGL 2.
* @param {Number} [options.resolutionScale=0.5] - 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(scene, camera, {
blendFunction = BlendFunction.SCREEN,
patternTexture = null,
patternScale = 1.0,
edgeStrength = 1.0,
pulseSpeed = 0.0,
visibleEdgeColor = 0xffffff,
hiddenEdgeColor = 0x22090a,
kernelSize = KernelSize.VERY_SMALL,
blur = false,
xRay = true,
multisampling = 0,
resolutionScale = 0.5,
width = Resolution.AUTO_SIZE,
height = Resolution.AUTO_SIZE,
resolutionX = width,
resolutionY = height
} = {}) {
super("OutlineEffect", fragmentShader, {
uniforms: new Map([
["maskTexture", new Uniform(null)],
["edgeTexture", new Uniform(null)],
["edgeStrength", new Uniform(edgeStrength)],
["visibleEdgeColor", new Uniform(new Color(visibleEdgeColor))],
["hiddenEdgeColor", new Uniform(new Color(hiddenEdgeColor))],
["pulse", new Uniform(1.0)],
["patternScale", new Uniform(patternScale)],
["patternTexture", new Uniform(null)]
])
});
// Handle alpha blending.
this.blendMode.addEventListener("change", (event) => {
if(this.blendMode.blendFunction === BlendFunction.ALPHA) {
this.defines.set("ALPHA", "1");
} else {
this.defines.delete("ALPHA");
}
this.setChanged();
});
this.blendMode.blendFunction = blendFunction;
this.patternTexture = patternTexture;
this.xRay = xRay;
/**
* The main scene.
*
* @type {Scene}
* @private
*/
this.scene = scene;
/**
* The main camera.
*
* @type {Camera}
* @private
*/
this.camera = camera;
/**
* A render target for the outline mask.
*
* @type {WebGLRenderTarget}
* @private
*/
this.renderTargetMask = new WebGLRenderTarget(1, 1);
this.renderTargetMask.samples = multisampling;
this.renderTargetMask.texture.name = "Outline.Mask";
this.uniforms.get("maskTexture").value = this.renderTargetMask.texture;
/**
* A render target for the edge detection.
*
* @type {WebGLRenderTarget}
* @private
*/
this.renderTargetOutline = new WebGLRenderTarget(1, 1, { depthBuffer: false });
this.renderTargetOutline.texture.name = "Outline.Edges";
this.uniforms.get("edgeTexture").value = this.renderTargetOutline.texture;
/**
* A clear pass.
*
* @type {ClearPass}
* @private
*/
this.clearPass = new ClearPass();
this.clearPass.overrideClearColor = new Color(0x000000);
this.clearPass.overrideClearAlpha = 1;
/**
* A depth pass.
*
* @type {DepthPass}
* @private
*/
this.depthPass = new DepthPass(scene, camera);
/**
* A depth comparison mask pass.
*
* @type {RenderPass}
* @private
*/
this.maskPass = new RenderPass(scene, camera, new DepthComparisonMaterial(this.depthPass.texture, camera));
const clearPass = this.maskPass.clearPass;
clearPass.overrideClearColor = new Color(0xffffff);
clearPass.overrideClearAlpha = 1;
/**
* A blur pass.
*
* @type {KawaseBlurPass}
*/
this.blurPass = new KawaseBlurPass({ resolutionScale, resolutionX, resolutionY, kernelSize });
this.blurPass.enabled = blur;
const resolution = this.blurPass.resolution;
resolution.addEventListener("change", (e) => this.setSize(resolution.baseWidth, resolution.baseHeight));
/**
* An outline detection pass.
*
* @type {ShaderPass}
* @private
*/
this.outlinePass = new ShaderPass(new OutlineMaterial());
const outlineMaterial = this.outlinePass.fullscreenMaterial;
outlineMaterial.inputBuffer = this.renderTargetMask.texture;
/**
* The current animation time.
*
* @type {Number}
* @private
*/
this.time = 0;
/**
* Indicates whether the outlines should be updated.
*
* @type {Boolean}
* @private
*/
this.forceUpdate = true;
/**
* A selection of objects that will be outlined.
*
* The default layer of this selection is 10.
*
* @type {Selection}
*/
this.selection = new Selection();
this.selection.layer = 10;
/**
* The pulse speed. Set to 0 to disable.
*
* @type {Number}
*/
this.pulseSpeed = pulseSpeed;
}
set mainScene(value) {
this.scene = value;
this.depthPass.mainScene = value;
this.maskPass.mainScene = value;
}
set mainCamera(value) {
this.camera = value;
this.depthPass.mainCamera = value;
this.maskPass.mainCamera = value;
this.maskPass.overrideMaterial.copyCameraSettings(value);
}
/**
* The resolution of this effect.
*
* @type {Resolution}
*/
get resolution() {
return this.blurPass.resolution;
}
/**
* Returns the resolution.
*
* @return {Resizer} The resolution.
*/
getResolution() {
return this.blurPass.getResolution();
}
/**
* The amount of MSAA samples.
*
* Requires WebGL 2. Set to zero to disable multisampling.
*
* @experimental Requires three >= r138.
* @type {Number}
*/
get multisampling() {
return this.renderTargetMask.samples;
}
set multisampling(value) {
this.renderTargetMask.samples = value;
this.renderTargetMask.dispose();
}
/**
* The pattern scale.
*
* @type {Number}
*/
get patternScale() {
return this.uniforms.get("patternScale").value;
}
set patternScale(value) {
this.uniforms.get("patternScale").value = value;
}
/**
* The edge strength.
*
* @type {Number}
*/
get edgeStrength() {
return this.uniforms.get("edgeStrength").value;
}
set edgeStrength(value) {
this.uniforms.get("edgeStrength").value = value;
}
/**
* The visible edge color.
*
* @type {Color}
*/
get visibleEdgeColor() {
return this.uniforms.get("visibleEdgeColor").value;
}
set visibleEdgeColor(value) {
this.uniforms.get("visibleEdgeColor").value = value;
}
/**
* The hidden edge color.
*
* @type {Color}
*/
get hiddenEdgeColor() {
return this.uniforms.get("hiddenEdgeColor").value;
}
set hiddenEdgeColor(value) {
this.uniforms.get("hiddenEdgeColor").value = value;
}
/**
* Returns the blur pass.
*
* @deprecated Use blurPass instead.
* @return {KawaseBlurPass} The blur pass.
*/
getBlurPass() {
return this.blurPass;
}
/**
* Returns the selection.
*
* @deprecated Use selection instead.
* @return {Selection} The selection.
*/
getSelection() {
return this.selection;
}
/**
* Returns the pulse speed.
*
* @deprecated Use pulseSpeed instead.
* @return {Number} The speed.
*/
getPulseSpeed() {
return this.pulseSpeed;
}
/**
* Sets the pulse speed. Set to zero to disable.
*
* @deprecated Use pulseSpeed instead.
* @param {Number} value - The speed.
*/
setPulseSpeed(value) {
this.pulseSpeed = value;
}
/**
* The current width of the internal render targets.
*
* @type {Number}
* @deprecated Use resolution.width instead.
*/
get width() {
return this.resolution.width;
}
set width(value) {
this.resolution.preferredWidth = value;
}
/**
* The current height of the internal render targets.
*
* @type {Number}
* @deprecated Use resolution.height instead.
*/
get height() {
return this.resolution.height;
}
set height(value) {
this.resolution.preferredHeight = value;
}
/**
* The selection layer.
*
* @type {Number}
* @deprecated Use selection.layer instead.
*/
get selectionLayer() {
return this.selection.layer;
}
set selectionLayer(value) {
this.selection.layer = value;
}
/**
* Indicates whether dithering is enabled.
*
* @type {Boolean}
* @deprecated
*/
get dithering() {
return this.blurPass.dithering;
}
set dithering(value) {
this.blurPass.dithering = value;
}
/**
* The blur kernel size.
*
* @type {KernelSize}
* @deprecated Use blurPass.kernelSize instead.
*/
get kernelSize() {
return this.blurPass.kernelSize;
}
set kernelSize(value) {
this.blurPass.kernelSize = value;
}
/**
* Indicates whether the outlines should be blurred.
*
* @type {Boolean}
* @deprecated Use blurPass.enabled instead.
*/
get blur() {
return this.blurPass.enabled;
}
set blur(value) {
this.blurPass.enabled = value;
}
/**
* Indicates whether X-ray mode is enabled.
*
* @type {Boolean}
*/
get xRay() {
return this.defines.has("X_RAY");
}
set xRay(value) {
if(this.xRay !== value) {
if(value) {
this.defines.set("X_RAY", "1");
} else {
this.defines.delete("X_RAY");
}
this.setChanged();
}
}
/**
* Indicates whether X-ray mode is enabled.
*
* @deprecated Use xRay instead.
* @return {Boolean} Whether X-ray mode is enabled.
*/
isXRayEnabled() {
return this.xRay;
}
/**
* Enables or disables X-ray outlines.
*
* @deprecated Use xRay instead.
* @param {Boolean} value - Whether X-ray should be enabled.
*/
setXRayEnabled(value) {
this.xRay = value;
}
/**
* The pattern texture. Set to `null` to disable.
*
* @type {Texture}
*/
get patternTexture() {
return this.uniforms.get("patternTexture").value;
}
set patternTexture(value) {
if(value !== null) {
value.wrapS = value.wrapT = RepeatWrapping;
this.defines.set("USE_PATTERN", "1");
this.setVertexShader(vertexShader);
} else {
this.defines.delete("USE_PATTERN");
this.setVertexShader(null);
}
this.uniforms.get("patternTexture").value = value;
this.setChanged();
}
/**
* Sets the pattern texture.
*
* @deprecated Use patternTexture instead.
* @param {Texture} value - The new texture.
*/
setPatternTexture(value) {
this.patternTexture = value;
}
/**
* Returns the current resolution scale.
*
* @return {Number} The resolution scale.
* @deprecated Use resolution instead.
*/
getResolutionScale() {
return this.resolution.scale;
}
/**
* Sets the resolution scale.
*
* @param {Number} scale - The new resolution scale.
* @deprecated Use resolution instead.
*/
setResolutionScale(scale) {
this.resolution.scale = scale;
}
/**
* Clears the current selection and selects a list of objects.
*
* @param {Object3D[]} objects - The objects that should be outlined. This array will be copied.
* @return {OutlinePass} This pass.
* @deprecated Use selection.set() instead.
*/
setSelection(objects) {
this.selection.set(objects);
return this;
}
/**
* Clears the list of selected objects.
*
* @return {OutlinePass} This pass.
* @deprecated Use selection.clear() instead.
*/
clearSelection() {
this.selection.clear();
return this;
}
/**
* Selects an object.
*
* @param {Object3D} object - The object that should be outlined.
* @return {OutlinePass} This pass.
* @deprecated Use selection.add() instead.
*/
selectObject(object) {
this.selection.add(object);
return this;
}
/**
* Deselects an object.
*
* @param {Object3D} object - The object that should no longer be outlined.
* @return {OutlinePass} This pass.
* @deprecated Use selection.delete() instead.
*/
deselectObject(object) {
this.selection.delete(object);
return this;
}
/**
* 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 scene = this.scene;
const camera = this.camera;
const selection = this.selection;
const uniforms = this.uniforms;
const pulse = uniforms.get("pulse");
const background = scene.background;
const mask = camera.layers.mask;
if(this.forceUpdate || selection.size > 0) {
scene.background = null;
pulse.value = 1;
if(this.pulseSpeed > 0) {
pulse.value = Math.cos(this.time * this.pulseSpeed * 10.0) * 0.375 + 0.625;
}
this.time += deltaTime;
// Render a custom depth texture and ignore selected objects.
selection.setVisible(false);
this.depthPass.render(renderer);
selection.setVisible(true);
// Compare the depth of the selected objects with the depth texture.
camera.layers.set(selection.layer);
this.maskPass.render(renderer, this.renderTargetMask);
// Restore the camera layer mask and the scene background.
camera.layers.mask = mask;
scene.background = background;
// Detect the outline.
this.outlinePass.render(renderer, null, this.renderTargetOutline);
if(this.blurPass.enabled) {
this.blurPass.render(renderer, this.renderTargetOutline, this.renderTargetOutline);
}
}
this.forceUpdate = selection.size > 0;
}
/**
* Updates the size of internal render targets.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
this.blurPass.setSize(width, height);
this.renderTargetMask.setSize(width, height);
const resolution = this.resolution;
resolution.setBaseSize(width, height);
const w = resolution.width, h = resolution.height;
this.depthPass.setSize(w, h);
this.renderTargetOutline.setSize(w, h);
this.outlinePass.fullscreenMaterial.setSize(w, h);
}
/**
* 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) {
// No need for high precision: the blur pass operates on a mask texture.
this.blurPass.initialize(renderer, alpha, UnsignedByteType);
if(frameBufferType !== undefined) {
// These passes ignore the buffer type.
this.depthPass.initialize(renderer, alpha, frameBufferType);
this.maskPass.initialize(renderer, alpha, frameBufferType);
this.outlinePass.initialize(renderer, alpha, frameBufferType);
}
}
}