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);
    
  • Create pipeline instance
    var pipelineInstance = scene.plugins.get('rexpixelationpipelineplugin').add(scene, key, 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);
    
  • Create pipeline instance
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(scene, key, config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import PixelationPipeline from 'phaser3-rex-plugins/plugins/pixelationpipeline.js';
    
  • Create pipeline instance
    var pipelineInstance = new PixelationPipeline(scene, key, config);
    

Apply filter

  1. Create pipeline instance
    var pipelineInstance = scene.plugins.get('rexPixelationPipeline').add(scene, key, {
        // pixelWidth: 0,
        // pixelHeight: 0
    });
    
  2. Add to render pipeline
    scene.game.renderer.addPipeline(pipelineName, pipelineInstance);
    
  3. Apply filter
    • To camera
      // var camera = scene.cameras.main;
      camera.setRenderToTexture(pipelineName);
      
    • To game object
      gameObject.setPipeline(pipelineName);
      
      • pipelineName : Name of this render pipeline, a string.

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);