melonjs-lighting
Skill này làm gì
Skill giải thích cách thêm một lớp môi trường tối và các đèn xuyên qua nó, cùng cách sprite có normal map phản ứng. Dành cho lập trình viên trò chơi làm cảnh 2D tối hoặc có ánh sáng.
Khi nào nên dùng
- ✓Khi lập trình viên muốn một căn phòng tối có đèn đuốc hoặc đèn bàn trong trò chơi melonJS.
- ✓Khi lập trình viên muốn điểm sáng specular trên sprite có normal map.
- ✓Khi đèn của lập trình viên biến mất sau khi chuyển sang camera 3D.
Liên kết skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-lighting/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm cho level melonJS của tôi có màu nền xanh đậm, với một đèn ấm đi theo chiếc đèn trong phòng.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill yêu cầu AI đặt màu môi trường của sân khấu để tạo bóng tối, rồi thêm các đối tượng đèn tại vị trí đã chọn với màu sắc và kích thước.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
2D dynamic lighting
Light2dis a world renderable that cuts through a stage's ambient darkness. The glow and the cutout work on every backend; only per-pixel normal-map shading needs a GPU one. It is 2D-only.
The model: ambient darkness plus cutouts
The Stage holds an ambient colour — a dark sheet painted over the scene after
the world is drawn — and each active Light2d punches a hole in it at its
visible area. The lights themselves are drawn additively on top.
class PlayScreen extends Stage {
onResetEvent(app) {
this.ambientLight.parseCSS("#1a1a2e"); // the darkness
app.world.addChild(new Light2d(x, y, radiusX, radiusY, "#ffddaa", 0.9));
}
}
The constructor is Light2d(x, y, radiusX, radiusY = radiusX, color = "#FFF", intensity = 0.7), and pos is the light's centre — its anchorPoint is
(0.5, 0.5), and centerX / centerY are overridden to return pos directly.
Give radiusY a different value for a stretched, elliptical light.
A Light2d added to the world registers itself with the stage's ambient
overlay on activate, so you add it like any other renderable and it takes part
in the lighting pass. It draws additively (blendMode = "lighter"); set
light.illuminationOnly = true to suppress the visible glow while still lighting
normal-mapped sprites.
Stage.ambientLight defaults to fully transparent black, and drawLighting()
returns immediately while its alpha is 0 — so with no ambient colour set there
is nothing to cut through and the lights just look like soft additive blobs.
Soft edges: softness
A light's hole in the darkness is hard-edged by default: full light inside its
radius, full darkness outside. light.softness (0 to 1) fades it instead:
const torch = new Light2d(x, y, 220, 220, "#ffb46a", 0.4);
torch.softness = 0.8; // lit core of 20% of the radius, then fading out
1thins the darkness straight from the centre to the edge, the same falloff as the glow; in between, a fully lit core of1 - softnessof the radius.- Hard and soft lights mix in one scene. Where lights overlap, what is left of the darkness is what each leaves, multiplied, so two half-lights make three quarters light.
- The hole is the light's size. For an opening wider than the glow (a torch whose
light reaches further than its flame's halo), add a second, bigger light with
illuminationOnly = true: it cuts the darkness and draws nothing. - Up to 32 lights in view are cut by one soft pass; lights outside the view do not count. A scene with no soft light draws exactly as before.
- To keep a camera out of the darkness (a minimap showing the level lit),
override
drawLightingon the stage and skip that camera. - To keep one light out of a camera, put it, or a container it is in, on a
visibilityLayerthe camera'scullMaskleaves out: there it neither glows, nor cuts the darkness, nor lights normal-mapped sprites (seemelonjs-camera-and-drawing). This is for aLight2d: aLight3dlights the meshes of every camera.
Multiple lights
Add as many as you need — the glow quads and the ambient cutouts accumulate with no fixed cap.
The normal-map pipeline is capped: lights are packed into a std140 uniform
block sized for MAX_LIGHTS = 32, and anything past the 32nd is silently dropped
from the per-pixel shading (it still draws its glow and punches its cutout). The
cap is an allocation, not a budget — the fragment loop still runs once per pixel
per live light.
Lights are positioned like any renderable, so they can follow an entity by
updating pos, or be added as children of one.
Per-pixel lighting with normal maps
For sprites that respond to light direction rather than just brightness, supply a normal map alongside the diffuse texture:
await loader.preload([
{ name: "hero", type: "image", src: "data/img/hero.png" },
{ name: "hero_n", type: "image", src: "data/img/hero_n.png" },
]);
const hero = new Sprite(x, y, {
image: "hero",
normalMap: "hero_n", // ← enables per-pixel lighting on this sprite
framewidth: 32, frameheight: 32,
});
normalMap takes a loader key, or any image-like source (HTMLImageElement,
canvas, ImageBitmap, a Texture2d). Assigning anything else — including an
HTMLVideoElement, which would freeze on frame 0 — throws a TypeError.
Normal maps are typically authored in SpriteIlluminator. The sidecar pattern above — one
Câu hỏi thường gặp
Vì sao đổ bóng từng pixel không hoạt động ở mọi nơi?
Skill nói đổ bóng normal-mapped cần backend GPU, còn phát sáng và cắt mặt nạ hoạt động trên mọi backend.
Đèn có thể bị kéo giãn không?
Có. Đặt bán kính dọc khác với bán kính ngang sẽ tạo đèn hình elip.
Ánh sáng có hoạt động trong 3D không?
Không. Skill nói đèn 2D không còn hiệu lực khi dùng camera 3D.
Chi tiết
Mô tả gốc của tác giả
Use this skill for 2D dynamic lighting in melonJS — Light2d, ambient light on a Stage, multiple lights, and per-pixel normal-mapped sprites authored in SpriteIlluminator. Covers specular highlights via shininess, the ambient-cutout model, which parts need a GPU backend, and why Light2d does not survive a Camera3d. Triggers on: Light2d, specular, shininess, highlight, glint, lighting, ambientLight, ambientLightingColor, normalMap, SpriteIlluminator, per-pixel lighting, drawLight, torch, glow, darkness.
repo: melonjs/melonJS
Độ phổ biến
- Sao GitHub
- 6.4k
- Vị trí xếp hạng
- #14205
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
melonjs-20-migration
6.4kGiúp agent của bạn viết mã trò chơi melonJS cho phiên bản 20 và tránh các phương thức cũ, như bước khởi động bất đồng bộ bắt buộc.
melonjs-3d-assets
6.4kGiúp agent của bạn tải mô hình 3D như file glTF, GLB và OBJ vào trò chơi trình duyệt melonJS, kèm tùy chọn tỷ lệ và ánh sáng.
melonjs-3d
6.4kGiúp agent của bạn xây dựng cảnh 3D hoặc 2.5D trong trò chơi trình duyệt melonJS, gồm camera, lưới, đèn và độ sâu, theo quy tắc trục của công cụ.
melonjs-audio
6.4kGiúp agent của bạn thêm hiệu ứng âm thanh và nhạc nền vào trò chơi melonJS, gồm thiết lập âm thanh, nhạc nền, điều chỉnh âm lượng và âm thanh không gian.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.