- 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
import { vertex } from '@tools/fragments';
import fragment from './motion-blur.frag';
import { Filter } from '@pixi/core';
import { ObservablePoint, Point } from '@pixi/math';
import type { IPoint } from '@pixi/math';
import type { FilterSystem, RenderTexture } from '@pixi/core';
import type { CLEAR_MODES } from '@pixi/constants';
/**
* The MotionBlurFilter applies a Motion blur to an object.<br>
* 
*
* @class
* @extends PIXI.Filter
* @memberof PIXI.filters
* @see @pixi/filter-motion-blur
* @see pixi-filters
*/
class MotionBlurFilter extends Filter
{
/**
* The kernelSize of the blur, higher values are slower but look better.
* Use odd value greater than 5.
*/
public kernelSize = 5;
private _velocity: IPoint;
/**
* @param {PIXI.ObservablePoint|PIXI.Point|number[]} [velocity=[0, 0]] - Sets the velocity of the motion for blur effect.
* @param {number} [kernelSize=5] - The kernelSize of the blur filter. Must be odd number >= 5
* @param {number} [offset=0] - The offset of the blur filter.
*/
constructor(velocity: number[] | Point | ObservablePoint = [0, 0], kernelSize = 5, offset = 0)
{
super(vertex, fragment);
this.uniforms.uVelocity = new Float32Array(2);
this._velocity = new ObservablePoint(this.velocityChanged, this);
this.setVelocity(velocity);
this.kernelSize = kernelSize;
this.offset = offset;
}
/**
* Override existing apply method in PIXI.Filter
* @private
*/
apply(filterManager: FilterSystem, input: RenderTexture, output: RenderTexture, clear: CLEAR_MODES): void
{
const { x, y } = this.velocity;
this.uniforms.uKernelSize = (x !== 0 || y !== 0) ? this.kernelSize : 0;
filterManager.applyFilter(this, input, output, clear);
}
/**
* Sets the velocity of the motion for blur effect.
*
* @member {PIXI.ObservablePoint|PIXI.Point|number[]}
*/
set velocity(value: IPoint)
{
this.setVelocity(value);
}
get velocity(): IPoint
{
return this._velocity;
}
/**
* Set velocity with more broad types
*/
private setVelocity(value: IPoint | number[])
{
if (Array.isArray(value))
{
const [x, y] = value;
this._velocity.set(x, y);
}
else
{
this._velocity.copyFrom(value);
}
}
/**
* Handle velocity changed
* @private
*/
private velocityChanged()
{
this.uniforms.uVelocity[0] = this._velocity.x;
this.uniforms.uVelocity[1] = this._velocity.y;
// The padding will be increased as the velocity and intern the blur size is changed
this.padding = (Math.max(Math.abs(this._velocity.x), Math.abs(this._velocity.y)) >> 0) + 1;
}
/**
* The offset of the blur filter.
* @default 0
*/
set offset(value: number)
{
this.uniforms.uOffset = value;
}
get offset(): number
{
return this.uniforms.uOffset;
}
}
export { MotionBlurFilter };