diff --git a/packages/core/src/batch/AbstractBatchRenderer.js b/packages/core/src/batch/AbstractBatchRenderer.js index d37b277..8cbefe0 100644 --- a/packages/core/src/batch/AbstractBatchRenderer.js +++ b/packages/core/src/batch/AbstractBatchRenderer.js @@ -1,5 +1,6 @@ import BatchDrawCall from './BatchDrawCall'; import BaseTexture from '../textures/BaseTexture'; +import builtinAttributeSizes from './utils/builtinAttributeSizes'; import State from '../state/State'; import ObjectRenderer from './ObjectRenderer'; import checkMaxIfStatementsInShader from '../shader/utils/checkMaxIfStatementsInShader'; @@ -9,18 +10,6 @@ import { ENV } from '@pixi/constants'; /** - * Sizes of built-in attributes provided by `BatchRenderer`, - * which includes the required `aTextureId` attribute. - * - * @type Map - * @see {AbstractBatchRenderer#vertexSizeOf} - */ -const builtinAttributeSizes = { - aColor: 1, - aTextureId: 1, -}; - -/** * Renderer dedicated to drawing and batching sprites. * * This is the default batch renderer. It buffers objects @@ -82,12 +71,12 @@ * | Property | Description | * |------------|-----------------------------------------------| * | property | Property name in objects for attribute data | - * | identifier | Name of attribute variable used in the shader | + * | name | Name of attribute variable used in the shader | * | size | Size of each attribute element in floats | * * `BatchRenderer` provides a few built-in attributes * that you can use like `aColor`. Instead of the - * attribute definition, just give the identifier to + * attribute definition, just give the name to * use them. * * The `attribute float aTextureId` attribute should @@ -101,12 +90,12 @@ * for each vertex. * * @default - * | Index | AttributeDefinition | - * |-------|--------------------------------------------------------------------| - * | 0 | `{ property: vertexData, identifier: 'aVertexPosition', size: 2 }` | - * | 1 | `{ property: uvs, identifier: 'aTextureCoord', size: 2 }` | - * | 2 | `'aColor'` | - * | 3 | `'aTextureId'` // mandatory | + * | Index | AttributeDefinition | + * |-------|--------------------------------------------------------------| + * | 0 | `{ property: vertexData, name: 'aVertexPosition', size: 2 }` | + * | 1 | `{ property: uvs, name: 'aTextureCoord', size: 2 }` | + * | 2 | `'aColor'` | + * | 3 | `'aTextureId'` // mandatory | * * @readonly */ @@ -326,7 +315,7 @@ for (let i = 0; i < this._packedGeometryPoolSize; i++) { /* eslint-disable max-len */ - this._packedGeometries[i] = new (this.geometryClass)(); + this._packedGeometries[i] = new (this.geometryClass)(false, this.attributeDefinitions); } } @@ -478,7 +467,8 @@ if (this._packedGeometryPoolSize <= this._flushId) { this._packedGeometryPoolSize++;// expand geometry pool - this._packedGeometries[this._flushId] = new (this.geometryClass)(); + this._packedGeometries[this._flushId] + = new (this.geometryClass)(false, this.attributeDefinitions); } packedGeometries[this._flushId]._buffer.update( @@ -735,7 +725,10 @@ for (let float = 0; float < size; float++) { - float32View[aIndex++] = source[offset++ % source.length]; + if (attribute === 'aColor') + { uint32View[aIndex++] = source[offset++ % source.length]; } + else + { float32View[aIndex++] = source[offset++ % source.length]; } } sourceOffsets[s] = offset; diff --git a/packages/core/src/batch/AbstractBatchRenderer.js b/packages/core/src/batch/AbstractBatchRenderer.js index d37b277..8cbefe0 100644 --- a/packages/core/src/batch/AbstractBatchRenderer.js +++ b/packages/core/src/batch/AbstractBatchRenderer.js @@ -1,5 +1,6 @@ import BatchDrawCall from './BatchDrawCall'; import BaseTexture from '../textures/BaseTexture'; +import builtinAttributeSizes from './utils/builtinAttributeSizes'; import State from '../state/State'; import ObjectRenderer from './ObjectRenderer'; import checkMaxIfStatementsInShader from '../shader/utils/checkMaxIfStatementsInShader'; @@ -9,18 +10,6 @@ import { ENV } from '@pixi/constants'; /** - * Sizes of built-in attributes provided by `BatchRenderer`, - * which includes the required `aTextureId` attribute. - * - * @type Map - * @see {AbstractBatchRenderer#vertexSizeOf} - */ -const builtinAttributeSizes = { - aColor: 1, - aTextureId: 1, -}; - -/** * Renderer dedicated to drawing and batching sprites. * * This is the default batch renderer. It buffers objects @@ -82,12 +71,12 @@ * | Property | Description | * |------------|-----------------------------------------------| * | property | Property name in objects for attribute data | - * | identifier | Name of attribute variable used in the shader | + * | name | Name of attribute variable used in the shader | * | size | Size of each attribute element in floats | * * `BatchRenderer` provides a few built-in attributes * that you can use like `aColor`. Instead of the - * attribute definition, just give the identifier to + * attribute definition, just give the name to * use them. * * The `attribute float aTextureId` attribute should @@ -101,12 +90,12 @@ * for each vertex. * * @default - * | Index | AttributeDefinition | - * |-------|--------------------------------------------------------------------| - * | 0 | `{ property: vertexData, identifier: 'aVertexPosition', size: 2 }` | - * | 1 | `{ property: uvs, identifier: 'aTextureCoord', size: 2 }` | - * | 2 | `'aColor'` | - * | 3 | `'aTextureId'` // mandatory | + * | Index | AttributeDefinition | + * |-------|--------------------------------------------------------------| + * | 0 | `{ property: vertexData, name: 'aVertexPosition', size: 2 }` | + * | 1 | `{ property: uvs, name: 'aTextureCoord', size: 2 }` | + * | 2 | `'aColor'` | + * | 3 | `'aTextureId'` // mandatory | * * @readonly */ @@ -326,7 +315,7 @@ for (let i = 0; i < this._packedGeometryPoolSize; i++) { /* eslint-disable max-len */ - this._packedGeometries[i] = new (this.geometryClass)(); + this._packedGeometries[i] = new (this.geometryClass)(false, this.attributeDefinitions); } } @@ -478,7 +467,8 @@ if (this._packedGeometryPoolSize <= this._flushId) { this._packedGeometryPoolSize++;// expand geometry pool - this._packedGeometries[this._flushId] = new (this.geometryClass)(); + this._packedGeometries[this._flushId] + = new (this.geometryClass)(false, this.attributeDefinitions); } packedGeometries[this._flushId]._buffer.update( @@ -735,7 +725,10 @@ for (let float = 0; float < size; float++) { - float32View[aIndex++] = source[offset++ % source.length]; + if (attribute === 'aColor') + { uint32View[aIndex++] = source[offset++ % source.length]; } + else + { float32View[aIndex++] = source[offset++ % source.length]; } } sourceOffsets[s] = offset; diff --git a/packages/core/src/batch/BatchGeometry.js b/packages/core/src/batch/BatchGeometry.js index 5b8afc4..5f74c25 100644 --- a/packages/core/src/batch/BatchGeometry.js +++ b/packages/core/src/batch/BatchGeometry.js @@ -1,6 +1,7 @@ -import { TYPES } from '@pixi/constants'; -import Geometry from '../geometry/Geometry'; import Buffer from '../geometry/Buffer'; +import builtinAttributeSizes from './utils/builtinAttributeSizes'; +import Geometry from '../geometry/Geometry'; +import { TYPES } from '@pixi/constants'; /** * Geometry used to batch standard PIXI content (e.g. Mesh, Sprite, @@ -14,8 +15,9 @@ /** * @param {boolean} [_static=false] Optimization flag, where `false` * is updated every frame, `true` doesn't change frame-to-frame. + * @param {Object[]} attributeDefinitions - attribute definitions */ - constructor(_static = false) + constructor(_static = false, attributeDefinitions) { super(); @@ -36,10 +38,22 @@ this._indexBuffer = new Buffer(null, _static, true); /* These are automatically interleaved by GeometrySystem. */ - this.addAttribute('aVertexPosition', this._buffer, 2, false, TYPES.FLOAT) - .addAttribute('aTextureCoord', this._buffer, 2, false, TYPES.FLOAT) - .addAttribute('aColor', this._buffer, 4, true, TYPES.UNSIGNED_BYTE) - .addAttribute('aTextureId', this._buffer, 1, true, TYPES.FLOAT) - .addIndex(this._indexBuffer); + attributeDefinitions.forEach((def) => + { + if (def === 'aColor') + { // special + this.addAttribute('aColor', this._buffer, 4, true, TYPES.UNSIGNED_BYTE); + + return; + } + + const isBuiltin = (typeof def === 'string'); + const identifier = isBuiltin ? def : def.name; + const size = isBuiltin ? builtinAttributeSizes[identifier] : def.size; + + this.addAttribute(identifier, this._buffer, size, def === 'aTextureId', TYPES.FLOAT); + }); + + this.addIndex(this._indexBuffer); } } diff --git a/packages/core/src/batch/AbstractBatchRenderer.js b/packages/core/src/batch/AbstractBatchRenderer.js index d37b277..8cbefe0 100644 --- a/packages/core/src/batch/AbstractBatchRenderer.js +++ b/packages/core/src/batch/AbstractBatchRenderer.js @@ -1,5 +1,6 @@ import BatchDrawCall from './BatchDrawCall'; import BaseTexture from '../textures/BaseTexture'; +import builtinAttributeSizes from './utils/builtinAttributeSizes'; import State from '../state/State'; import ObjectRenderer from './ObjectRenderer'; import checkMaxIfStatementsInShader from '../shader/utils/checkMaxIfStatementsInShader'; @@ -9,18 +10,6 @@ import { ENV } from '@pixi/constants'; /** - * Sizes of built-in attributes provided by `BatchRenderer`, - * which includes the required `aTextureId` attribute. - * - * @type Map - * @see {AbstractBatchRenderer#vertexSizeOf} - */ -const builtinAttributeSizes = { - aColor: 1, - aTextureId: 1, -}; - -/** * Renderer dedicated to drawing and batching sprites. * * This is the default batch renderer. It buffers objects @@ -82,12 +71,12 @@ * | Property | Description | * |------------|-----------------------------------------------| * | property | Property name in objects for attribute data | - * | identifier | Name of attribute variable used in the shader | + * | name | Name of attribute variable used in the shader | * | size | Size of each attribute element in floats | * * `BatchRenderer` provides a few built-in attributes * that you can use like `aColor`. Instead of the - * attribute definition, just give the identifier to + * attribute definition, just give the name to * use them. * * The `attribute float aTextureId` attribute should @@ -101,12 +90,12 @@ * for each vertex. * * @default - * | Index | AttributeDefinition | - * |-------|--------------------------------------------------------------------| - * | 0 | `{ property: vertexData, identifier: 'aVertexPosition', size: 2 }` | - * | 1 | `{ property: uvs, identifier: 'aTextureCoord', size: 2 }` | - * | 2 | `'aColor'` | - * | 3 | `'aTextureId'` // mandatory | + * | Index | AttributeDefinition | + * |-------|--------------------------------------------------------------| + * | 0 | `{ property: vertexData, name: 'aVertexPosition', size: 2 }` | + * | 1 | `{ property: uvs, name: 'aTextureCoord', size: 2 }` | + * | 2 | `'aColor'` | + * | 3 | `'aTextureId'` // mandatory | * * @readonly */ @@ -326,7 +315,7 @@ for (let i = 0; i < this._packedGeometryPoolSize; i++) { /* eslint-disable max-len */ - this._packedGeometries[i] = new (this.geometryClass)(); + this._packedGeometries[i] = new (this.geometryClass)(false, this.attributeDefinitions); } } @@ -478,7 +467,8 @@ if (this._packedGeometryPoolSize <= this._flushId) { this._packedGeometryPoolSize++;// expand geometry pool - this._packedGeometries[this._flushId] = new (this.geometryClass)(); + this._packedGeometries[this._flushId] + = new (this.geometryClass)(false, this.attributeDefinitions); } packedGeometries[this._flushId]._buffer.update( @@ -735,7 +725,10 @@ for (let float = 0; float < size; float++) { - float32View[aIndex++] = source[offset++ % source.length]; + if (attribute === 'aColor') + { uint32View[aIndex++] = source[offset++ % source.length]; } + else + { float32View[aIndex++] = source[offset++ % source.length]; } } sourceOffsets[s] = offset; diff --git a/packages/core/src/batch/BatchGeometry.js b/packages/core/src/batch/BatchGeometry.js index 5b8afc4..5f74c25 100644 --- a/packages/core/src/batch/BatchGeometry.js +++ b/packages/core/src/batch/BatchGeometry.js @@ -1,6 +1,7 @@ -import { TYPES } from '@pixi/constants'; -import Geometry from '../geometry/Geometry'; import Buffer from '../geometry/Buffer'; +import builtinAttributeSizes from './utils/builtinAttributeSizes'; +import Geometry from '../geometry/Geometry'; +import { TYPES } from '@pixi/constants'; /** * Geometry used to batch standard PIXI content (e.g. Mesh, Sprite, @@ -14,8 +15,9 @@ /** * @param {boolean} [_static=false] Optimization flag, where `false` * is updated every frame, `true` doesn't change frame-to-frame. + * @param {Object[]} attributeDefinitions - attribute definitions */ - constructor(_static = false) + constructor(_static = false, attributeDefinitions) { super(); @@ -36,10 +38,22 @@ this._indexBuffer = new Buffer(null, _static, true); /* These are automatically interleaved by GeometrySystem. */ - this.addAttribute('aVertexPosition', this._buffer, 2, false, TYPES.FLOAT) - .addAttribute('aTextureCoord', this._buffer, 2, false, TYPES.FLOAT) - .addAttribute('aColor', this._buffer, 4, true, TYPES.UNSIGNED_BYTE) - .addAttribute('aTextureId', this._buffer, 1, true, TYPES.FLOAT) - .addIndex(this._indexBuffer); + attributeDefinitions.forEach((def) => + { + if (def === 'aColor') + { // special + this.addAttribute('aColor', this._buffer, 4, true, TYPES.UNSIGNED_BYTE); + + return; + } + + const isBuiltin = (typeof def === 'string'); + const identifier = isBuiltin ? def : def.name; + const size = isBuiltin ? builtinAttributeSizes[identifier] : def.size; + + this.addAttribute(identifier, this._buffer, size, def === 'aTextureId', TYPES.FLOAT); + }); + + this.addIndex(this._indexBuffer); } } diff --git a/packages/core/src/batch/BatchPluginFactory.js b/packages/core/src/batch/BatchPluginFactory.js index 5dd645a..91c044e 100644 --- a/packages/core/src/batch/BatchPluginFactory.js +++ b/packages/core/src/batch/BatchPluginFactory.js @@ -52,7 +52,7 @@ vertex, } = Object.assign({ attributeDefinitions: [ - { property: 'vertexData', name: 'aTexturePosition', size: 2 }, + { property: 'vertexData', name: 'aVertexPosition', size: 2 }, { property: 'uvs', name: 'aTextureCoord', size: 2 }, 'aColor', // built-in attribute 'aTextureId',