|
7 | 7 |
|
8 | 8 | export default class VertexArrayBuffer { |
9 | 9 |
|
10 | | - constructor(vertex_size, vertex_per_quad) { |
| 10 | + constructor(vertex_size, vertex_per_obj) { |
11 | 11 | // the size of one vertex in float |
12 | 12 | this.vertexSize = vertex_size; |
13 | | - // size of a quad in vertex |
14 | | - this.quadSize = vertex_per_quad; |
| 13 | + // size of an object in vertex |
| 14 | + this.objSize = vertex_per_obj; |
15 | 15 | // the maximum number of vertices the vertex array buffer can hold |
16 | | - this.maxVertex = 256; |
| 16 | + this.maxVertex = 256; // (note: this seems to be the sweet spot performance-wise when using batching) |
17 | 17 | // the current number of vertices added to the vertex array buffer |
18 | 18 | this.vertexCount = 0; |
19 | 19 |
|
20 | 20 | // the actual vertex data buffer |
21 | | - this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize); |
| 21 | + this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize); |
22 | 22 | // Float32 and Uint32 view of the vertex data array buffer |
23 | 23 | this.bufferF32 = new Float32Array(this.buffer); |
24 | 24 | this.bufferU32 = new Uint32Array(this.buffer); |
|
37 | 37 | * return true if full |
38 | 38 | * @ignore |
39 | 39 | */ |
40 | | - isFull(vertex = this.quadSize) { |
| 40 | + isFull(vertex = this.objSize) { |
41 | 41 | return (this.vertexCount + vertex >= this.maxVertex); |
42 | 42 | } |
43 | 43 |
|
44 | 44 | /** |
45 | 45 | * resize the vertex buffer, retaining its original contents |
46 | 46 | * @ignore |
47 | 47 | */ |
48 | | - resize() { |
49 | | - // double the vertex size |
50 | | - this.maxVertex <<= 1; |
| 48 | + resize(vertexCount) { |
| 49 | + |
| 50 | + while (vertexCount > this.maxVertex) { |
| 51 | + // double the vertex size |
| 52 | + this.maxVertex <<= 1; |
| 53 | + } |
| 54 | + |
51 | 55 | // save a reference to the previous data |
52 | 56 | var data = this.bufferF32; |
53 | 57 |
|
54 | 58 | // recreate ArrayBuffer and views |
55 | | - this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize); |
| 59 | + this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize); |
56 | 60 | this.bufferF32 = new Float32Array(this.buffer); |
57 | 61 | this.bufferU32 = new Uint32Array(this.buffer); |
58 | 62 |
|
|
70 | 74 | var offset = this.vertexCount * this.vertexSize; |
71 | 75 |
|
72 | 76 | if (this.vertexCount >= this.maxVertex) { |
73 | | - this.resize(); |
| 77 | + this.resize(this.vertexCount); |
74 | 78 | } |
75 | 79 |
|
76 | | - this.bufferF32[offset + 0] = x; |
77 | | - this.bufferF32[offset + 1] = y; |
| 80 | + this.bufferF32[offset] = x; |
| 81 | + this.bufferF32[++offset] = y; |
78 | 82 |
|
79 | 83 | if (typeof u !== "undefined") { |
80 | | - this.bufferF32[offset + 2] = u; |
81 | | - this.bufferF32[offset + 3] = v; |
| 84 | + this.bufferF32[++offset] = u; |
| 85 | + this.bufferF32[++offset] = v; |
82 | 86 | } |
83 | 87 |
|
84 | 88 | if (typeof tint !== "undefined") { |
85 | | - this.bufferU32[offset + 4] = tint; |
| 89 | + this.bufferU32[++offset] = tint; |
86 | 90 | } |
87 | 91 |
|
88 | 92 | this.vertexCount++; |
|
0 commit comments