- 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
import VertexArrayBuffer from "../buffer/vertex.js";
import GLShader from "../glshader.js";
/**
* @classdesc
* A base Compositor object.
*/
export default class Compositor {
/**
* @param {WebGLRenderer} renderer - the current WebGL renderer session
* @param {object} settings - additional settings to initialize this compositors
* @param {object[]} settings.attribute - an array of attributes definition
* @param {string} settings.attribute.name - name of the attribute in the vertex shader
* @param {number} settings.attribute.size - number of components per vertex attribute. Must be 1, 2, 3, or 4.
* @param {GLenum} settings.attribute.type - data type of each component in the array
* @param {boolean} settings.attribute.normalized - whether integer data values should be normalized into a certain range when being cast to a float
* @param {number} settings.attribute.offset - offset in bytes of the first component in the vertex attribute array
* @param {object} settings.shader - an array of attributes definition
* @param {string} settings.shader.vertex - a string containing the GLSL source code to set
* @param {string} settings.shader.fragment - a string containing the GLSL source code to set
*/
constructor (renderer, settings) {
this.init(renderer, settings);
}
/**
* Initialize the compositor
* @ignore
*/
init (renderer, settings) {
// the associated renderer
this.renderer = renderer;
// WebGL context
this.gl = renderer.gl;
// Global fill color
this.color = renderer.currentColor;
// Global transformation matrix
this.viewMatrix = renderer.currentTransform;
/**
* the default shader created by this compositor
* @type {GLShader}
*/
this.defaultShader = undefined;
/**
* the shader currently used by this compositor
* @type {GLShader}
*/
this.currentShader = undefined;
/**
* primitive type to render (gl.POINTS, gl.LINE_STRIP, gl.LINE_LOOP, gl.LINES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.TRIANGLES)
* @type {number}
* @default gl.TRIANGLES
*/
this.mode = this.gl.TRIANGLES;
/**
* an array of vertex attribute properties
* @see WebGLCompositor.addAttribute
* @type {Array.<Object>}
*/
this.attributes = [];
/**
* the size of a single vertex in bytes
* (will automatically be calculated as attributes definitions are added)
* @see WebGLCompositor.addAttribute
* @type {number}
*/
this.vertexByteSize = 0;
/**
* the size of a single vertex in floats
* (will automatically be calculated as attributes definitions are added)
* @see WebGLCompositor.addAttribute
* @type {number}
*/
this.vertexSize = 0;
/**
* the vertex data buffer used by this compositor
* @type {VertexArrayBuffer}
*/
this.vertexData = null;
// parse given attibrutes
if (typeof settings !== "undefined" && Array.isArray(settings.attributes)) {
settings.attributes.forEach((attr) => {
this.addAttribute(attr.name, attr.size, attr.type, attr.normalized, attr.offset);
this.vertexData = new VertexArrayBuffer(this.vertexSize, 6);
});
} else {
throw new Error("attributes definition missing");
}
// parse and instantiate the default shader
if (typeof settings !== "undefined" && typeof settings.shader !== "undefined") {
this.defaultShader = new GLShader(this.gl, settings.shader.vertex, settings.shader.fragment);
} else {
throw new Error("shader definition missing");
}
}
/**
* Reset compositor internal state
* @ignore
*/
reset() {
// WebGL context
this.gl = this.renderer.gl;
// clear the vertex data buffer
this.vertexData.clear();
}
/**
* called by the WebGL renderer when a compositor become the current one
*/
bind() {
if (this.renderer.currentProgram !== this.defaultShader.program) {
this.useShader(this.defaultShader);
}
}
/**
* Select the shader to use for compositing
* @see GLShader
* @param {GLShader} shader - a reference to a GLShader instance
*/
useShader(shader) {
if (this.renderer.currentProgram !== shader.program) {
this.flush();
shader.bind();
shader.setUniform("uProjectionMatrix", this.renderer.projectionMatrix);
shader.setVertexAttributes(this.gl, this.attributes, this.vertexByteSize);
this.currentShader = shader;
this.renderer.currentProgram = this.currentShader.program;
}
}
/**
* add vertex attribute property definition to the compositor
* @param {string} name - name of the attribute in the vertex shader
* @param {number} size - number of components per vertex attribute. Must be 1, 2, 3, or 4.
* @param {GLenum} type - data type of each component in the array
* @param {boolean} normalized - whether integer data values should be normalized into a certain range when being cast to a float
* @param {number} offset - offset in bytes of the first component in the vertex attribute array
*/
addAttribute(name, size, type, normalized, offset) {
this.attributes.push({
name: name,
size: size,
type: type,
normalized: normalized,
offset: offset
});
switch (type) {
case this.gl.BYTE:
this.vertexByteSize += size * Int8Array.BYTES_PER_ELEMENT;
break;
case this.gl.UNSIGNED_BYTE:
this.vertexByteSize += size * Uint8Array.BYTES_PER_ELEMENT;
break;
case this.gl.SHORT:
this.vertexByteSize += size * Int16Array.BYTES_PER_ELEMENT;
break;
case this.gl.UNSIGNED_SHORT:
this.vertexByteSize += size * Uint16Array.BYTES_PER_ELEMENT;
break;
case this.gl.INT:
this.vertexByteSize += size * Int32Array.BYTES_PER_ELEMENT;
break;
case this.gl.UNSIGNED_INT:
this.vertexByteSize += size * Uint32Array.BYTES_PER_ELEMENT;
break;
case this.gl.FLOAT:
this.vertexByteSize += size * Float32Array.BYTES_PER_ELEMENT;
break;
default:
throw new Error("Invalid GL Attribute type");
}
this.vertexSize = this.vertexByteSize / Float32Array.BYTES_PER_ELEMENT;
}
/**
* set/change the current projection matrix
* @param {Matrix3d} matrix - the new projection matrix
*/
setProjection(matrix) {
this.currentShader.setUniform("uProjectionMatrix", matrix);
}
/**
* Flush batched vertex data to the GPU
* @param {number} [mode=gl.TRIANGLES] - the GL drawing mode
*/
flush(mode = this.mode) {
let vertex = this.vertexData;
let vertexCount = vertex.vertexCount;
if (vertexCount > 0) {
let gl = this.gl;
let vertexSize = vertex.vertexSize;
// Copy data into stream buffer
if (this.renderer.WebGLVersion > 1) {
gl.bufferData(gl.ARRAY_BUFFER, vertex.toFloat32(), gl.STREAM_DRAW, 0, vertexCount * vertexSize);
} else {
gl.bufferData(gl.ARRAY_BUFFER, vertex.toFloat32(0, vertexCount * vertexSize), gl.STREAM_DRAW);
}
gl.drawArrays(mode, 0, vertexCount);
// clear the vertex buffer
vertex.clear();
}
}
}