GithubHelp home page GithubHelp logo

pixijs / lights Goto Github PK

View Code? Open in Web Editor NEW
188.0 25.0 28.0 3.4 MB

Adds dynamic lighting via deferred shading to PixiJS

Home Page: https://pixijs.io/lights/docs/index.html

License: MIT License

TypeScript 100.00%

lights's Introduction

PixiJS Lights

Build CI npm version

A plugin that adds deferred lighting to PixiJS.

Note: This modules requires v7.0.0+ of PixiJS and v2.0.1+ of @pixi/layers.

Usage

You have to create three layers: one for sprites, one for their normals and one for lights. Sprites and normals are rendered to temporary RenderTexture, and lights have those two textures as an input.

// Get class references
import {Application, Sprite, Container, lights} from 'pixi.js';
import {Layer, Stage} from '@pixi/layers';
import {diffuseGroup, normalGroup, lightGroup, PointLight} from '@pixi/lights';

// Create new application
const app = new Application({
    backgroundColor: 0x000000, // Black is required!
    width: 800,
    height: 600
});
document.body.appendChild(app.view);

// Use the pixi-layers Stage instead of default Container
app.stage = new Stage();

// Add the background diffuse color
const diffuse = Sprite.fromImage('images/BGTextureTest.jpg');
diffuse.parentGroup = diffuseGroup;

// Add the background normal map
const normals = Sprite.fromImage('images/BGTextureNORM.jpg');
normals.parentGroup = normalGroup;

// Create the point light
const light = new PointLight(0xffffff, 1);
light.x = app.screen.width / 2;
light.y = app.screen.height / 2;

// Create a background container 
const background = new Container();
background.addChild(
    normals,
    diffuse,
    light
);

app.stage.addChild(
    // put all layers for deferred rendering of normals
    new Layer(diffuseGroup),
    new Layer(normalGroup),
    new Layer(lightGroup),
    // Add the lights and images
    background
);

Filters

If you want to use light shaders inside a filter, make sure its full-screen:

app.stage.filters = [new BlurFilter()];
app.stage.filterArea = app.screen;

Vanilla JS

Navigate pixi-lights npm package, take dist/pixi-lights.js file.

<script src='lib/pixi.js'></script>
<script src='lib/pixi-lights.umd.js'></script>

all classes can be accessed through PIXI.lights global namespace.

Building

You normally don't need to build this module, you can just download a release from the releases page.

Then you can install dependencies and build:

npm i && npm run build

That will output the built distributables to ./lib and ./dist.

Roadmap

  1. More lighting types, left are:
  • Spot lights
  • Hemisphere lights
  • Area lights
  1. Add dynamic shadows
  2. Write tests!

lights's People

Contributors

bigtimebuddy avatar englercj avatar ivanpopelyshev avatar msqaddura avatar sjlynch avatar vikpe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lights's Issues

no clearBeforeRender

Hello,

Renderer does not clear its content before rendering, even if force clearBeforeRenderboolean to true. Otherwise, this is great start, good work guys.

Regards,

pixiLoader support texturePacker "normal_map"

New update from texturepacker allow now support for normal Map metaTag.
A pixi guys should make a update in the pixiloader for support this..
pixijs/layers#30
Thank

Version 4.7.0 (2018-05-09)
Features
Added support for FaceBook AR Studio
> Added normal_map reference to json file's meta section (Pixi, Phaser, Generic)
Enabled support for rotated sprites in Phaser

pixi v4 compatibility

Currently getting Uncaught TypeError: Cannot read property 'registerPlugin' of undefined with latest dev branch of pixi.

Can't find variable: PIXI

typescript project
using pixi-js 5.0.3 and importing this way
import * as PIXI from 'pixi.js'
I was getting an error where PointLike wasn't found and in the browser I get a compilation error where lights can't find the PIXI package.

How would I go about limiting the max brightness when multiple lights overlap?

I used to use the old lighting style with pixi-layers a little over a year ago. I recently came upon this library and am trying to update my system to use it. It works great so far however I was wondering if there was a way to make it when multiple lights overlap so its not blindingly bright?

Kinda like some kind of limiter to hold back the combined power levels of the lights.

I'm not opposed to getting my hands dirty (I have many personalized pixi addons and build scripts), I just have no idea where to start or what kinds of terms to search to figure out if its even possible.

Shadows

Hey,
I don't think this is the proper place to send a message like this, but I didn't find a way of sending a personal message to anyone.

Anyhow, I have been a big fan of this project since I first saw it, but I also really wanted some shadows for my own project. Eventually with a lot of effort I managed to create a system for them myself, and made it public. I was wondering if you would mind having a look at it, possibly add a link in your project if you find it good enough.

