Skip to content

Barrel

Note

Phaser3.60 has a built-in barrel effect.

Introduction

Barrel 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('rexbarrelpipelineplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexbarrelpipelineplugin.min.js', true);
    
  • Apply effect
    • Apply effect to game object
      var pipelineInstance = scene.plugins.get('rexbarrelpipelineplugin').add(gameObject, config);
      
    • Apply effect to camera
      var pipelineInstance = scene.plugins.get('rexbarrelpipelineplugin').add(camera, config);
      

Import plugin

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

Import class

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

Apply effect

  • Apply effect to game object. A game object only can add 1 barrel effect.
    var pipelineInstance = scene.plugins.get('rexBarrelPipeline').add(gameObject, {
        // shrink: false,
        // center: {
        //    x: windowWidth / 2,
        //    y: windowHeight / 2
        //}
        // radius: 0,
        // power: 0.5,
        // intensity: 1,
    
        // name: 'rexBarrelPostFx'
    });
    
    • shrink :
      • false : Fish-eye effect
      • true : Anti fish-eye effect.
    • center.x, center.y : Local position of barrel center.
    • radius : Barrel radius.
    • power : 0~1.
    • intensity : 0(original) ~ 1(barrel). Default value is 1.
  • Apply effect to camera. A camera only can add 1 barrel effect.
    var pipelineInstance = scene.plugins.get('rexBarrelPipeline').add(camera, config);
    

Remove effect

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

Get effect

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

Shrink mode

  • Get
    var shrinkMode = pipelineInstance.shrinkMode;
    
  • Set
    pipelineInstance.setShrinkMode(true);
    // pipelineInstance.setShrinkMode(false);
    

Radius

  • Get
    var radius = pipelineInstance.radius;
    
  • Set
    pipelineInstance.radius = radius;
    // pipelineInstance.radius += value;
    
    or
    pipelineInstance.setRadius(radius);
    

Power

  • Get
    var power = pipelineInstance.power;
    
  • Set
    pipelineInstance.power = power;
    
    or
    pipelineInstance.setPower(power);
    

Intensity

  • Get
    var intensity = pipelineInstance.intensity;
    
  • Set
    pipelineInstance.intensity = intensity;
    // pipelineInstance.intensity += value;
    
    or
    pipelineInstance.setIntensity(radius);
    
    • intensity : 0(original) ~ 1(barrel)

Center position

Default value is center of window.

  • Get
    var x = pipelineInstance.centerX;
    var y = pipelineInstance.centerY;
    
  • Set
    pipelineInstance.centerX = x;
    pipelineInstance.centerY = y;
    
    or
    pipelineInstance.setCenter(x, y);
    // pipelineInstance.setCenter();   // set to center of window