# effect/trailrenderer (vanilla-engine 0.4.0)

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

Trail renderer component.
- A port of the original effect/trailrenderer.js. Records the places a node passed as points and draws them as a band whose width / color fades out. (the 2D counterpart of Unity's TrailRenderer)
- Points are recorded in the node's local position (parent coordinates), and when drawing they are converted to coordinates relative to the node origin and drawn under the node transform. (same as the original)
- One draw_line per segment, so batching gathers them into one draw call. (with additive blending it breaks before and after the blend change)
- Usage:
  ```text
    let trail = node.add_component(TrailRenderer::new());
    trail.set_time(0.5);
    trail.set_width(14.0, 0.0);
    trail.set_colors(Color::create(0.4, 0.8, 1.0, 0.9), Color::create(0.4, 0.8, 1.0, 0.0));
  ```
- Differences from the original: the original passes colors as "rgba(r, g, b, a)" strings, so RGB is rounded to 0 ~ 255 integers. The Rust version does the same rounding and passes a Color.

## TrailRenderer (struct) (Default, Component)

Trail renderer component.
- Every tick it marks the node local position as a point and removes points past their lifetime. With two or more points, it draws a band whose width and color fade from the new point side to the old point side.

Example: Move a comet node and pile up points

```rust
use vanilla::{Color, Engine, EngineConfiguration, NodeTree, TrailRenderer, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let comet_id = node_tree.create_child(root_id, "Comet");
let trail = node_tree[comet_id].add_component(TrailRenderer::new());
trail.set_time(0.5);
trail.set_width(14.0, 0.0);
trail.set_colors(Color::create(0.4, 0.8, 1.0, 0.9), Color::create(0.4, 0.8, 1.0, 0.0));
for step in 0..5 {
	node_tree[comet_id].set_local_position(Vector2::create(step as f64 * 20.0, 0.0));
	node_tree.tick(&engine, 0.1);
}
assert_eq!(node_tree[comet_id].get_component::<TrailRenderer>().map(|trail| trail.get_point_count()), Some(5));
```

- `pub fn new() -> TrailRenderer` (constructor, since 0.3.2): Constructor.
  - returns TrailRenderer: A trail with no points, lifetime 0.5 seconds, minimum distance 3, width 10 → 0, white alpha 0.9 → 0, normal blending, and point marking on.
- `pub fn clear(&mut self)` (method, since 0.3.2): Clears the trail.
- `pub fn set_time(&mut self, point_lifetime: f64)` (method, since 0.3.2): Sets how long points remain. (seconds)
  - `point_lifetime` f64: How long points remain. (seconds, default 0.5) Points this old are removed in tick.
- `pub fn set_min_vertex_distance(&mut self, min_vertex_distance: f64)` (method, since 0.3.2): Sets the minimum distance between points.
  - `min_vertex_distance` f64: Shortest distance to mark a new point. (parent coordinates, default 3) No point is marked if it moved less than this from the last point.
- `pub fn set_width(&mut self, start_width: f64, end_width: f64)` (method, since 0.3.2): Sets the width. (new point side, old point side)
  - `start_width` f64: Width on the new point side. (node coordinates, default 10)
  - `end_width` f64: Width on the old point side. (node coordinates, default 0. Never thinner than 0.1 when drawing)
- `pub fn set_colors(&mut self, start_color: impl Into<Color>, end_color: impl Into<Color>)` (method, since 0.3.2): Sets the colors. (new point side, old point side)
  - When drawing, RGB is rounded to 0 ~ 255 integers as in the original, and alpha is interpolated as is.
  - `start_color` impl Into<Color>: Color on the new point side. (a Color or text like "#rrggbb", default white alpha 0.9)
  - `end_color` impl Into<Color>: Color on the old point side. (default white alpha 0)
- `pub fn set_additive(&mut self, is_additive: bool)` (method, since 0.3.2): Sets whether to use additive blending. (lighter when on)
  - `is_additive` bool: If true, draws the band with additive blending and restores normal blending afterward. (default false)
- `pub fn set_emitting(&mut self, is_emitting: bool)` (method, since 0.3.2): Sets whether to make points. (if false, no new points are made. The remaining band fades out)
  - `is_emitting` bool: If true, marks a point every tick. (default true)
- `pub fn get_point_count(&self) -> usize` (method, since 0.3.2): Returns the number of remaining points.
  - returns usize: Number of points with lifetime left. (at least two are needed to draw the band)
