Displacement Filters

Code:
//=============================================================================
//Displacement Maps
//DisplacementMaps.js
//=============================================================================
var Imported = Imported || {};
Imported.DisplacementMap = true;

var Rachnera = Rachnera || {};
Rachnera.DisplacementMap = Rachnera.DisplacementMap || {};
Rachnera.DisplacementMap.version = 1.0

//=============================================================================
var displacementSprite = PIXI.Sprite.fromImage("img/filters/underwater.png");
displacementSprite.texture.baseTexture.wrapMode = PIXI.WRAP_MODES.REPEAT;
var displacementFilter = new PIXI.filters.DisplacementFilter(displacementSprite);

//=============================================================================
//SceneManager
//=============================================================================
SceneManager.renderScene = function() {
    if (this.isCurrentSceneStarted()) {
        if (this._scene.filters !== null)
        {
            displacementSprite.x += 1;
            displacementSprite.y += 1;
        }       
        Graphics.render(this._scene);
    } else if (this._scene) {
        this.onSceneLoading();
    }
};

//=============================================================================
//Game_Interpreter
//=============================================================================
Rachnera.DisplacementMap.Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args) {
    if (command === 'Rachnera') {
        if (args[0] === 'DisplacementMap') {
            if(SceneManager._scene.filters === null) {
                SceneManager._scene.filters = [displacementFilter];
                SceneManager._scene.addChild(displacementSprite);
            }
        }
    }
    else Rachnera.DisplacementMap.Game_Interpreter_pluginCommand.call(this, command, args);
}

I figured out what i did wrong, but still have a few things left to sort out.
1. It currently effects the entire stage. I don't want it to effect the windowLayer. Is this possible?
2. Is it possible to make it so displacementFilter and displacementSprite aren't global variables?
Bump
You can do all of that work inside of Scene_Map.

1. Add the displacement filter to the scenes spriteset, this will ensure it only affects the map spriteset and not the windows.
2. Yes, do it all in Scene_Map with one global variable to keep track of the mapIDs that require the filter.

This is what I was thinking, I never tested it but its the same idea you have. Only issue is the array that stores map IDs will reset everytime you restart the game and will not save with the save file. But you can remedy that by putting it in Game_System so it saves with the game.
Also keep in mind if the mapId is not already there when the map is loaded then it won't add the filter but I added a refresh plugin command to solve that issue.

PHP:
const mapsForDisplacement = []

Scene_Map.prototype.createDisplacementFilter = function () {
  mapsForDisplacement.forEach(mapId => {
    if (mapId === $gameMap.mapId() && !this._displacementSprite) {
      this._displacementSprite = PIXI.Sprite.fromImage('img/filters/underwater.png')
      this._displacementSprite.texture.baseTexture.wrapMode = PIXI.WRAP_MODES.REPEAT
      this._displacmentFilter = new PIXI.filters.DisplacementFilter(this._displacementSprite)

      // Add to the spriteset(Spriteset_Map) so the filter won't affect windows
      this._spriteset.filters = [this._displacmentFilter]
      this._spriteset.addChild(this._displacementSprite)
    }
  })
}

Rachnera.DisplacementMap.Scene_Map_createDisplayObjects = Scene_Map.prototype.createDisplayObjects
Scene_Map.prototype.createDisplayObjects = function () {
  Rachnera.DisplacementMap.Scene_Map_createDisplayObjects.call(this)

  // Attempt to create filter when map is initialized
  this.createDisplacementFilter()
}

Rachnera.DisplacementMap.Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand

Game_Interpreter.prototype.pluginCommand = function (command, args) {
  if (command === 'Rachnera') {
    if (args[0] === 'DisplacementMap') {
      mapsForDisplacement.push($gameMap.mapId())
    }
    if (args[0] === 'Refresh') {
      SceneManager._scene.createDisplacementFilter()
    }
  } else {
    Rachnera.DisplacementMap.Game_Interpreter_pluginCommand.call(this, command, args)
  }
}
Only issue that code has is that displacementSprite would be left undefined in SceneManager.renderScene()
Code:
SceneManager.renderScene = function() {
    if (this.isCurrentSceneStarted()) {
        if (this._scene.filters !== null)
        {
            displacementSprite.x += 1;
            displacementSprite.y += 1;
        }       
        Graphics.render(this._scene);
    } else if (this._scene) {
        this.onSceneLoading();
    }
};
Remove that entirely and just update the x and y in the Scene_Map update function instead. renderScene should really never be touched unless you absolutely need to, as all Scenes have update functions where you can perform those kinds of actions.
Pretty sure I tried all of this before and it didn't work, but I'll try again.
Code:
//=============================================================================
//Displacement Maps
//DisplacementMaps.js
//=============================================================================
var Imported = Imported || {};
Imported.DisplacementMap = true;

var Rachnera = Rachnera || {};
Rachnera.DisplacementMap = Rachnera.DisplacementMap || {};
Rachnera.DisplacementMap.version = 1.0

//=============================================================================
//DataManager
//=============================================================================

DataManager.getDisplacementMap = function() {
    if (!$dataMap) return '';
    var notedata = $dataMap.note.split(/[\r\n]+/);
    for (var i = 0; i < notedata.length; i++) {
        var line = notedata[i];
        if (line.match(/<DISPLACEMENTMAP:[ ](\w+)>/i)) {
            return String(RegExp.$1);;
        }
    }
    return '';
};

//=============================================================================
//Scene_Map
//=============================================================================
Rachnera.DisplacementMap.Scene_Map_createDisplayObjects = Scene_Map.prototype.createDisplayObjects
Scene_Map.prototype.createDisplayObjects = function () {
    Rachnera.DisplacementMap.Scene_Map_createDisplayObjects.call(this);
    this.createDisplacementFilter();
}

Scene_Map.prototype.createDisplacementFilter = function () {
    var filename = DataManager.getDisplacementMap();
    if (filename !== null && !this._displacementSprite) {
        var path = 'img/filters/' + encodeURIComponent(filename) + '.png';
        this._displacementSprite = PIXI.Sprite.fromImage(path);
        this._displacementSprite.texture.baseTexture.wrapMode = PIXI.WRAP_MODES.REPEAT;
        this._displacmentFilter = new PIXI.filters.DisplacementFilter(this._displacementSprite);

        // Add to the spriteset(Spriteset_Map) so the filter won't affect windows
        this._spriteset.filters = [this._displacmentFilter];
        this._spriteset.addChild(this._displacementSprite);
    }
}

Rachnera.DisplacementMap.Scene_Map.update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
    if(this.displacementSprite !== null){
        this._displacementSprite.x += 1;
        this._displacementSprite.y += 1;
    }
    Rachnera.DisplacementMap.Scene_Map.update.call(this);
}

Tried doing what you said. And the displacementSprite isn't scrolling. (which is the same problem i had before) Did i miss something?

edit: yes i did
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/displacement-filters.109301/