LittleJS 1.28.0

LightSystem

LightSystemPlugin

LightSystemPlugin

  • Owns the offscreen lightmap texture, falloff/composite shaders, and the per-frame render pass that multiplies the lightmap onto the WebGL scene
  • The composite is MULTIPLICATIVE: unlit areas get the ambient color, lit areas show the scene tinted by the accumulated light color. So you should draw your world at full brightness — the lightmap handles the darkening.

Constructor

new LightSystemPlugin(textureSize, ambientColor)lightSystem.js:51

Create the global light system plugin

textureSize Vector2 optional

Size of the lightmap texture (defaults to following mainCanvasSize, which is css pixels, so the lightmap is not scaled by canvasPixelRatio; pass mainCanvasSize.scale(getCanvasPixelRatio()) for a full resolution lightmap)

ambientColor Color optional

Color applied to unlit areas of the scene (defaults to BLACK = pitch dark). Set a small RGB like rgb(0.1,0.1,0.15) for a faint "moonlight" baseline so unlit areas aren't fully black.

// simplest usage
new LightSystemPlugin();

Members

Methods

Members

ambientColor : ColorlightSystem.js:71

Baseline color applied to unlit areas of the scene. Defaults to BLACK (pitch dark). Set to a small RGB for a faint ambient. The lightmap is cleared to this color each frame, then lights add on top, then the result multiplies the scene.

backgroundMap : WebGLTexture|undefinedlightSystem.js:118

The background map, objects with castBackgroundShadow drawn black on white, the shadow map's size and place, made by a directional light when something casts into it and kept while the light is, read only

compositeShader : WebGLProgram|undefinedlightSystem.js:85

Shader for compositing the lightmap over the main scene

compositeVAO : WebGLVertexArrayObject|undefinedlightSystem.js:91

Vertex array object for the composite shader

directionalLight : DirectionalLight|undefinedlightSystem.js:146

The scene's directional light, a sun, or undefined, read only

directionalPrograms : Object<string, (WebGLProgram|WebGLVertexArrayObject)>lightSystem.js:161
directionalTexture : WebGLTexture|undefinedlightSystem.js:153

The directional light as built this frame, white where it reaches, over the shadow map's area, read only

directionalTextureA : WebGLTexture|undefinedlightSystem.js:155
directionalTextureB : WebGLTexture|undefinedlightSystem.js:157
directionalTextureSize : numberlightSystem.js:149

Pixels across the square textures a directional light is built in, covering the shadow map's area; larger is sharper and slower; a power of two, as shadowMapSize, so their texels line up

emissivePass : booleanlightSystem.js:111

True while emissive objects draw into the lightmap, read only, see EngineObject.emissive

enabled : booleanlightSystem.js:68

When false, the render pass is skipped entirely

lightShader : WebGLProgram|undefinedlightSystem.js:82

Shader for drawing per-Light falloff blobs into the lightmap

lightVAO : WebGLVertexArrayObject|undefinedlightSystem.js:88

Vertex array object for the light shader

shadowContext : OffscreenCanvasRenderingContext2D|undefinedlightSystem.js:139

Where Canvas2D draws go during the shadow and emissive passes, a 1x1 canvas, so text in a render() is not drawn twice

shadowCopyShader : WebGLProgram|undefinedlightSystem.js:127

Copies the shadow map around a light into its texture

shadowCopyVAO : WebGLVertexArrayObject|undefinedlightSystem.js:133

Vertex array object for the copy shader

shadowMap : WebGLTexture|undefinedlightSystem.js:114

The shadow map, casters drawn black on white around the camera, read only

shadowMapOrigin : Vector2lightSystem.js:141

World position of the shadow map's bottom left corner, set each shadow pass

shadowMapScale : numberlightSystem.js:98

How many times the larger side of the view the shadow map covers, so casters just off screen still cast in; raise it when lights reach further than a view past the screen

shadowMapSize : numberlightSystem.js:96

Pixels across the square shadow map, made again when changed

shadowMapWorldSize : numberlightSystem.js:143

World size the shadow map covers, set each shadow pass

shadowPass : booleanlightSystem.js:109

True while the shadow pass runs, read only, so a render() can skip parts that should not cast

shadowPassCount : numberlightSystem.js:103

Stretch passes per shadow casting light, fewer is cheaper and shorter shadows

shadows : booleanlightSystem.js:94

Cast shadows: every object draws black into a shadow map once a frame and each light's rays stop at them; off by default and free when off

shadowSoftness : numberlightSystem.js:107

How much light bleeds into a caster's near side, 0 for hard edged casters, 1 for most; the bleed reaches further in under a bigger light, so a thin wall under a big one lets some through, lower it for those

shadowStretchShader : WebGLProgram|undefinedlightSystem.js:130

One stretch pass of a light's shadow texture

shadowStretchVAO : WebGLVertexArrayObject|undefinedlightSystem.js:136

Vertex array object for the stretch shader

shadowTextureA : WebGLTexture|undefinedlightSystem.js:121

One of the two textures each light's shadow is built in

shadowTextureB : WebGLTexture|undefinedlightSystem.js:124

The other

shadowTextureSize : numberlightSystem.js:101

Pixels across each light's own shadow texture, made again when changed; larger is sharper, and a gap between casters narrower than about 4*radius/shadowTextureSize world units closes

texture : WebGLTexture|undefinedlightSystem.js:79

The lightmap texture

Size of the lightmap texture, follows mainCanvasSize (css pixels, so it is not scaled by canvasPixelRatio) unless a size was passed

textureSizeAuto : booleanlightSystem.js:75

True when no size was passed, so the lightmap follows mainCanvasSize

Methods

clampTextureSizes()lightSystem.js:908

Bring the sizes of the textures it makes down to what the device can make: shadowMapSize, shadowTextureSize and a textureSize given by hand; called before they are made, so a size too big falls back instead of failing

drawLight(light)lightSystem.js:925

Draw a single Light's falloff blob into the currently bound lightmap. Called by Light.renderLight() during the plugin's render pass.

light Light
renderLightShadow(light)lightSystem.js:980

Build a light's shadow texture from the shadow map: the map around the light, its casters stretched away from the light a little further each pass with light bled into their near sides. Leaves the result on texture unit 1 and the lightmap bound again. Called by drawLight.

light Light
setShadowTransparent(transparent)lightSystem.js:1036

In the shadow pass, let the draws that follow keep their color in the shadow map, so light passing through them is tinted instead of blocked: a stained glass window, colored smoke. Any draw blocks light by its alpha, so a fading sprite casts a fading shadow; this keeps the color as well. It covers what was drawn under it in the map as any draw does, so glass drawn after a wall cuts a tinted window in the wall's shadow, and a wall drawn after the glass covers it. Does nothing outside the pass, so a render() can call it around those draws unconditionally; set it back to false after them.

transparent boolean optional, default true