//=============================================================================
// CBR_picNoise.js
// ----------------------------------------------------------------------------
// (C)2023 COBRA
// This software is released under the MIT License.
// https://twitter.com/onarinin_san
// ----------------------------------------------------------------------------
//=============================================================================
/*:ja
 *
 * @help オッサンが砂みたいに消える
 * 利用規約：なし
 * 用語：
 * 砂嵐→白と黒がチカチカなるヤツ
 * ノイズ→画像の一部分がバグみたいに表示されるヤツ
 */



PIXI.filters.breakFilter = function () {
	var fragmentSrc = [
		'precision lowp float;',
		'uniform sampler2D uSampler;',
		'varying vec2 vTextureCoord;',
		"uniform float key1;",
		"uniform float pxSize;",

		"float random (vec2 st) {",
			"return fract(sin(dot(floor(st.xy*pxSize)/pxSize, vec2(2.9,52.3)))*43718.341 * key1);",
		"}",

		'void main (void) {',
			"vec2 coord = vTextureCoord;",
			"vec2 st = vTextureCoord.xy;",
			"float hoge = random( st );",

			"if (key1 < fract(hoge)) {",//0-0.999乱数がkey未満だったら 0.1だと大部分が透明、0.9だとほぼそのまま
				"gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);",
			"} else {",
				"gl_FragColor = texture2D(uSampler, coord );",
			"}",
		'}'
	];
	PIXI.Filter.call(this,
		null, // vertex shader
		fragmentSrc.join('\n'), // fragment shader
		{t:0.0} // uniforms
	);
};

PIXI.filters.breakFilter.prototype = Object.create(PIXI.Filter.prototype);
PIXI.filters.breakFilter.prototype.constructor = PIXI.filters.breakFilter;