MV Replacing an existing blending mode

For my game, I'd like to replace an existing blending mode. At least for battle animations. I'd like to change the screen blend mode's behaviour to be like a difference blending mode, as it could make for some really spunky battle effects by turning part of the screen negative.

I looked a bit in the code, did some searches on google and on this forum but... I couldn't find a way to do it. Searching online gives no result, and I don't understand where the blending modes actually are...

This below is the most promising lead I found, at line 1830 in rpg_core.js

/**
* The alias of PIXI.blendModes.SCREEN.
*
* @static
* @property BLEND_SCREEN
* @type Number
* @final
*/
Graphics.BLEND_SCREEN = 3;

Thanks for your help !
MV supports 2 rendering modes by default: Canvas and WebGL. The blend modes themselves are implemented natively in the browser/graphics driver. Some technical rambling:
Spoiler: canvas
Canvas supports the standard blend modes, and it looks like the core scripts actually use the difference blend mode for tinting (cf rpg_core.js Sprite.prototype._executeTint). Unfortunately Graphics.BLEND_SCREEN doesn't seem to be referenced anywhere, but you may be able to work around that with a small plugin like this (untested):
JavaScript:
Object.defineProperty(Sprite, "blendMode", {
  get: function( ) {
    return this._blendMode;
  },
  set: function(v) {
    if (v === PIXI.blendModes.SCREEN)  // If SCREEN blend mode is specified...
      v = PIXI.blendModes.DIFFERENCE;  // ...actually set it to DIFFERENCE.
    this._blendMode = v;
  },
  configurable: true
});
Note that this is only for the fallback, Canvas renderer. Without any other modifications, under the WebGL renderer, I'd expect this to make all sprites using screen blend mode to effectively use normal blend mode (i.e. no blending). More details in the WebGL spoiler below.
Spoiler: webgl
I did a search in pixi.js and found the function mapWebGLBlendModesToPixi, with this line:
JavaScript:
    array[_const.BLEND_MODES.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA];
This data is interpreted via WebGLState.prototype.setBlendMode to determine the blending instructions sent to the GPU:
JavaScript:
        if (mode.length === 2) {
            this.gl.blendFunc(mode[0], mode[1]);
        } else {
            this.gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
        }
blendFunc & blendFuncSeparate are OpenGL operations:
As-is, PIXI maps the difference blend mode (and all the other "unsupported" blend modes) to normal (i.e. none) in WebGL. I assume that if the difference blend mode could be neatly expressed like this then PIXI would already support it.

It looks like pixi-picture.js (found alongside pixi.js in the libs subfolder) implements the hard-light and overlay blend modes via custom shaders, so I imagine it'd be possible to implement the difference mode in a similar way. I haven't really dug in to see how it works.

I did a quick search and found this old thread:
No solution there but it definitely looks like custom shaders are the way to go~
In summary: looks possible but probably not easy. I don't know if anyone's done it yet. 👋
1777674249547.png

