Giter Site home page Giter Site logo

oli414 / pixi-heaven Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pixijs/pixi-heaven

0.0 2.0 0.0 415 KB

**This fork is a hack to implement hue shifting. Please never use this.** PIXI.js advanced color modes, sprites, meshes.

License: MIT License

JavaScript 1.35% TypeScript 98.65%

pixi-heaven's Introduction

pixi-heaven

This is heaven for sprites. Want to color them better? Wanna use advanced colors? Its all here!

Examples are in their own repo.

Done:

  • Advanced color modes
  • Polygon packing
  • Mesh batching

Be careful!

It can affect performance. Though what is performance, when you game looks like s@#$? Spawn fewer objects but make it prettier!

Integration with pixi-spine

pixi-spine is optional, however it is REQUIRED by typescript definitions.

Two ways:

  1. <///reference types="pixi-spine.d.ts"/>
  2. Use pixi-spine.d.ts file from stubs folder.

In any case, put both pixi.js and pixi-spine.js before you include heaven in your build.

How to use

Just add pixi-heaven.js file in your build.

var sprite = new PIXI.heaven.Sprite();
// good old sprite tint
sprite.color.setLight(0.5, 1.0, 0.5);

// dark tint - new feature
sprite.color.setDark(0.2, 0.2, 0.2);

// change single component, useful for tweening
sprite.color.darkG = 0.1;

Or convert PIXI sprite

var sprite = new PIXI.Sprite(someTexture);

// pixi vanilla way, optional
sprite.tint = 0x80ff80;

// activate the plugin!
sprite.convertToHeaven();

Note that if you are using TS, sprite type will be changed

let coloredSprite : PIXI.heaven.Sprite = sprite.convertColors();

Useful example: invert the colors

sprite.color.setLight(0, 0, 0);
sprite.color.setDark(1, 1, 1);

Make whole sprite of one color:

sprite.tint = 0xffaacc;
sprite.color.dark[0] = sprite.color.light[0];
sprite.color.dark[1] = sprite.color.light[1];
sprite.color.dark[2] = sprite.color.light[2];
//dont forget to invalidate, after you changed dark DIRECTLY
sprite.color.invalidate();

Meshes

Heaven meshes can be batched with heaven sprites, and that significantly reduces number of Draw Calls but it eats CPU resources and doesn't work with advanced modes like uploadUvTransform

// Default, PixiJS vanilla mode
PIXI.heaven.settings.MESH_PLUGIN = 'meshHeaven';

// New mode, meshes batched as sprites
PIXI.heaven.settings.SPINE_MESH_PLUGIN = 'spriteHeaven';

// manually set one mesh to be batched with sprites
mesh.plugin = 'spriteHeaven';

How to use with spine

This plugin enables light-dark tint of spine 3.6.

Dark-light tint works like in sprites.

spine = new PIXI.heaven.spine.Spine();
spine.color.setLight(0.5, 1.0, 0.5);
spine.color.setDark(0.2, 0.2, 0.2);

How to mask sprites with sprites

Plugin adds special renderer that has faster masks than just sprite.mask. It also works on heaven meshes.

sprite = new PIXI.heaven.Sprite();
sprite.maskSprite = sprite2; //set it
sprite.pluginName = 'spriteMasked'; //enable special plugin rendering
sprite2.renderable = false; //turn off rendering

Batching works with spine, just enable maskSprite in any sprite of spine instance.

The best practice for spine meshes is to batch them with sprites. It eats some CPU resources.

// Default mode, meshes are rendered as sprites
PIXI.heaven.settings.SPINE_MESH_PLUGIN = 'spriteHeaven';

// PixiJS vanilla mode
PIXI.heaven.settings.SPINE_MESH_PLUGIN = 'meshHeaven';

Look at Spine file to see how it actually works.

How to use colored meshes

Please make sure everything is initialized, i didnt hook it up in refresh!!! see errors in console? make sure texture is initialized

Look in the code for details.

var strip = new PIXI.heaven.mesh.Rope(PIXI.Texture.fromImage('required/assets/snake.png'), 25, 2, 0);
strip.enableColors();

//lets make it randomy!
var len = strip.vertices.length / 2;
var rgb = new Float32Array(len*3);
//set light
for (var i=0;i<len*3;i++) rgb[i] = 0.5 + Math.random() * 0.5;
strip.setRGB(rgb);
//set dark
for (var i=0;i<len*3;i++) rgb[i] = Math.random() * 0.5;
strip.setRGB(rgb, true);

Animation

Unlike pixiJS vanilla AnimatedSprite, here animation is a component:

new PIXI.heaven.AnimationState(frames).bind(rope);
rope.animState.gotoAndStop(2);

It still uses PIXI.ticker.shared if you dont specify autoUpdate=false. It will be stopped and destroyed with the bound element.

Building

You will need to have node setup on your machine.

Make sure you have yarn installed:

npm install -g yarn

Then you can install dependencies and build:

yarn
yarn build

That will output the built distributables to ./dist.

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.