render method
Renders all live particles in particles. The canvas is already
transformed to the emitter component's local coordinate system.
Implementation
@override
void render(Canvas canvas, ParticleBuffer particles) {
final texture = this.texture;
final count = particles.length;
if (texture == null || count == 0) {
return;
}
if (_transforms.length < count * 4) {
_transforms = Float32List(particles.capacity * 4);
_rects = Float32List(particles.capacity * 4);
}
final src = srcRect;
final anchorX = src.width / 2;
final anchorY = src.height / 2;
final invWidth = 1 / src.width;
for (var i = 0; i < count; i++) {
final scale = particles.size[i] * invWidth;
final rotation = particles.rotation[i];
final scos = cos(rotation) * scale;
final ssin = sin(rotation) * scale;
final j = i * 4;
_transforms[j] = scos;
_transforms[j + 1] = ssin;
_transforms[j + 2] = particles.posX[i] - scos * anchorX + ssin * anchorY;
_transforms[j + 3] = particles.posY[i] - ssin * anchorX - scos * anchorY;
_rects[j] = src.left;
_rects[j + 1] = src.top;
_rects[j + 2] = src.right;
_rects[j + 3] = src.bottom;
}
canvas.drawRawAtlas(
texture,
Float32List.sublistView(_transforms, 0, count * 4),
Float32List.sublistView(_rects, 0, count * 4),
Int32List.sublistView(particles.color, 0, count),
BlendMode.modulate,
null,
paint,
);
}