# effect/shadersprite (vanilla-engine 0.4.0)

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

Shader sprite component.
- A port of the original effect/shadersprite.js. Same as Sprite but applies fragment shader effects (dissolve / flash / outline / hologram / wave / mosaic / glitch / shine / silhouette / grayscale / hue rotation / wipe / iris / chromatic aberration).
- The effect time runs by itself (tick), and the progress is pushed in by tweens / timelines.
- Effect names, default parameters, default colors and GLSL bodies are carried over from the original table as they are. Shaders are GLSL ES 3.00, and on desktop ShaderProgram changes only the header to #version 330 core.
- Drawing: if there is an effect, it swaps in the effect program with Graphic.set_shader_program_override, uploads the effect uniforms, and then follows the Sprite output path as is.
  While swapped in, it is not batched. (a ShaderSprite without an effect is batched together with Sprites)
- Usage:
  ```text
    let shader_sprite = node.add_component(ShaderSprite::create_with_image(image));
    shader_sprite.set_effect(ShaderSpriteEffect::Dissolve);
    shader_sprite.set_effect_progress(0.5);
  ```
- Differences from the original:
  - The original inherits from Sprite. The Rust version contains a Sprite and lets Sprite functions (set_image, set_image_rect, set_color ...) be called as is through Deref / DerefMut.
    It reports the embedded Sprite with Component::get_base_component, so it is also found with `get_component::<Sprite>()`. (same as the original instanceof)
  - Effects are the ShaderSpriteEffect enum instead of strings. (the original string names via get_name / from_name)
  - The effect program cache is held by Graphic.resolve_shader_program instead of the original's per-context WeakMap.
- If creating a program fails, both engines report it once and draw as a Sprite without the effect. (one failing effect does not stop the frame)

## ShaderSprite (struct) (Default, Deref, DerefMut, Component)

Shader sprite component.
- Contains a Sprite and calls Sprite functions as is through Deref. If there is an effect, it swaps in the effect program to draw.

Example: Apply a dissolve effect halfway to an image sprite

```rust
use vanilla::{Image, NodeTree, ShaderSprite, ShaderSpriteEffect};

let image = Image::create_from_pixels(32, 32, vec![255u8; 32 * 32 * 4]);
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let hero_id = node_tree.create_child(root_id, "Hero");
let shader_sprite = node_tree[hero_id].add_component(ShaderSprite::create_with_image(image));
shader_sprite.set_effect(ShaderSpriteEffect::Dissolve);
shader_sprite.set_effect_progress(0.5);
assert_eq!(shader_sprite.get_effect_parameters(), [8.0, 0.08, 0.0, 0.0]);
assert_eq!(shader_sprite.get_effect_progress(), 0.5);
assert!(shader_sprite.get_image().is_some());
```

- `pub fn new() -> ShaderSprite` (constructor, since 0.3.2): Constructor.
  - returns ShaderSprite: A shader sprite containing a Sprite without an image. (no effect, progress 0, effect time 0, seed random 0 ~ 100, effect color white, parameters 0)
- `pub fn create_with_image(image: Rc<Image>) -> ShaderSprite` (static, since 0.3.2): Constructor with an image.
  - `image` Rc<Image>: Image to set on the contained Sprite.
  - returns ShaderSprite: A shader sprite with that image set. (the rest is the same as new)
- `pub fn get_sprite(&self) -> &Sprite` (method, since 0.3.2): Returns the contained Sprite.
  - returns &Sprite: Reference to the contained Sprite.
- `pub fn get_sprite_mut(&mut self) -> &mut Sprite` (method, since 0.3.2): Returns the contained Sprite. (mutable)
  - returns &mut Sprite: Mutable reference to the contained Sprite.