I am a complete noob when it comes to opengl/webgl, so any help/feedback to make the system more efficient would also be appreciated. At the moment it is very intensive, and I will definitely have a great number of options in my game so less powerful computers can turn down the intensity.

Here is the link: https://github.com/TarVK/pixi-shadows
Thanks for your time.

Outline radius too sharpen, a way for blur ?

hi, Is there a way to get more smoother radius?
Like a kind of blur filter ?
I find the outline of the radius a little too sharpen.
lightradius

i also try to add a blur filter but not work, the light disappear.
Can be add a property called lens for soften the outline of the ray?
Is there currently a way to do it?
thank

How to implement the shadow like FOV ?

I want to create a game with FOV (Field of vision/view ) and Fog Of War.

Like http://christmasexperiments.com/experiments/11 .
and
http://www.unwinnable.com/wp-content/uploads/2013/06/Monaco.jpg
https://7yrl.files.wordpress.com/2013/12/helives.png
http://www.playchilla.com/wp-content/uploads/2012/12/2d-shadows.png
http://i.imgur.com/B8g2o7b.jpg
http://lab.wx3.com/temp/screenshot01.jpg

Could pixi-light help me ? How to implement ?

Thanks

P.S. :
I know the algorithm of FOV , and I could implement it on Canvas2D with hard-edge.
like this https://camo.githubusercontent.com/88bfed3e14132b417d9f5c7d01bca0b9a803abf9/687474703a2f2f692e696d6775722e636f6d2f335371706942542e6a7067 .

But I don't know how to create a FOV with soft-edges and dynamic 2d lighting effect by Pixi.js

Light biased towards two sides?

I tried the demo out locally and noticed that with an ambient light in the scene, the right side and bottom side of the rectangles are lit up (kind of neutral) when the point light is on the opposite sides.

I turned OFF directionalLight to be sure that wasn't it and the lighting issue remained (also directionalLight doesn't seem to do anything).

I boosted the ambient light brightness to 1.0 and that made the issue more apparent. After removing both ambient and directional lights the behavior is how I would expect (dark edges are always on opposite side of point light).

Is this a bug? This is problematic for my game because pink sided sprites (right-side and/or bottom side) do not darken when facing away from the point light. I'm going to try to hunt this issue down myself, but wanted to see if this is expected functionality or not. It's almost like AmbientLight washes out the red normal values.

stats block events

I've just downloaded the repo, and tried to play with it, I've noticed that stats blocks events, thus, I can't add new lights or move the light when mouse move..

Fixed it by commenting those lines only in index.html:

stats.domElement.style.right = '0px';
stats.domElement.style.bottom = '0px';

Spotlight Polygon (suggest)

It would be great to see another type of light.
A polygonal light for emulate a spot light.

"spot" : Spotlight
A directional light with with range cone.
var shape = new PIXI.Polygon (points,...)

If that can give a good idea.
It's a suggestion.

PIXI.lights.Spotlight({
        type: "Spotlight",        // Light type,
        diffuse:  [1.0,1.0,1.0],  // Diffuse Light color.
        specular: [0.1,0.1,0.1],  // Specular Light color.
        intensity: 1.0,           // Light intensity.
        distance: 10.0,           // Maximum light distance for point lights.
        cutoff: 60.0,             // For spotlight types this is the cut-off angle of the cone.
    });

other blend modes

It would be good to have other blend modes available, like multiply or overlay.

light clipped to a scene scale ?

If we add a light to a scene.
And resize the scene with scale.
The light does not follow the zoom scale.

What would be the best approach?
Do I have to do it manually by multiplying each value by the scale of the scene.
Or there is already something native planned and that will consume less resource ?
ezgif com-optimize
If i need to myself, where the best place to add a update in the module?

Is it possible to show different images for different viewports?

// Add the background diffuse color
const diffuse = Sprite.fromImage('images/BGTextureTest.jpg');
diffuse.parentGroup = diffuseGroup;

// Add the background normal map
const normals = Sprite.fromImage('images/BGTextureNORM.jpg');
normals.parentGroup = normalGroup;

Is there a possibility to change/overwrite the image for "diffuse" and "normals" when user changes the viewport/ resizes the browser window?
I want to show different images for mobile, tablet and desktop.

renderer.resize does not resize diffuseTexture and normalsTexture

Hi there,

just played a bit with pixi-lights but after calling the resize method of the renderer it looks like this:

before:
bildschirmfoto 2015-08-23 um 14 03 09

after:
bildschirmfoto 2015-08-23 um 14 03 20

I fixed the problem by resizing diffuseTextureand normalsTexture.

