- 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
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
- 657
- 658
- 659
- 660
- 661
- 662
- 663
- 664
- 665
- 666
- 667
- 668
- 669
- 670
- 671
- 672
- 673
- 674
- 675
- 676
- 677
- 678
- 679
- 680
- 681
- 682
- 683
- 684
- 685
- 686
- 687
- 688
- 689
- 690
- 691
- 692
- 693
- 694
- 695
- 696
- 697
- 698
- 699
- 700
- 701
- 702
- 703
- 704
- 705
- 706
- 707
- 708
- 709
- 710
- 711
- 712
- 713
- 714
- 715
- 716
- 717
- 718
- 719
- 720
- 721
- 722
- 723
- 724
- 725
- 726
- 727
- 728
- 729
- 730
- 731
- 732
- 733
- 734
- 735
- 736
- 737
- 738
- 739
- 740
- 741
- 742
- 743
- 744
- 745
- 746
- 747
- 748
- 749
- 750
- 751
- 752
- 753
- 754
- 755
- 756
- 757
- 758
- 759
import { AttributeRedirect } from './redirects/AttributeRedirect';
import Redirect from './redirects/Redirect';
import BatchRenderer from './BatchRenderer';
import { Buffer, Geometry, ViewableBuffer } from '@pixi/core';
import { StdBatch } from './StdBatch';
import { TYPES } from '@pixi/constants';
import { AggregateUniformsBatch } from './AggregateUniformsBatch';
import { nextPow2, log2 } from '@pixi/utils';
import type { DisplayObject } from '@pixi/display';
import type { Resolvable } from './utils/resolveProperty';
// BatchGeometryFactory uses this class internally to setup the attributes of
// the batches.
//
// Supports Uniforms+Standard Pipeline's in-batch/uniform ID.
export class BatchGeometry extends Geometry
{
// Interleaved attribute data buffer
attribBuffer: Buffer;
// Batched indicies
indexBuffer: Buffer;
constructor(attributeRedirects: AttributeRedirect[],
hasIndex: boolean,
texIDAttrib: string,
texturesPerObject: number,
inBatchIDAttrib: string,
uniformIDAttrib: string,
masterIDAttrib: string,
attributeBuffer?: Buffer,
indexBuffer?: Buffer
)
{
super();
attributeBuffer = attributeBuffer || new Buffer(null, false, false);
indexBuffer = indexBuffer || (hasIndex ? new Buffer(null, false, true) : null);
attributeRedirects.forEach((redirect) =>
{
const { glslIdentifer, glType, glSize, normalize } = redirect;
this.addAttribute(glslIdentifer, attributeBuffer, glSize, normalize, glType);
});
if (!masterIDAttrib)
{
if (texIDAttrib && texturesPerObject > 0)
{
this.addAttribute(texIDAttrib, attributeBuffer, texturesPerObject, true, TYPES.FLOAT);
}
if (inBatchIDAttrib)
{
this.addAttribute(inBatchIDAttrib, attributeBuffer, 1, false, TYPES.FLOAT);
}
if (uniformIDAttrib)
{
this.addAttribute(uniformIDAttrib, attributeBuffer, 1, false, TYPES.FLOAT);
}
}
else
{
this.addAttribute(masterIDAttrib, attributeBuffer, 1, false, TYPES.FLOAT);
}
if (hasIndex)
{
this.addIndex(indexBuffer);
}
this.attribBuffer = attributeBuffer;
this.indexBuffer = indexBuffer;
}
}
// To define the constructor shape, this is defined as an abstract class but documented
// as an interface.
export abstract class IBatchGeometryFactory
{
protected _renderer: BatchRenderer;
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-unused-vars
constructor(renderer: BatchRenderer)
{
// Implementation
this._renderer = renderer;
}
abstract init(verticesBatched: number, indiciesBatched: number): void;
abstract append(displayObject: DisplayObject, batch: any): void;
abstract build(): Geometry;
abstract release(geom: Geometry): void;
}
/**
* This interface defines the methods you need to implement to creating your own batch
* geometry factory.
*
* The constructor of an implementation should take only one argument - the batch renderer.
*
* @interface IBatchGeometryFactory
*/
/**
* Called before the batch renderer starts feeding the display-objects. This can be used
* to pre-allocated space for the batch geometry.
*
* @memberof IBatchGeometryFactory#
* @method init
* @param {number} verticesBatched
* @param {number}[indiciesBatched] - optional when display-object's don't use a index buffer
*/
/**
* Adds the display-object to the batch geometry.
*
* If the display-object's shader also uses textures (in `uSamplers` uniform), then it will
* be given a texture-ID to get the texture from the `uSamplers` array. If it uses multiple
* textures, then the texture-ID is an array of indices into `uSamplers`. The texture-attrib
* passed to the batch renderer sets the name of the texture-ID attribute (defualt is `aTextureId`).
*
* @memberof IBatchGeometryFactory#
* @method append
* @param {PIXI.DisplayObject} displayObject
* @param {object} batch - the batch
*/
/**
* This should wrap up the batch geometry in a `PIXI.Geometry` object.
*
* @memberof IBatchGeometryFactory#
* @method build
* @returns {PIXI.Geometry} batch geometry
*/
/**
* This is used to return a batch geometry so it can be pooled and reused in a future `build()`
* call.
*
* @memberof IBatchGeometryFactory#
* @method release
* @param {PIXI.Geometry} geom
*/
/**
* Factory class that generates the geometry for a whole batch by feeding on
* the individual display-object geometries. This factory is reusable, i.e. you
* can build another geometry after a {@link build} call.
*
* **Optimizations:** To speed up geometry generation, this compiles an optimized
* packing function that pushes attributes without looping through the attribute
* redirects.
*
* **Default Format:** If you are not using a custom draw-call issuer, then
* the batch geometry must have an interleaved attribute data buffer and one
* index buffer.
*
* **Customization:** If you want to customize the batch geometry, then you must
* also define your draw call issuer.
*
* **inBatchID Support**: If you specified an `inBatchID` attribute in the batch-renderer,
* then this will support it automatically. The aggregate-uniforms pipeline doesn't need a custom
* geometry factory.
*/
export class BatchGeometryFactory extends IBatchGeometryFactory
{
_targetCompositeAttributeBuffer: ViewableBuffer;
_targetCompositeIndexBuffer: Uint16Array;
_aIndex: number;
_iIndex: number;
// These properties are not protected because GeometryMerger uses them!
// Standard Pipeline
_attribRedirects: AttributeRedirect[];
_indexProperty: string;
_vertexCountProperty: string | number | ((object: DisplayObject) => number);
_vertexSize: number;
_texturesPerObject: number;
_textureProperty: string;
_texIDAttrib: string;
_inBatchIDAttrib: string;
_inBatchID: number;
// Uniform+Standard Pipeline
_uniformIDAttrib: string;
_uniformID: number;
// Master-ID attribute feature
_masterIDAttrib: string;
/* Set to the indicies of the display-object's textures in uSamplers uniform before
invoking geometryMerger(). */
protected _texID: number | number[];
protected _aBuffers: ViewableBuffer[];
protected _iBuffers: Uint16Array[];
protected _geometryPool: Array<Geometry>;
_geometryMerger: (displayObject: DisplayObject, factory: BatchGeometryFactory) => void;
/**
* @param renderer
*/
constructor(renderer: BatchRenderer)
{
super(renderer);
this._targetCompositeAttributeBuffer = null;
this._targetCompositeIndexBuffer = null;
this._aIndex = 0;
this._iIndex = 0;
this._attribRedirects = renderer._attribRedirects;
this._indexProperty = renderer._indexProperty;
this._vertexCountProperty = renderer._vertexCountProperty;
this._vertexSize = AttributeRedirect.vertexSizeFor(this._attribRedirects);
this._texturesPerObject = renderer._texturesPerObject;
this._textureProperty = renderer._textureProperty;
this._texIDAttrib = renderer._texIDAttrib;
this._inBatchIDAttrib = renderer._inBatchIDAttrib;
this._uniformIDAttrib = renderer._uniformIDAttrib;
this._masterIDAttrib = renderer._masterIDAttrib;
if (!this._masterIDAttrib)
{
this._vertexSize += this._texturesPerObject * 4;// texture indices are also passed
if (this._inBatchIDAttrib)
{
this._vertexSize += 4;
}
if (this._uniformIDAttrib)
{
this._vertexSize += 4;
}
}
else
{
this._vertexSize += 4;
}
if (this._texturesPerObject === 1)
{
this._texID = 0;
}
else if (this._texturesPerObject > 1)
{
this._texID = new Array(this._texturesPerObject);
}
this._aBuffers = [];// @see _getAttributeBuffer
this._iBuffers = [];// @see _getIndexBuffer
/**
* Batch geometries that can be reused.
*
* @member {PIXI.Geometry}
* @protected
* @see IBatchGeometryFactory#release
*/
this._geometryPool = [];
}
/**
* Ensures this factory has enough space to buffer the given number of vertices
* and indices. This should be called before feeding display-objects from the
* batch.
*
* @param {number} verticesBatched
* @param {number} indiciesBatched
*/
init(verticesBatched: number, indiciesBatched?: number): void
{
this._targetCompositeAttributeBuffer = this.getAttributeBuffer(verticesBatched);
if (this._indexProperty)
{
this._targetCompositeIndexBuffer = this.getIndexBuffer(indiciesBatched);
}
this._aIndex = this._iIndex = 0;
}
/**
* Append's the display-object geometry to this batch's geometry. You must override
* this you need to "modify" the geometry of the display-object before merging into
* the composite geometry (for example, adding an ID to a special uniform)
*/
append(targetObject: DisplayObject, batch_: any): void
{
const batch: StdBatch = batch_ as StdBatch;
const tex = (targetObject as any)[this._textureProperty];
// GeometryMerger uses _texID for texIDAttrib
if (this._texturesPerObject === 1)
{
const texUID = tex.baseTexture ? tex.baseTexture.uid : tex.uid;
this._texID = batch.uidMap[texUID];
}
else if (this._texturesPerObject > 1)
{
let _tex;
for (let k = 0; k < tex.length; k++)
{
_tex = tex[k];
const texUID = _tex.baseTexture ? _tex.baseTexture.uid : _tex.uid;
(this._texID as number[])[k] = batch.uidMap[texUID];
}
}
// GeometryMerger uses this
if (this._inBatchIDAttrib || this._uniformIDAttrib)
{
this._inBatchID = batch.batchBuffer.indexOf(targetObject);
}
if (this._uniformIDAttrib)
{
this._uniformID = (batch as AggregateUniformsBatch).uniformMap[this._inBatchID];
}
// If _masterIDAttrib, then it is expected you override this function.
this.geometryMerger(targetObject, this);
}
/**
* @override
* @returns {PIXI.Geometry} the generated batch geometry
* @example
* build(): PIXI.Geometry
* {
* // Make sure you're not allocating new geometries if _geometryPool has some
* // already. (Otherwise, a memory leak will result!)
* const geom: ExampleGeometry = (this._geometryPool.pop() || new ExampleGeometry(
* // ...your arguments... //)) as ExampleGeometry;
*
* // Put data into geometry's buffer
*
* return geom;
* }
*/
build(): Geometry
{
const geom: BatchGeometry = (this._geometryPool.pop() || new BatchGeometry(
this._attribRedirects,
true,
this._texIDAttrib,
this._texturesPerObject,
this._inBatchIDAttrib,
this._uniformIDAttrib,
this._masterIDAttrib,
)) as BatchGeometry;
// We don't really have to remove the buffers because BatchRenderer won't reuse
// the data in these buffers after the next build() call.
geom.attribBuffer.update(this._targetCompositeAttributeBuffer.float32View);
geom.indexBuffer.update(this._targetCompositeIndexBuffer);
return geom;
}
/**
* @param {PIXI.Geometry} geom - releases back the geometry to be reused. It is expected
* that it is not used externally again.
* @override
*/
release(geom: Geometry): void
{
this._geometryPool.push(geom);
}
/**
* This lazy getter returns the geometry-merger function. This function
* takes one argument - the display-object to be appended to the batch -
* and pushes its geometry to the batch geometry.
*
* You can overwrite this property with a custom geometry-merger function
* if customizing `BatchGeometryFactory`.
*
* @member {IGeometryMerger}
*/
protected get geometryMerger(): (displayObject: DisplayObject, factory: BatchGeometryFactory) => void
{
if (!this._geometryMerger)
{
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this._geometryMerger = new GeometryMergerFactory(this).compile();
}
return this._geometryMerger;
}
// eslint-disable-next-line require-jsdoc
protected set geometryMerger(func: (displayObject: DisplayObject, factory: BatchGeometryFactory) => void)
{
this._geometryMerger = func;
}
/**
* @protected
*/
get _indexCountProperty(): Resolvable<number>
{
return this._renderer._indexCountProperty;
}
/**
* Allocates an attribute buffer with sufficient capacity to hold `size` elements.
*
* @param {number} size
* @protected
*/
protected getAttributeBuffer(size: number): ViewableBuffer
{
const roundedP2 = nextPow2(size);
const roundedSizeIndex = log2(roundedP2);
const roundedSize = roundedP2;
if (this._aBuffers.length <= roundedSizeIndex)
{
this._aBuffers.length = roundedSizeIndex + 1;
}
let buffer = this._aBuffers[roundedSizeIndex];
if (!buffer)
{
this._aBuffers[roundedSizeIndex] = buffer = new ViewableBuffer(roundedSize * this._vertexSize);
}
return buffer;
}
/**
* Allocates an index buffer (`Uint16Array`) with sufficient capacity to hold `size` indices.
*
* @param size
* @protected
*/
protected getIndexBuffer(size: number): Uint16Array
{
// 12 indices is enough for 2 quads
const roundedP2 = nextPow2(Math.ceil(size / 12));
const roundedSizeIndex = log2(roundedP2);
const roundedSize = roundedP2 * 12;
if (this._iBuffers.length <= roundedSizeIndex)
{
this._iBuffers.length = roundedSizeIndex + 1;
}
let buffer = this._iBuffers[roundedSizeIndex];
if (!buffer)
{
this._iBuffers[roundedSizeIndex] = buffer = new Uint16Array(roundedSize);
}
return buffer;
}
}
// GeometryMergerFactory uses these variable names.
const CompilerConstants = {
INDICES_OFFSET: '__offset_indices_',
FUNC_SOURCE_BUFFER: 'getSourceBuffer',
// Argument names for the geometryMerger() function.
packerArguments: [
'targetObject',
'factory',
],
};
// This was intended to be an inner class of BatchGeometryFactory; however, due to
// a bug in JSDoc, it was placed outside.
// https://github.com/jsdoc/jsdoc/issues/1673
// Factory for generating a geometry-merger function (which appends the geometry of
// a display-object to the batch geometry).
const GeometryMergerFactory = class
{
packer: BatchGeometryFactory;
// We need the BatchGeometryFactory for attribute redirect information.
constructor(packer: BatchGeometryFactory)
{
this.packer = packer;
}
compile(): (displayObject: DisplayObject, factory: BatchGeometryFactory) => void
{
const packer = this.packer;
// The function's body/code is placed here.
let packerBody = `
const compositeAttributes = factory._targetCompositeAttributeBuffer;
const compositeIndices = factory._targetCompositeIndexBuffer;
let aIndex = factory._aIndex;
let iIndex = factory._iIndex;
const textureId = factory._texID;
const attributeRedirects = factory._attribRedirects;
`;
// Define __offset_${i}, the offset of each attribute in the display-object's
// geometry, __buffer_${i} the source buffer of the attribute data.
packer._attribRedirects.forEach((redirect, i) =>
{
packerBody += `
let __offset_${i} = 0;
const __buffer_${i} = (
${this.generateSourceBufferExpr(redirect, i)});
`;
});
// This loops through each vertex in the display-object's geometry and appends
// them (attributes are interleaved, so each attribute element is pushed per vertex)
packerBody += `
const {
int8View,
uint8View,
int16View,
uint16View,
int32View,
uint32View,
float32View,
} = compositeAttributes;
const vertexCount = ${this.generateVertexCountExpr()};
let adjustedAIndex = 0;
for (let vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++)
{
`;
// Eliminate offset conversion when adjacent attributes
// have similar source-types.
let skipByteIndexConversion = false;
// Appends a vertice's attributes (inside the for-loop above).
for (let i = 0; i < packer._attribRedirects.length; i++)
{
const redirect = packer._attribRedirects[i];
// Initialize adjustedAIndex in terms of source type.
if (!skipByteIndexConversion)
{
packerBody += `
adjustedAIndex = aIndex / ${this._sizeOf(i)};
`;
}
if (typeof redirect.size === 'number')
{
for (let j = 0; j < redirect.size; j++)
{
packerBody += `
${redirect.type}View[adjustedAIndex++] = __buffer_${i}[__offset_${i}++];
`;
}
}
else
{
packerBody += `
${redirect.type}View[adjustedAIndex++] = __buffer_${i};
`;
}
if (packer._attribRedirects[i + 1] && (this._sizeOf(i + 1) !== this._sizeOf(i)))
{
packerBody += `
aIndex = adjustedAIndex * ${this._sizeOf(i)};
`;
}
else
{
skipByteIndexConversion = true;
}
}
if (skipByteIndexConversion)
{
if (this._sizeOf(packer._attribRedirects.length - 1) !== 4)
{
packerBody += `
aIndex = adjustedAIndex * ${this._sizeOf(packer._attribRedirects.length - 1)}
`;
skipByteIndexConversion = false;
}
}
if (!packer._masterIDAttrib)
{
if (packer._texturesPerObject > 0)
{
if (packer._texturesPerObject > 1)
{
if (!skipByteIndexConversion)
{
packerBody += `
adjustedAIndex = aIndex / 4;
`;
}
for (let k = 0; k < packer._texturesPerObject; k++)
{
packerBody += `
float32View[adjustedAIndex++] = textureId[${k}];
`;
}
packerBody += `
aIndex = adjustedAIndex * 4;
`;
}
else if (!skipByteIndexConversion)
{
packerBody += `
float32View[aIndex / 4] = textureId;
`;
}
else
{
packerBody += `
float32View[adjustedAIndex++] = textureId;
aIndex = adjustedAIndex * 4;
`;
}
}
if (packer._inBatchIDAttrib)
{
packerBody += `
float32View[adjustedAIndex++] = factory._inBatchID;
aIndex = adjustedAIndex * 4;
`;
}
if (packer._uniformIDAttrib)
{
packerBody += `
float32View[adjustedAIndex++] = factory._uniformID;
aIndex = adjustedAIndex * 4;
`;
}
}
else
{
if (!skipByteIndexConversion)
{
packerBody += `
adjustedAIndex = aIndex / 4;
`;
}
packerBody += `
float32View[adjustedAIndex++] = factory._masterID;
aIndex = adjustedAIndex * 4;
`;
}
/* Close the packing for-loop. */
packerBody += `}
${this.packer._indexProperty
? `const oldAIndex = this._aIndex;`
: ''}
this._aIndex = aIndex;
`;
if (this.packer._indexProperty)
{
packerBody += `
const verticesBefore = oldAIndex / ${this.packer._vertexSize};
const indexCount = ${this.generateIndexCountExpr()};
for (let j = 0; j < indexCount; j++)
{
compositeIndices[iIndex++] = verticesBefore + targetObject['${this.packer._indexProperty}'][j];
}
this._iIndex = iIndex;
`;
}
// eslint-disable-next-line no-new-func
return new Function(
...CompilerConstants.packerArguments,
packerBody) as
(displayObject: DisplayObject, factory: BatchGeometryFactory) => void;
}
// Returns an expression that fetches the attribute data source from
// targetObject (DisplayObject).
generateSourceBufferExpr(redirect: Redirect, i: number): string
{
return (typeof redirect.source === 'string')
? `targetObject['${redirect.source}']`
: `attributeRedirects[${i}].source(targetObject, factory._renderer)`;
}
generateVertexCountExpr(): string
{
if (!this.packer._vertexCountProperty)
{
// auto-calculate based on primary attribute
return `__buffer_0.length / ${this.packer._attribRedirects[0].size}`;
}
if (typeof this.packer._vertexCountProperty === 'function')
{
return `factory._vertexCountProperty(targetObject)`;
}
return (
(typeof this.packer._vertexCountProperty === 'string')
? `targetObject.${this.packer._vertexCountProperty}`
: `${this.packer._vertexCountProperty}`
);
}
generateIndexCountExpr(): string
{
const idxCountProp = this.packer._indexCountProperty;
const idxProp = this.packer._indexProperty;
if (!idxCountProp)
{
return `targetObject['${idxProp}'].length`;
}
if (typeof idxCountProp === 'function')
{
return `factory._indexCountProperty(targetObject)`;
}
else if (typeof idxCountProp === 'string')
{
return `targetObject['${idxCountProp}']`;
}
return `${idxCountProp}`;
}
_sizeOf(i: number): number
{
return ViewableBuffer.sizeOf(
this.packer._attribRedirects[i].type);
}
};
export default BatchGeometryFactory;