/
Geometry.js
431 lines (357 loc) · 12 KB
/
Geometry.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { Attribute } from './Attribute';
import { Buffer } from './Buffer';
import { interleaveTypedArrays } from './utils/interleaveTypedArrays';
import { getBufferType } from './utils/getBufferType';
import { Runner } from '@pixi/runner';
const byteSizeMap = { 5126: 4, 5123: 2, 5121: 1 };
let UID = 0;
/* eslint-disable object-shorthand */
const map = {
Float32Array: Float32Array,
Uint32Array: Uint32Array,
Int32Array: Int32Array,
Uint8Array: Uint8Array,
Uint16Array: Uint16Array,
};
/* eslint-disable max-len */
/**
* The Geometry represents a model. It consists of two components:
* - GeometryStyle - The structure of the model such as the attributes layout
* - GeometryData - the data of the model - this consists of buffers.
* This can include anything from positions, uvs, normals, colors etc.
*
* Geometry can be defined without passing in a style or data if required (thats how I prefer!)
*
* ```js
* let geometry = new PIXI.Geometry();
*
* geometry.addAttribute('positions', [0, 0, 100, 0, 100, 100, 0, 100], 2);
* geometry.addAttribute('uvs', [0,0,1,0,1,1,0,1],2)
* geometry.addIndex([0,1,2,1,3,2])
*
* ```
* @class
* @memberof PIXI
*/
export class Geometry
{
/**
* @param {PIXI.Buffer[]} [buffers] an array of buffers. optional.
* @param {object} [attributes] of the geometry, optional structure of the attributes layout
*/
constructor(buffers = [], attributes = {})
{
this.buffers = buffers;
this.indexBuffer = null;
this.attributes = attributes;
/**
* A map of renderer IDs to webgl VAOs
*
* @protected
* @type {object}
*/
this.glVertexArrayObjects = {};
this.id = UID++;
this.instanced = false;
/**
* Number of instances in this geometry, pass it to `GeometrySystem.draw()`
* @member {number}
* @default 1
*/
this.instanceCount = 1;
this.disposeRunner = new Runner('disposeGeometry', 2);
/**
* Count of existing (not destroyed) meshes that reference this geometry
* @member {number}
*/
this.refCount = 0;
}
/**
*
* Adds an attribute to the geometry
*
* @param {String} id - the name of the attribute (matching up to a shader)
* @param {PIXI.Buffer|number[]} [buffer] the buffer that holds the data of the attribute . You can also provide an Array and a buffer will be created from it.
* @param {Number} [size=0] the size of the attribute. If you have 2 floats per vertex (eg position x and y) this would be 2
* @param {Boolean} [normalized=false] should the data be normalized.
* @param {Number} [type=PIXI.TYPES.FLOAT] what type of number is the attribute. Check {PIXI.TYPES} to see the ones available
* @param {Number} [stride=0] How far apart (in floats) the start of each value is. (used for interleaving data)
* @param {Number} [start=0] How far into the array to start reading values (used for interleaving data)
*
* @return {PIXI.Geometry} returns self, useful for chaining.
*/
addAttribute(id, buffer, size, normalized = false, type, stride, start, instance = false)
{
if (!buffer)
{
throw new Error('You must pass a buffer when creating an attribute');
}
// check if this is a buffer!
if (!buffer.data)
{
// its an array!
if (buffer instanceof Array)
{
buffer = new Float32Array(buffer);
}
buffer = new Buffer(buffer);
}
const ids = id.split('|');
if (ids.length > 1)
{
for (let i = 0; i < ids.length; i++)
{
this.addAttribute(ids[i], buffer, size, normalized, type);
}
return this;
}
let bufferIndex = this.buffers.indexOf(buffer);
if (bufferIndex === -1)
{
this.buffers.push(buffer);
bufferIndex = this.buffers.length - 1;
}
this.attributes[id] = new Attribute(bufferIndex, size, normalized, type, stride, start, instance);
// assuming that if there is instanced data then this will be drawn with instancing!
this.instanced = this.instanced || instance;
return this;
}
/**
* returns the requested attribute
*
* @param {String} id the name of the attribute required
* @return {PIXI.Attribute} the attribute requested.
*/
getAttribute(id)
{
return this.attributes[id];
}
/**
* returns the requested buffer
*
* @param {String} id the name of the buffer required
* @return {PIXI.Buffer} the buffer requested.
*/
getBuffer(id)
{
return this.buffers[this.getAttribute(id).buffer];
}
/**
*
* Adds an index buffer to the geometry
* The index buffer contains integers, three for each triangle in the geometry, which reference the various attribute buffers (position, colour, UV coordinates, other UV coordinates, normal, …). There is only ONE index buffer.
*
* @param {PIXI.Buffer|number[]} [buffer] the buffer that holds the data of the index buffer. You can also provide an Array and a buffer will be created from it.
* @return {PIXI.Geometry} returns self, useful for chaining.
*/
addIndex(buffer)
{
if (!buffer.data)
{
// its an array!
if (buffer instanceof Array)
{
buffer = new Uint16Array(buffer);
}
buffer = new Buffer(buffer);
}
buffer.index = true;
this.indexBuffer = buffer;
if (this.buffers.indexOf(buffer) === -1)
{
this.buffers.push(buffer);
}
return this;
}
/**
* returns the index buffer
*
* @return {PIXI.Buffer} the index buffer.
*/
getIndex()
{
return this.indexBuffer;
}
/**
* this function modifies the structure so that all current attributes become interleaved into a single buffer
* This can be useful if your model remains static as it offers a little performance boost
*
* @return {PIXI.Geometry} returns self, useful for chaining.
*/
interleave()
{
// a simple check to see if buffers are already interleaved..
if (this.buffers.length === 1 || (this.buffers.length === 2 && this.indexBuffer)) return this;
// assume already that no buffers are interleaved
const arrays = [];
const sizes = [];
const interleavedBuffer = new Buffer();
let i;
for (i in this.attributes)
{
const attribute = this.attributes[i];
const buffer = this.buffers[attribute.buffer];
arrays.push(buffer.data);
sizes.push((attribute.size * byteSizeMap[attribute.type]) / 4);
attribute.buffer = 0;
}
interleavedBuffer.data = interleaveTypedArrays(arrays, sizes);
for (i = 0; i < this.buffers.length; i++)
{
if (this.buffers[i] !== this.indexBuffer)
{
this.buffers[i].destroy();
}
}
this.buffers = [interleavedBuffer];
if (this.indexBuffer)
{
this.buffers.push(this.indexBuffer);
}
return this;
}
getSize()
{
for (const i in this.attributes)
{
const attribute = this.attributes[i];
const buffer = this.buffers[attribute.buffer];
return buffer.data.length / ((attribute.stride / 4) || attribute.size);
}
return 0;
}
/**
* disposes WebGL resources that are connected to this geometry
*/
dispose()
{
this.disposeRunner.run(this, false);
}
/**
* Destroys the geometry.
*/
destroy()
{
this.dispose();
this.buffers = null;
this.indexBuffer = null;
this.attributes = null;
}
/**
* returns a clone of the geometry
*
* @returns {PIXI.Geometry} a new clone of this geometry
*/
clone()
{
const geometry = new Geometry();
for (let i = 0; i < this.buffers.length; i++)
{
geometry.buffers[i] = new Buffer(this.buffers[i].data.slice());
}
for (const i in this.attributes)
{
const attrib = this.attributes[i];
geometry.attributes[i] = new Attribute(
attrib.buffer,
attrib.size,
attrib.normalized,
attrib.type,
attrib.stride,
attrib.start,
attrib.instance
);
}
if (this.indexBuffer)
{
geometry.indexBuffer = geometry.buffers[this.buffers.indexOf(this.indexBuffer)];
geometry.indexBuffer.index = true;
}
return geometry;
}
/**
* merges an array of geometries into a new single one
* geometry attribute styles must match for this operation to work
*
* @param {PIXI.Geometry[]} geometries array of geometries to merge
* @returns {PIXI.Geometry} shiny new geometry!
*/
static merge(geometries)
{
// todo add a geometry check!
// also a size check.. cant be too big!]
const geometryOut = new Geometry();
const arrays = [];
const sizes = [];
const offsets = [];
let geometry;
// pass one.. get sizes..
for (let i = 0; i < geometries.length; i++)
{
geometry = geometries[i];
for (let j = 0; j < geometry.buffers.length; j++)
{
sizes[j] = sizes[j] || 0;
sizes[j] += geometry.buffers[j].data.length;
offsets[j] = 0;
}
}
// build the correct size arrays..
for (let i = 0; i < geometry.buffers.length; i++)
{
// TODO types!
arrays[i] = new map[getBufferType(geometry.buffers[i].data)](sizes[i]);
geometryOut.buffers[i] = new Buffer(arrays[i]);
}
// pass to set data..
for (let i = 0; i < geometries.length; i++)
{
geometry = geometries[i];
for (let j = 0; j < geometry.buffers.length; j++)
{
arrays[j].set(geometry.buffers[j].data, offsets[j]);
offsets[j] += geometry.buffers[j].data.length;
}
}
geometryOut.attributes = geometry.attributes;
if (geometry.indexBuffer)
{
geometryOut.indexBuffer = geometryOut.buffers[geometry.buffers.indexOf(geometry.indexBuffer)];
geometryOut.indexBuffer.index = true;
let offset = 0;
let stride = 0;
let offset2 = 0;
let bufferIndexToCount = 0;
// get a buffer
for (let i = 0; i < geometry.buffers.length; i++)
{
if (geometry.buffers[i] !== geometry.indexBuffer)
{
bufferIndexToCount = i;
break;
}
}
// figure out the stride of one buffer..
for (const i in geometry.attributes)
{
const attribute = geometry.attributes[i];
if ((attribute.buffer | 0) === bufferIndexToCount)
{
stride += ((attribute.size * byteSizeMap[attribute.type]) / 4);
}
}
// time to off set all indexes..
for (let i = 0; i < geometries.length; i++)
{
const indexBufferData = geometries[i].indexBuffer.data;
for (let j = 0; j < indexBufferData.length; j++)
{
geometryOut.indexBuffer.data[j + offset2] += offset;
}
offset += geometry.buffers[bufferIndexToCount].data.length / (stride);
offset2 += indexBufferData.length;
}
}
return geometryOut;
}
}