Report a Bug

SIGN IN!

Dynamic Lighting/ Shadow Casting


This plugin provides a simple shadow casting engine.
Plugin URL:  https://github.com/fourty2/ShadowImpact
More Information:  https://github.com/fourty2/ShadowImpact
Plugin Demo:  http://coldspace.henklein.com/plugin_demo/

Features

  • dynamic light sources for multiple entities
  • pulsating shine (torch style)
  • configurable colors including radial gradients
  • configurable light source offset
  • separate drawing methods for darkening layer and light layer

Discussion
There is a discussion thread on: http://impactjs.com/forums/code/lighting-shadow-casting-class

Notes
in v.0.30 the shadowGradient doesn't take any effect

Example

To use the example, copy and overwrite the example files into an existing Jump'n'Run Demo directory
I provided only the Jump'n'Run Demo JS files without the media files or the impact framework.

A working example is available at: http://coldspace.henklein.com/plugin_demo/

Installation

1. copy the plugins folder into your lib folder
2. register the plugin in your main.js:

  1. .requires(
  2. 'plugins.lights'
  3. )

3. register a LightManager instance in your main.js:

  1. MyGame = ig.Game.extend({
  2. lightManager: '',
  3. init: function() {
  4. this.lightManager = new ig.LightManager();
  5. }

4. add the light manager to the update cycle (in your main.js):

  1. update: function() {
  2. this.parent();
  3. this.lightManager.update();
  4. }

5. add the Lighting Draw calls to the impact.js lib/impact/game.js around the entitydrawing ( or somewhere else )

  1. draw: function() {
  2. ...
  3. this.lightManager.drawLightMap();
  4. this.drawEntities();
  5. this.lightManager.drawShadowMap();
  6. }

6. add your light sources

Usage

The Lighting Engine is Entity driven. That means, you're attaching a light source to an entity.
Lets add a light to our Player:

1. in your init function, you add the light source:

  1. init: function(x,y,settings) {
  2. ...
  3. this.light = ig.game.lightManager.addLight(this, {
  4. angle: 5,
  5. angleSpread: 125,
  6. radius: 80,
  7. color:'rgba(255,255,255,0.1)', // there is an extra shadowColor option
  8. useGradients: true, // false will use color/ shadowColor
  9. shadowGradientStart: 'rgba(0,0,0,0.1)', // 2-stop radial gradient at 0.0 and 1.0
  10. shadowGradientStop: 'rgba(0,0,0,0.1)',
  11. lightGradientStart: 'rgba(255,255,100,0.1)', // 2-stop radial gradient at 0.0 and 1.0
  12. lightGradientStop: 'rgba(0,0,0,0.6)',
  13. pulseFactor: 5,
  14. lightOffset: {x:0,y:0} // lightsource offset from the middle of the entity
  15. });
  16.  
  17. }

2. to kill a light, you can use the

  1. ig.game.lightManager.removeLight(light)
and
  1. ig.game.lightManager.removeLightByIndex(index)
methods.
3. that's it!
4. you can change any configuration attribute within game livecycle.
for example, if your player is in a flip-state, the starting angle has to flip also:

  1. update: function() {
  2. ...
  3. if (this.flip) {
  4. this.light.angle = 185;
  5. } else {
  6. this.light.angle = 5;
  7. }
  8. }

Changelog

v0.30
* rewrite of the canvas drawing method - no globalCompositeOperation anymore ( https://github.com/fourty2/ShadowImpact/issues/1 )
* rewrite of the raycasting "engine" - it's much more accurate

v0.20
* improved canvas drawing
* added gradients
* provided example within the repo

v0.11
* added removeLight method

v0.10
* initial commit


snooze
@empika: thanks :) i'll take a look at some other plugins for 'coding guidelines', and write an update :)
empika
Wow, very impressive! Definitely going to give this a go.
Would be nice if was made a plugin though rather than putting it in the /game directory.
   PLUGIN 
Contributed by:
snooze
View Profile
Category:Math/Physics
Updated:May 8, 2012
Version:0.30
Rating:
Your Rating (5)
Average Rating (5)
Ready to get to the point?

Your Email will remain private and is only used for good. We promise!


Please use only letters, numbers or underscores.

SIGN UP!