-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathcompositingnode.js
More file actions
82 lines (70 loc) · 2.87 KB
/
compositingnode.js
File metadata and controls
82 lines (70 loc) · 2.87 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
//Matthew Shotton, R&D User Experience,© BBC 2015
import ProcessingNode from "./processingnode";
import { createElementTexture } from "../utils.js";
const TYPE = "CompositingNode";
class CompositingNode extends ProcessingNode {
/**
* Initialise an instance of a Compositing Node. You should not instantiate this directly, but use VideoContest.createCompositingNode().
*/
constructor(gl, renderGraph, definition) {
let placeholderTexture = createElementTexture(gl);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
1,
1,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
new Uint8Array([0, 0, 0, 0])
);
super(gl, renderGraph, definition, definition.inputs, false);
this._placeholderTexture = placeholderTexture;
this._displayName = TYPE;
}
_render() {
let gl = this._gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
this._texture,
0
);
gl.clearColor(0, 0, 0, 0); // green;
gl.clear(gl.COLOR_BUFFER_BIT);
// Set the initial blend function to 'proiritize' the SRC so that the background
// clearColor doesn't bleed / blend into output
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ZERO);
this.inputs.forEach(node => {
if (node === undefined) return;
super._render();
//map the input textures input the node
var texture = node._texture;
for (let mapping of this._shaderInputsTextureUnitMapping) {
gl.activeTexture(mapping.textureUnit);
gl.uniform1i(mapping.location, mapping.textureUnitIndex);
gl.bindTexture(gl.TEXTURE_2D, texture);
}
gl.drawArrays(gl.TRIANGLES, 0, 6);
// Update the blend function to allow for 'default' blend of transparency
// of the next inputs of the node
gl.blendFuncSeparate(
gl.SRC_ALPHA,
gl.ONE_MINUS_SRC_ALPHA,
gl.ONE,
gl.ONE_MINUS_SRC_ALPHA
);
// We blend RGB and Alpha separately because as you stack layers in a CompositionNode, we don’t want to interpolate alpha
// (i.e. we don’t want a mid-point or a weighted average of the alpha channels)
// Transparent things in real life don’t blend. The colors blend, but the opacity gets monotonically more opaque as things pile up
// (i.e. stack two transparent gels and the result is more opaque than either one individually)
});
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
}
export { TYPE as COMPOSITINGTYPE };
export default CompositingNode;