forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeDetectionMaterial.js
More file actions
469 lines (331 loc) · 9.05 KB
/
EdgeDetectionMaterial.js
File metadata and controls
469 lines (331 loc) · 9.05 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
import { BasicDepthPacking, NoBlending, REVISION, ShaderMaterial, Uniform, Vector2 } from "three";
import { EdgeDetectionMode } from "../enums/index.js";
import fragmentShader from "./glsl/edge-detection.frag";
import vertexShader from "./glsl/edge-detection.vert";
/**
* An edge detection material. Mainly used for SMAA.
*
* @implements {Resizable}
*/
export class EdgeDetectionMaterial extends ShaderMaterial {
/**
* Constructs a new edge detection material.
*
* TODO Remove parameters.
* @param {Vector2} [texelSize] - The screen texel size.
* @param {EdgeDetectionMode} [mode=EdgeDetectionMode.COLOR] - The edge detection mode.
*/
constructor(texelSize = new Vector2(), mode = EdgeDetectionMode.COLOR) {
super({
name: "EdgeDetectionMaterial",
defines: {
THREE_REVISION: REVISION.replace(/\D+/g, ""),
LOCAL_CONTRAST_ADAPTATION_FACTOR: "2.0",
EDGE_THRESHOLD: "0.1",
DEPTH_THRESHOLD: "0.01",
PREDICATION_MODE: "0",
PREDICATION_THRESHOLD: "0.01",
PREDICATION_SCALE: "2.0",
PREDICATION_STRENGTH: "1.0",
DEPTH_PACKING: "0"
},
uniforms: {
inputBuffer: new Uniform(null),
depthBuffer: new Uniform(null),
predicationBuffer: new Uniform(null),
texelSize: new Uniform(texelSize)
},
blending: NoBlending,
toneMapped: false,
depthWrite: false,
depthTest: false,
fragmentShader,
vertexShader
});
this.edgeDetectionMode = mode;
}
/**
* The depth buffer.
*
* @type {Texture}
*/
set depthBuffer(value) {
this.uniforms.depthBuffer.value = value;
}
/**
* The depth packing strategy.
*
* @type {DepthPackingStrategies}
*/
set depthPacking(value) {
this.defines.DEPTH_PACKING = value.toFixed(0);
this.needsUpdate = true;
}
/**
* Sets the depth buffer.
*
* @deprecated Use depthBuffer and depthPacking instead.
* @param {Texture} buffer - The depth texture.
* @param {DepthPackingStrategies} [depthPacking=BasicDepthPacking] - The depth packing strategy.
*/
setDepthBuffer(buffer, depthPacking = BasicDepthPacking) {
this.depthBuffer = buffer;
this.depthPacking = depthPacking;
}
/**
* The edge detection mode.
*
* @type {EdgeDetectionMode}
*/
get edgeDetectionMode() {
return Number(this.defines.EDGE_DETECTION_MODE);
}
set edgeDetectionMode(value) {
this.defines.EDGE_DETECTION_MODE = value.toFixed(0);
this.needsUpdate = true;
}
/**
* Returns the edge detection mode.
*
* @deprecated Use edgeDetectionMode instead.
* @return {EdgeDetectionMode} The mode.
*/
getEdgeDetectionMode() {
return this.edgeDetectionMode;
}
/**
* Sets the edge detection mode.
*
* @deprecated Use edgeDetectionMode instead.
* @param {EdgeDetectionMode} value - The edge detection mode.
*/
setEdgeDetectionMode(value) {
this.edgeDetectionMode = value;
}
/**
* The local contrast adaptation factor. Has no effect if the edge detection mode is set to DEPTH. Default is 2.0.
*
* If a neighbor edge has _factor_ times bigger contrast than the current edge, the edge will be discarded.
*
* This allows to eliminate spurious crossing edges and is based on the fact that if there is too much contrast in a
* direction, the perceptual contrast in the other neighbors will be hidden.
*
* @type {Number}
*/
get localContrastAdaptationFactor() {
return Number(this.defines.LOCAL_CONTRAST_ADAPTATION_FACTOR);
}
set localContrastAdaptationFactor(value) {
this.defines.LOCAL_CONTRAST_ADAPTATION_FACTOR = value.toFixed("6");
this.needsUpdate = true;
}
/**
* Returns the local contrast adaptation factor.
*
* @deprecated Use localContrastAdaptationFactor instead.
* @return {Number} The factor.
*/
getLocalContrastAdaptationFactor() {
return this.localContrastAdaptationFactor;
}
/**
* Sets the local contrast adaptation factor. Has no effect if the edge detection mode is set to DEPTH.
*
* @deprecated Use localContrastAdaptationFactor instead.
* @param {Number} value - The local contrast adaptation factor. Default is 2.0.
*/
setLocalContrastAdaptationFactor(value) {
this.localContrastAdaptationFactor = value;
}
/**
* The edge detection threshold. Range: [0.0, 0.5].
*
* A lower value results in more edges being detected at the expense of performance.
*
* For luma- and chroma-based edge detection, 0.1 is a reasonable value and allows to catch most visible edges. 0.05
* is a rather overkill value that allows to catch 'em all. Darker scenes may require an even lower threshold.
*
* If depth-based edge detection is used, the threshold will depend on the scene depth.
*
* @type {Number}
*/
get edgeDetectionThreshold() {
return Number(this.defines.EDGE_THRESHOLD);
}
set edgeDetectionThreshold(value) {
this.defines.EDGE_THRESHOLD = value.toFixed("6");
this.defines.DEPTH_THRESHOLD = (value * 0.1).toFixed("6");
this.needsUpdate = true;
}
/**
* Returns the edge detection threshold.
*
* @deprecated Use edgeDetectionThreshold instead.
* @return {Number} The threshold.
*/
getEdgeDetectionThreshold() {
return this.edgeDetectionThreshold;
}
/**
* Sets the edge detection threshold.
*
* @deprecated Use edgeDetectionThreshold instead.
* @param {Number} value - The edge detection threshold. Range: [0.0, 0.5].
*/
setEdgeDetectionThreshold(value) {
this.edgeDetectionThreshold = value;
}
/**
* The predication mode.
*
* Predicated thresholding allows to better preserve texture details and to improve edge detection using an additional
* buffer such as a light accumulation or depth buffer.
*
* @type {PredicationMode}
*/
get predicationMode() {
return Number(this.defines.PREDICATION_MODE);
}
set predicationMode(value) {
this.defines.PREDICATION_MODE = value.toFixed(0);
this.needsUpdate = true;
}
/**
* Returns the predication mode.
*
* @deprecated Use predicationMode instead.
* @return {PredicationMode} The mode.
*/
getPredicationMode() {
return this.predicationMode;
}
/**
* Sets the predication mode.
*
* @deprecated Use predicationMode instead.
* @param {PredicationMode} value - The predication mode.
*/
setPredicationMode(value) {
this.predicationMode = value;
}
/**
* The predication buffer.
*
* @type {Texture}
*/
set predicationBuffer(value) {
this.uniforms.predicationBuffer.value = value;
}
/**
* Sets a custom predication buffer.
*
* @deprecated Use predicationBuffer instead.
* @param {Texture} value - The predication buffer.
*/
setPredicationBuffer(value) {
this.uniforms.predicationBuffer.value = value;
}
/**
* The predication threshold.
*
* @type {Number}
*/
get predicationThreshold() {
return Number(this.defines.PREDICATION_THRESHOLD);
}
set predicationThreshold(value) {
this.defines.PREDICATION_THRESHOLD = value.toFixed("6");
this.needsUpdate = true;
}
/**
* Returns the predication threshold.
*
* @deprecated Use predicationThreshold instead.
* @return {Number} The threshold.
*/
getPredicationThreshold() {
return this.predicationThreshold;
}
/**
* Sets the predication threshold.
*
* @deprecated Use predicationThreshold instead.
* @param {Number} value - The threshold.
*/
setPredicationThreshold(value) {
this.predicationThreshold = value;
}
/**
* The predication scale. Range: [1.0, 5.0].
*
* Determines how much the edge detection threshold should be scaled when using predication.
*
* @type {Boolean|Texture|Number}
*/
get predicationScale() {
return Number(this.defines.PREDICATION_SCALE);
}
set predicationScale(value) {
this.defines.PREDICATION_SCALE = value.toFixed("6");
this.needsUpdate = true;
}
/**
* Returns the predication scale.
*
* @deprecated Use predicationScale instead.
* @return {Number} The scale.
*/
getPredicationScale() {
return this.predicationScale;
}
/**
* Sets the predication scale.
*
* @deprecated Use predicationScale instead.
* @param {Number} value - The scale. Range: [1.0, 5.0].
*/
setPredicationScale(value) {
this.predicationScale = value;
}
/**
* The predication strength. Range: [0.0, 1.0].
*
* Determines how much the edge detection threshold should be decreased locally when using predication.
*
* @type {Number}
*/
get predicationStrength() {
return Number(this.defines.PREDICATION_STRENGTH);
}
set predicationStrength(value) {
this.defines.PREDICATION_STRENGTH = value.toFixed("6");
this.needsUpdate = true;
}
/**
* Returns the predication strength.
*
* @deprecated Use predicationStrength instead.
* @return {Number} The strength.
*/
getPredicationStrength() {
return this.predicationStrength;
}
/**
* Sets the predication strength.
*
* @deprecated Use predicationStrength instead.
* @param {Number} value - The strength. Range: [0.0, 1.0].
*/
setPredicationStrength(value) {
this.predicationStrength = value;
}
/**
* Sets the size of this object.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
this.uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
}
}