Skip to content

Commit

Permalink
refactor: pass the rest parameters of options to super()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui Chen committed Jan 8, 2025
1 parent 52eb24d commit 91735ee
Show file tree
Hide file tree
Showing 38 changed files with 516 additions and 189 deletions.
28 changes: 17 additions & 11 deletions src/adjustment/AdjustmentFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,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 @@ -111,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
20 changes: 15 additions & 5 deletions src/advanced-bloom/AdvancedBloomFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export class AdvancedBloomFilter extends Filter
{
options = { ...AdvancedBloomFilter.DEFAULT_OPTIONS, ...options };

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

const gpuProgram = GpuProgram.from({
vertex: {
source: wgslVertex,
Expand All @@ -113,22 +122,23 @@ 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);
Expand Down
8 changes: 6 additions & 2 deletions src/advanced-bloom/ExtractBrightnessFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,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 @@ -55,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
14 changes: 9 additions & 5 deletions src/ascii/AsciiFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,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 @@ -105,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
24 changes: 15 additions & 9 deletions src/bevel/BevelFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,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 @@ -104,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
18 changes: 11 additions & 7 deletions src/bloom/BloomFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,25 @@ export class BloomFilter extends AlphaFilter

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

super();
const {
strength,
} = 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 Down
14 changes: 10 additions & 4 deletions src/bulge-pinch/BulgePinchFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export class BulgePinchFilter extends Filter
*/
constructor(options?: BulgePinchFilterOptions)
{
options = { ...BulgePinchFilter.DEFAULT_OPTIONS, ...options };
const {
center,
radius,
strength,
...rest
} = { ...BulgePinchFilter.DEFAULT_OPTIONS, ...options };

const gpuProgram = GpuProgram.from({
vertex: {
Expand All @@ -79,11 +84,12 @@ export class BulgePinchFilter extends Filter
resources: {
bulgePinchUniforms: {
uDimensions: { value: [0, 0], type: 'vec2<f32>' },
uCenter: { value: options.center, type: 'vec2<f32>' },
uRadius: { value: options.radius, type: 'f32' },
uStrength: { value: options.strength, type: 'f32' },
uCenter: { value: center, type: 'vec2<f32>' },
uRadius: { value: radius, type: 'f32' },
uStrength: { value: strength, type: 'f32' },
}
},
...rest
});

this.uniforms = this.resources.bulgePinchUniforms.uniforms;
Expand Down
27 changes: 19 additions & 8 deletions src/color-gradient/ColorGradientFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@ export class ColorGradientFilter extends Filter
options = { ...ColorGradientFilter.defaults, ...options };
}

if (!options.stops || options.stops.length < 2)
const {
type,
angle,
alpha,
replace,
stops,
maxColors,
...rest
} = options;

if (!stops || stops.length < 2)
{
throw new Error('ColorGradientFilter requires at least 2 color stops.');
}
Expand Down Expand Up @@ -159,22 +169,22 @@ export class ColorGradientFilter extends Filter
uOptions: {
value: [
// Gradient Type
options.type,
type,
// Gradient Angle
options.angle ?? ANGLE_OFFSET,
angle ?? ANGLE_OFFSET,
// Master Alpha
options.alpha,
alpha,
// Replace Base Color
options.replace ? 1 : 0,
replace ? 1 : 0,
],
type: 'vec4<f32>',
},
uCounts: {
value: [
// Number of Stops
options.stops.length,
stops.length,
// Max Gradient Colors
options.maxColors,
maxColors,
],
type: 'vec2<f32>',
},
Expand All @@ -184,8 +194,9 @@ export class ColorGradientFilter extends Filter

// We only need vec2, but we need to pad to eliminate the WGSL warning, TODO: @Mat ?
uStops: { value: new Float32Array(maxStops * 4), type: 'vec4<f32>', size: maxStops },
}
},
},
...rest
});

this.baseUniforms = this.resources.baseUniforms.uniforms;
Expand Down
17 changes: 13 additions & 4 deletions src/color-map/ColorMapFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export class ColorMapFilter extends Filter

if (!options.colorMap) throw Error('No color map texture source was provided to ColorMapFilter');

const {
colorMap,
nearest,
mix,
...rest
} = options;

const gpuProgram = GpuProgram.from({
vertex: {
source: wgslVertex,
Expand All @@ -111,20 +118,22 @@ export class ColorMapFilter extends Filter
glProgram,
resources: {
colorMapUniforms: {
uMix: { value: options.mix, type: 'f32' },
uMix: { value: mix, type: 'f32' },
uSize: { value: 0, type: 'f32' },
uSliceSize: { value: 0, type: 'f32' },
uSlicePixelSize: { value: 0, type: 'f32' },
uSliceInnerSize: { value: 0, type: 'f32' },
},
uMapTexture: options.colorMap.source,
uMapSampler: options.colorMap.source.style,
uMapTexture: colorMap.source,
uMapSampler: colorMap.source.style,

},
...rest
});

this.uniforms = this.resources.colorMapUniforms.uniforms;

Object.assign(this, options);
Object.assign(this, { colorMap, nearest, mix });
}

/** The mix from 0 to 1, where 0 is the original image and 1 is the color mapped image. */
Expand Down
9 changes: 7 additions & 2 deletions src/color-overlay/ColorOverlayFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export class ColorOverlayFilter extends Filter

options = { ...ColorOverlayFilter.DEFAULT_OPTIONS, ...options };

options.color = options?.color ?? 0x000000;

const { color, alpha, ...rest } = options;

const gpuProgram = GpuProgram.from({
vertex: {
source: wgslVertex,
Expand All @@ -96,15 +100,16 @@ export class ColorOverlayFilter extends Filter
resources: {
colorOverlayUniforms: {
uColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uAlpha: { value: options.alpha, type: 'f32' },
uAlpha: { value: alpha, type: 'f32' },
},
},
...rest
});

this.uniforms = this.resources.colorOverlayUniforms.uniforms;

this._color = new Color();
this.color = options.color ?? 0x000000;
this.color = color;
}

/**
Expand Down
Loading

0 comments on commit 91735ee

Please sign in to comment.