Render the particle, automatically called each frame
()
| 473 | |
| 474 | /** Render the particle, automatically called each frame */ |
| 475 | render() |
| 476 | { |
| 477 | // emitter properties |
| 478 | const emitter = this.emitter; |
| 479 | const localSpace = emitter.localSpace; |
| 480 | const additive = emitter.additive; |
| 481 | const trailScale = emitter.trailScale; |
| 482 | const fadeRate = emitter.fadeRate / 2; |
| 483 | |
| 484 | // lerp color and size |
| 485 | const p1 = this.lifeTime > 0 ? min((time - this.spawnTime) / this.lifeTime, 1) : 1, p2 = 1-p1; |
| 486 | const radius = p2 * this.sizeStart + p1 * this.sizeEnd; |
| 487 | const size = vec2(radius); |
| 488 | const alphaFade = p1 < fadeRate ? p1/fadeRate : |
| 489 | p1 > 1-fadeRate ? (1-p1)/fadeRate : 1; |
| 490 | this.color.r = p2 * this.colorStart.r + p1 * this.colorEnd.r; |
| 491 | this.color.g = p2 * this.colorStart.g + p1 * this.colorEnd.g; |
| 492 | this.color.b = p2 * this.colorStart.b + p1 * this.colorEnd.b; |
| 493 | this.color.a = (p2 * this.colorStart.a + p1 * this.colorEnd.a) * alphaFade; |
| 494 | |
| 495 | // draw the particle |
| 496 | additive && setBlendMode(true); |
| 497 | |
| 498 | // update the position and angle for drawing |
| 499 | const pos = particleDrawPos.set(this.pos.x, this.pos.y); |
| 500 | let angle = this.angle; |
| 501 | if (localSpace) |
| 502 | { |
| 503 | // in local space of emitter |
| 504 | const a = emitter.angle; |
| 505 | const c = cos(a), s = sin(a); |
| 506 | pos.set(emitter.pos.x + pos.x*c - pos.y*s, |
| 507 | emitter.pos.y + pos.x*s + pos.y*c); |
| 508 | angle += a; |
| 509 | } |
| 510 | if (trailScale) |
| 511 | { |
| 512 | // trail style particles |
| 513 | const velocity = localSpace ? |
| 514 | this.velocity.rotate(-emitter.angle) : this.velocity; |
| 515 | const speed = velocity.length(); |
| 516 | if (speed) |
| 517 | { |
| 518 | // stretch in direction of motion |
| 519 | const trailLength = speed * trailScale; |
| 520 | size.y = max(size.x, trailLength); |
| 521 | angle = atan2(velocity.x, velocity.y); |
| 522 | drawTile(pos, size, this.tileInfo, this.color, angle, this.mirror); |
| 523 | } |
| 524 | } |
| 525 | else |
| 526 | drawTile(pos, size, this.tileInfo, this.color, angle, this.mirror); |
| 527 | additive && setBlendMode(); |
| 528 | debugParticles && debugRect(pos, size, '#f005', 0, angle); |
| 529 | } |
| 530 | } |