Skip to content

Commit

Permalink
Fix incompatibility of PIXI.Graphics (Fixes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
guansss committed Sep 21, 2020
1 parent 929d253 commit babdcd4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Live2DModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ export class Live2DModel extends Container {

this.internal.updateWebGLContext(renderer.gl, this.glContextID);

const flipY = renderer.gl.getParameter(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL);
renderer.gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, true);

for (let i = 0; i < this.textures.length; i++) {
Expand All @@ -302,8 +301,6 @@ export class Live2DModel extends Container {
// kind of ugly but it does the trick :/
this.internal.bindTexture(i, (baseTexture as any)._glTextures[this.glContextID].texture);
}

renderer.gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, flipY);
}

for (const texture of this.textures) {
Expand All @@ -317,7 +314,12 @@ export class Live2DModel extends Container {
this.internal.draw(this.transform.getDrawingMatrix(renderer.gl));

// reset the active texture that has been changed by Live2D's drawing system
renderer.gl.activeTexture(WebGLRenderingContext.TEXTURE0 + renderer.texture.currentLocation);
if (renderer.texture.currentLocation >= 0) {
renderer.gl.activeTexture(WebGLRenderingContext.TEXTURE0 + renderer.texture.currentLocation);
}

// reset WebGL state
renderer.state.reset();
}

/** @override */
Expand Down
38 changes: 37 additions & 1 deletion test/module/Live2DModel.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Application } from '@pixi/app';
import { Renderer } from '@pixi/core';
import { BatchRenderer, Renderer } from '@pixi/core';
import { Graphics } from '@pixi/graphics';
import { InteractionManager } from '@pixi/interaction';
import { Ticker, TickerPlugin } from '@pixi/ticker';
import { Live2DModel } from '../../src';
Expand All @@ -8,6 +9,7 @@ import { createApp } from '../utils';
import { TEST_MODEL } from './../env';

Application.registerPlugin(TickerPlugin);
Renderer.registerPlugin('batch', BatchRenderer);
Renderer.registerPlugin('interaction', InteractionManager);
Live2DModel.registerTicker(Ticker);

Expand Down Expand Up @@ -79,6 +81,40 @@ describe('Live2DModel', async () => {
expect(bounds.height).to.equal(modelBaseHeight * 3);
});

it('should not break rendering of PIXI.Graphics', function() {
// https://github.com/guansss/pixi-live2d-display/issues/5

const SIZE = 50;

const graphics = new Graphics();
graphics.beginFill(0xff0000);
graphics.drawRect(0, 0, SIZE, SIZE);
app.stage.addChild(graphics);
app.render();

const offscreenCanvas = document.createElement('canvas');
offscreenCanvas.width = app.view.width;
offscreenCanvas.height = app.view.height;

const context = offscreenCanvas.getContext('2d');
context.drawImage(app.view, 0, 0);

const pixels = context.getImageData(0, 0, SIZE, SIZE).data;

// detect the red square
expect(pixels).to.satisfy(() => {
for (let i = 0; i < pixels.length; i += 4) {
if (pixels[i] !== 255 ||
pixels[i + 1] !== 0 ||
pixels[i + 2] !== 0 ||
pixels[i + 3] !== 255) {
return false;
}
}
return true;
});
});

it('should handle tapping', () => {
const listener = sinon.spy();

Expand Down

0 comments on commit babdcd4

Please sign in to comment.