I tried the plugin above but... These squares all use the blending mode in order (so from top to bottom it's normal, add, multiply, screen/diff)
That last square is using the screen blending mode, and neither normal nor difference.
I wanted to quickly test which renderer my setup was using.

Also, looking at the WEBGL block you sent, I assume that if I hijack the BLEND_MODES_SCREEN with another set of instruction that would create a difference--ish blend mode, it would have to be in 2 instructions ? All the blend modes of that table are in 2... I'll try to tinker with that.

Thanks a lot ! I'll report here if I manage to do anything cool.
I wanted to quickly test which renderer my setup was using.
If you press F2 to show the in-game framerate widget, that should be labelled either Canvas or WebGL (almost certainly WebGL, unless your system is really old). 👋

Also, looking at the WEBGL block you sent, I assume that if I hijack the BLEND_MODES_SCREEN with another set of instruction that would create a difference--ish blend mode, it would have to be in 2 instructions ?
The difference blend mode is defined per-colour as | Cb - Cs |, where Cb is the backdrop value and Cs is the source value. I don't see any way to use the operations available on blendFunc (or blendFuncSeparate) to create that result.

The blendEquation might also be relevant? It offers subtractive arithmetic, but in general for difference blending you'd also need to make sure you're subtracting the smaller value from the larger one.

Like I said, I suspect that if you want "true" difference blending, you'll need to implement a custom shader as seen in pixi-picture.js. Of course, feel free to play around with stuff if you like~

There might be other options, too...I'm not really an expert at this stuff. 😴

Good luck!
Research log... Mods, LMK if this needs to be moved by the way

I found this article, which seems close to what I want : https://gamedev.stackexchange.com/questions/28640/how-to-invert-background-pixels-color
with the args being GL_ONE_MINUS_DST_COLOR, GL_ZERO
1777715289494.png


It's close, we can see the blue being turned to yellow and the pink to green. It seems to be reversing the hue on the brighter colours ?

1777715484398.png

If I make the picture all white, we can see that quite clearly. But it doesn't change the brightness...

Your post just happened for me, so I tried putting just gl.FUNC_ADD and...
1777716134688.png

It's only doing a multiply lol. Strange... Adding more args changes nothing. It seems glBlendEquations makes everything into a multiply...

Also, I can add two args to a function ! I feel like i'm getting a bit closer...
SURPRISE i got it to work

1777725685585.png


I ended up asking a programmer flan who found how to make it work

[gl.ONE_MINUS_DST_COLOR, gl.ZERO, gl.ZERO, gl.ONE]; < these are the args.
There are four args, which means its using blendFuncSeparate. The first two arguments are for RGB values, the latter two for the alpha channel. If we only put two arguments, then it ends up making a neutral grey (because putting a difference layer at half opacity makes a neutral gray).

If you try this you should still backup your pixi.js before anything else... Also, i'm now going to try to mess around with blendEquation.

Thanks a lot Caethyril for putting me on the right path !
I lied.
It's working well except... the transparency.

1777730420407.png

Pictured : square on the left is my test image with the normal blending mode, and on the right is with my custome blending mode. As you can see, the transparency doesn't work, which is certainly annoying. As you can also see, black areas return as black (in a difference blend, black areas return the colour behind them).
So... something is up.

Someone else suggested doing two passes which...

WebGLState.prototype.setBlendMode = function setBlendMode(value) {
if (value === this.activeState[BLEND_FUNC]) {
return;
}

this.activeState[BLEND_FUNC] = value;

var mode = this.blendModes[value];

if (mode.length > 2) {
this.gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
this.gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
} else {
this.gl.blendFunc(mode[0], mode[1]);
}
};

I suppose I can do ?
But I have no idea what those two passes should be.

The problem is that, no matter what arguments are used as 3rd and 4th (so the part that controls opacity), the result is always the same, AKA no opacity and black being passed as pure black.
It's only doing a multiply lol.
No, it's doing a subtraction. That will always darken the result because the values involved are non-negative. That's why I said this:
It offers subtractive arithmetic, but in general for difference blending you'd also need to make sure you're subtracting the smaller value from the larger one.

[gl.ONE_MINUS_DST_COLOR, gl.ZERO, gl.ZERO, gl.ONE];< these are the args.
It's working well except... the transparency.
Yes. This is the definition of blendFuncSeparate, which defines how much of each channel to mix:

void glBlendFuncSeparate( GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha);
The default blend equations (additive) are in this form for R, G, B, and A values:

final_value = destination_value * destinaton_blend_factor + source_value * source_blend_factor
So your args mean:
  • Multiply source RGB by the inverse of the destination RGB;
  • Multiply destination RGB by zero, i.e. ignore it;
  • Multiply source alpha by zero, i.e. ignore it;
  • Multiply destination alpha by one, i.e. final alpha = destination alpha.
E.g. opaque white source (1, 1, 1, 1) + 80% opaque blue destination (0, 0, 1, 0.8):
  • R = 1 × (1 - 0) + 0 × 0 = 1
  • G = 1 × (1 - 0) + 0 × 0 = 1
  • B = 1 × (1 - 1) + 1 × 0 = 0
  • A = 1 × 0 + 0.8 × 1 = 0.8
I.e. 80% opaque yellow.

Maybe try scaling the source opacity based on source colour instead? E.g. (untested):
  • [gl.ONE_MINUS_DST_COLOR, gl.ZERO, gl.SRC_COLOR, gl.ZERO]


Edit - oh, just saw this, guess that's a no-go then:
The problem is that, no matter what arguments are used as 3rd and 4th (so the part that controls opacity), the result is always the same, AKA no opacity and black being passed as pure black.


this.gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
this.gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
This is not the render pass. The blend functions are set before rendering occurs, as a setup phase. Multi-pass renders are typically done as filters in PIXI, but if you're going that route then the pixi-picture setup seems more promising.
I got transparency to work on the map, actually !

1777738602676.png


gl.ONE_MINUS_DST_COLOR, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE
Works just fine, though the blacks aren't transparent, which is normally the case in a difference blend. I suppose that's fine, as I could only use white assets.

I should also post a mock up I did to show what I would like the effect to be :

like this.gif


The big straight lines that go to the edge of the screen are white, but using a Difference blend.
However...


1777738216855.png


Despite it working just fine on the map, the battle animations are giving me some trouble, which is annoying because that's precisely what I wanted this effect for...
The line and the circle are white in their asset, and set to screen (IE difference) mode, but they appear fully black.

Could the battle animation function be doing something strange ? I'm not sure.
1777744206704.png


I fixed it (???)
If the battle animation is set to 255 opacity it shows up as black, with its edges being correct
Just need to set it to 254 and it works ? Oh well. I won't complain. That was literally Everything I Wanted Out Of This.

Thank you so much Caethyril ! I'll definitely credit you in my game whenever (gestures) it releases.
I hope this thread can work for other people who want to know the same thing in the future.

Original thread: https://forums.rpgmakerweb.com/threads/replacing-an-existing-blending-mode.183456/