MV Rainbow Enemies!?

Edit: Full plugin in post below!

Hello Makers!

So recently I was looking at a thread where someone wanted to change a monster battles color mid-battle.
I did a little digging and not only found the battlerHue function, but rewrote it so it can make a monster flash a rainbow of colors in battle!

Very cool looking effect IMO.

Now, the catch for me is that when a monster is set for this color, it takes a short while to be drawn on-screen.
I wanted to share the snippet in the hopes someone else could test it and see if they experience that too and if if so, could maybe share any ideas on how to fix it.

So here it is:

JavaScript:
Game_Enemy.prototype.battlerHue = function() { //TEST
    if(!this.isRainbowEnemy()) return this.enemy().battlerHue;
    if(!$gameParty.testColor) $gameParty.testColor = 0;
    $gameParty.testColor += 1;
    if($gameParty.testColor == 361) $gameParty.testColor = 0;
    return $gameParty.testColor;
    return this.enemy().battlerHue; //ORIGINAL LINE
};

Game_Enemy.prototype.isRainbowEnemy = function() {
    if($dataEnemies[this.enemy().id].meta.rainbow) return true;
    return false;
};

Just tag a enemy battler with <rainbow> to see the magic!

Thanks in advance for anyone who can assist.
I place this snippet into my base plugin for test, and it does work,
which is kind of cool for really special enemies for rare drops.

but it does indeed gives +/- 2 seconds before they pop up the screen.
but no idea for the fix though, but I only know 2 possible solution.

some wait before all battlers are loaded before battle screen is visible.
maybe preload the enemy (which I could test if that works).

or some update, if all is loaded, stop updating, but I personally don't mind,
but I love the effect 🙂

Edit:
preloading didn't work =)
MV implements hue shifts on the CPU, then caches each hue of each image separately. I think performing a hue shift every frame like this is likely to be slow because:
  • The CPU is not optimised for highly parallel tasks like image processing;
  • It will quickly saturate the image cache (default 10 MPix), meaning non-reserved images (an MV-only cache mechanic) have to be re-loaded from disk, which is notably slower than RAM;
  • Hue updates result in loading the 0-hue image from file or cache (Sprite_Enemy#updateBitmap). [Edit: actually it loads the image with specified hue.]
Ideas for workarounds:
  • Use the Community_Basic plugin to greatly increase the image cache size limit;
  • Patch the core updateBitmap to reserve base images for rainbow enemies, so they don't get pushed out of cache prematurely; [Edit: actually I don't think this would work, since image loads include the target hue.]
  • Edit your code so as to cache fewer copies, e.g. +5 hue per 5 frames instead of +1 per frame.
There may be other issues contributing to the observed slowdown.

(In comparison, MZ implements hue shifts via a WebGL shader, skipping the above problems.)
updating to +5 and color max to +5 = 0.3 seconds to load, but the hue is also
far faster, so that part works like caethyril said, but I think + 1 is smoother
or maybe +2, so updating elsewhere can be a good idea ^^

but once it's loaded, it works perfectly, increase cache size didn't work much,
so increase frame did ^^

Edit:
+2 load also far faster than +1, +2 is around the same loading time as +5.
+10 is almost instantly, so it can work if you could update the bitmapUpdate
for rainbow, so I set it to 2 and it works nicely 🙂
Hmmm... So maybe the best way to do this would be to maybe hide the loading via some entry animation or something...
It's a really cool effect, so I'd like to implement it somehow.
I'll keep trying.

Thanks for the help thus far!

(And thanks for giving me one more reason to love/hate MZ! )
:hswt:

EDIT: Side note: I also noticed that the rainbow enemies do not fade out properly on KO.
Fun effect, but still might be more trouble than it's worth to implement...
Webgl mode utilizes shaders to create filter effects, while canvas has lots of build-in blend modes and filters. Hue rotate is one of the canvas filters if I recall.
I guess MV cache the hue shifted bitmap for compatible purpose.
@Maliki79 - also, I just realised: you're incrementing the hue every time battlerHue is fetched for an applicable enemy, but that might happen multiple times per frame...especially if you have multiple rainbow enemies! 😅

I realise it was just a proof of concept kind of thing, but if you want to make something like this, I'd suggest patching a method like Game_Troop.prototype.update (called once per mechanical frame) for incrementing the hue offset. I'd also suggest storing testColor on, say, $gameTroop or $gameTemp instead of $gameParty, since the testColor value does not need to be saved. 👋


@utunnels - as far as I'm aware, the MV core scripts make very little use of shaders directly. E.g. ToneFilter (rpg_core.js) is only ever applied to Spriteset_Base (rpg_sprites.js); unless I'm mistaken, individual sprites always use the canvas tinter. 😴

MV attaches Bitmap#rotateHue (rpg_core.js) as a load listener in ImageManager.loadNormalBitmap (rpg_managers.js). As far as I can tell, the calculations and image manipulation in rotateHue are done manually, all the way down to getImageData/putImageData, irrespective of render mode.
Worth a try!

I'll see if that helps.

Thank you!
@caethyril I mean, it would be easier to hack with a hue-rotate filter if pixi is in canvas mode.

For example, if spr is a sprite:

JavaScript:
spr._renderCanvas = function(renderer){
  this.globalHue=this.globalHue||0;
  this.globalHue +=1;
  this.globalHue %= 360;
  var f = renderer.context.filter;
  renderer.context.filter = 'hue-rotate('+this.globalHue+'deg)';
  Sprite.prototype._renderCanvas.call(this,renderer);
  renderer.context.filter = f;
}

​

FYI - I finally managed to get some time to review to fix this.

I'll release it HERE.
It doesn't have the full header info, so I won't release it as a full plugin.
Might do that later.
Not sure if this is something similar, but I wrote a few functions/methods and got pretty good results here hooking it into the Scene_Battle update and using the Color Matrix Filter (individual enemies with <cycleHue> will cycle through the hue degrees):


Edit: Re-did the video so it should show the battle from the beginning.
maybe there is also a way for map events to cycle?
unless that is hooked to ScreenTint?
I never even considered events!

Now that you say that, I wanted to find a way to do silhouettes of events without having to manually recolor them. Maybe this can lean into it!

Thx for the suggestion!
I do have something as sillouet for actors to walk behind sprites.
but its not the same for events I think.

I still can add color differently, but no idea for events my self.
but if Game_Events can call the hue cycle, it would be very easy to add.

like

Game_Event.prototype.cycleHue = function() {
sprite.prototype.cycleHue.call(this)
}

but I think it should be different in this case as events aren't always
sprites unless set?

I never went looking how far game event can or is possible to hook
with the function you currently have, but the above is a sample.

I did something with numberInput calling the choice function
as they share the same code, so it was easier that way.

but unsure for Game_Event, but if you make rainbow enemies on
map, they are equal in battle (front/sideview) as well.
I'll check it out when I can. Seems like a very worthwhile addition.

Original thread: https://forums.rpgmakerweb.com/threads/rainbow-enemies.171189/