Well I'm a bit lost. The method seems to work, partially.
The idea is making a custom rendering function for the sprite. Get the pixel data of the screen area under the sprite as a new texture, draw the texture of the sprite over the new texture using canvas 2d api, then use the new texture in place of the original sprite.
Now the problem is, it only works well when the sprite display above or below the character. But when the event has the same priority as the player, it has a strange problem that gl.readPixels only get the map image, but not the events.
I suspect there should be a flush or something before reading, but I having figure it out.
This is my current code snippet that add a sprite to the map scene with 'soft-light' blend mode.
Test using an event.
Screenshots:
This method works when the sprite is above the other events.
Otherwise it looks like this:
The idea is making a custom rendering function for the sprite. Get the pixel data of the screen area under the sprite as a new texture, draw the texture of the sprite over the new texture using canvas 2d api, then use the new texture in place of the original sprite.
Now the problem is, it only works well when the sprite display above or below the character. But when the event has the same priority as the player, it has a strange problem that gl.readPixels only get the map image, but not the events.
I suspect there should be a flush or something before reading, but I having figure it out.
This is my current code snippet that add a sprite to the map scene with 'soft-light' blend mode.
JavaScript:
var bmp = ImageManager.loadEnemy('Firespirit');var spr = new Sprite(bmp);SceneManager._scene.addChild(spr); spr.x =408; spr.y=312;spr.scale.set(3,2);spr.rotation=10; spr.anchor.set(0.5,0.5);
spr._renderWebGL = function(renderer){ if(!this.bitmap||!this.bitmap.isReady()) return;
var gl = renderer.gl;function getRenderSpriteBounds(sprite,renderer){var spriteBounds=sprite.getBounds();var renderTarget=renderer._activeRenderTarget;var matrix=renderTarget.projectionMatrix;var flipX=matrix.a<0;var flipY=matrix.d<0;var resolution=renderTarget.resolution;var screen={};var fr=renderTarget.sourceFrame||renderTarget.destinationFrame;screen.x=0;screen.y=0;screen.width=fr.width;screen.height=fr.height;var bounds={};var fbw=fr.width*resolution,fbh=fr.height*resolution;bounds.x=(spriteBounds.x+matrix.tx/matrix.a)*resolution+fbw/2;bounds.y=(spriteBounds.y+matrix.ty/matrix.d)*resolution+fbh/2;bounds.width=spriteBounds.width*resolution;bounds.height=spriteBounds.height*resolution;if(flipX){bounds.x=fbw-bounds.width-bounds.x;bounds.flipX=true;}if(flipY){bounds.y=fbh-bounds.height-bounds.y;bounds.flipY=true;}return bounds}var bd = getRenderSpriteBounds(this,renderer);var bb=this.getBounds();
if(!this.subSpr){
var bmp = new Bitmap(bd.width,bd.height); var subSpr = new Sprite(bmp);this.subSpr = subSpr; this.img = this.img||new ImageData(bmp.width,bmp.height); this.pixels = this.pixels||new Uint8Array(this.img.data.buffer);this.subSpr._renderWebGL = function(){};SceneManager._scene.addChild(subSpr);
}this.subSpr.x=bb.x;this.subSpr.y=bb.y;
var bmp = this.subSpr.bitmap;gl.readPixels(bd.x,bd.y,bmp.width,bmp.height,gl.RGBA, gl.UNSIGNED_BYTE,this.pixels);bmp._context.putImageData(this.img,0,0);bmp._context.save();var dx=this.x-bb.x,dy=this.y-bb.y;var ax=this.width*this.anchor.x,ay=this.height*this.anchor.y;var sx=this.scale.x,sy=this.scale.y;var ctx=bmp._context;ctx.translate(dx,dy);ctx.scale(sx,sy);ctx.rotate(this.rotation);ctx.translate(-ax,-ay);ctx.translate(bd.flipX?this.width:0,bd.flipY?this.height:0);ctx.scale(bd.flipX?-1:1,bd.flipY?-1:1);bmp._context.globalCompositeOperation ='color-dodge'; bmp._context.drawImage(this.bitmap._context.canvas,this._frame.x,this._frame.y,this.width,this.height,0,0,this.width,this.height);bmp._context.restore();bmp._baseTexture.update();this.subSpr.scale.set(bd.flipX?-1:1,bd.flipY?-1:1);this.subSpr.y += bd.flipY?bb.height:0;this.subSpr.x+=bd.flipX?bb.width:0;
Sprite.prototype._renderWebGL.call(this.subSpr,renderer);
}
Test using an event.
JavaScript:
var c=$gameMap.event(this.eventId());var spr = SceneManager._scene._spriteset._characterSprites.find(function(s){return s._character==c;});
spr._renderWebGL = function(renderer){ if(!this.bitmap||!this.bitmap.isReady()) return;
var gl = renderer.gl;function getRenderSpriteBounds(sprite,renderer){var spriteBounds=sprite.getBounds();var renderTarget=renderer._activeRenderTarget;var matrix=renderTarget.projectionMatrix;var flipX=matrix.a<0;var flipY=matrix.d<0;var resolution=renderTarget.resolution;var screen={};var fr=renderTarget.sourceFrame||renderTarget.destinationFrame;screen.x=0;screen.y=0;screen.width=fr.width;screen.height=fr.height;var bounds={};var fbw=fr.width*resolution,fbh=fr.height*resolution;bounds.x=(spriteBounds.x+matrix.tx/matrix.a)*resolution+fbw/2;bounds.y=(spriteBounds.y+matrix.ty/matrix.d)*resolution+fbh/2;bounds.width=spriteBounds.width*resolution;bounds.height=spriteBounds.height*resolution;if(flipX){bounds.x=fbw-bounds.width-bounds.x;bounds.flipX=true;}if(flipY){bounds.y=fbh-bounds.height-bounds.y;bounds.flipY=true;}return bounds}var bd = getRenderSpriteBounds(this,renderer);var bb=this.getBounds();
if(!this.subSpr){
var bmp = new Bitmap(bd.width,bd.height); var subSpr = new Sprite(bmp);this.subSpr = subSpr; this.img = this.img||new ImageData(bmp.width,bmp.height); this.pixels = this.pixels||new Uint8Array(this.img.data.buffer);this.subSpr._renderWebGL = function(){};SceneManager._scene.addChild(subSpr);
}this.subSpr.x=bb.x;this.subSpr.y=bb.y;
var bmp = this.subSpr.bitmap;gl.readPixels(bd.x,bd.y,bmp.width,bmp.height,gl.RGBA, gl.UNSIGNED_BYTE,this.pixels);bmp._context.putImageData(this.img,0,0);bmp._context.save();var dx=this.x-bb.x,dy=this.y-bb.y;var ax=this.width*this.anchor.x,ay=this.height*this.anchor.y;var sx=this.scale.x,sy=this.scale.y;var ctx=bmp._context;ctx.translate(dx,dy);ctx.scale(sx,sy);ctx.rotate(this.rotation);ctx.translate(-ax,-ay);ctx.translate(bd.flipX?this.width:0,bd.flipY?this.height:0);ctx.scale(bd.flipX?-1:1,bd.flipY?-1:1);bmp._context.globalCompositeOperation ='color-dodge'; bmp._context.drawImage(this.bitmap._context.canvas,this._frame.x,this._frame.y,this.width,this.height,0,0,this.width,this.height);bmp._context.restore();bmp._baseTexture.update();this.subSpr.scale.set(bd.flipX?-1:1,bd.flipY?-1:1);this.subSpr.y += bd.flipY?bb.height:0;this.subSpr.x+=bd.flipX?bb.width:0;
Sprite.prototype._renderWebGL.call(this.subSpr,renderer);
}
This is my test code on a map event, using blend mode 'soft-light'.
Another problem is readPixels sometimes get an upside down flipped image, and sometimes I need to shift x and y a few pixels to get the correct image. It is not consistent. For example, the code above shift both x and y 4 pixels to get the correct image, while the code below (which I test with a sprite added directly to the map scene) get an upside down image so I have to correct it manually (which is pretty slow on old computers).
Edit* After looking at pixi js code, it seems there's a projection matrix, so the flip problem can be solved, with some calculations. And I can just draw the sprite upside down.
JavaScript:
var c=$gameMap.event(this.eventId());var spr = SceneManager._scene._spriteset._characterSprites.find(function(s){return s._character==c;});
spr._renderWebGL = function(renderer){
var gl = renderer.gl;
if(!this.subSpr){
var bmp = new Bitmap(this.width,this.height); var pixels = new Uint8Array(bmp.width*bmp.height*4); var subSpr = new Sprite(bmp);subSpr.anchor.copy(this.anchor);this.addChild(subSpr); this.subSpr = subSpr; this.img = this.img||new ImageData(bmp.width,bmp.height); this.pixels = this.pixels||new Uint8Array(this.img.data.buffer); this.subSpr._renderWebGL = function(){};
}
var bmp = this.subSpr.bitmap; var bd = this.getBounds();gl.readPixels(bd.x+4,bd.y+4,bd.width,bd.height,gl.RGBA, gl.UNSIGNED_BYTE,this.pixels);bmp._context.putImageData(this.img,0,0);
bmp._context.globalCompositeOperation ='soft-light'; bmp._context.drawImage(this.bitmap._context.canvas,this._realFrame.x,this._realFrame.y,this.width,this.height,0,0,this.width,this.height);
bmp._baseTexture.update();
Sprite.prototype._renderWebGL.call(this.subSpr,renderer);}
Another problem is readPixels sometimes get an upside down flipped image, and sometimes I need to shift x and y a few pixels to get the correct image. It is not consistent. For example, the code above shift both x and y 4 pixels to get the correct image, while the code below (which I test with a sprite added directly to the map scene) get an upside down image so I have to correct it manually (which is pretty slow on old computers).
Edit* After looking at pixi js code, it seems there's a projection matrix, so the flip problem can be solved, with some calculations. And I can just draw the sprite upside down.
JavaScript:
var bmp = ImageManager.loadEnemy('Firespirit');var spr = new Sprite(bmp);SceneManager._scene.addChild(spr); spr.x =300; spr.y=300;
spr._renderWebGL = function(renderer){ if(!this.bitmap||!this.bitmap.isReady()) return;
var gl = renderer.gl;
if(!this.subSpr){
var bmp = new Bitmap(this.width,this.height); var pixels = new Uint8Array(bmp.width*bmp.height*4); var subSpr = new Sprite(bmp);subSpr.anchor.copy(this.anchor);this.addChild(subSpr); this.subSpr = subSpr; this.img = this.img||new ImageData(bmp.width,bmp.height); this.pixels = this.pixels||new Uint8Array(this.width*this.height*4); this.subSpr._renderWebGL = function(){};
}
var bmp = this.subSpr.bitmap; var bd = this.getBounds(); gl.readPixels(bd.x,bd.y-bd.height+24,bd.width,bd.height,gl.RGBA, gl.UNSIGNED_BYTE,this.pixels);for(var y=0;y<bmp.height;y++){ for(var x=0;x<bmp.width;x++){var i=x+y*bmp.width, j=(bmp.height-1-y)*bmp.width+x;i*=4;j*=4;this.img.data[i]=this.pixels[j]; this.img.data[i+1]=this.pixels[j+1];this.img.data[i+2]=this.pixels[j+2];this.img.data[i+3]=this.pixels[j+3];}}bmp._context.putImageData(this.img,0,0);
bmp._context.globalCompositeOperation ='soft-light'; bmp._context.drawImage(this.bitmap._context.canvas,this._frame.x,this._frame.y,this.width,this.height,0,0,this.width,this.height);
bmp._baseTexture.update();
Sprite.prototype._renderWebGL.call(this.subSpr,renderer);
}
Screenshots:
This method works when the sprite is above the other events.
Otherwise it looks like this: