forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLUT3DEffect.js
More file actions
373 lines (252 loc) · 7.66 KB
/
LUT3DEffect.js
File metadata and controls
373 lines (252 loc) · 7.66 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
import {
Data3DTexture,
FloatType,
HalfFloatType,
LinearFilter,
NearestFilter,
sRGBEncoding,
Uniform,
Vector3
} from "three";
import { BlendFunction } from "../enums/index.js";
import { LookupTexture } from "../textures/index.js";
import { encodingToColorSpace } from "../utils/index.js";
import { Effect } from "./Effect.js";
import fragmentShader from "./glsl/lut-3d.frag";
/**
* A LUT effect.
*
* The tetrahedral interpolation algorithm was inspired by an implementation from OpenColorIO which is licensed under
* the BSD 3-Clause License.
*
* The manual trilinear interpolation algorithm is based on an implementation by Garret Johnson which is licensed under
* the MIT License.
*
* References:
* https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-24-using-lookup-tables-accelerate-color
* https://www.nvidia.com/content/GTC/posters/2010/V01-Real-Time-Color-Space-Conversion-for-High-Resolution-Video.pdf
* https://github.com/AcademySoftwareFoundation/OpenColorIO/blob/master/src/OpenColorIO/ops/lut3d/
* https://github.com/gkjohnson/threejs-sandbox/tree/master/3d-lut
*/
export class LUT3DEffect extends Effect {
/**
* Constructs a new color grading effect.
*
* @param {Texture} lut - The lookup texture.
* @param {Object} [options] - The options.
* @param {BlendFunction} [options.blendFunction=BlendFunction.SRC] - The blend function of this effect.
* @param {Boolean} [options.tetrahedralInterpolation=false] - Enables or disables tetrahedral interpolation.
* @param {TextureEncoding} [options.inputEncoding=sRGBEncoding] - Deprecated.
* @param {ColorSpace} [options.inputColorSpace=SRGBColorSpace] - The input color space.
*/
constructor(lut, {
blendFunction = BlendFunction.SRC,
tetrahedralInterpolation = false,
inputEncoding = sRGBEncoding,
inputColorSpace
} = {}) {
super("LUT3DEffect", fragmentShader, {
blendFunction,
uniforms: new Map([
["lut", new Uniform(null)],
["scale", new Uniform(new Vector3())],
["offset", new Uniform(new Vector3())],
["domainMin", new Uniform(null)],
["domainMax", new Uniform(null)]
])
});
this.tetrahedralInterpolation = tetrahedralInterpolation;
this.inputColorSpace = inputColorSpace || encodingToColorSpace.get(inputEncoding);
this.lut = lut;
}
/**
* The input encoding. Default is `sRGBEncoding`.
*
* Set this to `LinearEncoding` if your LUT expects linear color input.
*
* @deprecated Use inputColorSpace instead.
* @type {TextureEncoding}
*/
get inputEncoding() {
return this.inputColorSpace;
}
set inputEncoding(value) {
this.inputColorSpace = value;
}
/**
* Returns the input encoding.
*
* @deprecated Use inputColorSpace instead.
* @return {TextureEncoding} The encoding.
*/
getInputEncoding() {
return this.inputColorSpace;
}
/**
* Sets the input encoding.
*
* @deprecated Use inputColorSpace instead.
* @param {TextureEncoding} value - The encoding.
*/
setInputEncoding(value) {
this.inputColorSpace = value;
}
/**
* Returns the output encoding.
*
* @deprecated Use outputColorSpace instead.
* @return {TextureEncoding} The encoding.
*/
getOutputEncoding() {
return this.outputColorSpace;
}
/**
* The LUT.
*
* @type {Texture}
*/
get lut() {
return this.uniforms.get("lut").value;
}
set lut(value) {
const defines = this.defines;
const uniforms = this.uniforms;
if(this.lut !== value) {
uniforms.get("lut").value = value;
if(value !== null) {
const image = value.image;
// Remember settings that are backed by defines.
const tetrahedralInterpolation = this.tetrahedralInterpolation;
defines.clear();
defines.set("LUT_SIZE", Math.min(image.width, image.height).toFixed(16));
defines.set("LUT_TEXEL_WIDTH", (1.0 / image.width).toFixed(16));
defines.set("LUT_TEXEL_HEIGHT", (1.0 / image.height).toFixed(16));
uniforms.get("domainMin").value = null;
uniforms.get("domainMax").value = null;
if(value.type === FloatType || value.type === HalfFloatType) {
defines.set("LUT_PRECISION_HIGH", "1");
}
if(image.width > image.height) {
defines.set("LUT_STRIP_HORIZONTAL", "1");
} else if(value instanceof Data3DTexture) {
defines.set("LUT_3D", "1");
}
if(value instanceof LookupTexture) {
const min = value.domainMin;
const max = value.domainMax;
if(min.x !== 0 || min.y !== 0 || min.z !== 0 || max.x !== 1 || max.y !== 1 || max.z !== 1) {
defines.set("CUSTOM_INPUT_DOMAIN", "1");
uniforms.get("domainMin").value = min.clone();
uniforms.get("domainMax").value = max.clone();
}
}
// Refresh settings that affect the LUT.
this.tetrahedralInterpolation = tetrahedralInterpolation;
}
}
}
/**
* Returns the current LUT.
*
* @deprecated Use lut instead.
* @return {Texture} The LUT.
*/
getLUT() {
return this.lut;
}
/**
* Sets the LUT.
*
* @deprecated Use lut instead.
* @param {Texture} value - The LUT.
*/
setLUT(value) {
this.lut = value;
}
/**
* Updates the scale and offset for the LUT sampling coordinates.
*
* @private
*/
updateScaleOffset() {
const lut = this.lut;
if(lut !== null) {
const size = Math.min(lut.image.width, lut.image.height);
const scale = this.uniforms.get("scale").value;
const offset = this.uniforms.get("offset").value;
if(this.tetrahedralInterpolation && lut instanceof Data3DTexture) {
if(this.defines.has("CUSTOM_INPUT_DOMAIN")) {
const domainScale = lut.domainMax.clone().sub(lut.domainMin);
scale.setScalar(size - 1).divide(domainScale);
offset.copy(lut.domainMin).negate().multiply(scale);
} else {
scale.setScalar(size - 1);
offset.setScalar(0);
}
} else {
if(this.defines.has("CUSTOM_INPUT_DOMAIN")) {
const domainScale = lut.domainMax.clone().sub(lut.domainMin).multiplyScalar(size);
scale.setScalar(size - 1).divide(domainScale);
offset.copy(lut.domainMin).negate().multiply(scale).addScalar(1.0 / (2.0 * size));
} else {
scale.setScalar((size - 1) / size);
offset.setScalar(1.0 / (2.0 * size));
}
}
}
}
/**
* Configures parameters for tetrahedral interpolation.
*
* @private
*/
configureTetrahedralInterpolation() {
const lut = this.lut;
if(lut !== null) {
lut.minFilter = LinearFilter;
lut.magFilter = LinearFilter;
if(this.tetrahedralInterpolation) {
if(lut instanceof Data3DTexture) {
// Interpolate samples manually.
lut.minFilter = NearestFilter;
lut.magFilter = NearestFilter;
} else {
console.warn("Tetrahedral interpolation requires a 3D texture");
}
}
// TODO Added for compatibility with r138. Remove later.
if(lut.source === undefined) {
lut.needsUpdate = true;
}
}
}
/**
* Indicates whether tetrahedral interpolation is enabled. Requires a 3D LUT, disabled by default.
*
* Tetrahedral interpolation produces highly accurate results but is slower than hardware interpolation.
*
* @type {Boolean}
*/
get tetrahedralInterpolation() {
return this.defines.has("TETRAHEDRAL_INTERPOLATION");
}
set tetrahedralInterpolation(value) {
if(value) {
this.defines.set("TETRAHEDRAL_INTERPOLATION", "1");
} else {
this.defines.delete("TETRAHEDRAL_INTERPOLATION");
}
this.configureTetrahedralInterpolation();
this.updateScaleOffset();
this.setChanged();
}
/**
* Enables or disables tetrahedral interpolation.
*
* @deprecated Use tetrahedralInterpolation instead.
* @param {Boolean} value - Whether tetrahedral interpolation should be enabled.
*/
setTetrahedralInterpolationEnabled(value) {
this.tetrahedralInterpolation = value;
}
}