{"id":15722439,"url":"https://github.com/defold/sample-lights-and-shadows","last_synced_at":"2025-10-18T17:02:35.121Z","repository":{"id":44597564,"uuid":"388603503","full_name":"defold/sample-lights-and-shadows","owner":"defold","description":"Example of how to achieve pixel perfect shadows in a 2D view","archived":false,"fork":false,"pushed_at":"2024-09-30T09:13:46.000Z","size":822,"stargazers_count":33,"open_issues_count":2,"forks_count":5,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-01-30T15:24:50.378Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/defold.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-22T21:37:45.000Z","updated_at":"2024-10-08T13:09:43.000Z","dependencies_parsed_at":"2022-09-17T07:22:01.769Z","dependency_job_id":null,"html_url":"https://github.com/defold/sample-lights-and-shadows","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-lights-and-shadows","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-lights-and-shadows/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-lights-and-shadows/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defold%2Fsample-lights-and-shadows/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/defold","download_url":"https://codeload.github.com/defold/sample-lights-and-shadows/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237267955,"owners_count":19282317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T22:07:40.971Z","updated_at":"2025-10-18T17:02:35.032Z","avatar_url":"https://github.com/defold.png","language":"Lua","funding_links":[],"categories":["Libraries"],"sub_categories":["Programming Language"],"readme":"# Lights and Shadows\nExample of how to achieve pixel perfect shadows in a 2D view. The sample is based on the following sources:\n\n* 2D Pixel Perfect Shadows LIBGDX example: https://github.com/mattdesl/lwjgl-basics/wiki/2D-Pixel-Perfect-Shadows\n* Adapted to Defold with technical discussion by d954mas: https://forum.defold.com/t/problem-with-shader/3808\n* Final Defold sample project by Sayuris1: https://github.com/Sayuris1/2d_light_defold\n\n\n## Installation\nYou can use this solution in your own project by adding this this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:\n\nhttps://github.com/defold/sample-lights-and-shadows/archive/master.zip\n\n\n## Usage\nThe main components of this light and shadow casting example are:\n\n* `lights/lightsource.script` - Attach this script to any game object that should act as a lightsource. Modify the exposed script properties to control the light properties:\n   * `radius` [number] - Radius of the lightsource in pixels\n   * `color` [vector4] - Color of the lightsource (RGBA)\n   * `arc_angle` [number] - Arc angle of the lightsource. Can be used to generate a cone of light up to an arc angle of 180 degrees. Anything above 180 degrees will result in a full circle.\n   * `static` [boolean] - Use this for static lights that do not move or rotate to skip updates of light position and rotation each frame.\n   * `falloff` [number] - This controls the light intensity. With a falloff of 1.0 the light intensity gradually decreases until there is no light right at the edge. With a falloff of 2 the light intensity decreases twice as fast.\n   * `enabled` [boolean] - Set to false to disable the light.\n* `lights/render/light_quad.go` - A game object with a basic model quad, used as a render target when drawing lights and shadows.\n* `lights/render/lights.render` and `lights/render/lights.render_script` - The render file and render script used when drawing lights and shadows (and also all of the standard Defold components such as sprites, particles, tilemaps etc)\n* `lights/materials/light_occluder_*.material` - Materials to use for sprite, tilemaps and other components that should occlude light and cast shadows.\n\n\n### Step 1 - Render script\nOpen **game.project** and scroll down to `Bootstrap` and change Render file to `lights/render/lights.render`. This render script works like the default render script with the addition of also drawing lights and shadows.\n\n![](/docs/add_render_file_to_bootstrap.png)\n\n\n### Step 2 - Add light quad\nAdd the `lights/render/light_quad.go` to a collection where lights and shadows should be calculated.\n\n![](/docs/add_light_quad.png)\n\n\n### Step 3 - Add lightsources\nAttach the `lights/lightsource.script` to any game object that should act as a lightsource.\n\n![](/docs/add_lightsource.png)\n\nConfigure the lightsource properties to your liking.\n\n![](/docs/configure_lightsource.png)\n\n\n### Step 4 - Add light occluders\nChange the material for any component that should cast shadows when lit by a lightsource. Select a material from `lights/materials/` matching the component type casting shadows.\n\n![](/docs/configure_light_occluder.png)\n\n\n## Example\nThere is an example included in this repository. It sets up a few lightsources and occluders and lets you move around the scene.\n\n![](/docs/example.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefold%2Fsample-lights-and-shadows","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefold%2Fsample-lights-and-shadows","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefold%2Fsample-lights-and-shadows/lists"}