# effect/shadersprite (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/effect/shadersprite/

Shader sprite component.

## ShaderSprite extends Sprite

Shader sprite component.
- Same as Sprite but applies fragment shader effects (dissolve / flash / outline / hologram / ripple / mosaic / glitch / shine / silhouette / grayscale / hue rotation / wipe / iris / chromatic aberration).
- The effect time advances on its own (tick), and the progress is pushed in by tweens / timelines.
- Usage:
    const sprite = node.addComponent(ShaderSprite);
    sprite.setImage(image);
    sprite.setEffect(ShaderSpriteEffect.dissolve);
    sprite.setEffectProgress(0.5);

Example: Make an image disappear with a dissolve

```js
import { Engine, EngineConfiguration, Scene, WorldNode, ImageAsset, ShaderSprite, ShaderSpriteEffect, Tween, Vector2 } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const node = new WorldNode();
		node.setLocalPosition(Vector2.create(320, 180));
		node.setContentSize(Vector2.create(300, 300));
		const root = this.getRoot();
		root.addChild(node);
		const shaderSprite = node.addComponent(ShaderSprite);
		shaderSprite.setImage(imageAsset);
		shaderSprite.setEffect(ShaderSpriteEffect.dissolve);
		const tween = new Tween({ progress: 0 });
		tween.to({ progress: 1 }, 0.8);
		tween.setUpdate((values) => {
			shaderSprite.setEffectProgress(values.progress);
		});
		this.startTween(tween);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `tick(timeDelta)` (method, since 0.3.0): Update. (accumulates effect time)
  - `timeDelta` number: Elapsed time. (seconds, added to the effect time)
- `draw(graphic)` (method, since 0.3.0): Draw. (if there is an effect, swaps in the program and goes through the Sprite draw path as is)
  - `graphic` Graphic: Graphic to draw with.
- `isDrawnInsideContent()` (method, since 0.4.0): Return whether it draws only inside the content area. (the effect is drawn by a shader inside the same rectangle as Sprite, so this is the same as Sprite)
  - Even when flipped, it draws mirrored inside the content area. Subclasses that override draw return false.
  - returns boolean: True if draw is not overridden.
- `setEffect(effect)` (method, since 0.3.0): Sets the effect. (resets to the effect's default parameters / color)
  - `effect` string: ShaderSpriteEffect value. (none if unknown)
- `getEffect()` (method, since 0.3.0): Returns the effect.
  - returns string: ShaderSpriteEffect value. (default none)
- `setEffectProgress(effectProgress)` (method, since 0.3.0): Sets the progress. (0 ~ 1)
  - `effectProgress` number: Progress. (clamped to 0 ~ 1)
- `getEffectProgress()` (method, since 0.3.0): Returns the progress.
  - returns number: Progress. (0 ~ 1)
- `setEffectTime(effectTime)` (method, since 0.3.0): Sets the effect time.
  - `effectTime` number: Effect time. (seconds)
- `getEffectTime()` (method, since 0.3.0): Returns the effect time.
  - returns number: Effect time. (seconds)
- `setEffectSeed(effectSeed)` (method, since 0.3.0): Sets the seed. (fix it when you want the same noise pattern)
  - `effectSeed` number: Noise seed. (default is a random value in 0 ~ 100)
- `getEffectSeed()` (method, since 0.3.0): Returns the seed.
  - returns number: Noise seed.
- `setEffectColor(color)` (method, since 0.3.0): Sets the effect color.
  - `color` Color | string: Effect color. (kept as a copy, ignored if null)
- `getEffectColor()` (method, since 0.3.0): Returns the effect color.
  - returns Color: Effect color.
- `setEffectParameters(x, y = 0, z = 0, w = 0)` (method, since 0.3.0): Sets the effect parameters. (meaning differs per effect, see the ShaderSpriteEffect comments)
  - `x` number: First parameter.
  - `y` number: Second parameter. (default 0)
  - `z` number: Third parameter. (default 0)
  - `w` number: Fourth parameter. (default 0)

  Example: Change the outline color and thickness

  ```js
  import { ShaderSprite, ShaderSpriteEffect, Color } from "./build/vanilla.js";
  
  const shaderSprite = new ShaderSprite();
  shaderSprite.setEffect(ShaderSpriteEffect.outline);
  console.log(shaderSprite.getEffectParameters());
  shaderSprite.setEffectColor(new Color(1, 0.9, 0.2, 1));
  shaderSprite.setEffectParameters(3);
  const defaultParameters = ShaderSprite.getDefaultParameters(ShaderSpriteEffect.outline);
  console.log(shaderSprite.getEffectParameters(), defaultParameters);
  ```

- `getEffectParameters()` (method, since 0.3.0): Returns the effect parameters.
  - returns number[]: Effect parameters. ([x, y, z, w])
- `static getDefaultParameters(effect)` (static, since 0.3.0): Returns the effect's default parameters. (static)
  - `effect` string: ShaderSpriteEffect value. (that of none if unknown)
  - returns number[]: A new array of the default parameters. ([x, y, z, w])
- `static getDefaultColor(effect)` (static, since 0.3.0): Returns the effect's default color. (static)
  - `effect` string: ShaderSpriteEffect value. (that of none if unknown)
  - returns Color: Default color. (a new Color)

## ShaderSpriteEffect (enum)

Shader sprite effect types.

- `none` = `none`: No effect. (drawn the same as Sprite)
- `flash` = `flash`: --- Color ---
- `silhouette` = `silhouette`: Solid silhouette. (progress = strength, color = color, shadow / afterimage)
- `colorize` = `colorize`: Multiplicative tint. (progress = strength, color = color to multiply)
- `grayscale` = `grayscale`: Grayscale. (progress = strength, color = tint)
- `sepia` = `sepia`: Sepia.
- `invert` = `invert`: Color inversion.
- `hueShift` = `hueShift`: Hue rotation. (progress 0 → 1 = 0 → 360 degrees)
- `colorAdjust` = `colorAdjust`: Contrast / saturation / brightness. (parameters = [contrast, saturation, brightness], progress = apply ratio)
- `posterize` = `posterize`: Posterize. (parameters = [step count])
- `threshold` = `threshold`: Black-and-white threshold. (parameters = [threshold])
- `gradientMap` = `gradientMap`: Maps brightness between two colors. (parameters.xyz = dark color, color = bright color)
- `outline` = `outline`: --- Edge ---
- `innerOutline` = `innerOutline`: Inner outline of the silhouette. (color = line color, parameters = [thickness pixels])
- `glow` = `glow`: Outer glow. (color = glow color, parameters = [radius pixels])
- `innerGlow` = `innerGlow`: Inner glow. (color = glow color, parameters = [radius pixels])
- `shadow` = `shadow`: Drop shadow. (color = shadow color, parameters = [offset x pixels, offset y pixels])
- `edgeDetect` = `edgeDetect`: Edge detection. (color = line color)
- `emboss` = `emboss`: Emboss.
- `sharpen` = `sharpen`: Sharpen.
- `blur` = `blur`: --- Blur ---
- `motionBlur` = `motionBlur`: Directional blur. (parameters = [direction x, direction y, length pixels])
- `radialBlur` = `radialBlur`: Radial blur from the center. (parameters = [length 0 ~ 1])
- `wave` = `wave`: --- Distortion ---
- `flag` = `flag`: Flag waving. (parameters = [frequency, speed, amplitude])
- `ripple` = `ripple`: Ripple from the center. (parameters = [frequency, speed, amplitude])
- `heatHaze` = `heatHaze`: Heat haze. (parameters = [noise scale, speed, amplitude])
- `bulge` = `bulge`: Bulge / pinch. (parameters = [strength, negative for pinch])
- `swirl` = `swirl`: Swirl. (parameters = [turn count])
- `mirror` = `mirror`: Mirror symmetry. (parameters = [0 horizontal / 1 vertical])
- `kaleidoscope` = `kaleidoscope`: Kaleidoscope. (parameters = [segment count])
- `jitter` = `jitter`: Shake. (parameters = [amplitude pixels, changes per second])
- `pixelate` = `pixelate`: Mosaic. (parameters = [coarsest block count])
- `glitch` = `glitch`: Horizontal line offset + color split. (parameters = [line count, changes per second, offset width])
- `chromatic` = `chromatic`: Chromatic aberration. (parameters = [split pixels])
- `hologram` = `hologram`: --- Screen patterns ---
- `scanlines` = `scanlines`: Scanlines. (parameters = [scanline count])
- `oldFilm` = `oldFilm`: Old film. (sepia + grain + scratches + flicker)
- `vignette` = `vignette`: Vignette. (color = edge color, parameters = [start, end])
- `shine` = `shine`: A diagonal highlight band sweeps across. (color = band color, parameters = [band width])
- `dissolve` = `dissolve`: --- Disappear / reveal (progress 0 → 1) ---
- `burn` = `burn`: Disappears as if burning. (bright inner band + outer scorch, parameters = [noise scale, band width])
- `noiseFade` = `noiseFade`: Noise alpha fade. (parameters = [noise scale, softness])
- `pixelDissolve` = `pixelDissolve`: Disappears block by block. (parameters = [block count])
- `wipe` = `wipe`: Reveals with a directional wipe. (parameters = [direction x, direction y, softness])
- `iris` = `iris`: Reveals with a circular iris. (parameters = [center x, center y, softness])
- `diamondWipe` = `diamondWipe`: Diamond wipe. (parameters = [softness])
- `clockWipe` = `clockWipe`: Clockwise wipe. (parameters = [softness])
- `blinds` = `blinds`: Blinds. (parameters = [line count, softness])
- `checkerWipe` = `checkerWipe`: Checkerboard. (parameters = [cell count])
