diff --git a/src/filters/AsciiFilter.js b/src/filters/AsciiFilter.js deleted file mode 100644 index 0cfd2fc..0000000 --- a/src/filters/AsciiFilter.js +++ /dev/null @@ -1,91 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); - -/** - * @author Vico @vicocotea - * original shader : https://www.shadertoy.com/view/lssGDj by @movAX13h - */ - -/** - * An ASCII filter. - * - * @class - * @extends AbstractFilter - * @namespace PIXI - */ -function AsciiFilter() -{ - AbstractFilter.call(this); - - // set the uniforms - this.uniforms = { - dimensions: { type: '4fv', value: new Float32Array([10000, 100, 10, 10]) }, - pixelSize: { type: '1f', value: 8} - }; - - this.fragmentSrc = [ - 'precision mediump float;', - - 'uniform vec4 dimensions;', - 'uniform float pixelSize;', - 'uniform sampler2D uSampler;', - - 'float character(float n, vec2 p)', - '{', - ' p = floor(p*vec2(4.0, -4.0) + 2.5);', - ' if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)', - ' {', - ' if (int(mod(n/exp2(p.x + 5.0*p.y), 2.0)) == 1) return 1.0;', - ' }', - ' return 0.0;', - '}', - - 'void main()', - '{', - ' vec2 uv = gl_FragCoord.xy;', - ' vec3 col = texture2D(uSampler, floor( uv / pixelSize ) * pixelSize / dimensions.xy).rgb;', - - ' #ifdef HAS_GREENSCREEN', - ' float gray = (col.r + col.b)/2.0;', - ' #else', - ' float gray = (col.r + col.g + col.b)/3.0;', - ' #endif', - - ' float n = 65536.0; // .', - ' if (gray > 0.2) n = 65600.0; // :', - ' if (gray > 0.3) n = 332772.0; // *', - ' if (gray > 0.4) n = 15255086.0; // o', - ' if (gray > 0.5) n = 23385164.0; // &', - ' if (gray > 0.6) n = 15252014.0; // 8', - ' if (gray > 0.7) n = 13199452.0; // @', - ' if (gray > 0.8) n = 11512810.0; // #', - - ' vec2 p = mod( uv / ( pixelSize * 0.5 ), 2.0) - vec2(1.0);', - ' col = col * character(n, p);', - - ' gl_FragColor = vec4(col, 1.0);', - '}' - ]; -} - -AsciiFilter.prototype = Object.create(AbstractFilter.prototype); -AsciiFilter.prototype.constructor = AsciiFilter; -module.exports = AsciiFilter; - -Object.defineProperties(AsciiFilter.prototype, { - /** - * The pixel size used by the filter. - * - * @member {number} - * @memberof AsciiFilter# - */ - size: { - get: function () - { - return this.uniforms.pixelSize.value; - }, - set: function (value) - { - this.uniforms.pixelSize.value = value; - } - } -}); diff --git a/src/filters/AsciiFilter.js b/src/filters/AsciiFilter.js deleted file mode 100644 index 0cfd2fc..0000000 --- a/src/filters/AsciiFilter.js +++ /dev/null @@ -1,91 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); - -/** - * @author Vico @vicocotea - * original shader : https://www.shadertoy.com/view/lssGDj by @movAX13h - */ - -/** - * An ASCII filter. - * - * @class - * @extends AbstractFilter - * @namespace PIXI - */ -function AsciiFilter() -{ - AbstractFilter.call(this); - - // set the uniforms - this.uniforms = { - dimensions: { type: '4fv', value: new Float32Array([10000, 100, 10, 10]) }, - pixelSize: { type: '1f', value: 8} - }; - - this.fragmentSrc = [ - 'precision mediump float;', - - 'uniform vec4 dimensions;', - 'uniform float pixelSize;', - 'uniform sampler2D uSampler;', - - 'float character(float n, vec2 p)', - '{', - ' p = floor(p*vec2(4.0, -4.0) + 2.5);', - ' if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)', - ' {', - ' if (int(mod(n/exp2(p.x + 5.0*p.y), 2.0)) == 1) return 1.0;', - ' }', - ' return 0.0;', - '}', - - 'void main()', - '{', - ' vec2 uv = gl_FragCoord.xy;', - ' vec3 col = texture2D(uSampler, floor( uv / pixelSize ) * pixelSize / dimensions.xy).rgb;', - - ' #ifdef HAS_GREENSCREEN', - ' float gray = (col.r + col.b)/2.0;', - ' #else', - ' float gray = (col.r + col.g + col.b)/3.0;', - ' #endif', - - ' float n = 65536.0; // .', - ' if (gray > 0.2) n = 65600.0; // :', - ' if (gray > 0.3) n = 332772.0; // *', - ' if (gray > 0.4) n = 15255086.0; // o', - ' if (gray > 0.5) n = 23385164.0; // &', - ' if (gray > 0.6) n = 15252014.0; // 8', - ' if (gray > 0.7) n = 13199452.0; // @', - ' if (gray > 0.8) n = 11512810.0; // #', - - ' vec2 p = mod( uv / ( pixelSize * 0.5 ), 2.0) - vec2(1.0);', - ' col = col * character(n, p);', - - ' gl_FragColor = vec4(col, 1.0);', - '}' - ]; -} - -AsciiFilter.prototype = Object.create(AbstractFilter.prototype); -AsciiFilter.prototype.constructor = AsciiFilter; -module.exports = AsciiFilter; - -Object.defineProperties(AsciiFilter.prototype, { - /** - * The pixel size used by the filter. - * - * @member {number} - * @memberof AsciiFilter# - */ - size: { - get: function () - { - return this.uniforms.pixelSize.value; - }, - set: function (value) - { - this.uniforms.pixelSize.value = value; - } - } -}); diff --git a/src/filters/ascii/AsciiFilter.js b/src/filters/ascii/AsciiFilter.js new file mode 100644 index 0000000..c5c225d --- /dev/null +++ b/src/filters/ascii/AsciiFilter.js @@ -0,0 +1,51 @@ +var core = require('../../core'); + +/** + * @author Vico @vicocotea + * original shader : https://www.shadertoy.com/view/lssGDj by @movAX13h + */ + +/** + * An ASCII filter. + * + * @class + * @extends AbstractFilter + * @namespace PIXI + */ +function AsciiFilter() +{ + core.AbstractFilter.call(this, + // vertex shader + null, + // fragment shader + require('fs').readFileSync(__dirname + '/ascii.frag', 'utf8'), + // custom uniforms + { + dimensions: { type: '4fv', value: new Float32Array([10000, 100, 10, 10]) }, + pixelSize: { type: '1f', value: 8} + } + ); +} + +AsciiFilter.prototype = Object.create(core.AbstractFilter.prototype); +AsciiFilter.prototype.constructor = AsciiFilter; +module.exports = AsciiFilter; + +Object.defineProperties(AsciiFilter.prototype, { + /** + * The pixel size used by the filter. + * + * @member {number} + * @memberof AsciiFilter# + */ + size: { + get: function () + { + return this.uniforms.pixelSize.value; + }, + set: function (value) + { + this.uniforms.pixelSize.value = value; + } + } +}); diff --git a/src/filters/AsciiFilter.js b/src/filters/AsciiFilter.js deleted file mode 100644 index 0cfd2fc..0000000 --- a/src/filters/AsciiFilter.js +++ /dev/null @@ -1,91 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); - -/** - * @author Vico @vicocotea - * original shader : https://www.shadertoy.com/view/lssGDj by @movAX13h - */ - -/** - * An ASCII filter. - * - * @class - * @extends AbstractFilter - * @namespace PIXI - */ -function AsciiFilter() -{ - AbstractFilter.call(this); - - // set the uniforms - this.uniforms = { - dimensions: { type: '4fv', value: new Float32Array([10000, 100, 10, 10]) }, - pixelSize: { type: '1f', value: 8} - }; - - this.fragmentSrc = [ - 'precision mediump float;', - - 'uniform vec4 dimensions;', - 'uniform float pixelSize;', - 'uniform sampler2D uSampler;', - - 'float character(float n, vec2 p)', - '{', - ' p = floor(p*vec2(4.0, -4.0) + 2.5);', - ' if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)', - ' {', - ' if (int(mod(n/exp2(p.x + 5.0*p.y), 2.0)) == 1) return 1.0;', - ' }', - ' return 0.0;', - '}', - - 'void main()', - '{', - ' vec2 uv = gl_FragCoord.xy;', - ' vec3 col = texture2D(uSampler, floor( uv / pixelSize ) * pixelSize / dimensions.xy).rgb;', - - ' #ifdef HAS_GREENSCREEN', - ' float gray = (col.r + col.b)/2.0;', - ' #else', - ' float gray = (col.r + col.g + col.b)/3.0;', - ' #endif', - - ' float n = 65536.0; // .', - ' if (gray > 0.2) n = 65600.0; // :', - ' if (gray > 0.3) n = 332772.0; // *', - ' if (gray > 0.4) n = 15255086.0; // o', - ' if (gray > 0.5) n = 23385164.0; // &', - ' if (gray > 0.6) n = 15252014.0; // 8', - ' if (gray > 0.7) n = 13199452.0; // @', - ' if (gray > 0.8) n = 11512810.0; // #', - - ' vec2 p = mod( uv / ( pixelSize * 0.5 ), 2.0) - vec2(1.0);', - ' col = col * character(n, p);', - - ' gl_FragColor = vec4(col, 1.0);', - '}' - ]; -} - -AsciiFilter.prototype = Object.create(AbstractFilter.prototype); -AsciiFilter.prototype.constructor = AsciiFilter; -module.exports = AsciiFilter; - -Object.defineProperties(AsciiFilter.prototype, { - /** - * The pixel size used by the filter. - * - * @member {number} - * @memberof AsciiFilter# - */ - size: { - get: function () - { - return this.uniforms.pixelSize.value; - }, - set: function (value) - { - this.uniforms.pixelSize.value = value; - } - } -}); diff --git a/src/filters/ascii/AsciiFilter.js b/src/filters/ascii/AsciiFilter.js new file mode 100644 index 0000000..c5c225d --- /dev/null +++ b/src/filters/ascii/AsciiFilter.js @@ -0,0 +1,51 @@ +var core = require('../../core'); + +/** + * @author Vico @vicocotea + * original shader : https://www.shadertoy.com/view/lssGDj by @movAX13h + */ + +/** + * An ASCII filter. + * + * @class + * @extends AbstractFilter + * @namespace PIXI + */ +function AsciiFilter() +{ + core.AbstractFilter.call(this, + // vertex shader + null, + // fragment shader + require('fs').readFileSync(__dirname + '/ascii.frag', 'utf8'), + // custom uniforms + { + dimensions: { type: '4fv', value: new Float32Array([10000, 100, 10, 10]) }, + pixelSize: { type: '1f', value: 8} + } + ); +} + +AsciiFilter.prototype = Object.create(core.AbstractFilter.prototype); +AsciiFilter.prototype.constructor = AsciiFilter; +module.exports = AsciiFilter; + +Object.defineProperties(AsciiFilter.prototype, { + /** + * The pixel size used by the filter. + * + * @member {number} + * @memberof AsciiFilter# + */ + size: { + get: function () + { + return this.uniforms.pixelSize.value; + }, + set: function (value) + { + this.uniforms.pixelSize.value = value; + } + } +}); diff --git a/src/filters/ascii/ascii.frag b/src/filters/ascii/ascii.frag new file mode 100644 index 0000000..b6844d0 --- /dev/null +++ b/src/filters/ascii/ascii.frag @@ -0,0 +1,41 @@ +precision mediump float; + +uniform vec4 dimensions; +uniform float pixelSize; +uniform sampler2D uSampler; + +float character(float n, vec2 p) +{ + p = floor(p*vec2(4.0, -4.0) + 2.5); + if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y) + { + if (int(mod(n/exp2(p.x + 5.0*p.y), 2.0)) == 1) return 1.0; + } + return 0.0; +} + +void main() +{ + vec2 uv = gl_FragCoord.xy; + vec3 col = texture2D(uSampler, floor( uv / pixelSize ) * pixelSize / dimensions.xy).rgb; + + #ifdef HAS_GREENSCREEN + float gray = (col.r + col.b)/2.0; + #else + float gray = (col.r + col.g + col.b)/3.0; + #endif + + float n = 65536.0; // . + if (gray > 0.2) n = 65600.0; // : + if (gray > 0.3) n = 332772.0; // * + if (gray > 0.4) n = 15255086.0; // o + if (gray > 0.5) n = 23385164.0; // & + if (gray > 0.6) n = 15252014.0; // 8 + if (gray > 0.7) n = 13199452.0; // @ + if (gray > 0.8) n = 11512810.0; // # + + vec2 p = mod( uv / ( pixelSize * 0.5 ), 2.0) - vec2(1.0); + col = col * character(n, p); + + gl_FragColor = vec4(col, 1.0); +}