- `pub fn set_effect(&mut self, effect: ShaderSpriteEffect)` (method, since 0.3.2): Sets the effect. (resets to the effect's default parameters / color)
  - Progress and effect time are kept. To change parameters or color, call those after this.
  - `effect` ShaderSpriteEffect: Effect to apply. (None draws as a Sprite without an effect)

  Example: Pick an effect, then change the thickness and color

  ```rust
  use vanilla::{Color, ShaderSprite, ShaderSpriteEffect};
  
  let mut shader_sprite = ShaderSprite::new();
  shader_sprite.set_effect(ShaderSpriteEffect::Glow);
  assert_eq!(shader_sprite.get_effect_parameters(), [6.0, 0.0, 0.0, 0.0]);
  assert_eq!(shader_sprite.get_effect_color(), Color::create(1.0, 0.85, 0.4, 1.0));
  shader_sprite.set_effect(ShaderSpriteEffect::Outline);
  shader_sprite.set_effect_parameters(4.0, 0.0, 0.0, 0.0);
  shader_sprite.set_effect_color(Color::create(1.0, 0.0, 0.0, 1.0));
  assert_eq!(shader_sprite.get_effect_parameters(), [4.0, 0.0, 0.0, 0.0]);
  assert_eq!(shader_sprite.get_effect(), ShaderSpriteEffect::Outline);
  ```

- `pub fn get_effect(&self) -> ShaderSpriteEffect` (method, since 0.3.2): Returns the effect.
  - returns ShaderSpriteEffect: Current effect. (default None)
- `pub fn set_effect_progress(&mut self, effect_progress: f64)` (method, since 0.3.2): Sets the progress. (0 ~ 1)
  - `effect_progress` f64: Effect progress. (clamped to 0 ~ 1, the meaning differs per effect. Strength or how far it has disappeared)
- `pub fn get_effect_progress(&self) -> f64` (method, since 0.3.2): Returns the progress.
  - returns f64: Effect progress. (0 ~ 1)
- `pub fn set_effect_time(&mut self, effect_time: f64)` (method, since 0.3.2): Sets the effect time.
  - `effect_time` f64: Effect time. (seconds, used by moving effects like waves or flicker. Grows by time_delta every tick)
- `pub fn get_effect_time(&self) -> f64` (method, since 0.3.2): Returns the effect time.
  - returns f64: Effect time. (seconds)
- `pub fn set_effect_seed(&mut self, effect_seed: f64)` (method, since 0.3.2): Sets the seed. (fix it when you want the same noise pattern)
  - `effect_seed` f64: Noise seed value. (by default random 0 ~ 100 at creation)
- `pub fn get_effect_seed(&self) -> f64` (method, since 0.3.2): Returns the seed.
  - returns f64: Noise seed value.
- `pub fn set_effect_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the effect color. (a Color or a string like "#rrggbb")
  - `color` impl Into<Color>: Effect color. (the meaning differs per effect, like a line color, a glow color or a tint)
- `pub fn get_effect_color(&self) -> Color` (method, since 0.3.2): Returns the effect color.
  - returns Color: Effect color. (set_effect resets it to the effect's default color)
- `pub fn set_effect_parameters(&mut self, x: f64, y: f64, z: f64, w: f64)` (method, since 0.3.2): Sets the effect parameters. (the meaning differs per effect. See the ShaderSpriteEffect comments, pass the original's y / z / w default 0 explicitly)
  - `x` f64: First parameter. (like the thickness in pixels for Outline)
  - `y` f64: Second parameter. (0 for effects that do not use it)
  - `z` f64: Third parameter. (0 for effects that do not use it)
  - `w` f64: Fourth parameter. (0 for effects that do not use it)
- `pub fn get_effect_parameters(&self) -> [f64; 4]` (method, since 0.3.2): Returns the effect parameters.
  - returns [f64; 4]: Parameters [x, y, z, w].
- `pub fn get_default_parameters(effect: ShaderSpriteEffect) -> [f64; 4]` (static, since 0.3.2): Returns the effect's default parameters. (static)
  - `effect` ShaderSpriteEffect: Effect.
  - returns [f64; 4]: The original default parameters [x, y, z, w] of that effect. (Glow is [6, 0, 0, 0])
- `pub fn get_default_color(effect: ShaderSpriteEffect) -> Color` (static, since 0.3.2): Returns the effect's default color. (static)
  - `effect` ShaderSpriteEffect: Effect.
  - returns Color: The original default color of that effect. (white for effects that do not use color)

## ShaderSpriteEffect (enum) (Clone, Copy, Debug, PartialEq, Eq, Hash)

Shader sprite effect types.

- `None`: No effect. (drawn like a Sprite, and those without an effect are batched together)
- `Flash`: Covers the silhouette with a solid color. (hit flash: progress = strength, color = color to cover with)
- `Silhouette`: Solid silhouette. (progress = strength, color = color: shadow / afterimage)
- `Colorize`: Multiplicative tint. (progress = strength, color = color to multiply)
- `Grayscale`: Grayscale. (progress = strength, color = tint)
- `Sepia`: Sepia.
- `Invert`: Color inversion.
- `HueShift`: Hue rotation. (progress 0 → 1 = 0 → 360 degrees)
- `ColorAdjust`: Contrast / saturation / brightness. (parameters = `[contrast, saturation, brightness]`, progress = apply ratio)
- `Posterize`: Posterize. (parameters = `[step count]`)
- `Threshold`: Black-and-white threshold. (parameters = `[threshold]`)
- `GradientMap`: Maps brightness between two colors. (parameters.xyz = dark color, color = bright color)
- `Outline`: Outline outside the silhouette. (color = line color, parameters = `[thickness pixels]`)
- `InnerOutline`: Inner outline of the silhouette. (color = line color, parameters = `[thickness pixels]`)
- `Glow`: Outer glow. (color = glow color, parameters = `[radius pixels]`)
- `InnerGlow`: Inner glow. (color = glow color, parameters = `[radius pixels]`)
- `Shadow`: Drop shadow. (color = shadow color, parameters = `[offset x pixels, offset y pixels]`)
- `EdgeDetect`: Edge detection. (color = line color)
- `Emboss`: Emboss.
- `Sharpen`: Sharpen.
- `Blur`: Gaussian blur. (parameters = `[radius pixels]`)
- `MotionBlur`: Directional blur. (parameters = `[direction x, direction y, length pixels]`)
- `RadialBlur`: Radial blur from the center. (parameters = `[length 0 ~ 1]`)
- `Wave`: Horizontal wave distortion. (parameters = `[frequency, speed, amplitude]`)
- `Flag`: Flag waving. (parameters = `[frequency, speed, amplitude]`)
- `Ripple`: Ripple from the center. (parameters = `[frequency, speed, amplitude]`)
- `HeatHaze`: Heat haze. (parameters = `[noise scale, speed, amplitude]`)
- `Bulge`: Bulge / pinch. (parameters = `[strength: pinch if negative]`)
- `Swirl`: Swirl. (parameters = `[turn count]`)
- `Mirror`: Mirror symmetry. (parameters = `[0 horizontal / 1 vertical]`)
- `Kaleidoscope`: Kaleidoscope. (parameters = `[segment count]`)
- `Jitter`: Shake. (parameters = `[amplitude pixels, changes per second]`)
- `Pixelate`: Mosaic. (parameters = `[coarsest block count]`)
- `Glitch`: Horizontal line offset + color split. (parameters = `[line count, changes per second, offset width]`)
- `Chromatic`: Chromatic aberration. (parameters = `[split pixels]`)
- `Hologram`: Scanlines + tint + flicker. (color = tint, parameters = `[scanline count, scroll speed]`)
- `Scanlines`: Scanlines. (parameters = `[scanline count]`)
- `OldFilm`: Old film. (sepia + grain + scratches + flicker)
- `Vignette`: Vignette. (color = edge color, parameters = `[start, end]`)
- `Shine`: A diagonal highlight band sweeps across. (color = band color, parameters = `[band width]`)
- `Dissolve`: Burns away with noise. (color = edge color, parameters = `[noise scale, edge width]`)
- `Burn`: Disappears as if burning. (bright inner band + outer scorch, parameters = `[noise scale, band width]`)
- `NoiseFade`: Noise alpha fade. (parameters = `[noise scale, softness]`)
- `PixelDissolve`: Disappears block by block. (parameters = `[block count]`)
- `Wipe`: Reveals with a directional wipe. (parameters = `[direction x, direction y, softness]`)
- `Iris`: Reveals with a circular iris. (parameters = `[center x, center y, softness]`)
- `DiamondWipe`: Diamond wipe. (parameters = `[softness]`)
- `ClockWipe`: Clockwise wipe. (parameters = `[softness]`)
- `Blinds`: Blinds. (parameters = `[line count, softness]`)
- `CheckerWipe`: Checkerboard. (parameters = `[cell count]`)

- `pub fn get_name(&self) -> &'static str` (method, since 0.3.2): Returns the original name. (a string like "dissolve")
  - returns &'static str: Original effect name. (camelCase, "none" for none)
- `pub fn from_name(name: &str) -> Option<ShaderSpriteEffect>` (static, since 0.3.2): Finds by the original name. (None for an unknown name)
  - `name` &str: Original effect name. (case-sensitive, camelCase like "heatHaze")
  - returns Option<ShaderSpriteEffect>: The effect with that name. None for an unknown name.

  Example: Convert between names and effects

  ```rust
  use vanilla::ShaderSpriteEffect;
  
  assert_eq!(ShaderSpriteEffect::from_name("dissolve"), Some(ShaderSpriteEffect::Dissolve));
  assert_eq!(ShaderSpriteEffect::Dissolve.get_name(), "dissolve");
  assert_eq!(ShaderSpriteEffect::from_name("Dissolve"), None);
  assert_eq!(ShaderSpriteEffect::get_effect_list().len(), 50);
  ```

- `pub fn get_effect_list() -> &'static [ShaderSpriteEffect]` (static, since 0.3.2): Returns the list of effects. (declaration order, in place of the original Object.keys(ShaderSpriteEffect))
  - returns &'static [ShaderSpriteEffect]: All 50 effects. (None first)