resize: function (width, height)
    {
      PIXI.WebGLRenderer.prototype.resize.call(this, width, height);

      if (this.diffuseTexture && this.normalsTexture) {

        this.diffuseTexture.resize(width, height);
        this.normalsTexture.resize(width, height);
      }
    },

I don't know exactly if this solves the problem entirely and it surely could be made better than checking for this.diffuseTexture && this.normalsTexture so there is no pull request.

i did the check because WebGLRenderer.prototype._initContext calls the resize method before this.diffuseTexture and this.normalsTexture are instantiated.

greeting from berlin,
tststs

Can't mix lighting and sorting?

Apologies if I'm just missing something obvious due to being new at PIXI. Also apologies because I edit a lot :-/

As I understand it...

  • To use lighting, the main (diffuse) sprite must be in the diffuseGroup.
  • To use layer sorting, the same sprite must be on a custom-made group with the sort handler.

Currently I can have one of these lines uncommented and get layer sorting; or I can uncomment the other line and get lighting. Is there another pattern I'm missing?

//    sprite.parentGroup = PIXI.lights.diffuseGroup;  // uncomment and lighting works
//    sprite.parentGroup = entityGroup; // uncomment and layer sorting works

I tried this in order to get both features:

PIXI.lights.diffuseGroup.on('sort', (sprite) => { sprite.zOrder = -sprite.y; })

But no sorting occurs; I then also added a sort to the normal, also no change. I've tried adding the container holding the diffuse/normal pair, to the entity group, but still no sorting.

Thank you very much for these libraries and for any help past either my blindness or whatever's going on here :)

EDIT: Probably found the answer already: https://pixijs.io/examples/#/layers/normals-drag.js

will close asap after confirming.

polygonal/graphics/sprite lights

It would be good to have lights in arbitrary shapes (as drawn into a graphics object, for example, or in the shape of a sprite or group)

v5

Any news or plan in the near future to port this with pixiv5 engine ?
i just ask

Using for pixel art: low res lighting?

Hi there!

I'm using pixi-lights for a retro game with pixel art. The original low res image is loaded with PIXI.SCALE_MODES.NEAREST and then scaled up. It looks pretty good. However, the lighting cast onto the sprite is soft and high res. I want to maintain that low res pixel art effect. For example:

image

That's 4 pixels from the original sprite, but scaled up (5x) and with the lighting effect, it's lost that low res effect to a degree. I'm not sure if I'm explaining this very well.

So how would I go about fixing this?

Thanks so much!

Jagged edges (aliasing) on graphics objects

https://i.imgur.com/wvEmB1N.jpg

I'm using PIXI.Graphics to draw basic shapes. I'm drawing them twice, once in the correct color and then adding that to PIXI.lights.diffuseGroup, and then again in color 0x7f7fff to create a flat normal, and then adding that to PIXI.lights.normalGroup. First of all is this the correct way to get flat, colored shapes rendering?

Second, see the image I've linked above, the shapes are badly aliased. This goes away when I render the scene normally without pixi-lights. Is there any way to fix this? Thanks!

Any roadmap on this module? How can I help?

Hey @englercj !

I really appreciate your work on Pixi and pixi-lights! I would like to know if you have any agenda to push this module on NPM? Also how can I help to speed the development considering I have only a basic knowledge of WebGL/OpenGL (I can understand your code but will never be able to do it myself, especially if we talk about optimization x) )?

I will start to use pixi-lights in my project (with a development flag in the meantime before the first release) and make feedbacks if needed, for the moment this is the best I can do :) don't hesitate if you need some support with docs generation, build process, showcase site or stuff like that.

changing position of container doesn't work

setting the topmost container's position doesn't have any effect. For example, modifying the stage variable from your example file like so:

stage.position.set(-50, -50);

has no effect.

Ambient light seems to come from the bottom right?

It seems that when I use a bright ambient light, the light is actually coming from the bottom right of the screen. I'm doing some funny translation of the stage to make (0,0) the bottom left, perhaps this is why?

How do I change the position of the ambient light? I've looked at the indices and vertices arrays but I don't see how what's in them relates to on screen co-ordinates.

Maybe I'm misunderstanding ambient light? All I'm looking for is a way to lighten the whole scene evenly so it's not totally black without any other lights.

Thanks for all the help and for the engine!

No longer working in Windows 7?

Interesting, within the last 3 months this feature stopped working in Windows 7. Works fine in windows 10. Tested in both chrome and firefox.

Windows 7 now just shows the normal map as the graphic and no light shading.

Anyone else have this issue?

npm build:dev ?

Am sorry am new with npm since hour, it my first try.
i succeed build a version but i get only de minified version ?
how i can get a dev version?
i try npm run build:dev but it now seem work
stack Error: missing script: build:dev
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.