Skip to content
Open
34 changes: 21 additions & 13 deletions src/adjustment/AdjustmentFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { vertex, wgslVertex } from '../defaults';
import fragment from './adjustment.frag';
import source from './adjustment.wgsl';

import type { FilterOptions } from 'pixi.js';

/** Options for the AdjustmentFilter constructor */
export interface AdjustmentFilterOptions
export interface AdjustmentFilterOptions extends FilterOptions
{
/**
* The amount of luminance
Expand Down Expand Up @@ -61,7 +63,7 @@ export interface AdjustmentFilterOptions
export class AdjustmentFilter extends Filter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: AdjustmentFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
gamma: 1,
contrast: 1,
saturation: 1,
Expand All @@ -85,7 +87,17 @@ export class AdjustmentFilter extends Filter
*/
constructor(options?: AdjustmentFilterOptions)
{
options = { ...AdjustmentFilter.DEFAULT_OPTIONS, ...options };
const {
gamma,
contrast,
saturation,
brightness,
red,
green,
blue,
alpha,
...rest
} = { ...AdjustmentFilter.DEFAULT_OPTIONS, ...options };

const gpuProgram = GpuProgram.from({
vertex: {
Expand All @@ -109,21 +121,17 @@ export class AdjustmentFilter extends Filter
glProgram,
resources: {
adjustmentUniforms: {
uGamma: { value: options.gamma, type: 'f32' },
uContrast: { value: options.contrast, type: 'f32' },
uSaturation: { value: options.saturation, type: 'f32' },
uBrightness: { value: options.brightness, type: 'f32' },
uGamma: { value: gamma, type: 'f32' },
uContrast: { value: contrast, type: 'f32' },
uSaturation: { value: saturation, type: 'f32' },
uBrightness: { value: brightness, type: 'f32' },
uColor: {
value: [
options.red,
options.green,
options.blue,
options.alpha,
],
value: [red, green, blue, alpha],
type: 'vec4<f32>',
},
}
},
...rest
});

this.uniforms = this.resources.adjustmentUniforms.uniforms;
Expand Down
30 changes: 22 additions & 8 deletions src/advanced-bloom/AdvancedBloomFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import fragment from './advanced-bloom.frag';
import source from './advanced-bloom.wgsl';
import { ExtractBrightnessFilter } from './ExtractBrightnessFilter';

import type { FilterOptions } from 'pixi.js';

/** Options for the AdvancedBloomFilter constructor. */
export interface AdvancedBloomFilterOptions
export interface AdvancedBloomFilterOptions extends FilterOptions
{
/**
* Defines how bright a color needs to be to affect bloom.
Expand Down Expand Up @@ -59,7 +61,7 @@ export interface AdvancedBloomFilterOptions
export class AdvancedBloomFilter extends Filter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: AdvancedBloomFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
threshold: 0.5,
bloomScale: 1,
brightness: 1,
Expand Down Expand Up @@ -89,6 +91,17 @@ export class AdvancedBloomFilter extends Filter
{
options = { ...AdvancedBloomFilter.DEFAULT_OPTIONS, ...options };

const {
threshold,
bloomScale,
brightness,
blur,
quality,
kernels,
pixelSize,
...rest
} = options;

const gpuProgram = GpuProgram.from({
vertex: {
source: wgslVertex,
Expand All @@ -111,25 +124,26 @@ export class AdvancedBloomFilter extends Filter
glProgram,
resources: {
advancedBloomUniforms: {
uBloomScale: { value: options.bloomScale, type: 'f32' },
uBrightness: { value: options.brightness, type: 'f32' },
uBloomScale: { value: bloomScale, type: 'f32' },
uBrightness: { value: brightness, type: 'f32' },
},
uMapTexture: Texture.WHITE,
},
...rest
});

this.uniforms = this.resources.advancedBloomUniforms.uniforms;

this._extractFilter = new ExtractBrightnessFilter({
threshold: options.threshold
threshold
});

this._blurFilter = new KawaseBlurFilter({
strength: options.kernels as [number, number] ?? options.blur,
quality: options.kernels ? undefined : options.quality,
strength: options.kernels as [number, number] ?? blur,
quality: options.kernels ? undefined : quality,
});

Object.assign(this, options);
Object.assign(this, { threshold, bloomScale, brightness, blur, quality, kernels, pixelSize });
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/advanced-bloom/ExtractBrightnessFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { vertex, wgslVertex } from '../defaults';
import fragment from './extract-brightness.frag';
import source from './extract-brightness.wgsl';

export interface ExtractBrightnessFilterOptions
import type { FilterOptions } from 'pixi.js';

export interface ExtractBrightnessFilterOptions extends FilterOptions
{
/**
* Defines how bright a color needs to be extracted.
Expand All @@ -19,7 +21,7 @@ export interface ExtractBrightnessFilterOptions
export class ExtractBrightnessFilter extends Filter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: ExtractBrightnessFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
threshold: 0.5
};

Expand All @@ -29,7 +31,10 @@ export class ExtractBrightnessFilter extends Filter

constructor(options?: ExtractBrightnessFilterOptions)
{
options = { ...ExtractBrightnessFilter.DEFAULT_OPTIONS, ...options };
const {
threshold,
...rest
} = { ...ExtractBrightnessFilter.DEFAULT_OPTIONS, ...options };

const gpuProgram = GpuProgram.from({
vertex: {
Expand All @@ -53,9 +58,10 @@ export class ExtractBrightnessFilter extends Filter
glProgram,
resources: {
extractBrightnessUniforms: {
uThreshold: { value: options.threshold, type: 'f32' },
uThreshold: { value: threshold, type: 'f32' },
}
},
...rest
});

this.uniforms = this.resources.extractBrightnessUniforms.uniforms;
Expand Down
20 changes: 13 additions & 7 deletions src/ascii/AsciiFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { vertex, wgslVertex } from '../defaults';
import fragment from './ascii.frag';
import source from './ascii.wgsl';

import type { FilterOptions } from 'pixi.js';

// This WebGPU filter has been ported from the WebGL renderer that was originally created by Vico (@vicocotea)

/** Options for AsciiFilter constructor. */
export interface AsciiFilterOptions
export interface AsciiFilterOptions extends FilterOptions
{
/**
* The pixel size used by the filter
Expand Down Expand Up @@ -39,7 +41,7 @@ export interface AsciiFilterOptions
export class AsciiFilter extends Filter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: AsciiFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
size: 8,
color: 0xffffff,
replaceColor: false,
Expand Down Expand Up @@ -77,9 +79,12 @@ export class AsciiFilter extends Filter
options = { size: options };
}

const replaceColor = options?.color && options.replaceColor !== false;

options = { ...AsciiFilter.DEFAULT_OPTIONS, ...options } as AsciiFilterOptions;
const {
size,
color,
replaceColor,
...rest
} = { ...AsciiFilter.DEFAULT_OPTIONS, ...options };

const gpuProgram = GpuProgram.from({
vertex: {
Expand All @@ -103,16 +108,17 @@ export class AsciiFilter extends Filter
glProgram,
resources: {
asciiUniforms: {
uSize: { value: options.size, type: 'f32' },
uSize: { value: size, type: 'f32' },
uColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uReplaceColor: { value: Number(replaceColor), type: 'f32' },
},
},
...rest
});

this.uniforms = this.resources.asciiUniforms.uniforms;
this._color = new Color();
this.color = options.color ?? 0xffffff;
this.color = color ?? 0xffffff;
}

/**
Expand Down
30 changes: 19 additions & 11 deletions src/bevel/BevelFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { vertex, wgslVertex } from '../defaults';
import fragment from './bevel.frag';
import source from './bevel.wgsl';

import type { FilterOptions } from 'pixi.js';

/** Options for the BevelFilter constructor. */
export interface BevelFilterOptions
export interface BevelFilterOptions extends FilterOptions
{
/**
* The angle of the light in degrees
Expand Down Expand Up @@ -50,7 +52,7 @@ export interface BevelFilterOptions
export class BevelFilter extends Filter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: BevelFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
rotation: 45,
thickness: 2,
lightColor: 0xffffff,
Expand All @@ -77,7 +79,15 @@ export class BevelFilter extends Filter
*/
constructor(options?: BevelFilterOptions)
{
options = { ...BevelFilter.DEFAULT_OPTIONS, ...options };
const {
rotation,
thickness,
lightColor,
lightAlpha,
shadowColor,
shadowAlpha,
...rest
} = { ...BevelFilter.DEFAULT_OPTIONS, ...options };

const gpuProgram = GpuProgram.from({
vertex: {
Expand All @@ -102,25 +112,23 @@ export class BevelFilter extends Filter
resources: {
bevelUniforms: {
uLightColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uLightAlpha: { value: options.lightAlpha, type: 'f32' },
uLightAlpha: { value: lightAlpha, type: 'f32' },
uShadowColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uShadowAlpha: { value: options.shadowAlpha, type: 'f32' },
uShadowAlpha: { value: shadowAlpha, type: 'f32' },
uTransform: { value: new Float32Array(2), type: 'vec2<f32>' },
}
},
// Workaround: https://github.com/pixijs/filters/issues/230
// applies correctly only if there is at least a single-pixel padding with alpha=0 around an image
// To solve this problem, a padding of 1 put on the filter should suffice
padding: 1,
...rest
});

this.uniforms = this.resources.bevelUniforms.uniforms;
this._lightColor = new Color();
this._shadowColor = new Color();
this.lightColor = options.lightColor ?? 0xffffff;
this.shadowColor = options.shadowColor ?? 0x000000;
this.lightColor = lightColor ?? 0xffffff;
this.shadowColor = shadowColor ?? 0x000000;

Object.assign(this, options);
Object.assign(this, { rotation, thickness });
}

/**
Expand Down
29 changes: 19 additions & 10 deletions src/bloom/BloomFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
TexturePool,
} from 'pixi.js';

import type { AlphaFilterOptions } from 'pixi.js';

type DeprecatedBlurValue = number | PointData | number[];

/** Options for the BloomFilter constructor. */
export interface BloomFilterOptions
export interface BloomFilterOptions extends Partial<AlphaFilterOptions>
{
/**
* Sets the strength of the blur. If only a number is provided, it will assign to both x and y.
Expand Down Expand Up @@ -48,7 +50,7 @@ export interface BloomFilterOptions
export class BloomFilter extends AlphaFilter
{
/** Default values for options. */
public static readonly DEFAULT_OPTIONS: BloomFilterOptions = {
public static readonly DEFAULT_OPTIONS = {
strength: { x: 2, y: 2 },
quality: 4,
resolution: 1,
Expand Down Expand Up @@ -95,21 +97,28 @@ export class BloomFilter extends AlphaFilter

options = { ...BloomFilter.DEFAULT_OPTIONS, ...options } as BloomFilterOptions;

super();
const {
strength,
quality,
resolution,
kernelSize
} = options;

super({ alpha: options?.alpha ?? 1 });

this._strength = { x: 2, y: 2 };

if (options.strength)
if (strength)
{
if (typeof options.strength === 'number')
if (typeof strength === 'number')
{
this._strength.x = options.strength;
this._strength.y = options.strength;
this._strength.x = strength;
this._strength.y = strength;
}
else
{
this._strength.x = options.strength.x;
this._strength.y = options.strength.y;
this._strength.x = strength.x;
this._strength.y = strength.y;
}
}

Expand All @@ -127,7 +136,7 @@ export class BloomFilter extends AlphaFilter

this._blurYFilter.blendMode = 'screen';

Object.assign(this, options);
Object.assign(this, { strength, quality, resolution, kernelSize });
}

/**
Expand Down
Loading