JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.0.0-rc3/pixi.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script id="default-vertex-shader" type="notjs">
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat3 projectionMatrix;
varying vec2 vTextureCoord;
void main(void){
gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
<script id="palette-fragment-shader" type="notjs">
precision mediump float;
uniform sampler2D uSampler;
uniform float palette[15];
uniform float pIndex;
varying vec2 vTextureCoord;
void main() {
//TODO: get from pIndex to these values if pIndex == 3
gl_FragColor.r = palette[9];
gl_FragColor.g = palette[10];
gl_FragColor.b = palette[11];
gl_FragColor.a = 1.0;
}
</script>
JavaScript
var PaletteFilter = function(initPalettes) {
var vertexShader = $("#default-vertex-shader").text();
var fragmentShader = $("#palette-fragment-shader").text();
PIXI.Filter.call(this, vertexShader, fragmentShader, {
palette: {
value: initPalettes
},
pIndex: {
value: 3
}
});
}
PaletteFilter.prototype = Object.create(PIXI.Filter.prototype);
PaletteFilter.prototype.constructor = PaletteFilter;
Object.defineProperties(PaletteFilter.prototype, {
palette: {
get: function() {
return this.uniforms.palette;
},
set: function(value) {
this.uniforms.palette = value;
}
},
pIndex: {
get: function() {
return this.uniforms.pIndex;
},
set: function(value) {
this.uniforms.pIndex = value;
}
},
});
var renderer = PIXI.autoDetectRenderer(500, 500);
document.body.appendChild(renderer.view);
var stage = new PIXI.Container();
var graphics = new PIXI.Graphics();
graphics.drawRect(0,0,500,500);
graphics.filters = [new PaletteFilter(
[
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 0.0, //yellow
1.0, 1.0, 1.0
]
)];
stage.addChild(graphics);
requestAnimationFrame(mainGameLoop);
function mainGameLoop() {
requestAnimationFrame(mainGameLoop);
renderer.render(stage);
}