melonjs-effects-and-shaders
它能做什么
它介绍 melonJS 内置的视觉效果,以及如何将它们附加到精灵或整个相机上。适用于希望画面更精致的游戏开发者。
适用场景
- ✓当开发者想在 melonJS 游戏的精灵上添加光晕或暗角效果时。
- ✓当开发者想对整个游戏画面进行色彩分级处理时。
- ✓当开发者的效果在没有 GPU 后端的设备上不起作用时。
技能链接
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-effects-and-shaders/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在 melonJS 中为我的角色精灵添加光晕效果。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能展示如何将内置效果附加到可渲染对象或相机上,并提醒着色器效果需要 GPU 后端,在 Canvas 回退模式下不会生效。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Post effects and custom shaders
melonJS ships a catalogue of post effects and a dual-language shader system. The recurring trap is that every shader path needs a GPU backend and degrades to nothing on Canvas rather than erroring. Blend modes are the exception — those work on all three renderers.
Applying an effect
Effects attach to a renderable or to a camera (for full-screen grading):
import { VignetteEffect, GlowEffect } from "melonjs";
sprite.addPostEffect(new GlowEffect(app.renderer));
app.viewport.addPostEffect(new VignetteEffect(app.renderer));
renderable.shader = … is deprecated since 19.2.0, and its setter destroys
whatever it replaces. Use addPostEffect / getPostEffect / removePostEffect.
A camera effect covers the HUD too
A camera's post-effect brackets the entire world draw — floating children included. So the obvious way to write a full-screen pass also washes over every HUD label in that world, which is rarely what you want.
To land a pass after the world but before the HUD, host the effect on a screen-filling floating renderable ordered below the HUD's z instead:
const quad = new Sprite(0, 0, { image: anyImage });
quad.anchorPoint.set(0, 0);
quad.scale(viewW / anyImage.width, viewH / anyImage.height);
quad.floating = true; // its own uv is now screen space
quad.blendMode = "additive";
quad.addPostEffect(myEffect);
world.addChild(quad, HUD_Z - 10); // 2D: below the labels. See the note.
Two things follow from the quad filling the frame: its own uv is screen space
(so a pass like this needs none of the screen_uv / screen_texture builtins),
and the incoming color is the quad's own texture, which a body that paints
from scratch can ignore entirely.
Depth here is easy to get backwards, and the rule FLIPS between the two sort
modes. Under the default sortOn = "z" a higher z draws later, i.e. on top,
so HUD_Z - 10 puts the pass under the labels as written above. Under
sortOn = "depth", which every Camera3d scene sets, a floating child's sort
key is pos.z * pos.z and the walk paints far to near, so the SMALLER
magnitude is nearer and draws on top — HUD_Z - 10 there puts the pass OVER
the labels, the opposite of the comment.
So in a 3D scene give the overlay a larger magnitude than the HUD, and give
the HUD a small one (melonjs-3d and melonjs-ui-and-text both say a HUD
wants a small depth, not the huge z that would put it on top in 2D).
Either way, verify it rather than reasoning about it: return a flat colour from the body for one frame and see what it tints.
A renderable that draws with PRIMITIVES has to ask to be captured
One effect is applied the cheap way: the renderable is drawn with the effect's
own program instead of being captured offscreen and post-processed, which costs
no render target. That is equivalent only when everything the renderable draws
is a textured quad. fillRect, strokeRect and renderer.fill(shape) go
through a batcher that never reads that shader, so one effect on such a
renderable would silently do nothing, while two would work, because a chain
always captures.
So a renderable of your own that draws with primitives says so:
class Bar extends Renderable {
constructor(x, y, w, h) {
super(x, y, w, h);
this.postEffectNeedsCapture = true; // ← or one effect is a no-op
}
draw(renderer) {
renderer.fillRect(this.pos.x, this.pos.y, this.width, this.height);
}
}
ProgressBar and Trail set it for you, and a sprite needs nothing: the flag
defaults to false and only ever changes the single-effect case.
Toggle with enabled, do not remove
removePostEffect() destroys the effect — it calls effect.destroy() and
frees GPU resources, so the instance cannot be re-added. Same for
clearPostEffects() and for reassigning the deprecated .shader.
// ✗ destroys it; re-adding later fails or forces a recompile
sprite.removePostEffect(effect);
// ✓
effect.enabled = false;
An effect with effect.shared === true opts out of auto-destroy because it is
reused across renderables.
Built-in effects
Twenty-three presets, every one constructed as new XEffect(renderer, options)
and every one shipping both a GLSL and a WGSL body, so they all run on either
GPU backend: VignetteEffect, ScanlineEffect (optional CRT curvature),
BloomEffect, GlowEffect, BlurEffect, `PixelateEffect
常见问题
为什么我的着色器效果不显示?
该技能说明,着色器效果需要 GPU 后端,在 Canvas 回退模式下会静默地不产生任何效果。
效果能只作用于屏幕的一部分吗?
该技能描述了一种方法,可以将全屏处理层放在世界之上、屏幕显示之下。
旧的着色器属性还能用吗?
不能。该技能说明它已被弃用,并推荐使用后处理效果的方法。
详情
作者的原始描述
Use this skill for post-processing effects, custom shaders, blend modes and colour grading in melonJS. Covers the built-in ShaderEffect presets, addPostEffect on renderables and cameras, writing a custom dual-language GLSL/WGSL effect, the screen_texture builtins, and why effects silently do nothing on the Canvas fallback. Triggers on: Tween, easing, animate an effect, ShaderEffect, addPostEffect, removePostEffect, getPostEffect, postEffectNeedsCapture, VignetteEffect, GlowEffect, BlurEffect, PixelateEffect, ScanlineEffect, shader, GLSL, WGSL, uniform, setUniform, setTexture, setTime, blendMod
repo: melonjs/melonJS
热度
- GitHub 星标
- 6.4k
- 排名
- #14201
- 许可证
- 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 游戏添加音效和音乐,包括音频设置、背景音轨、音量控制和空间声音。