- 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
import { createIndicesForQuads } from '@pixi/utils';
import { Geometry, Buffer } from '@pixi/core';
import { TYPES } from '@pixi/constants';
import type { Sprite } from '@pixi/sprite';
import type { IParticleRendererProperty } from './ParticleRenderer';
/*
* @author Mat Groves
*
* Big thanks to the very clever Matt DesLauriers <mattdesl> https://github.com/mattdesl/
* for creating the original PixiJS version!
* Also a thanks to https://github.com/bchevalier for tweaking the tint and alpha so that
* they now share 4 bytes on the vertex buffer
*
* Heavily inspired by LibGDX's ParticleBuffer:
* https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/ParticleBuffer.java
*/
/**
* The particle buffer manages the static and dynamic buffers for a particle container.
* @private
* @memberof PIXI
*/
export class ParticleBuffer
{
public geometry: Geometry;
public staticStride: number;
public staticBuffer: Buffer;
public staticData: Float32Array;
public staticDataUint32: Uint32Array;
public dynamicStride: number;
public dynamicBuffer: Buffer;
public dynamicData: Float32Array;
public dynamicDataUint32: Uint32Array;
public _updateID: number;
/** Holds the indices of the geometry (quads) to draw. */
indexBuffer: Buffer;
/** The number of particles the buffer can hold. */
private size: number;
/** A list of the properties that are dynamic. */
private dynamicProperties: IParticleRendererProperty[];
/** A list of the properties that are static. */
private staticProperties: IParticleRendererProperty[];
/**
* @param {object} properties - The properties to upload.
* @param {boolean[]} dynamicPropertyFlags - Flags for which properties are dynamic.
* @param {number} size - The size of the batch.
*/
constructor(properties: IParticleRendererProperty[], dynamicPropertyFlags: boolean[], size: number)
{
this.geometry = new Geometry();
this.indexBuffer = null;
this.size = size;
this.dynamicProperties = [];
this.staticProperties = [];
for (let i = 0; i < properties.length; ++i)
{
let property = properties[i];
// Make copy of properties object so that when we edit the offset it doesn't
// change all other instances of the object literal
property = {
attributeName: property.attributeName,
size: property.size,
uploadFunction: property.uploadFunction,
type: property.type || TYPES.FLOAT,
offset: property.offset,
};
if (dynamicPropertyFlags[i])
{
this.dynamicProperties.push(property);
}
else
{
this.staticProperties.push(property);
}
}
this.staticStride = 0;
this.staticBuffer = null;
this.staticData = null;
this.staticDataUint32 = null;
this.dynamicStride = 0;
this.dynamicBuffer = null;
this.dynamicData = null;
this.dynamicDataUint32 = null;
this._updateID = 0;
this.initBuffers();
}
/** Sets up the renderer context and necessary buffers. */
private initBuffers(): void
{
const geometry = this.geometry;
let dynamicOffset = 0;
this.indexBuffer = new Buffer(createIndicesForQuads(this.size), true, true);
geometry.addIndex(this.indexBuffer);
this.dynamicStride = 0;
for (let i = 0; i < this.dynamicProperties.length; ++i)
{
const property = this.dynamicProperties[i];
property.offset = dynamicOffset;
dynamicOffset += property.size;
this.dynamicStride += property.size;
}
const dynBuffer = new ArrayBuffer(this.size * this.dynamicStride * 4 * 4);
this.dynamicData = new Float32Array(dynBuffer);
this.dynamicDataUint32 = new Uint32Array(dynBuffer);
this.dynamicBuffer = new Buffer(this.dynamicData, false, false);
// static //
let staticOffset = 0;
this.staticStride = 0;
for (let i = 0; i < this.staticProperties.length; ++i)
{
const property = this.staticProperties[i];
property.offset = staticOffset;
staticOffset += property.size;
this.staticStride += property.size;
}
const statBuffer = new ArrayBuffer(this.size * this.staticStride * 4 * 4);
this.staticData = new Float32Array(statBuffer);
this.staticDataUint32 = new Uint32Array(statBuffer);
this.staticBuffer = new Buffer(this.staticData, true, false);
for (let i = 0; i < this.dynamicProperties.length; ++i)
{
const property = this.dynamicProperties[i];
geometry.addAttribute(
property.attributeName,
this.dynamicBuffer,
0,
property.type === TYPES.UNSIGNED_BYTE,
property.type,
this.dynamicStride * 4,
property.offset * 4
);
}
for (let i = 0; i < this.staticProperties.length; ++i)
{
const property = this.staticProperties[i];
geometry.addAttribute(
property.attributeName,
this.staticBuffer,
0,
property.type === TYPES.UNSIGNED_BYTE,
property.type,
this.staticStride * 4,
property.offset * 4
);
}
}
/**
* Uploads the dynamic properties.
* @param children - The children to upload.
* @param startIndex - The index to start at.
* @param amount - The number to upload.
*/
uploadDynamic(children: Sprite[], startIndex: number, amount: number): void
{
for (let i = 0; i < this.dynamicProperties.length; i++)
{
const property = this.dynamicProperties[i];
property.uploadFunction(children, startIndex, amount,
property.type === TYPES.UNSIGNED_BYTE ? this.dynamicDataUint32 : this.dynamicData,
this.dynamicStride, property.offset);
}
this.dynamicBuffer._updateID++;
}
/**
* Uploads the static properties.
* @param children - The children to upload.
* @param startIndex - The index to start at.
* @param amount - The number to upload.
*/
uploadStatic(children: Sprite[], startIndex: number, amount: number): void
{
for (let i = 0; i < this.staticProperties.length; i++)
{
const property = this.staticProperties[i];
property.uploadFunction(children, startIndex, amount,
property.type === TYPES.UNSIGNED_BYTE ? this.staticDataUint32 : this.staticData,
this.staticStride, property.offset);
}
this.staticBuffer._updateID++;
}
/** Destroys the ParticleBuffer. */
destroy(): void
{
this.indexBuffer = null;
this.dynamicProperties = null;
this.dynamicBuffer = null;
this.dynamicData = null;
this.dynamicDataUint32 = null;
this.staticProperties = null;
this.staticBuffer = null;
this.staticData = null;
this.staticDataUint32 = null;
// all buffers are destroyed inside geometry
this.geometry.destroy();
}
}