# core/component/softshade (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/component/softshade/

부드러운 그늘. (vanilla.js core/component/softshade.js 의 SoftShade, SoftShadeShape)
- 글 뒤를 어둡게 눌러 밝은 바탕 위에서도 읽히게 할 때 쓴다. 모양을 작은 그림 한 장에 구워 노드의 내용 크기로 늘려 그린다.
- 부드러운 그라데이션이라 낮은 해상도로 구워 늘려도 자국이 보이지 않는다. (구운 한 픽셀이 굽기 단위만큼을 덮고, 가로세로 상한이 있다)
- 노드 크기, 모양, 색, 짙기, 양옆 흐림이 바뀌면 다시 굽는다. 같은 모양을 구운 것이 있으면 나눠 쓴다. (sharedbakecache.rs) 노드의 불투명도는 한 장째로 먹는다.
- 원본 getBakeCanvas 는 구운 캔버스 대신 구운 Image 를 돌려주는 get_bake_image 다.

## SoftShade (struct) (Default, Component)

부드러운 그늘 컴포넌트. (모양을 작은 그림 한 장에 구워 노드 content_size 로 늘려 그린다)
- 처음은 Band 모양, 검정, 가장 짙은 곳의 불투명도 0.5, 양옆 흐림 0, 굽기 단위 4, 가로세로 상한 256.
- 글 뒤를 어둡게 눌러 밝은 바탕 위에서도 읽히게 할 때 쓴다. 노드의 불투명도는 구운 한 장째로 먹는다.

예제: 글 뒤에 까는 어두운 띠

```rust
use vanilla::{Color, NodeTree, SoftShade, SoftShadeShape, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let shade_id = node_tree.create_child(root_id, "Shade");
node_tree[shade_id].set_content_size(Vector2::create(400.0, 80.0));
let soft_shade = node_tree.add_component(shade_id, SoftShade::new()).unwrap();
soft_shade.set_shade(SoftShadeShape::Band, Color::black(), 0.6, 60.0);

let soft_shade = node_tree[shade_id].get_component::<SoftShade>().unwrap();
assert_eq!(soft_shade.get_shape(), SoftShadeShape::Band);
assert_eq!(soft_shade.get_fade_width(), 60.0);
assert!(soft_shade.get_bake_image().is_none());
```

- `pub fn new() -> SoftShade` (constructor, since 0.4.0): 생성.
  - returns SoftShade: Band 모양, 검정, 불투명도 0.5, 양옆 흐림 0, 굽기 단위 4, 가로세로 상한 256 인 새 컴포넌트.
