• Stars
    star
    186
  • Rank 201,383 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

pixi-projection

npm version

Collection of projections, both 2d and 3d.

To-do:

  • Docs
  • Graphics support

Compatibility

Works with PixiJS v6. Compatibility with v5 is not guaranteed.

For v4 please see v4.x branch, npm version 0.2.8

For v5.1 please use npm version 0.3.5

For >= v5.2 please see v5.x branch, npm version 0.3.15

For >= v6 please see v6.x branch, npm version 0.4.3

It even works with CanvasRenderer, though results can be strange.

Examples

3d Projection (Yummy!)

Cards

Runner

Projective sprites: Container2d, Sprite2d, Text2d

Two-point projection

One-point with return to affine

Projective transform of quad

Bilinear projection

There are many ways to define projections even when we use only 2 dimensions.

Surface sprites: Container2s, Sprite2s, Text2s for now only bilinear.

Bilinear transform of quad

Usage

Special classes

For every projective way, there are corresponding classes:

  • Container2d, Sprite2d, Text2d, TilingSprite2d, Mesh2d, Spine2d
  • Sprite3d, Text3d, Mesh3d2d, Spine3d, Camera3d
  • Container2s, Sprite2s *WORK IN PROGRESS

We dont support Graphics yet :(

Conversion of regular pixi objects

Bear in mind that if you dont use at least one class from pixi-projection, it might be tree-shaken away.

Here's how to use regular pixi projects to 3d projection:

import {Sprite, Container} from 'pixi.js';
var sprite = new Sprite();
sprite.convertTo3d();
sprite.position3d.set(0, 0, 1); //available now!

var container = new Container();
container.convertTo3d();
sprite.position3d.set(0, 0, 1); //available now!

You can also convert whole subtree:

import {Container} from 'pixi.js';
var tree = new Container();
var child = new Container();
tree.addChild(child);
tree.convertSubtreeTo2d(tree);
child.position3d.set(0, 0, 1); //available now!

(2d projection in this example)

3D transforms

The most useful thing is 3D transforms.

It all starts from a camera, dont use 3d elements outside of it - it doesn't make sense.

You can create several cameras if you want each element to have its own perspective parameters.

import {Camera3d} from 'pixi-projection';
var camera = new Camera3d();
camera.setPlanes(400, 10, 10000, false); // true if you want orthographics projection
// I assume you have an app or renderer already
camera.position.set(app.screen.width / 2, app.screen.height / 2);

In this case, 400 is focus distance. If the width of the screen is 800, that means 90 degrees horizontal FOV. Everything that's behind z < -390 will be cut by near plane, everything that's too far away z > 9600 will be cut too.

We position the camera at the center of the screen, so an element with position3d=(0,0,0) will appear right in the center. However, the camera can look at something else - a character, or just the point with same coords as center of the screen.

camera.position3d.set(app.screen.width/2, app.screen.height/2);

With this snippet, every element in the camera that does not use extra 3d fields (z, euler) will appear exactly like in pixi stage. That's how awesome our Camera implementation is!

Camera transform differs from other elements:

//camera follows player
camera.position3d.copy(player.position3d);
// player is two times smaller now
player.scale3d.set(0.5);
// but camera follows it too, now everything except player is two times bigger on screen :)
camera.scale3d.set(0.5);

Containers and Sprites have extra fields for positioning inside 3d space.

PixiJS gives only position, scale, rotation, pivot, and projection plugin adds position3d, euler, scale3d, pivot3d. Those fields applied in transforms after vanilla pixi fields.

The only exception is a Camera3d, that applies projection just after pixi fields, and then applies 3d fields in reversed order. That's why it can follow elements - its transform negates the element transform.

Spine

You can apply mixin from @pixi-spine/projection to force spine objects to spawn 2d or 3d instances of sprites and meshes.

import {applySpine3dMixin} from 'pixi-projection';
import {SpineBase} from '@pixi-spine/base';

applySpine3dMixin(SpineBase.prototype);
// now all spine instances can be put in 3d projective space

If you apply only mixin for 2d, dont expect fields like position3d to be accessible.

If your spine instance always exists in screen spcae, you can use it as it is, like in Runner example

Typing are injected in SpineBase class of @pixi-spine/base package. This package is usually tree-shaken away, hope its not a problem to see it in your node_modules even if you dont use spine.

For UMD version, you should use

PIXI.projection.applySpine3dMixin(PIXI.spine.Spine.prototype);

Heaven

No, we dont support pixi-heaven sprites yet.

What if element is not supported by library?

For complex objects that are not supported by library, there is a way to add them inside the camera If their plane is perpendicular to the camera.

Create Container3d that returns all children to 2d space: container3d.affine = PIXI.projection.AFFINE.AXIS_X; Any 2d elements added to that container will think of it as a simple 2d container, and custom renderers will work with it just fine.

This way is also more performant because Sprite works faster than Sprite3d. 4x4 matrices ARE VERY SLOW.

Sorting

pixi-projection provides extra fields to handle sorting.

  • getDepth returns the distance from near plane to the object local (0,0,0), you can pass it to zIndex or zOrder as element.zIndex = -element.getDepth()
  • isFrontFace detects the face of the object plane

Those fields can be used with custom sorting solution or with pixi-layers

Culling

Will be available after we add it to @pixi/layers

Vanilla JS, UMD build

All pixiJS v6 plugins has special umd build suited for vanilla. Navigate pixi-projection npm package, take dist/pixi-projection.umd.js file.

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

all classes can be accessed through PIXI.projection package.

Building

You will need to have node setup on your machine.

Then you can install dependencies and build:

npm i
npm run build

That will output the built distributables to ./dist.

More Repositories

1

pixijs

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
TypeScript
42,668
star
2

pixi-react

Write PIXI apps using React declarative style
JavaScript
2,167
star
3

filters

Collection of community-authored custom display filters for PixiJS
TypeScript
886
star
4

particle-emitter

A particle system for PixiJS
TypeScript
777
star
5

spine

Pixi.js plugin that enables Spine support.
TypeScript
538
star
6

examples

Rendering examples using PixiJS and related plugins
JavaScript
469
star
7

sound

WebAudio API playback library, with filters. Modern audio playback for modern browsers.
TypeScript
376
star
8

pixi-ui

Provide a definitive user experience in your PixiJS application without any frontend library!
JavaScript
299
star
9

tilemap

Rectangular tilemap implementation for PixiJS
TypeScript
275
star
10

pixi-typescript

Typescript definitions for Pixi.js
TypeScript
238
star
11

layers

Separate the z-hierarchy of your scene tree from its canonical structure.
TypeScript
220
star
12

animate

PixiJS runtime library for content from Adobe Animate CC
JavaScript
198
star
13

lights

Adds dynamic lighting via deferred shading to PixiJS
TypeScript
191
star
14

open-games

TypeScript
190
star
15

pixi-haxe

Externs of Pixi.js for Haxe
Haxe
179
star
16

graphics-smooth

Drop-in replacement for Graphics but with anti-aliasing
TypeScript
151
star
17

ui

Commonly used UI components for PixiJS
TypeScript
149
star
18

animate-extension

Custom Platform Plugin for Adobe Animate CC (Formerly Adobe Flash CC) - to export animation for the PixiJS renderer
C++
124
star
19

pixi-heaven

PIXI.js advanced color modes, sprites, meshes
TypeScript
115
star
20

pixi-compressed-textures

Compressed textures and retina support for pixijs. Loader can choose textures depends on platform and rendering mode.
TypeScript
92
star
21

pixi-native

A native version of pixi
Kotlin
85
star
22

pixi-gl-core

A set of tidy little pixi objects that make working with WebGL simpler
JavaScript
83
star
23

assetpack

TypeScript
77
star
24

pixi-extra-filters

[deprecated] Please see https://github.com/pixijs/pixi-filters
JavaScript
66
star
25

pixi-particles-editor

PIXI-based particle editor
JavaScript
65
star
26

picture

A plugin that includes a sprite renderer that reduces border artifacts and 3 blend mode implementations for WebGL
TypeScript
61
star
27

layout

Library for arranging/resizing pixiJS rendered elements basing on css like configs
TypeScript
53
star
28

html-text

Multi-Style Text Rendering Plugin for PixiJS
TypeScript
38
star
29

gif

Plugin to support playback of animated GIF images in PixiJS.
TypeScript
35
star
30

pixi-jsdoc-template

PixiJS Flavored JSDoc Template
JavaScript
30
star
31

extension-boilerplate

Pixi.js example renderer plugin. Clamps textures to reduce border artifacts.
TypeScript
30
star
32

pixijs.com

JavaScript
30
star
33

pixi-text-style

PIXI.TextStyle Generator
JavaScript
30
star
34

floss

Unit-testing for those hard to reach places
TypeScript
28
star
35

storybook

TypeScript
27
star
36

node

Run PixiJS but in Node.js environments, no browser required!
TypeScript
25
star
37

bunny-mark

Simulation for performance testing PIXI releases
JavaScript
20
star
38

devtools

TypeScript
18
star
39

batch

PixiJS batch rendering plugin library
TypeScript
17
star
40

pixify

Browserify bundle process for PIXI libraries
JavaScript
16
star
41

guides

PixiJS User Guides
SCSS
12
star
42

customize

Customize the list of packages to use for PixiJS v5
TypeScript
11
star
43

animate-examples

Document Examples for testing PixiAnimate publishing
10
star
44

docs

API Documentation container
HTML
9
star
45

examples-v4

Rendering examples using PixiJS v4 and related plugins
JavaScript
9
star
46

spine-v8

TypeScript
9
star
47

extension-scripts

Build-time scripts for setting up extensions
JavaScript
6
star
48

pixi-closure-compiler

closure compiler externs for pixijs
JavaScript
6
star
49

eslint-config

ESLint config for pixi.js plugin/integration libraries
JavaScript
5
star
50

webdoc-template

PixiJS template ported to webdoc
TypeScript
4
star
51

generator-pixi-plugin

A Yeoman generator for creating pixi.js plugins.
JavaScript
4
star
52

bundler

Bundle all release into a single package
JavaScript
3
star
53

pixi-animate-tests-tool

Used to generate unit tests solutions for PixiAnimate
JavaScript
3
star
54

legacy.pixijs.com

SCSS
3
star
55

examples-v5

Rendering examples using PixiJS and related plugins
JavaScript
2
star
56

webworker-plugins

TypeScript
1
star
57

examples-v7

JavaScript
1
star