All Projects → nick-paul → LightMask

nick-paul / LightMask

Licence: MIT license
A tiny 2D header-only flood-fill lighting engine

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to LightMask

awesome-zigbee
Curated List of ZigBee related stuff
Stars: ✭ 45 (-36.62%)
Mutual labels:  lighting
ecs
Build your own Game-Engine based on the Entity Component System concept in Golang.
Stars: ✭ 68 (-4.23%)
Mutual labels:  game-engine-2d
goldfish
Goldfish: a game created for the 7-day Roguelike 2017 challenge. Playable, winnable, losable. Features colorful and aesthetic maps, aquatic fauna and fishermen!
Stars: ✭ 28 (-60.56%)
Mutual labels:  roguelike
Tradfri-FHEM
A Module for the FHEM Home-Control Software which enables connectivity to the IKEA Trådfri gateway
Stars: ✭ 23 (-67.61%)
Mutual labels:  lighting
BakeManager
Simple bake manager for lightmapping in Unity
Stars: ✭ 68 (-4.23%)
Mutual labels:  lighting
android-3d-model-viewer
Android app to load 3D models in obj, stl, dae & gltf format using pure OpenGL ES 2.0. Published on Play Store https://play.google.com/store/apps/details?id=org.andresoviedo.dddmodel2
Stars: ✭ 150 (+111.27%)
Mutual labels:  lighting
rpgwizard
2D RPG Game creator
Stars: ✭ 37 (-47.89%)
Mutual labels:  game-engine-2d
Rogue-Collection
Retro Rogue Collection: Six versions of the classic Rogue game with customizable retro graphics. Now with Rogomatic support!
Stars: ✭ 75 (+5.63%)
Mutual labels:  roguelike
roguelike tutorial
RoguelikeDev Tutorial 2018
Stars: ✭ 33 (-53.52%)
Mutual labels:  roguelike
tradfri-flux
f.lux-like temp adjustments on IKEA trådfri 💡
Stars: ✭ 18 (-74.65%)
Mutual labels:  lighting
rogue.js
JavaScript porting of original Rogue source code using Emscripten
Stars: ✭ 33 (-53.52%)
Mutual labels:  roguelike
cavernos
Retro fantasy terminal for building DOS-era ASCII games, powered by WebAssembly
Stars: ✭ 32 (-54.93%)
Mutual labels:  roguelike
thelegendofericc
Single player roguelike tile-based game written in Java using LibGDX and Ashley
Stars: ✭ 22 (-69.01%)
Mutual labels:  roguelike
OverEngine
Tiny little game engine
Stars: ✭ 175 (+146.48%)
Mutual labels:  game-engine-2d
yiufcrawl
Unofficial preservationist fork of DCSS
Stars: ✭ 13 (-81.69%)
Mutual labels:  roguelike
tomenet
TomeNET is an online multiplayer roguelike role-playing game
Stars: ✭ 59 (-16.9%)
Mutual labels:  roguelike
runeHunter
A coffee break roguelike game written in JS. Explore the dungeon, collect 3 runes and escape alive.
Stars: ✭ 25 (-64.79%)
Mutual labels:  roguelike
eruption
Realtime RGB LED Driver for Linux
Stars: ✭ 140 (+97.18%)
Mutual labels:  lighting
Ockero
Ockero: Kotlin OpenGl Game Engine
Stars: ✭ 67 (-5.63%)
Mutual labels:  game-engine-2d
wglt
WebGL Terminal
Stars: ✭ 43 (-39.44%)
Mutual labels:  roguelike

LightMask

A 2d flood-fill lighting engine

Demo

Colors

Full-color lighting can be achieved by using a separate LightMask for each color channel or by adjusting the computations to use 3 channels instead of 1.

Running the demo

The demo requires SDL2 to run. The LightMask engine itself has no external dependencies.

git clone https://github.com/nick-paul/LightMask.git
cd LightMask
mkdir build
cd build
cmake ..
make
./demo

How to use

Initilize LightMask and variables

    // The lightmask itself
    LightMask lightmask(WIDTH, HEIGHT);
    // Intensity: Proportional to how far light spreads
    lightmask.setIntensity(40.0f);
    // Ambient: Ambient light (0.0f - 1.0f)
    lightmask.setAmbient(0.4f);

    // Vector representing wall opacities (1.0: solid, 0.0: clear)
    // Stored in a single dimensional vector
    // To set a wall value at (x,y) use walls[x + WIDTH * y] = ...
    std::vector<float> walls(WIDTH * HEIGHT, 1.0f);

Adding lights and computing the mask

    // Reset the light mask
    // Must be called every frame
    lightmask.reset();

    // All lights must be added between `reset()` and `computeMask()`
    // Add a light with given brightness at location (x,y)
    // brightness: 0.0 = no light, 1.0f = full light
    lightmask.addLight(x, y, brightness);

    // Compute the mask
    // Pass the `walls` vector to the compute function
    lightmask.computeMask(walls);

Rendering

Assume we are using the following to represent a Color object where each channel is a value from 0.0 to 1.0

    struct Color {
        float r;
        float g;
        float b;
    }

To compute the color of a tile at location (x,y) for rendering, multiply the color channels by the light mask at that location

    // Assume the call `map.getTileColor(x,y)`
    // gets the color of the map tile at location (x,y)
    Color color = map.getTileColor(x,y);

    // Multiply each of the channels by the light mask
    // 0.0: dark, 1.0: bright
    float tile_brightness = lightmask.mask[x + y * width];
    Color lighting_color(
        color.r * tile_brightness,
        color.g * tile_brightness,
        color.b * tile_brightness
    );

    // Render the tile with the new color
    // Assume `render()` takes an object to render and a color to render it
    // render(object, color)
    render(map.getTile(x, y), lighting_color);

Limitations

  • This was originally developed for a roguelike engine so framerate and performance were not a huge concern. If you have any performance improving ideas, submit an issue or a PR and I'd be happy to take a look.
  • Currently, the blur function creates a black border of unlit tiles on the outer edges of the map. To avoid this, don't use the blur function or make the lightmask slightly larger than the renderer size.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].