Skip to content

Commit 8836abc

Browse files
committed
Create LUT1DEffect
Closes #576
1 parent 01b49e8 commit 8836abc

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎src/effects/LUT1DEffect.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { FloatType, HalfFloatType, Texture, Uniform } from "three";
2+
import { Effect } from "./Effect.js";
3+
4+
import fragmentShader from "./shaders/lut-1d.frag";
5+
6+
/**
7+
* A 1D LUT effect.
8+
*
9+
* @category Effects
10+
*/
11+
12+
export class LUT1DEffect extends Effect {
13+
14+
/**
15+
* Constructs a new LUT effect.
16+
*/
17+
18+
constructor(lut: Texture) {
19+
20+
super("LUT1DEffect");
21+
22+
this.fragmentShader = fragmentShader;
23+
this.input.uniforms.set("lut", new Uniform(null));
24+
this.lut = lut;
25+
26+
}
27+
28+
/**
29+
* Indicates whether the LUT uses high precision.
30+
*/
31+
32+
private get lutPrecisionHigh(): boolean {
33+
34+
return this.input.defines.has("LUT_PRECISION_HIGH");
35+
36+
}
37+
38+
private set lutPrecisionHigh(value: boolean) {
39+
40+
if(this.lutPrecisionHigh !== value) {
41+
42+
if(value) {
43+
44+
this.input.defines.set("LUT_PRECISION_HIGH", true);
45+
46+
} else {
47+
48+
this.input.defines.delete("LUT_PRECISION_HIGH");
49+
50+
}
51+
52+
this.setChanged();
53+
54+
}
55+
56+
}
57+
58+
/**
59+
* The LUT.
60+
*/
61+
62+
get lut(): Texture {
63+
64+
return this.input.uniforms.get("lut")!.value as Texture;
65+
66+
}
67+
68+
set lut(value: Texture) {
69+
70+
this.input.uniforms.get("lut")!.value = value;
71+
this.lutPrecisionHigh = (value.type === FloatType || value.type === HalfFloatType);
72+
73+
}
74+
75+
}

‎src/effects/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./Effect.js";
66
export * from "./FXAAEffect.js";
77
export * from "./HalftoneEffect.js";
88
export * from "./LensDistortionEffect.js";
9+
export * from "./LUT1DEffect.js";
910
export * from "./ScanlineEffect.js";
1011
export * from "./SMAAEffect.js";
1112
export * from "./TextureEffect.js";

‎src/effects/shaders/lut-1d.frag‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifdef LUT_PRECISION_HIGH
2+
3+
#ifdef GL_FRAGMENT_PRECISION_HIGH
4+
5+
uniform highp sampler2D lut;
6+
7+
#else
8+
9+
uniform mediump sampler2D lut;
10+
11+
#endif
12+
13+
#else
14+
15+
uniform lowp sampler2D lut;
16+
17+
#endif
18+
19+
vec4 mainImage(const in vec4 inputColor, const in vec2 uv, const in GData gData) {
20+
21+
return vec4(
22+
texture(lut, vec2(inputColor.r, 0.5)).r,
23+
texture(lut, vec2(inputColor.g, 0.5)).r,
24+
texture(lut, vec2(inputColor.b, 0.5)).r,
25+
inputColor.a
26+
);
27+
28+
}

‎test/effects/LUT1DEffect.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import test from "ava";
2+
import { Texture } from "three";
3+
import { LUT1DEffect } from "postprocessing";
4+
5+
test("can be created and destroyed", t => {
6+
7+
const object = new LUT1DEffect(new Texture());
8+
object.dispose();
9+
10+
t.pass();
11+
12+
});

0 commit comments

Comments
 (0)