- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
import { vertex } from '@tools/fragments';
import fragment from './outline.frag';
import { Filter } from '@pixi/core';
import { rgb2hex, hex2rgb } from '@pixi/utils';
import type { FilterSystem, RenderTexture } from '@pixi/core';
import type { CLEAR_MODES } from '@pixi/constants';
/**
* OutlineFilter, originally by mishaa
* http://www.html5gamedevs.com/topic/10640-outline-a-sprite-change-certain-colors/?p=69966
* http://codepen.io/mishaa/pen/emGNRB<br>
* 
*
* @class
* @extends PIXI.Filter
* @memberof PIXI.filters
* @see @pixi/filter-outline
* @see pixi-filters *
* @example
* someSprite.filters = [new OutlineFilter(2, 0x99ff99)];
*/
class OutlineFilter extends Filter
{
/** The minimum number of samples for rendering outline. */
public static MIN_SAMPLES = 1;
/** The maximum number of samples for rendering outline. */
public static MAX_SAMPLES = 100;
private _thickness = 1;
/**
* @param {number} [thickness=1] - The tickness of the outline. Make it 2 times more for resolution 2
* @param {number} [color=0x000000] - The color of the outline.
* @param {number} [quality=0.1] - The quality of the outline from `0` to `1`, using a higher quality
* setting will result in slower performance and more accuracy.
*/
constructor(thickness = 1, color = 0x000000, quality = 0.1)
{
super(vertex, fragment.replace(/\$\{angleStep\}/, OutlineFilter.getAngleStep(quality)));
this.uniforms.thickness = new Float32Array([0, 0]);
this.uniforms.outlineColor = new Float32Array([0, 0, 0, 1]);
Object.assign(this, { thickness, color, quality });
}
/**
* Get the angleStep by quality
* @private
*/
private static getAngleStep(quality: number): string
{
const samples = Math.max(
quality * OutlineFilter.MAX_SAMPLES,
OutlineFilter.MIN_SAMPLES,
);
return (Math.PI * 2 / samples).toFixed(7);
}
apply(filterManager: FilterSystem, input: RenderTexture, output: RenderTexture, clear: CLEAR_MODES): void
{
this.uniforms.thickness[0] = this._thickness / input._frame.width;
this.uniforms.thickness[1] = this._thickness / input._frame.height;
filterManager.applyFilter(this, input, output, clear);
}
/**
* The color of the glow.
* @default 0x000000
*/
get color(): number
{
return rgb2hex(this.uniforms.outlineColor);
}
set color(value: number)
{
hex2rgb(value, this.uniforms.outlineColor);
}
/**
* The thickness of the outline.
* @default 1
*/
get thickness(): number
{
return this._thickness;
}
set thickness(value: number)
{
this._thickness = value;
this.padding = value;
}
}
export { OutlineFilter };