Skip to content

Commit

Permalink
Merge pull request #69 from opulisDynamics/spine-ts
Browse files Browse the repository at this point in the history
Fix WebGL Rendering not taking camera into account. Cleanup example.
  • Loading branch information
AleBles authored Aug 18, 2017
2 parents 8f86e58 + 7a386cf commit 2c1dc83
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 27 deletions.
6 changes: 5 additions & 1 deletion build/phaser-spine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ declare module PhaserSpine {
currentShader: PIXI.PrimitiveShader;
_currentId: number;
}
interface IPIXIRectangle extends PIXI.Rectangle {
centerX: number;
centerY: number;
}
}
}
declare module PhaserSpine {
Expand All @@ -119,7 +123,7 @@ declare module PhaserSpine {
private debugShader;
private shapes;
constructor(game: Phaser.Game);
resize(bounds: PIXI.Rectangle, position: Phaser.Point, scale2: Phaser.Point, renderSession: IRenderSession): void;
resize(skeleton: spine.Skeleton, spriteBounds: IPIXIRectangle, scale2: Phaser.Point, renderSession: IRenderSession): void;
draw(skeleton: spine.Skeleton, renderSession: IRenderSession): void;
}
}
Expand Down
21 changes: 12 additions & 9 deletions build/phaser-spine.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/phaser-spine.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/phaser-spine.min.js

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
var spines = {};
var names = ['spineboy', 'goblins', 'raptor', 'stretchyman', 'tank', 'vine', 'buddy'];

// var renderer = Phaser[location.href.split('?')[1]] || Phaser.AUTO

var game = new Phaser.Game({
width: window.innerWidth,
height: window.innerWidth / 4 * 3,
// renderer: Phaser.AUTO,
renderer: Phaser[location.href.split('?')[1]] || Phaser.AUTO,
parent: 'content',
transparent: true,
Expand Down
2 changes: 1 addition & 1 deletion ts/Spine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module PhaserSpine {
return;
}

(<WebGL.Renderer>this.renderer).resize(this.specialBounds, this.position, this.scale, renderSession);
(<WebGL.Renderer>this.renderer).resize(this.skeleton, <WebGL.IPIXIRectangle>this.getBounds(), this.scale, renderSession);
(<WebGL.Renderer>this.renderer).draw(this.skeleton, renderSession);
}
}
Expand Down
5 changes: 5 additions & 0 deletions ts/WebGL/IRenderSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ module PhaserSpine {
currentShader: PIXI.PrimitiveShader;
_currentId: number;
}

export interface IPIXIRectangle extends PIXI.Rectangle {
centerX: number;
centerY: number;
}
}
}
23 changes: 12 additions & 11 deletions ts/WebGL/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,25 @@ module PhaserSpine {
this.shapes = new spine.webgl.ShapeRenderer(gl);
}

public resize(bounds: PIXI.Rectangle, position: Phaser.Point, scale2: Phaser.Point, renderSession: IRenderSession): void {
public resize(skeleton: spine.Skeleton, spriteBounds: IPIXIRectangle, scale2: Phaser.Point, renderSession: IRenderSession): void {
var w = this.game.width;
var h = this.game.height;
var res = this.game.resolution;
var res = renderSession.resolution;

skeleton.flipX = scale2.x < 0;
skeleton.flipY = scale2.y < 0;

var scale = Math.max(scale2.x, scale2.y);

var signs = {
x: scale2.x < 0 ? -1 : 1,
y: scale2.y < 0 ? -1 : 1
}
var width = w / scale;
var height = h / scale;
var centerX = - spriteBounds.centerX;
var centerY = (-h + spriteBounds.centerY) * res + spriteBounds.height / 2;

var x = - position.x / scale * signs.x * res,
y = (position.y - h) / scale * signs.y * res + bounds.height / 2,
width = w / scale * signs.x * res,
height = h / scale * signs.y * res;
var x = centerX / scale;
var y = centerY / scale;

this.mvp.ortho2d(x, y, width, height);
this.mvp.ortho2d(x * res, y, width * res, height * res);
renderSession.gl.viewport(0, 0, w * res, h * res);
}

Expand Down

0 comments on commit 2c1dc83

Please sign in to comment.