Hi all!
For a plugin I'm making I need to use the GlowFilter effect.
I know that there are valid free plugins that handle new PIXI.filters out there but I wanted to incorporate it in my plugin to avoid forcing people to download other stuff for compatibility.
I'm NOT versed in all the webGL stuff needed but somehow I managed to come to this:
When I create a new filter it SEEMS to give a decent result in line with other filters but then if I try to make a test image it won't show up
The image is normal, without glow.
Anyone with a better knoweledge can help me?
For a plugin I'm making I need to use the GlowFilter effect.
I know that there are valid free plugins that handle new PIXI.filters out there but I wanted to incorporate it in my plugin to avoid forcing people to download other stuff for compatibility.
I'm NOT versed in all the webGL stuff needed but somehow I managed to come to this:
JavaScript:
class GlowFilter extends PIXI.Filter {
constructor(dist,outerStr,innerStr,colorHex,qualityValue,knockFlag) {
const vertex = "attribute vec2 aVertexPosition;\nattribute vec2 aTextureCoord;\n\nuniform mat3 projectionMatrix;\n\nvarying vec2 vTextureCoord;\n\nvoid main(void)\n{\n gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);\n vTextureCoord = aTextureCoord;\n}";
let fragment = "varying vec2 vTextureCoord;\nvarying vec4 vColor;\n\nuniform sampler2D uSampler;\n\nuniform float outerStrength;\nuniform float innerStrength;\n\nuniform vec4 glowColor;\n\nuniform vec4 filterArea;\nuniform vec4 filterClamp;\nuniform bool knockout;\n\nconst float PI = 3.14159265358979323846264;\n\nconst float DIST = __DIST__;\nconst float ANGLE_STEP_SIZE = min(__ANGLE_STEP_SIZE__, PI * 2.0);\nconst float ANGLE_STEP_NUM = ceil(PI * 2.0 / ANGLE_STEP_SIZE);\n\nconst float MAX_TOTAL_ALPHA = ANGLE_STEP_NUM * DIST * (DIST + 1.0) / 2.0;\n\nvoid main(void) {\n vec2 px = vec2(1.0 / filterArea.x, 1.0 / filterArea.y);\n\n float totalAlpha = 0.0;\n\n vec2 direction;\n vec2 displaced;\n vec4 curColor;\n\n for (float angle = 0.0; angle < PI * 2.0; angle += ANGLE_STEP_SIZE) {\n direction = vec2(cos(angle), sin(angle)) * px;\n\n for (float curDistance = 0.0; curDistance < DIST; curDistance++) {\n displaced = clamp(vTextureCoord + direction * \n (curDistance + 1.0), filterClamp.xy, filterClamp.zw);\n\n curColor = texture2D(uSampler, displaced);\n\n totalAlpha += (DIST - curDistance) * curColor.a;\n }\n }\n \n curColor = texture2D(uSampler, vTextureCoord);\n\n float alphaRatio = (totalAlpha / MAX_TOTAL_ALPHA);\n\n float innerGlowAlpha = (1.0 - alphaRatio) * innerStrength * curColor.a;\n float innerGlowStrength = min(1.0, innerGlowAlpha);\n \n vec4 innerColor = mix(curColor, glowColor, innerGlowStrength);\n\n float outerGlowAlpha = alphaRatio * outerStrength * (1. - curColor.a);\n float outerGlowStrength = min(1.0 - innerColor.a, outerGlowAlpha);\n\n vec4 outerGlowColor = outerGlowStrength * glowColor.rgba;\n \n if (knockout) {\n float resultAlpha = outerGlowAlpha + innerGlowAlpha;\n gl_FragColor = vec4(glowColor.rgb * resultAlpha, resultAlpha);\n }\n else {\n gl_FragColor = innerColor + outerGlowColor;\n }\n}\n";
fragment = fragment.replace(/__ANGLE_STEP_SIZE__/gi, "" + (1 / (qualityValue || 0.1) / Math.round(dist || 10)).toFixed(7));
fragment = fragment.replace(/__DIST__/gi, Math.round(dist || 10).toFixed(0) + ".0");
super(vertex, fragment);
// Set default properties
this.distance = Math.round(dist || 10);
this.outerStrength = outerStr || 4;
this.innerStrength = innerStr || 0;
this.color = colorHex || 0xffffff;
this.quality = qualityValue || 0.1;
this.knockout = knockFlag || false;
this.uniforms.glowColor = new Float32Array([0, 0, 0, 1]);
}
}
PIXI.filters.glowFilter = GlowFilter;
When I create a new filter it SEEMS to give a decent result in line with other filters but then if I try to make a test image it won't show up
JavaScript:
const sprite = new Sprite(ImageManager.loadPicture("Actor1_1"));
checkLoad();
function checkLoad() {
if (ImageManager.isReady()) {
sprite.x = 0;
sprite.y = 0;
const glowFilter = new PIXI.filters.glowFilter(20, 10, 0, 0xffffff, 0.2, false);
sprite.filters = [glowFilter];
console.log("Glow filter is...");
console.dir(glowFilter);
SceneManager._scene.addChild(sprite);
} else {
requestAnimationFrame(checkLoad);
}
}
The image is normal, without glow.
Anyone with a better knoweledge can help me?