Prevent jitter when tile sets are swapped.

I am looking to have overpasses in my maps and am exploring options like the "default" overpasstile.js and Avery's tile-set-swap method.


Swapping out tile sets seems to be the easiest to manage, but there is a noticeable jitter/hiccup when the tiles are swapped. Would there be any way to reduce this? Since it also happens when the two tile sets use the exact same graphical resources I suspect that it has to do with reading the json files. Would there, for example, be a way to have these json files pre-loaded?
Have you tried using a preloader itself?
Can you test something. Does the lag spike happen literally every time the tile set it swapped, or does it only happen the very first time the tileset is swapped and then smooths out there after?
Have you tried using a preloader itself?
Can you test something. Does the lag spike happen literally every time the tile set it swapped, or does it only happen the very first time the tileset is swapped and then smooths out there after?
It happens everytime. I’ve tried preloading the tiles using VS CORE, but that didn’t help, hence my suspicion that it’s the json reading/parsing that is the bottleneck.
$dataTilesets is part of the database, which is loaded once per session, on game boot. Do you have some reason to believe otherwise?

Without plugins, the Change Tileset command (rmmz_objects):
  • Loads the new tileset's images into memory as needed;
  • Once they're all loaded, the map updates its tileset ID and refreshes itself.
The spriteset (rmmz_sprites) then assigns the bitmaps to the tilemap, which updates its render data accordingly (rmmz_core).

I just tested with an event that cycles through the 6 default tilesets when activated:

◆Change Tileset:Dungeon ◆Change Tileset:SF Outside ◆Change Tileset:SF Inside ◆Change Tileset:Overworld ◆Change Tileset:Outside ◆Change Tileset:Inside ◆Text:None, None, Window, Bottom : :Whoa, what just happened?
  • The first test, the framerate (F2) drops dramatically while each of the tilesets flicks by.
  • Subsequent tests show a solid 60 fps with no sign that the commands are even there.
Leaving and returning to the map causes the first test case to happen again. This is expected, because the image cache gets cleared on map transfer.

The delay you're seeing might be various things, e.g.
  • Disk access, when loading images into memory;
  • Map refresh, which involves refreshing all map events etc;
  • Something in the tilemap's render pipeline, e.g. slow GPU upload.
Of course, plugins can affect this, e.g. by adding other processes on map/event refresh. Other processes on your system (e.g. Windows Update) might also consume relevant resources to the point that your game slows unexpectedly. Framerate is just a threshold.

Have you tested:
  1. With all plugins turned off?
  2. On a single-screen map with, like, only 1 event?
Have you tested:
  1. With all plugins turned off?
  2. On a single-screen map with, like, only 1 event?
Thanks for the detailed explanation. Will try to test with a smaller map later. Turning the plugins off does make the FPS drop less, but not go away (160 to 120-ish when everything turned off, versus 160 to 50-ish with everything turned on). Graphics are RTX 3060, by the way, so on machines with less I fear it might be really jarring.
Turning the plugins off does make the FPS drop less, but not go away (160 to 120-ish when everything turned off
I might misunderstand something, but...you shouldn't notice this unless you're doing something that displays the frame rate.

MZ's refreshes are all based on 60 frames a second. So while your monitor might display more, that's what the game is actually doing - you shouldn't be able to visually distinguish any kind of lag unless the FPS goes below 60.

versus 160 to 50-ish with everything turned on
You might want to test specific plugins being on or off to affect that. I know you use VisuStella products, and a number of them have been tested to have a significant effect on frame rate.

Graphics are RTX 3060, by the way, so on machines with less I fear it might be really jarring.
Again, I might misunderstand something, but I don't believe that means much. Unless you're doing a lot of stuff with special PIXI effects or something, your graphics card shouldn't be doing much to run an MZ game. It's primarily CPU-driven.
MZ's refreshes are all based on 60 frames a second. So while your monitor might display more, that's what the game is actually doing - you shouldn't be able to visually distinguish any kind of lag unless the FPS goes below 60

I did test it with the FPS display off and still noticed it, so I turned it on to check. I have no idea how the FPS on screen is calculated, but if it stops for a few frames and picks up from there, the average over x ms would probably still show an FPS above 60.

You might want to test specific plugins being on or off to affect that. I know you use VisuStella products, and a number of them have been tested to have a significant effect on frame rate.

Yes, events and movement core might be the biggest offender. Which would be a real pain to replace…
Have you considered moving the tiles into one or a few large character sheets and then having the event change priority?
It’s a big bridge, so I am not entirely sure I want to go that route… I do make use of that method in a couple other places, so perhaps I can do a combination of “event tiles” and overpasstile.js… in any case will do some more testing…


It's primarily CPU-driven.

Forgot to comment on this but I could have sworn that when testing this, the GPU was at 20% and the CPU practically idle. Will check again later.

EDIT: home at last and can confirm that my playtests seem to be handled by GPU mostly.

1712395473922.png
Yea, I would suggest OverpassTile or Ocram's Layers, something like that. 👋
Spoiler: some clarifications
The engine implements a fluid timestep algorithm to try to keep its mechanical framerate at 60 fps without updating too much/little over a given interval (SceneManager.update).

However, the PIXI app's ticker updates based on the browser's refresh rate, which is often matched to the monitor but may be delayed e.g. if relevant resources are scarce. The engine's framerate widget (F2) counts every tick, keeps a weighted running average, and refreshes its displayed value every 15th tick (Graphics.FPSCounter). So it is possible that it freezes up for more than 1/60 second while never reporting a dip below 60 fps.

MZ only uses the GPU for rendering (no compute shaders in WebGL); CPU is used for the bulk of the unique engine mechanics, like event commands and coordinate updates. The relevant information is then uploaded to the GPU via shader uniforms, to be processed for that frame's render cycle. (MV uses more CPU than GPU compared to MZ in some aspects, like hue-shifts.)

CPU/GPU percentage usage depends on their relative specs. Also bear in mind that the GPU is rendering the whole screen, not just your game; similarly, the CPU will typically be close to idle if nothing much is happening, e.g. just changing map-scroll and party coordinates on an empty map.
Spoiler: technical diagnostics
If you want a more detailed approach, there's the Performance tab of the dev tools window (F8 or F12 during test). Simply click the ⬤ Record button, do your heavy task (change tileset) in-game, then click the Stop button. Wait a few moments for it to process the data, then it'll show you a bunch of graphs. Mouse over them for details!

Here I think you'd be primarily looking for a layer in the timeline that took significantly more time than the layer "below" it? In my test, on my system, that seems to consistently be gl.texSubImage2D.
Spoiler: screenshot
perf.png
I'm not an expert in this area, but I imagine that to resolve this you'd need some kind of system to permit binding a new texture without replacing the current one, then swap "slot" when appropriate. So probably a shader code edit to accept some extra uniforms, plus the JS to interface with it. Easier said than done. 😴
Thanks for the detailed explanation! I have decided to abandon the tile set swap method and after some consultation here and there have started testing Shaz_TileChangerMZ. It's surprisingly light weight and does not have the aforementioned hiccup at all. So I think I'll use this for my overpasslust going forward. (big thanks to @ATT_Turan for suggesting this)



CPU/GPU percentage usage depends on their relative specs.

It appears that a Ryzen 7 5800X is overkill to accompany a 3060, so this may explain the 5% versus 20% for CPU/GPU (when actively walking on the map). BTW, upon closing the playtest the GPU is at 5%
Easier said than done.
I will stay away... 😊
This thread is being closed due to being reported as solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/prevent-jitter-when-tile-sets-are-swapped.167492/