melonjs-performance
它能做什么
它介绍如何在 melonJS 游戏中衡量性能,以及加快速度的主要改动。适用于游戏卡顿或掉帧的游戏开发者。
适用场景
- ✓当开发者的 melonJS 游戏在屏幕上对象较多时掉帧时。
- ✓当开发者的游戏因每帧都创建大量对象而卡顿时。
- ✓当开发者想在优化前衡量绘制调用次数和帧时间时。
技能链接
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-performance/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的 melonJS 游戏在屏幕上有很多敌人时会卡顿。我该怎样提速?”
每周 5 个新技能,1 分钟读完。
工作原理
该技能告诉 AI 先添加调试面板并读取数据,然后建议一些修复方法,例如使用纹理图集、按混合模式分组以及复用对象。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Performance
Measure first
Register the debug plugin and read the numbers before changing anything:
import { plugin } from "melonjs";
import { DebugPanelPlugin } from "@melonjs/debug-plugin";
await app.init();
plugin.register(DebugPanelPlugin, "debugPanel");
It reports frame time, draw calls, object counts, and can overlay collision shapes and bounds. Draw calls is usually the number that matters.
Batching: the biggest lever
Both GPU backends batch across textures, not just within one: the quad
batcher assigns each source image a texture unit and embeds the unit in the
vertex data, so several different images still flush as a single draw. The pool
is finite — the WebGL batcher gets renderer.maxTextures units (the
maxTextures application setting, "auto" = the device limit capped at 32), the
WebGPU batcher eight per segment. Overflow the pool and the batch flushes.
So the goal is not "one image" but "few state changes". What actually ends a batch:
- Overflowing the texture-unit pool — pack sprites into a
TextureAtlasand prefer one large tileset over several small ones, so a screenful of sprites fits inside the pool with room to spare. - Changing
blendMode—setBlendModeflushes before it touches GL state. Group by blend mode where you can. - Switching batcher — interleaving primitives (
strokeRect,fillRect, debug shapes) or meshes between sprites flushes the quad batcher and rebinds. Draw all the sprites, then all the primitives. - Binding a custom shader — a
ShaderEffecton the quad batcher also turns multi-texture batching off for the duration, dropping you back to one texture per draw.
The advanced blend modes are the expensive ones: overlay, difference,
hard-light, soft-light, color-dodge, color-burn, darken and lighten
cannot be expressed as fixed-function blending, so each such draw is bracketed
on its own — capture the destination, redraw offscreen, composite through a
shader. Right for accents, wrong for hundreds of objects. normal, additive,
multiply, screen and exclusion are plain fixed-function state and cost
nothing extra.
Pooling
Anything spawned frequently — bullets, particles, pickups, damage numbers — should be pooled rather than allocated:
import { createPool } from "melonjs";
const bulletPool = createPool((x, y) => {
const instance = new Bullet(x, y);
return { instance, reset: (x, y) => instance.onResetEvent(x, y) };
});
const b = bulletPool.get(x, y); // recycled: reset(x, y)
// fresh: new Bullet(x, y)
world.removeChild(b); // returns to bulletPool automatically
The container returns a child to whichever pool built it, so removal is enough;
you only call release() yourself for something that was never added to the
world.
me.pool is the older, string-keyed version and is deprecated since
18.0.0. pool.register(name, Class, true) / pool.pull(name) still work and
print a one-off console notice, but a recycled object is no longer returned on
removal, so pooling through it now costs an allocation per spawn. To register a
class so a Tiled map can name it, use registerTiledObjectClass — that is a
separate registry and is not deprecated.
Two rules that cause subtle bugs when missed:
onResetEventmust restore everything the object's lifetime mutates — alpha, tint, scale, animation, velocity. Anything you forget carries into the next use, which looks like a random visual glitch.Pooled objects do not fire
onDestroyEventon removal. Removal always callsonDeactivateEvent, then tries to return the object to the pool that built it; only when nothing owns it does it fall through todestroy(), which is what callsonDestroyEvent. So a successfully recycled object never sees it. Pair event subscriptions withonActivateEvent/onDeactivateEventinstead, or you leak handlers.removeChildis DEFERRED, which breaks a pool you re-lend in the same frame. The removal is queued and runs after the update and draw stack has unwound, so an object you remove and immediately hand out again is torn out from under its new owner when the queue drains: it vanishes, and the frame after that the removal throwsChild is not mine.Take it out immediately instead, keeping the instance alive for reuse:world.removeChildNow(bullet, true); // keepalive: do NOT destro
常见问题
我应该先衡量什么?
该技能推荐使用调试面板,它会显示帧时间和绘制调用次数。
什么是合批?
合批将许多精灵合并为较少的绘制调用,通常是最大的提速来源。
为什么更改混合模式会变慢?
该技能说明,更改混合模式会结束当前批次,因此按混合模式对精灵分组会有所帮助。
详情
作者的原始描述
Use this skill when a melonJS game drops frames, allocates heavily, or needs to scale to many objects. Covers object pooling, draw-call batching, atlases, baking with CanvasRenderTarget, instancing, culling and alwaysUpdate, the debug plugin, and where the real costs are. Triggers on: performance, slow, frame rate, fps, lag, pool, pool.register, pool.pull, batching, draw calls, InstancedMesh, culling, alwaysUpdate, inViewport, CanvasRenderTarget, optimise, profiling, debug plugin, memory, render targets, hdr.
repo: melonjs/melonJS
热度
- GitHub 星标
- 6.4k
- 排名
- #14209
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
melonjs-20-migration
6.4k帮你的智能体按 melonJS 20 版本编写游戏代码,避免使用旧版本中已过时的方法,例如必需的异步启动。
melonjs-3d-assets
6.4k帮你的智能体将 glTF、GLB 和 OBJ 等 3D 模型加载到 melonJS 浏览器游戏中,并提供缩放和光照选项。
melonjs-3d
6.4k帮你的智能体在 melonJS 浏览器游戏中构建 3D 或 2.5D 场景,涵盖相机、网格、灯光和深度,并遵循引擎自身的坐标轴规则。
melonjs-audio
6.4k帮你的智能体为 melonJS 游戏添加音效和音乐,包括音频设置、背景音轨、音量控制和空间声音。