Toonify

Introduction

Draw outlines and quantize color in HSV domain, 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('rextoonifypipelineplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rextoonifypipelineplugin.min.js', true);
    
  • Create pipeline instance
    var pipelineInstance = scene.plugins.get('rextoonifypipelineplugin').add(scene, key, config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import ToonifyPipelinePlugin from 'phaser3-rex-plugins/plugins/toonifypipeline-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexToonifyPipeline',
                plugin: ToonifyPipelinePlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Create pipeline instance
    var pipelineInstance = scene.plugins.get('rexToonifyPipeline').add(scene, key, config);
    

Import class

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

Apply filter

  1. Create pipeline instance
    var pipelineInstance = scene.plugins.get('rexToonifyPipeline').add(scene, key, {
        // edgeThreshold: 0.2,
        // hueLevels: 0,
        // sLevels: 0,
        // vLevels: 0,
        // edgeColor: 0
    });
    
    • edgeThreshold : Threshold of edge. Set 1.1 (or any number larger then 1) to disable this feature.
    • hueLevels : Amount of hue levels. Set 0 to disable this feature.
    • sLevels : Amount of saturation levels. Set 0 to disable this feature.
    • vLevels : Amount of value levels. Set 0 to disable this feature.
    • edgeColor : Color of edge, could be a number 0xRRGGBB, or a JSON object {r:255, g:255, b:255}
  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.

Edge threshold

  • Get
    var edgeThreshold = pipelineInstance.edgeThreshold;
    
  • Set
    pipelineInstance.edgeThreshold = edgeThreshold;
    
    or
    pipelineInstance.setEdgeThreshold(value);
    
    • Set 1.1 (or any number larger then 1) to disable this feature.

Hue levels

  • Get
    var hueLevels = pipelineInstance.hueLevels;
    
  • Set
    pipelineInstance.hueLevels = hueLevels;
    
    or
    pipelineInstance.setHueLevels(value);
    
    • Set 0 to disable this feature.

Saturation levels

  • Get
    var satLevels = pipelineInstance.satLevels;
    
  • Set
    pipelineInstance.satLevels = satLevels;
    
    or
    pipelineInstance.setSatLevels(value);
    
    • Set 0 to disable this feature.

Value levels

  • Get
    var valLevels = pipelineInstance.valLevels;
    
  • Set
    pipelineInstance.valLevels = valLevels;
    
    or
    pipelineInstance.setValLevels(value);
    
    • Set 0 to disable this feature.

Edge color

  • Get
    var color = pipelineInstance.edgeColor;
    
    • color : Color object.
      • Red: color.red, 0~255.
      • Green: color.green, 0~255.
      • Blue: color.blue, 0~255.
  • Set
    pipelineInstance.setEdgeColor(value);
    
    or
    pipelineInstance.edgeColor = value;
    
    • value : A number 0xRRGGBB, or a JSON object {r:255, g:255, b:255}