- 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
import { TYPES } from '@pixi/constants';
import { ExtensionType, ObjectRenderer, Shader, State } from '@pixi/core';
import { Matrix } from '@pixi/math';
import { correctBlendMode, premultiplyRgba, premultiplyTint } from '@pixi/utils';
import { ParticleBuffer } from './ParticleBuffer';
import fragment from './particles.frag';
import vertex from './particles.vert';
import type { ParticleContainer } from './ParticleContainer';
import type { Renderer, ExtensionMetadata } from '@pixi/core';
import type { Sprite } from '@pixi/sprite';
export interface IParticleRendererProperty
{
attributeName: string;
size: number;
type?: TYPES;
uploadFunction: (...params: any[]) => any;
offset: number;
}
/*
* @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 ParticleRenderer:
* https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/ParticleRenderer.java
*/
/**
* Renderer for Particles that is designer for speed over feature set.
* @memberof PIXI
*/
export class ParticleRenderer extends ObjectRenderer
{
/** @ignore */
static extension: ExtensionMetadata = {
name: 'particle',
type: ExtensionType.RendererPlugin,
};
/** The WebGL state in which this renderer will work. */
public readonly state: State;
/** The default shader that is used if a sprite doesn't have a more specific one. */
public shader: Shader;
public tempMatrix: Matrix;
public properties: IParticleRendererProperty[];
/**
* @param renderer - The renderer this sprite batch works for.
*/
constructor(renderer: Renderer)
{
super(renderer);
// 65535 is max vertex index in the index buffer (see ParticleRenderer)
// so max number of particles is 65536 / 4 = 16384
// and max number of element in the index buffer is 16384 * 6 = 98304
// Creating a full index buffer, overhead is 98304 * 2 = 196Ko
// let numIndices = 98304;
this.shader = null;
this.properties = null;
this.tempMatrix = new Matrix();
this.properties = [
// verticesData
{
attributeName: 'aVertexPosition',
size: 2,
uploadFunction: this.uploadVertices,
offset: 0,
},
// positionData
{
attributeName: 'aPositionCoord',
size: 2,
uploadFunction: this.uploadPosition,
offset: 0,
},
// rotationData
{
attributeName: 'aRotation',
size: 1,
uploadFunction: this.uploadRotation,
offset: 0,
},
// uvsData
{
attributeName: 'aTextureCoord',
size: 2,
uploadFunction: this.uploadUvs,
offset: 0,
},
// tintData
{
attributeName: 'aColor',
size: 1,
type: TYPES.UNSIGNED_BYTE,
uploadFunction: this.uploadTint,
offset: 0,
},
];
this.shader = Shader.from(vertex, fragment, {});
this.state = State.for2d();
}
/**
* Renders the particle container object.
* @param container - The container to render using this ParticleRenderer.
*/
public render(container: ParticleContainer): void
{
const children = container.children;
const maxSize = container._maxSize;
const batchSize = container._batchSize;
const renderer = this.renderer;
let totalChildren = children.length;
if (totalChildren === 0)
{
return;
}
else if (totalChildren > maxSize && !container.autoResize)
{
totalChildren = maxSize;
}
let buffers = container._buffers;
if (!buffers)
{
buffers = container._buffers = this.generateBuffers(container);
}
const baseTexture = children[0]._texture.baseTexture;
const premultiplied = baseTexture.alphaMode > 0;
// if the uvs have not updated then no point rendering just yet!
this.state.blendMode = correctBlendMode(container.blendMode, premultiplied);
renderer.state.set(this.state);
const gl = renderer.gl;
const m = container.worldTransform.copyTo(this.tempMatrix);
m.prepend(renderer.globalUniforms.uniforms.projectionMatrix);
this.shader.uniforms.translationMatrix = m.toArray(true);
this.shader.uniforms.uColor = premultiplyRgba(container.tintRgb,
container.worldAlpha, this.shader.uniforms.uColor, premultiplied);
this.shader.uniforms.uSampler = baseTexture;
this.renderer.shader.bind(this.shader);
let updateStatic = false;
// now lets upload and render the buffers..
for (let i = 0, j = 0; i < totalChildren; i += batchSize, j += 1)
{
let amount = (totalChildren - i);
if (amount > batchSize)
{
amount = batchSize;
}
if (j >= buffers.length)
{
buffers.push(this._generateOneMoreBuffer(container));
}
const buffer = buffers[j];
// we always upload the dynamic
buffer.uploadDynamic(children, i, amount);
const bid = container._bufferUpdateIDs[j] || 0;
updateStatic = updateStatic || (buffer._updateID < bid);
// we only upload the static content when we have to!
if (updateStatic)
{
buffer._updateID = container._updateID;
buffer.uploadStatic(children, i, amount);
}
// bind the buffer
renderer.geometry.bind(buffer.geometry);
gl.drawElements(gl.TRIANGLES, amount * 6, gl.UNSIGNED_SHORT, 0);
}
}
/**
* Creates one particle buffer for each child in the container we want to render and updates internal properties.
* @param container - The container to render using this ParticleRenderer
* @returns - The buffers
*/
private generateBuffers(container: ParticleContainer): ParticleBuffer[]
{
const buffers = [];
const size = container._maxSize;
const batchSize = container._batchSize;
const dynamicPropertyFlags = container._properties;
for (let i = 0; i < size; i += batchSize)
{
buffers.push(new ParticleBuffer(this.properties, dynamicPropertyFlags, batchSize));
}
return buffers;
}
/**
* Creates one more particle buffer, because container has autoResize feature.
* @param container - The container to render using this ParticleRenderer
* @returns - The generated buffer
*/
private _generateOneMoreBuffer(container: ParticleContainer): ParticleBuffer
{
const batchSize = container._batchSize;
const dynamicPropertyFlags = container._properties;
return new ParticleBuffer(this.properties, dynamicPropertyFlags, batchSize);
}
/**
* Uploads the vertices.
* @param children - the array of sprites to render
* @param startIndex - the index to start from in the children array
* @param amount - the amount of children that will have their vertices uploaded
* @param array - The vertices to upload.
* @param stride - Stride to use for iteration.
* @param offset - Offset to start at.
*/
public uploadVertices(
children: Sprite[], startIndex: number, amount: number,
array: number[], stride: number, offset: number
): void
{
let w0 = 0;
let w1 = 0;
let h0 = 0;
let h1 = 0;
for (let i = 0; i < amount; ++i)
{
const sprite = children[startIndex + i];
const texture = sprite._texture;
const sx = sprite.scale.x;
const sy = sprite.scale.y;
const trim = texture.trim;
const orig = texture.orig;
if (trim)
{
// if the sprite is trimmed and is not a tilingsprite then we need to add the
// extra space before transforming the sprite coords..
w1 = trim.x - (sprite.anchor.x * orig.width);
w0 = w1 + trim.width;
h1 = trim.y - (sprite.anchor.y * orig.height);
h0 = h1 + trim.height;
}
else
{
w0 = (orig.width) * (1 - sprite.anchor.x);
w1 = (orig.width) * -sprite.anchor.x;
h0 = orig.height * (1 - sprite.anchor.y);
h1 = orig.height * -sprite.anchor.y;
}
array[offset] = w1 * sx;
array[offset + 1] = h1 * sy;
array[offset + stride] = w0 * sx;
array[offset + stride + 1] = h1 * sy;
array[offset + (stride * 2)] = w0 * sx;
array[offset + (stride * 2) + 1] = h0 * sy;
array[offset + (stride * 3)] = w1 * sx;
array[offset + (stride * 3) + 1] = h0 * sy;
offset += stride * 4;
}
}
/**
* Uploads the position.
* @param children - the array of sprites to render
* @param startIndex - the index to start from in the children array
* @param amount - the amount of children that will have their positions uploaded
* @param array - The vertices to upload.
* @param stride - Stride to use for iteration.
* @param offset - Offset to start at.
*/
public uploadPosition(
children: Sprite[], startIndex: number, amount: number,
array: number[], stride: number, offset: number
): void
{
for (let i = 0; i < amount; i++)
{
const spritePosition = children[startIndex + i].position;
array[offset] = spritePosition.x;
array[offset + 1] = spritePosition.y;
array[offset + stride] = spritePosition.x;
array[offset + stride + 1] = spritePosition.y;
array[offset + (stride * 2)] = spritePosition.x;
array[offset + (stride * 2) + 1] = spritePosition.y;
array[offset + (stride * 3)] = spritePosition.x;
array[offset + (stride * 3) + 1] = spritePosition.y;
offset += stride * 4;
}
}
/**
* Uploads the rotation.
* @param children - the array of sprites to render
* @param startIndex - the index to start from in the children array
* @param amount - the amount of children that will have their rotation uploaded
* @param array - The vertices to upload.
* @param stride - Stride to use for iteration.
* @param offset - Offset to start at.
*/
public uploadRotation(
children: Sprite[], startIndex: number, amount: number,
array: number[], stride: number, offset: number
): void
{
for (let i = 0; i < amount; i++)
{
const spriteRotation = children[startIndex + i].rotation;
array[offset] = spriteRotation;
array[offset + stride] = spriteRotation;
array[offset + (stride * 2)] = spriteRotation;
array[offset + (stride * 3)] = spriteRotation;
offset += stride * 4;
}
}
/**
* Uploads the UVs.
* @param children - the array of sprites to render
* @param startIndex - the index to start from in the children array
* @param amount - the amount of children that will have their rotation uploaded
* @param array - The vertices to upload.
* @param stride - Stride to use for iteration.
* @param offset - Offset to start at.
*/
public uploadUvs(
children: Sprite[], startIndex: number, amount: number,
array: number[], stride: number, offset: number
): void
{
for (let i = 0; i < amount; ++i)
{
const textureUvs = children[startIndex + i]._texture._uvs;
if (textureUvs)
{
array[offset] = textureUvs.x0;
array[offset + 1] = textureUvs.y0;
array[offset + stride] = textureUvs.x1;
array[offset + stride + 1] = textureUvs.y1;
array[offset + (stride * 2)] = textureUvs.x2;
array[offset + (stride * 2) + 1] = textureUvs.y2;
array[offset + (stride * 3)] = textureUvs.x3;
array[offset + (stride * 3) + 1] = textureUvs.y3;
offset += stride * 4;
}
else
{
// TODO you know this can be easier!
array[offset] = 0;
array[offset + 1] = 0;
array[offset + stride] = 0;
array[offset + stride + 1] = 0;
array[offset + (stride * 2)] = 0;
array[offset + (stride * 2) + 1] = 0;
array[offset + (stride * 3)] = 0;
array[offset + (stride * 3) + 1] = 0;
offset += stride * 4;
}
}
}
/**
* Uploads the tint.
* @param children - the array of sprites to render
* @param startIndex - the index to start from in the children array
* @param amount - the amount of children that will have their rotation uploaded
* @param array - The vertices to upload.
* @param stride - Stride to use for iteration.
* @param offset - Offset to start at.
*/
public uploadTint(
children: Sprite[], startIndex: number, amount: number,
array: number[], stride: number, offset: number
): void
{
for (let i = 0; i < amount; ++i)
{
const sprite = children[startIndex + i];
const premultiplied = sprite._texture.baseTexture.alphaMode > 0;
const alpha = sprite.alpha;
// we dont call extra function if alpha is 1.0, that's faster
const argb = alpha < 1.0 && premultiplied
? premultiplyTint(sprite._tintRGB, alpha) : sprite._tintRGB + (alpha * 255 << 24);
array[offset] = argb;
array[offset + stride] = argb;
array[offset + (stride * 2)] = argb;
array[offset + (stride * 3)] = argb;
offset += stride * 4;
}
}
/** Destroys the ParticleRenderer. */
public destroy(): void
{
super.destroy();
if (this.shader)
{
this.shader.destroy();
this.shader = null;
}
this.tempMatrix = null;
}
}