Skip to content

Commit

Permalink
fix onRender bug in renderGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital committed Apr 29, 2024
1 parent ff67a1d commit 28c14ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/scene/container/RenderGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ export class RenderGroup implements Instruction
{
child.relativeRenderGroupDepth = child.parent.relativeRenderGroupDepth + 1;
}

if (child._onRender)
{
this.addOnRender(child);
}
}

if (child.renderGroup)
Expand All @@ -126,6 +121,22 @@ export class RenderGroup implements Instruction
child.didChange = true;
}

if (child._onRender)
{
// Add the child to the onRender list under the following conditions:
// 1. If the child is not a render group.
// 2. If the child is a render group root of this render group

if (!child.isRenderGroupRoot)
{
this.addOnRender(child);
}
else if (child.renderGroup.root === child)
{
this.addOnRender(child);
}
}

const children = child.children;

if (!child.isRenderGroupRoot)
Expand All @@ -146,7 +157,18 @@ export class RenderGroup implements Instruction

if (child._onRender)
{
this.removeOnRender(child);
// Remove the child to the onRender list under the following conditions:
// 1. If the child is not a render group.
// 2. If the child is a render group root of this render group

if (!child.isRenderGroupRoot)
{
this.removeOnRender(child);
}
else if (child.renderGroup.root === child)
{
this.removeOnRender(child);
}
}

if (child.renderGroup.root !== child)
Expand Down
34 changes: 34 additions & 0 deletions tests/renderering/scene/RenderGroupSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,38 @@ describe('RenderGroupSystem', () =>

expect(container.renderGroup.childrenRenderablesToUpdate.index).toEqual(0);
});

it('should only call on render once for a render group after conversion', async () =>
{
const renderer = await getWebGLRenderer();

const container = new Container({ isRenderGroup: false, label: 'root' });

const child = new Container({ isRenderGroup: true, label: 'child' });

container.addChild(child);

child.onRender = jest.fn();

renderer.render(container);

expect(child.onRender).toHaveBeenCalledTimes(1);
});

it('should only call on render once for a render group before conversion', async () =>
{
const renderer = await getWebGLRenderer();

const container = new Container({ isRenderGroup: true, label: 'root' });

const child = new Container({ isRenderGroup: true, label: 'child' });

container.addChild(child);

child.onRender = jest.fn();

renderer.render(container);

expect(child.onRender).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 28c14ce

Please sign in to comment.