Skip to content
Skillpedia
General coding6.4k Β· #14205

melonjs-lighting

What it does

It explains how to add a dark ambient layer and lights that cut through it, plus how normal-mapped sprites react. It is for game developers making moody or lit 2D scenes.

When to use it

  • βœ“When a developer wants a dark room with a torch or lamp light in a melonJS game.
  • βœ“When a developer wants specular highlights on sprites that have normal maps.
  • βœ“When a developer's light disappears after switching to a 3D camera.

Skill link

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-lighting/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-lighting/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œMake my melonJS level dark blue with a warm light that follows the lamp in the room.”

5 new skills a week. 1 minute to read.

How it works

The skill tells the AI to set the stage ambient color for darkness, then add light objects at chosen positions with a color and size.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

2D dynamic lighting

Light2d is 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
  • 1 thins the darkness straight from the centre to the edge, the same falloff as the glow; in between, a fully lit core of 1 - softness of 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 drawLighting on the stage and skip that camera.
  • To keep one light out of a camera, put it, or a container it is in, on a visibilityLayer the camera's cullMask leaves out: there it neither glows, nor cuts the darkness, nor lights normal-mapped sprites (see melonjs-camera-and-drawing). This is for a Light2d: a Light3d lights 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

FAQ

Why does per-pixel shading not work everywhere?

The skill says normal-mapped shading needs a GPU backend, while the glow and cutout work on every backend.

Can a light be stretched?

Yes. Giving the vertical radius a different value makes an elliptical light.

Does lighting work in 3D?

No. The skill says the 2D light does not survive a 3D camera.

Details

Original description by the author

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

Popularity

GitHub stars
6.4k
Ranking position
#14205
License
MIT
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills