Alpha Gradient
by roomyrooms
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/5.2.2/pixi.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pixi-filters@latest/dist/pixi-filters.js"></script>
<script type="x-data-url" id="tree-url">
...
JavaScript
var treeUrl = document.getElementById('tree-url').textContent;
var treeImg = document.getElementById('tree-preview');
treeImg.src = treeUrl;
var fragmentShader = `
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform highp vec4 inputSize;
uniform highp vec4 outputFrame;
uniform vec2 lightDist;
uniform float time;
void main() {
vec2 f = vTextureCoord * inputSize.xy / outputFrame.zw;
vec4 color = texture2D(uSampler, vTextureCoord);
float dist = 1.0-distance(f,lightDist);
float adjDist = dist * clamp(smoothstep(0.75, 1.0, lightDist.y),0.1,0.8);
gl_FragColor = color * adjDist;
}`
function defaultValue(inputValue, defaultValue) {
inputValue = typeof inputValue !== 'undefined' ? inputValue : defaultValue;
return inputValue;
}
function LightFilter(lightDist) {
PIXI.Filter.call(this,undefined,fragmentShader);
this.uniforms.lightDist = defaultValue(lightDist,[0,0]);
this.uniforms.time = performance.now();
}
LightFilter.prototype = Object.create(PIXI.Filter.prototype);
LightFilter.prototype.constructor = LightFilter;
LightFilter.prototype.apply = function (filterManager, input, output, clear) {
clear = PIXI.CLEAR_MODES;
this.uniforms.time = performance.now();
// draw the filter...
filterManager.applyFilter(this, input, output);
}
var stage = new PIXI.Container();
// create a renderer instance.
var renderer = PIXI.autoDetectRenderer(512, 512);
renderer.roundPixels = true;
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
// tex creation
var treeBaseTexture = new PIXI.BaseTexture(treeImg);
var treeTexture = new PIXI.Texture(treeBaseTexture);
var treeSprite = new PIXI.Sprite(treeTexture);
var treeBaseTexture = new PIXI.BaseTexture(treeImg);
var treeTexture = new PIXI.Texture(treeBaseTexture);
var treeSprite = new PIXI.Sprite(treeTexture);
// ADDITIVE blend mode for pretty intersecting lights
// move a little bit one light on right to check proper lights intersect
treeSprite.x =...