Skip to content

Commit

Permalink
fix particle container issue (#11006)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital authored Oct 22, 2024
1 parent c689a4d commit 8616384
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/scene/container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,15 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE
this.destroyed = true;

// remove children is faster than removeChild..
const oldChildren = this.removeChildren(0, this.children.length);

let oldChildren: ContainerChild[];

// we add this check as calling removeChildren on particle container will throw an error
// As we know it does cannot have any children, check before calling the function.
if (this.children.length)
{
oldChildren = this.removeChildren(0, this.children.length);
}

this.removeFromParent();
this.parent = null;
Expand All @@ -1361,7 +1369,7 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE

const destroyChildren = typeof options === 'boolean' ? options : options?.children;

if (destroyChildren)
if (destroyChildren && oldChildren)
{
for (let i = 0; i < oldChildren.length; ++i)
{
Expand Down
92 changes: 92 additions & 0 deletions tests/renderering/particle-container/ParticleContainer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Rectangle } from '../../../src/maths/shapes/Rectangle';
import { Texture } from '../../../src/rendering/renderers/shared/texture/Texture';
import { Container } from '../../../src/scene/container/Container';
import { Particle } from '../../../src/scene/particle-container/shared/Particle';
import { ParticleContainer } from '../../../src/scene/particle-container/shared/ParticleContainer';
import { getWebGLRenderer } from '../../utils/getRenderer';

describe('ParticleContainer', () =>
{
describe('constructor', () =>
{
it('should support no arguments', () =>
{
const container = new ParticleContainer();

expect(container).toBeDefined();
expect(container.texture).toEqual(null);

container.destroy();
});

it('should support options with no texture', () =>
{
const container = new ParticleContainer({
texture: Texture.WHITE,
});

expect(container).toBeDefined();
expect(container.texture).toEqual(Texture.WHITE);

container.destroy();
});
});

describe('destroy', () =>
{
it('should not throw when destroyed', () =>
{
const container = new ParticleContainer();

expect(() => container.destroy()).not.toThrow();
});

it('should clean up correctly on the pipe and system when destroyed', async () =>
{
const renderer = await getWebGLRenderer();

const container = new Container();

const particleContainer = new ParticleContainer();

particleContainer.addParticle(new Particle({
texture: Texture.WHITE
}));

container.addChild(particleContainer);

renderer.render({ container });

particleContainer.destroy();

expect(renderer.renderPipes.particle['_gpuBufferHash'][particleContainer.uid]).toBeNull();
});
});

describe('width', () =>
{
it('should set width correctly if no bounds is set', () =>
{
const container = new ParticleContainer();

container.width = 100;

// this should be 0 as no bounds have been specified
expect(container.width).toEqual(0);
});

it('should set bounds on the constructor', () =>
{
const container = new ParticleContainer({
texture: Texture.WHITE,
boundsArea: new Rectangle(0, 0, 50, 100),
});

expect(container.width).toEqual(50);
expect(container.height).toEqual(100);

container.width = 100;
expect(container.width).toEqual(100);
});
});
});

0 comments on commit 8616384

Please sign in to comment.