Skip to content

Commit

Permalink
fix: gpu particles size is fixed and tolerant of non-integer values
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Dec 6, 2024
1 parent d919a34 commit 655cd8b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
46 changes: 21 additions & 25 deletions sandbox/tests/gpu-particles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,27 @@ var game = new ex.Engine({
var swordImg = new ex.ImageSource('https://cdn.rawgit.com/excaliburjs/Excalibur/7dd48128/assets/sword.png');

var particles = new ex.GpuParticleEmitter({
pos: ex.vec(300, 500),
maxParticles: 10_000,
emitRate: 1000,
radius: 100,
width: 200,
height: 100,
emitterType: ex.EmitterType.Rectangle,
pos: ex.vec(500, 500),
z: 1,
emitterType: ex.EmitterType.Circle,
maxParticles: 1000,
particle: {
acc: ex.vec(0, -100),
// opacity: 0.1,
beginColor: ex.Color.Orange,
endColor: ex.Color.Purple,
// fade: true,
focus: ex.vec(0, -400),
focusAccel: 1000,
startSize: 100,
endSize: 0,
life: 3000,
minSpeed: -100,
maxSpeed: 100,
angularVelocity: 2,
randomRotation: true,
transform: ex.ParticleTransform.Local
// graphic: swordImg.toSprite()
}
minSpeed: 1,
maxSpeed: 10,
minAngle: 3.4,
maxAngle: 6,
opacity: 0.7,
life: 2000,
maxSize: 5,
minSize: 5,
startSize: 5,
endSize: 1,
beginColor: ex.Color.fromRGB(23, 106, 170, 0.1),
endColor: ex.Color.Transparent
},
radius: 1,
emitRate: 1,
isEmitting: true
});

game.input.pointers.primary.on('move', (evt) => {
Expand All @@ -44,7 +40,7 @@ game.add(particles);

game.add(
new ex.Actor({
width: 200,
width: 100,
height: 100,
color: ex.Color.Red,
pos: ex.vec(400, 400)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main(){

float lifePercent = finalLifeMs / maxLifeMs;
vec2 transformedPos = (u_matrix * u_transform * vec4(finalPosition,0.,1.)).xy;

float scale = sqrt(u_transform[0][0] * u_transform[0][0] + u_transform[1][1] * u_transform[1][1]);
gl_Position = vec4(transformedPos, 1.0 - lifePercent, 1.); // use life percent to sort z
gl_PointSize = mix(startSize, endSize, 1.0 - lifePercent);
gl_PointSize = mix(startSize, endSize, 1.0 - lifePercent) * scale;
}
5 changes: 4 additions & 1 deletion src/engine/Particles/GpuParticleEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export class GpuParticleEmitter extends Actor {
}

public emitParticles(particleCount: number) {
this.renderer.emitParticles(particleCount);
if (particleCount <= 0) {
return;
}
this.renderer.emitParticles(particleCount | 0); // coerce to integer
}

public clearParticles() {
Expand Down
4 changes: 4 additions & 0 deletions src/engine/Particles/ParticleEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export class ParticleEmitter extends Actor {
* @param particleCount Number of particles to emit right now
*/
public emitParticles(particleCount: number) {
if (particleCount <= 0) {
return;
}
particleCount = particleCount | 0; // coerce to int
for (let i = 0; i < particleCount; i++) {
const p = this._createParticle();
if (this?.scene?.world) {
Expand Down
Binary file modified src/spec/images/GpuParticlesSpec/particles-wrapped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/spec/images/GpuParticlesSpec/particles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 655cd8b

Please sign in to comment.