- 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
/**
* @classdesc
* a Vertex Buffer object
* @class VertexArrayBuffer
* @ignore
*/
export default class VertexArrayBuffer {
constructor(vertex_size, vertex_per_obj) {
// the size of one vertex in float
this.vertexSize = vertex_size;
// size of an object in vertex
this.objSize = vertex_per_obj;
// the maximum number of vertices the vertex array buffer can hold
this.maxVertex = 256; // (note: this seems to be the sweet spot performance-wise when using batching)
// the current number of vertices added to the vertex array buffer
this.vertexCount = 0;
// the actual vertex data buffer
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize);
// Float32 and Uint32 view of the vertex data array buffer
this.bufferF32 = new Float32Array(this.buffer);
this.bufferU32 = new Uint32Array(this.buffer);
}
/**
* clear the vertex array buffer
* @ignore
*/
clear() {
this.vertexCount = 0;
}
/**
* return true if full
* @ignore
*/
isFull(vertex = this.objSize) {
return (this.vertexCount + vertex >= this.maxVertex);
}
/**
* resize the vertex buffer, retaining its original contents
* @ignore
*/
resize(vertexCount) {
while (vertexCount > this.maxVertex) {
// double the vertex size
this.maxVertex <<= 1;
}
// save a reference to the previous data
let data = this.bufferF32;
// recreate ArrayBuffer and views
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize);
this.bufferF32 = new Float32Array(this.buffer);
this.bufferU32 = new Uint32Array(this.buffer);
// copy previous data
this.bufferF32.set(data);
return this;
}
/**
* push a new vertex to the buffer
* @ignore
*/
push(x, y, u, v, tint) {
let offset = this.vertexCount * this.vertexSize;
if (this.vertexCount >= this.maxVertex) {
this.resize(this.vertexCount);
}
this.bufferF32[offset] = x;
this.bufferF32[++offset] = y;
if (typeof u !== "undefined") {
this.bufferF32[++offset] = u;
this.bufferF32[++offset] = v;
}
if (typeof tint !== "undefined") {
this.bufferU32[++offset] = tint;
}
this.vertexCount++;
return this;
}
/**
* return a reference to the data in Float32 format
* @ignore
*/
toFloat32(begin, end) {
if (typeof end !== "undefined") {
return this.bufferF32.subarray(begin, end);
} else {
return this.bufferF32;
}
}
/**
* return a reference to the data in Uint32 format
* @ignore
*/
toUint32(begin, end) {
if (typeof end !== "undefined") {
return this.bufferU32.subarray(begin, end);
} else {
return this.bufferU32;
}
}
/**
* return the size of the vertex in vertex
* @ignore
*/
length() {
return this.vertexCount;
}
/**
* return true if empty
* @ignore
*/
isEmpty() {
return this.vertexCount === 0;
}
}