MV [Abandoned]More blend modes in WebGL mode (Currently just testing things)

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.
Spoiler
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.
Spoiler
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);
}


Spoiler: Old code
This is my test code on a map event, using blend mode 'soft-light'.
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.
1724115821496.png
1724118934535.png
1724119373431.png


Otherwise it looks like this:
1724127591661.png
Migrate the code to MZ and wrap the functions in a plugin.
Spoiler
1724168294748.png
1724168310248.png

JavaScript:
function $e(n){
  if(n instanceof Game_Event) return n;
  if(typeof n.eventId=='function') n = n.eventId();
  return $gameMap._events[n]||$gameMap._events.find(function(e){return e&&e.event().name==n;})||$gameMap._events.find(function(e){return e&&e.event().note.contains(n);})||null;
}

function $spr(c){
  if(c instanceof Game_Character == false){
    c = $e(c);
  }
  if(!c) return null;
  return SceneManager._scene._spriteset._characterSprites.find(function(s){return s._character==c;});
}

function getRenderSpriteBounds(sprite, renderer) {
  var spriteBounds = sprite.getBounds();
  var renderTarget = renderer.renderTexture.current;
  var matrix = renderer.projection.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
}

/*
        BLEND_MODES[BLEND_MODES["NORMAL"] = 0] = "NORMAL";
        BLEND_MODES[BLEND_MODES["ADD"] = 1] = "ADD";
        BLEND_MODES[BLEND_MODES["MULTIPLY"] = 2] = "MULTIPLY";
        BLEND_MODES[BLEND_MODES["SCREEN"] = 3] = "SCREEN";
        BLEND_MODES[BLEND_MODES["OVERLAY"] = 4] = "OVERLAY";
        BLEND_MODES[BLEND_MODES["DARKEN"] = 5] = "DARKEN";
        BLEND_MODES[BLEND_MODES["LIGHTEN"] = 6] = "LIGHTEN";
        BLEND_MODES[BLEND_MODES["COLOR_DODGE"] = 7] = "COLOR_DODGE";
        BLEND_MODES[BLEND_MODES["COLOR_BURN"] = 8] = "COLOR_BURN";
        BLEND_MODES[BLEND_MODES["HARD_LIGHT"] = 9] = "HARD_LIGHT";
        BLEND_MODES[BLEND_MODES["SOFT_LIGHT"] = 10] = "SOFT_LIGHT";
        BLEND_MODES[BLEND_MODES["DIFFERENCE"] = 11] = "DIFFERENCE";
        BLEND_MODES[BLEND_MODES["EXCLUSION"] = 12] = "EXCLUSION";
        BLEND_MODES[BLEND_MODES["HUE"] = 13] = "HUE";
        BLEND_MODES[BLEND_MODES["SATURATION"] = 14] = "SATURATION";
        BLEND_MODES[BLEND_MODES["COLOR"] = 15] = "COLOR";
        BLEND_MODES[BLEND_MODES["LUMINOSITY"] = 16] = "LUMINOSITY";
        BLEND_MODES[BLEND_MODES["NORMAL_NPM"] = 17] = "NORMAL_NPM";
        BLEND_MODES[BLEND_MODES["ADD_NPM"] = 18] = "ADD_NPM";
        BLEND_MODES[BLEND_MODES["SCREEN_NPM"] = 19] = "SCREEN_NPM";
        BLEND_MODES[BLEND_MODES["NONE"] = 20] = "NONE";
        BLEND_MODES[BLEND_MODES["SRC_OVER"] = 0] = "SRC_OVER";
        BLEND_MODES[BLEND_MODES["SRC_IN"] = 21] = "SRC_IN";
        BLEND_MODES[BLEND_MODES["SRC_OUT"] = 22] = "SRC_OUT";
        BLEND_MODES[BLEND_MODES["SRC_ATOP"] = 23] = "SRC_ATOP";
        BLEND_MODES[BLEND_MODES["DST_OVER"] = 24] = "DST_OVER";
        BLEND_MODES[BLEND_MODES["DST_IN"] = 25] = "DST_IN";
        BLEND_MODES[BLEND_MODES["DST_OUT"] = 26] = "DST_OUT";
        BLEND_MODES[BLEND_MODES["DST_ATOP"] = 27] = "DST_ATOP";
        BLEND_MODES[BLEND_MODES["ERASE"] = 26] = "ERASE";
        BLEND_MODES[BLEND_MODES["SUBTRACT"] = 28] = "SUBTRACT";
        BLEND_MODES[BLEND_MODES["XOR"] = 29] = "XOR";
*/
var blendModeNames = [
  '',
  '',
  'multiply',
  'screen',
  'overlay',
  'darken',
  'lighten',
  'color-dodge',
  'color-burn',
  'hard-light',
  'soft-light',
  'difference',
  'exclusion',
  'hue',
  'saturation',
  'color',
  'luminosity',
  '',
  '',
  '',
  '',
  'source-over',
  'source-in',
  'source-out',
  'source-atop',
  'destination-over',
  'destination-in',
  'destination-out',
  'destination-atop',
  '',
  '',
  'xor'
];
function getBlendModeName(blendMode){
  return blendModeNames[blendMode]||blendMode||'';
}

function renderfn(renderer) {
  if (!this.bitmap || !this.bitmap.isReady()) return;
  var gl = renderer.gl;
  var bd = getRenderSpriteBounds(this, renderer);
  var bb = this.getBounds();
  if (!this.subSpr) {
      console.log(this.bitmap)
      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._render = function() {};
      SceneManager._scene.addChild(subSpr);
  }
  this.subSpr.x = bb.x;
  this.subSpr.y = bb.y;
  var bmp = this.subSpr.bitmap;
  var ctx = bmp._context;
  gl.readPixels(bd.x, bd.y, bmp.width, bmp.height, gl.RGBA, gl.UNSIGNED_BYTE, this.pixels);
  ctx.putImageData(this.img, 0, 0);
  ctx.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;
  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);
  ctx.globalCompositeOperation = getBlendModeName(this.blendMode);
  var fn = this._fn;
  if(typeof fn=='function'){
    fn(ctx);
  }else if(fn){
    if(fn.blendMode){
      ctx.globalCompositeOperation = fn.blendMode;
    }
    if(fn.filter){
      ctx.filter = fn.filter;
    }
  }
  if(this._drawfn){
    this._drawfn(ctx);
  }else{
    ctx.drawImage(this.bitmap.image||this.bitmap.canvas, this._frame.x, this._frame.y, this.width, this.height, 0, 0, this.width, this.height);
  }
  ctx.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._render.call(this.subSpr, renderer);
}

function spriteRendererHack(spr, fn, drawfn) {
  spr._render=renderfn;
  spr._fn = fn;
  spr._drawfn = drawfn;
}

Edit*
This thread can be closed.
Because a plugin has been made.

Original thread: https://forums.rpgmakerweb.com/threads/abandoned-more-blend-modes-in-webgl-mode-currently-just-testing-things.171247/