Pixelation

Introduction

Pixelation post processing filter. Reference

  • Author: Rex
  • A camera filter

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,
    
        // nane: '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);
    
  • Get effect from camera
    var pipelineInstance = 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);