melonjs-lighting
Fungsinya
Panduan ini menjelaskan cara menambahkan lapisan ambient gelap dan lampu yang menembus kegelapan itu, serta cara sprite dengan normal map bereaksi. Panduan ini untuk pengembang game yang membuat adegan 2D yang bersuasana atau diterangi.
Kapan dipakai
- βSaat seorang pengembang menginginkan ruangan gelap dengan obor atau lampu dalam game melonJS.
- βSaat seorang pengembang menginginkan sorotan specular pada sprite yang memiliki normal map.
- βSaat cahaya seorang pengembang menghilang setelah beralih ke kamera 3D.
Link skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-lighting/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βBuat level melonJS saya berwarna biru gelap dengan cahaya hangat yang mengikuti lampu di ruangan.β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Skill meminta AI mengatur warna ambient panggung untuk kegelapan, lalu menambahkan objek cahaya pada posisi yang dipilih dengan warna dan ukuran tertentu.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.
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
Pertanyaan umum
Mengapa shading per piksel tidak berfungsi di semua tempat?
Skill menyatakan shading normal-mapped memerlukan backend GPU, sedangkan glow dan pemotongan berfungsi di semua backend.
Bisakah cahaya diregangkan?
Ya. Memberi radius vertikal nilai yang berbeda membuat cahaya berbentuk elips.
Apakah pencahayaan berfungsi dalam 3D?
Tidak. Skill menyatakan cahaya 2D tidak bertahan saat memakai kamera 3D.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 6.4k
- Peringkat
- #14205
- Lisensi
- MIT
- Diperbarui
- 2/11/2026
Komentar
- β¦
Skill lain dari repositori ini
melonjs-20-migration
6.4kMembantu agent Anda menulis kode game melonJS versi 20 dan menghindari metode yang usang dari versi lama, seperti startup async yang wajib.
melonjs-3d-assets
6.4kMembantu agent Anda memuat model 3D seperti berkas glTF, GLB, dan OBJ ke dalam game browser melonJS, dengan opsi skala dan pencahayaan.
melonjs-3d
6.4kMembantu agent Anda membangun adegan 3D atau 2.5D dalam game browser melonJS, mencakup kamera, mesh, lampu, dan kedalaman, dengan aturan sumbu mesin.
melonjs-audio
6.4kMembantu agent Anda menambahkan efek suara dan musik ke game melonJS, termasuk pengaturan audio, musik latar, kontrol volume, dan suara spasial.
Skill serupa
diffs
394kMemungkinkan agent Anda menghasilkan diff perubahan yang nyata dan dapat dibagikan sebagai tautan viewer atau file gambar, bukan ringkasan yang ditulis manual.
coding-agent
394kMembantu agent Anda menyerahkan pekerjaan pemrograman besar ke Codex, Claude Code, atau OpenCode di latar belakang, lalu memeriksa kemajuannya.
gh-issues
394kMembantu agent Anda menemukan issue GitHub, memilih yang dikerjakan, dan membuka pull request perbaikan, termasuk tindak lanjut komentar tinjauan.