- `pub fn draw_shade(&mut self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.4.0): 출력.
  - 구울 크기는 content_size 를 굽기 단위로 나눠 올림한 값이고 2 ~ 가로세로 상한으로 맞춘다. 양옆 흐림 비율은 fade_width / 너비 (0.5 까지).
  - 크기, 모양, 색, 불투명도, 양옆 흐림이 지난번과 다르면 다시 굽는다. (같은 모양을 구운 것이 있으면 나눠 쓴다)
  - 너비나 높이가 0 이하이거나 불투명도가 0 이하면 그리지 않는다.
  - `transform` &NodeTransform: 그늘을 그릴 노드 트랜스폼. (content_size 를 읽는다)
  - `graphic` &mut Graphic: 그릴 그래픽. (노드 트랜스폼이 걸린 상태)
- `pub fn bake_shade(&self, bake_width: u32, bake_height: u32, fade_ratio: f64) -> Rc<Image>` (method, since 0.4.0): 굽기. (픽셀마다 알파를 셈한다, 텍스처 캐시가 그림마다 한 번 올리므로 구울 때마다 새 그림)
  - 모든 픽셀의 색은 그늘 색(알파는 쓰지 않는다)이고, 알파는 255 x 불투명도 x 세로 짙기 x 가로 짙기다.
  - Band: 세로는 가운데 줄이 1 이고 위아래 끝으로 부드럽게 0, 가로는 양끝에서 fade_ratio 안쪽까지 부드럽게 0 에서 1. Bottom: 세로는 위 끝 0 에서 아래 끝 1 로 곧게, 가로는 1.
  - `bake_width` u32: 구울 그림의 너비. (픽셀)
  - `bake_height` u32: 구울 그림의 높이. (픽셀)
  - `fade_ratio` f64: Band 의 양옆이 엷어지는 너비를 그림 너비에 대한 비율로. (0 ~ 0.5, 0 이면 양옆을 흐리지 않는다, Bottom 은 쓰지 않는다)
  - returns Rc<Image>: 구운 새 그림. (bake_width x bake_height)

  예제: 아래로 짙어지는 그늘을 1 x 2 로 굽기

  ```rust
  use vanilla::{Color, SoftShade, SoftShadeShape};
  
  let mut soft_shade = SoftShade::new();
  soft_shade.set_shade(SoftShadeShape::Bottom, Color::black(), 1.0, 0.0);
  let image = soft_shade.bake_shade(1, 2, 0.0);
  assert_eq!(image.get_width(), 1);
  assert_eq!(image.get_height(), 2);
  let pixels = image.get_pixels();
  assert_eq!(pixels[3], 64);
  assert_eq!(pixels[7], 191);
  ```

- `pub fn set_shade(&mut self, shape: SoftShadeShape, color: impl Into<Color>, opacity: f64, fade_width: f64)` (method, since 0.4.0): 그늘 설정. (모양, 색, 가장 짙은 곳의 불투명도, 띠의 양옆이 엷어지는 너비)
  - `shape` SoftShadeShape: 그늘 모양. (처음 Band)
  - `color` impl Into<Color>: 그늘 색. (Color 또는 "#rrggbb" 같은 문자열, 알파는 쓰지 않는다, 처음 검정)
  - `opacity` f64: 가장 짙은 곳의 불투명도. (0 ~ 1, 0 이하면 그리지 않는다, 처음 0.5)
  - `fade_width` f64: Band 의 양옆이 엷어지는 너비. (노드 좌표, 노드 너비의 반까지만 먹는다, 처음 0)
- `pub fn set_bake_resolution(&mut self, bake_unit: f64, bake_maximum_size: u32)` (method, since 0.4.0): 굽기 해상도 설정. (구운 한 픽셀이 덮는 단위, 가로세로 상한)
  - `bake_unit` f64: 구운 한 픽셀이 덮는 노드 좌표 크기. (1 보다 작으면 1, 처음 4)
  - `bake_maximum_size` u32: 구운 그림의 가로세로 상한. (픽셀, 2 보다 작으면 2, 처음 256)

  예제: 더 성기게 굽기와 아래 한계

  ```rust
  use vanilla::SoftShade;
  
  let mut soft_shade = SoftShade::new();
  soft_shade.set_bake_resolution(8.0, 128);
  assert_eq!(soft_shade.get_bake_unit(), 8.0);
  assert_eq!(soft_shade.get_bake_maximum_size(), 128);
  
  soft_shade.set_bake_resolution(0.5, 1);
  assert_eq!(soft_shade.get_bake_unit(), 1.0);
  assert_eq!(soft_shade.get_bake_maximum_size(), 2);
  ```

- `pub fn get_shape(&self) -> SoftShadeShape` (method, since 0.4.0): 모양 반환.
  - returns SoftShadeShape: 그늘 모양.
- `pub fn get_color(&self) -> Color` (method, since 0.4.0): 색 반환.
  - returns Color: 그늘 색.
- `pub fn get_opacity(&self) -> f64` (method, since 0.4.0): 가장 짙은 곳의 불투명도 반환.
  - returns f64: 가장 짙은 곳의 불투명도. (0 ~ 1)
- `pub fn get_fade_width(&self) -> f64` (method, since 0.4.0): 띠의 양옆이 엷어지는 너비 반환.
  - returns f64: 띠의 양옆이 엷어지는 너비. (노드 좌표)
- `pub fn get_bake_unit(&self) -> f64` (method, since 0.4.0): 구운 한 픽셀이 덮는 단위 반환.
  - returns f64: 구운 한 픽셀이 덮는 노드 좌표 크기. (1 이상)
- `pub fn get_bake_maximum_size(&self) -> u32` (method, since 0.4.0): 구운 그림의 가로세로 상한 반환.
  - returns u32: 구운 그림의 가로세로 상한. (픽셀, 2 이상)
- `pub fn get_bake_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): 구운 그림 반환. (아직 굽지 않았으면 None, 원본 getBakeCanvas)
  - returns Option<&Rc<Image>>: 마지막으로 구운 그림. 아직 한 번도 그리지 않았으면 None.

## SoftShadeShape (enum) (Clone, Copy, PartialEq, Eq, Debug)

부드러운 그늘의 모양.

- `Band`: 가운데 가로줄이 가장 짙고 위아래 끝으로 차츰 엷어지는 띠. (양옆은 fade_width 안쪽까지 차츰)
- `Bottom`: 아래 끝이 가장 짙고 위 끝에서 0 인 세로 그라데이션.
