Edit:
I’ve demonstrated this using lab code for replicability’s sake:
You can input the following code into http://labs.phaser.io console.
Click Grayscale, Reset, then grayscale again.
var DemoA = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function DemoA ()
{
Phaser.Scene.call(this, { key: 'demoA', active: true });
},
preload: function ()
{
this.load.image('picA', 'assets/pics/lance-overdose-loader-eye.png');
this.load.image('picB', 'assets/pics/sukasuka-chtholly.png');
},
create: function ()
{
this.grayscalePipeline = this.game.renderer.addPipeline('Grayscale', new GrayscalePipeline(this.game));
const text = this.add.text(0, 0, 'grayscale', {
color: 'white',
fontSize: 20
});
const newText = this.add.text(0, 50, 'reset scene', {
color: 'white',
fontSize: 20
});
text.setInteractive({ useHandCursor: true });
newText.setInteractive({ useHandCursor: true });
text.on('pointerup', () => {
this.cameras.main.setRenderToTexture(this.grayscalePipeline);
});
newText.on('pointerup', () => {
this.scene.restart();
});
this.add.image(400, 300, 'picA');
this.input.once('pointerdown', function () {
this.scene.sendToBack();
}, this);
}
});
var DemoB = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function DemoB ()
{
Phaser.Scene.call(this, { key: 'demoB', active: true });
},
preload: function ()
{
this.load.image('picB', 'assets/pics/sukasuka-chtholly.png');
},
create: function ()
{
this.add.image(400, 500, 'picB');
}
});
var DemoC = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function DemoC ()
{
Phaser.Scene.call(this, { key: 'demoC', active: true });
},
preload: function ()
{
this.load.image('picC', 'assets/pics/titan-mech.png');
},
create: function ()
{
this.add.image(300, 300, 'picC');
}
});
var GrayscalePipeline = new Phaser.Class({
Extends: Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline,
initialize:
function GrayscalePipeline (game)
{
Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline.call(this, {
game: game,
renderer: game.renderer,
fragShader:`
precision mediump float;
uniform sampler2D uMainSampler;
varying vec2 outTexCoord;
void main(void) {
vec4 color = texture2D(uMainSampler, outTexCoord);
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(gray), 1.0);
}`
});
}
});
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-example',
scene: [ DemoB, DemoC, DemoA ]
};
var game = new Phaser.Game(config);