Skip to content

Commit dbf8947

Browse files
committed
Add optional channel weights setting
1 parent bca7b41 commit dbf8947

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎src/materials/CopyMaterial.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AlwaysDepth, Texture, Uniform } from "three";
1+
import { AlwaysDepth, Texture, Uniform, Vector4 } from "three";
22
import { FullscreenMaterial } from "./FullscreenMaterial.js";
33

44
import fragmentShader from "./shaders/copy.frag";
@@ -27,7 +27,8 @@ export class CopyMaterial extends FullscreenMaterial {
2727
COLOR_WRITE: true
2828
},
2929
uniforms: {
30-
depthBuffer: new Uniform(null)
30+
depthBuffer: new Uniform(null),
31+
channelWeights: new Uniform(null)
3132
}
3233
});
3334

@@ -132,4 +133,33 @@ export class CopyMaterial extends FullscreenMaterial {
132133

133134
}
134135

136+
/**
137+
* Color channel weights that modulate texels from the input buffer.
138+
*
139+
* Set to `null` to disable.
140+
*/
141+
142+
get channelWeights(): Vector4 | null {
143+
144+
return this.uniforms.channelWeights.value as Vector4;
145+
146+
}
147+
148+
set channelWeights(value: Vector4 | null) {
149+
150+
if(value !== null) {
151+
152+
this.defines.USE_WEIGHTS = true;
153+
this.uniforms.channelWeights.value = value;
154+
155+
} else {
156+
157+
delete this.defines.USE_WEIGHTS;
158+
159+
}
160+
161+
this.needsUpdate = true;
162+
163+
}
164+
135165
}

‎src/materials/shaders/copy.frag‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@
1414

1515
#endif
1616

17+
#ifdef USE_WEIGHTS
18+
19+
uniform vec4 channelWeights;
20+
21+
#endif
22+
1723
in vec2 vUv;
1824

1925
void main() {
2026

2127
#ifdef COLOR_WRITE
2228

23-
out_Color = texture(inputBuffer, vUv);
29+
vec4 texel = texture2D(inputBuffer, vUv);
30+
31+
#ifdef USE_WEIGHTS
32+
33+
texel *= channelWeights;
34+
35+
#endif
36+
37+
out_Color = texel;
2438

2539
#ifdef COLOR_SPACE_CONVERSION
2640

0 commit comments

Comments
 (0)