From 80c74a6b950dc44d84949e00620cd57900e6f57e Mon Sep 17 00:00:00 2001 From: troytt <47798984@qq.com> Date: Thu, 17 Sep 2026 05:48:51 +0000 Subject: [PATCH] test(cull): add unit test for bounds rebuild on level switch --- tests/cull.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/cull.test.ts b/tests/cull.test.ts index db77537..cecbaf9 100644 --- a/tests/cull.test.ts +++ b/tests/cull.test.ts @@ -77,6 +77,30 @@ describe('视口剔除', () => { expect(intersects(buildBounds([drawable(120, 0, 1, 1)]), 0, view)).toBe(true) expect(intersects(buildBounds([drawable(140, 0, 1, 1)]), 0, view)).toBe(false) }) + + test('换关卡时重建包围盒避免越界误剔除', () => { + // 模拟从 57x41 关卡(2337 个图元)切换到 80x80 关卡(6400 个图元) + const smallLevelDrawables = Array.from({ length: 2337 }, (_, i) => drawable(i * 10, 0, 80, 40)) + const largeLevelDrawables = Array.from({ length: 6400 }, () => drawable(0, 0, 80, 40)) + + const view = viewportRect(0, 0, 1280, 720, 1) + + // 旧实现未重建 bounds,largeLevel 使用 smallLevel 的 stale bounds + const staleBounds = buildBounds(smallLevelDrawables) + let visibleWithStaleBounds = 0 + for (let i = 0; i < largeLevelDrawables.length; i += 1) { + if (intersects(staleBounds, i, view)) visibleWithStaleBounds += 1 + } + expect(visibleWithStaleBounds).toBeLessThanOrEqual(2337) + + // 修复后重建 bounds,所有落在视口内的图元正确通过剔除 + const freshBounds = buildBounds(largeLevelDrawables) + let visibleWithFreshBounds = 0 + for (let i = 0; i < largeLevelDrawables.length; i += 1) { + if (intersects(freshBounds, i, view)) visibleWithFreshBounds += 1 + } + expect(visibleWithFreshBounds).toBe(6400) + }) }) /** 资源包位置;缺资源时整组测试跳过而不是报红。 */