- 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
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
import { vertex } from '@tools/fragments';
import fragment from './glow.frag';
import { Filter } from '@pixi/core';
import { rgb2hex, hex2rgb } from '@pixi/utils';
interface GlowFilterOptions {
distance: number;
outerStrength: number;
innerStrength: number;
color: number;
quality: number;
knockout: boolean;
}
/**
* GlowFilter, originally by mishaa
* [codepen]{@link http://codepen.io/mishaa/pen/raKzrm}.<br>
* 
* @class
*
* @extends PIXI.Filter
* @memberof PIXI.filters
* @see @pixi/filter-glow
* @see pixi-filters
*
* @example
* someSprite.filters = [
* new GlowFilter({ distance: 15, outerStrength: 2 })
* ];
*/
class GlowFilter extends Filter
{
/** Default values for options. */
static readonly defaults: GlowFilterOptions = {
distance: 10,
outerStrength: 4,
innerStrength: 0,
color: 0xffffff,
quality: 0.1,
knockout: false,
};
/**
* @param {number} [options] - Options for glow.
* @param {number} [options.distance=10] - The distance of the glow. Make it 2 times more for resolution=2.
* It can't be changed after filter creation.
* @param {number} [options.outerStrength=4] - The strength of the glow outward from the edge of the sprite.
* @param {number} [options.innerStrength=0] - The strength of the glow inward from the edge of the sprite.
* @param {number} [options.color=0xffffff] - The color of the glow.
* @param {number} [options.quality=0.1] - A number between 0 and 1 that describes the quality of the glow.
* The higher the number the less performant.
* @param {boolean} [options.knockout=false] - Toggle to hide the contents and only show glow.
*/
constructor(options?: Partial<GlowFilterOptions>)
{
const opts: GlowFilterOptions = Object.assign({}, GlowFilter.defaults, options);
const {
outerStrength,
innerStrength,
color,
knockout,
quality } = opts;
const distance = Math.round(opts.distance);
super(vertex, fragment
.replace(/__ANGLE_STEP_SIZE__/gi, `${(1 / quality / distance).toFixed(7)}`)
.replace(/__DIST__/gi, `${distance.toFixed(0)}.0`));
this.uniforms.glowColor = new Float32Array([0, 0, 0, 1]);
Object.assign(this, {
color,
outerStrength,
innerStrength,
padding: distance,
knockout,
});
}
/**
* The color of the glow.
* @default 0xFFFFFF
*/
get color(): number
{
return rgb2hex(this.uniforms.glowColor);
}
set color(value: number)
{
hex2rgb(value, this.uniforms.glowColor);
}
/**
* The strength of the glow outward from the edge of the sprite.
* @default 4
*/
get outerStrength(): number
{
return this.uniforms.outerStrength;
}
set outerStrength(value: number)
{
this.uniforms.outerStrength = value;
}
/**
* The strength of the glow inward from the edge of the sprite.
* @default 0
*/
get innerStrength(): number
{
return this.uniforms.innerStrength;
}
set innerStrength(value: number)
{
this.uniforms.innerStrength = value;
}
/**
* Only draw the glow, not the texture itself
* @default false
*/
get knockout(): boolean
{
return this.uniforms.knockout;
}
set knockout(value: boolean)
{
this.uniforms.knockout = value;
}
}
export { GlowFilter };
export type { GlowFilterOptions };