Skip to content

Pixelation

Note

Phaser3.60 has a built-in pixelate effect.

Introduction

Pixelation post processing filter. Reference

  • Author: Rex
  • A post-fx shader effect

WebGL only

Only work in WebGL render mode.

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rexpixelationpipelineplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexpixelationpipelineplugin.min.js', true);
    
  • Apply effect
    • Apply effect to game object
      var pipelineInstance = scene.plugins.get('rexpixelationpipelineplugin').add(gameObject, config);
      
    • Apply effect to camera
      var pipelineInstance = scene.plugins.get('rexpixelationpipelineplugin').add(camera, config);
      

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import PixelationPipelinePlugin from 'phaser3-rex-plugins/plugins/pixelationpipeline-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexPixelationPipeline',
                plugin: PixelationPipelinePlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Apply effect
    • Apply effect to game object
      var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(gameObject, config);
      
    • Apply effect to camera
      var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(camera, config);
      

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Add to game config
    import PixelationPostFx from 'phaser3-rex-plugins/plugins/pixelationpipeline.js';
    var config = {
        // ...
        pipeline: [PixelationPostFx]
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Apply effect
    • Apply effect to game object
      gameObject.setPostPipeline(PixelationPostFx);
      
    • Apply effect to camera
      camera.setPostPipeline(PixelationPostFx);
      

Apply effect

  • Apply effect to game object. A game object only can add 1 pixelation effect.
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(gameObject, {
        // pixelWidth: 0,
        // pixelHeight: 0,
    
        // name: 'rexPixelationPostFx'
    });
    
    • pixelWidth, pixelHeight : Pixel size.
  • Apply effect to camera. A camera only can add 1 pixelation effect.
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(camera, config);
    

Remove effect

  • Remove effect from game object
    scene.plugins.get('rexPixelationPipeline').remove(gameObject);
    
  • Remove effect from camera
    scene.plugins.get('rexPixelationPipeline').remove(camera);
    

Get effect

  • Get effect from game object
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').get(gameObject)[0];
    // var pipelineInstances = scene.plugins.get('rexPixelationPipeline').get(gameObject);
    
  • Get effect from camera
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').get(camera)[0];
    // var pipelineInstances = scene.plugins.get('rexPixelationPipeline').get(camera);
    

Pixel size

  • Get
    var pixelWidth = pipelineInstance.pixelWidth;
    var pixelHeight = pipelineInstance.pixelHeight;
    
  • Set
    pipelineInstance.pixelWidth = pixelWidth;
    pipelineInstance.pixelHeight = pixelHeight;
    // pipelineInstance.pixelWidth += value;
    // pipelineInstance.pixelHeight += value;
    
    or
    pipelineInstance.setPixelWidth(pixelWidth);
    pipelineInstance.setPixelHeight(pixelHeight);
    pipelineInstance.setPixelSize(pixelWidth, pixelHeight);