# graphics3d/toon/toonscreenoutline (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/toon/toonscreenoutline/

툰 화면 공간 바깥선. (노멀, 선 번호, 깊이를 선의 판에 그리고, 바로 이웃 사이의 경계에서 선을 잡아 가는 선은 둘레 칸을 바로 보고, 굵은 선은 점프 플러딩으로 넓힌 뒤 거리로 덮는 몫을 셈해 섞는다)

예제: 툰 화면 공간 바깥선을 한 프레임에 긋기

```js
import { ToonRenderer, ToonScreenOutline, ToonOutlineWidthUnit, MeshBuilder, createBoxGeometry, Matrix4, Vector3 } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const renderer = new ToonRenderer(webGL2RenderingContext);
const outline = new ToonScreenOutline(webGL2RenderingContext);
outline.setWidth(2, ToonOutlineWidthUnit.pixel);
outline.setCrease(50, 1, 0.8);
const builder = new MeshBuilder();
builder.addPart(createBoxGeometry(1, 1, 1), Matrix4.createIdentity(), "#e0c8a8", { hasOutline: false });
const mesh = renderer.createMesh(builder.readArrays());
const viewMatrix = Matrix4.createLookAt(Vector3.create(2, 2, 3), Vector3.create(0, 0, 0), Vector3.create(0, 1, 0));
const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
const modelMatrix = Matrix4.createIdentity();
if (outline.beginLineIds(viewMatrix, projectionMatrix)) {
	outline.drawMesh(mesh, modelMatrix, 1);
	outline.endLineIds();
}
renderer.beginFrameWithMatrices(viewMatrix, projectionMatrix);
renderer.drawMesh(mesh, modelMatrix);
renderer.endFrame();
outline.drawOutline();
```

## ToonScreenOutline

툰 화면 공간 바깥선. (노멀, 선 번호, 깊이를 선의 판에 그리고, 바로 이웃 사이의 경계에서 선을 잡아 가는 선은 둘레 칸을 바로 보고, 굵은 선은 점프 플러딩으로 넓힌 뒤 거리로 덮는 몫을 셈해 섞는다)
- 선의 판: ToonRenderer 의 createMesh 결과를 같은 행렬로 한 번 더 그려 노멀(팔면체 접기), 선 번호(14 비트), 꼴(바깥 선, 꺾임 선을 켤지)과 깊이를 쓴다.
- 잡기: 화소와 바로 이웃 넷 사이의 경계마다 한 번 본다. 선은 화소가 아니라 화소 사이의 경계 가운데에 선다. (같은 경계를 두 번 긋지 않는다)
  바깥 선은 빈 곳과 맞닿은 실루엣, 다른 번호와 맞닿은 곳, 같은 번호라도 가린 화소가 가려진 화소의 접평면에서 깊이 문턱과 오차 여유보다 떨어진 곳(가려진 실루엣)이다.
  꺾임 선은 같은 번호 안에서 이웃한 두 노멀이 꺾임 각보다 더 벌어진 곳이다. 화소 하나 사이의 노멀만 보므로 둥근 면의 실루엣 둘레나 비스듬한 면에 띠가 서지 않는다.
  깊이를 접평면 거리로 견주므로 비스듬히 보는 바닥에 가짜 선이 서지 않는다. 문턱에 잰 오차(노멀 8 비트 각 오차 x 두 자리 사이 거리, 꺾임의 화소 한 칸 x 사인, 깊이 24 비트 한 칸) x 배율을 더해 먼 것도 같은 뜻이 되고, 나란한 낮은 턱은 화소 한 칸보다 낮아도 잡힌다.
- 가는 선(굵기 약 3.24 픽셀까지): 씨앗 고르기와 같은 차례로 화소마다 첫 경계 쪽(R8UI 한 바이트)을 고르고, 섞기가 둘레 3 x 3 화소의 씨앗을 점프 플러딩 마지막 걸음과 같은 차례로 보아 가장 가까운 것까지의 거리를 잰다.
  화면 전체 패스가 씨앗 쪽 판 하나와 섞기 하나다. (모바일 타일 GPU 에서 패스 수와 판 대역폭이 굵은 선 길의 몇 분의 1)
- 넓히기(굵은 선): 경계 토막까지의 거리를 점프 플러딩으로 퍼뜨린다. 굵기에 상관없이 걸음 수는 log2 이고, 바깥 선과 꺾임 선은 따로 퍼뜨려 굵기를 따로 준다.
- 섞기: 그리는 영역의 화소마다 가장 가까운 경계까지의 거리로 clamp(굵기 / 2 - 거리 + 0.5, 0, 1) 만큼 덮는다. (가장자리가 부드럽고, 넓히기 판을 줄여도 계단과 점선이 없다)
- 모형마다: drawMesh 의 hasScreenOutline 이 false 면 그 모형은 선을 내지 않고 뒤의 선만 가린다. hasCreaseLine 이 false 면 꺾임 선만 끈다.
- 번호마다 색: setLineColor 로 번호마다 선 색을 준다. (언리얼 Custom Stencil 꼴, 맞닿은 선은 앞쪽 번호의 색, 표에 없는 번호와 표가 빈 때는 setColor 의 색 그대로)
- 굵기: setWidth 와 setCrease 의 굵기는 선 하나의 굵기다. (경계를 가운데로 양쪽에 반씩) 단위는 ToonOutlineWidthUnit, 한 픽셀 밑으로 내리지 않는다.
- 깊이: 투영 행렬로 직교와 원근을 가리고, 깊이를 보기 공간 자리로 풀어 견준다. (화면 사각형처럼 클립 x, y 를 옮긴 투영도 된다)
- 값: setResolutionScale 로 섞기 앞의 모든 판(선의 판, 가면, 씨앗, 넓히기 판)을 줄인다. (기본 1 은 그리는 영역 그대로, 줄이면 가는 선은 선의 판 해상도에서 덮는 몫 판 하나를 셈해 쌍선형으로 한 번 읽어 섞는다)
- 인스턴스: drawMeshInstanced 는 인스턴스 배열을 걸러 그때 올린다. drawMeshInstanceBuffer 는 ToonRenderer.createInstanceBuffer 의 버퍼를 몸과 같이 쓰고 다시 올리지 않는다.
  버퍼의 인스턴스마다 번호는 꼴의 instanceLineId 칸에서 읽는다. (없으면 공통 번호)
- 사용: 매 프레임 beginLineIds(보기, 투영) → drawMesh / drawMeshInstanced / drawMeshInstanceBuffer → endLineIds() (씨앗과 넓히기까지 셈) → 툰 몸 그리기 → drawOutline().
  beginLineIds 는 그때의 뷰포트를 그리는 영역으로 적어 두고, 선의 판을 그 크기에 맞춰 짓는다. endLineIds 와 drawOutline 이 프레임버퍼, 뷰포트, 상태를 돌린다.
  ToonDrawQueue.setScreenOutline 으로 맡기면 그리기 줄이 이 차례를 스스로 한다. (엔진 2D 그리기 사이에 끼울 때, 그 전에 Graphic.flushBatch)

예제: 꺾임 선 없이 실루엣만 굵게

```js
import { ToonScreenOutline, ToonOutlineWidthUnit } from "./vanilla.js";
const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
outline.setColor([0.1, 0.08, 0.12, 1]);
outline.setWidth(3, ToonOutlineWidthUnit.pixel);
outline.setCrease(180);
outline.setResolutionScale(0.5);
```

- `constructor(webGL2RenderingContext, options = null)` (constructor, since 0.4.0): 생성. (아홉 프로그램과 빈 버텍스 어레이를 짓는다, 선의 판은 첫 beginLineIds 에서)
  - `webGL2RenderingContext` WebGL2RenderingContext: 툰 렌더러와 같은 컨텍스트.
  - `options` object | null: { colorSpace (ColorSpace, 기본은 엔진의 색 공간, ToonRenderer 와 같은 값) }
- `static readProgramSources(colorSpace = readEngineColorSpace())` (static, since 0.4.0): 셰이더 소스 반환. (ShaderWarmer 로 미리 링크할 때)
  - `colorSpace` string: 색 공간. (ColorSpace, 기본은 엔진의 색 공간, 지을 ToonScreenOutline 과 같은 값을 준다)
  - returns Array<object>: { vertexShader, fragmentShader, attributeLocationTable } 아홉. (번호, 씨앗, 모으기, 넓히기, 섞기, 가는 선의 씨앗 쪽 판, 가는 선 섞기, 줄인 판의 가는 선 덮는 몫, 덮는 몫 판 섞기)

  예제: 셰이더를 미리 링크할 목록에 넣기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const sources = ToonScreenOutline.readProgramSources();
  const vertexShaders = sources.map((source) => source.vertexShader);
  ```

- `setColor(color)` (method, since 0.4.0): 선 색 설정. (sRGB 0 ~ 1 과 알파, 바깥 선과 꺾임 선이 같이 쓴다)
  - `color` Array<number>: [r, g, b, a] 의 sRGB. (a 를 빼면 1)
- `setLineColor(lineId, color)` (method, since 0.4.0): 번호마다 선 색 설정. (그 번호의 모형이 앞에 선 바깥 선과 꺾임 선을 이 색으로, 표에 없는 번호는 setColor 의 색 그대로)
  - 언리얼 Custom Stencil 의 후처리 바깥선처럼 선의 판에 이미 있는 번호로 색을 고른다. 고른 모형을 강조하거나 편마다 색을 달리할 때.
  - 표는 선 번호 전부(1 ~ TOON_SCREEN_OUTLINE_MAXIMUM_LINE_ID)를 덮는 128 x 128 찾기 텍스처다. 넣는 번호 수에 상한이 없고, 화소마다 한 번 읽는다.
  - 두 번호가 맞닿은 선은 앞(카메라에 가까운) 쪽 번호의 색이다. 색은 sRGB 8 비트로 담고, 알파 0 은 1/255 로 담는다. (표에 없음과 가르려고, 보이지 않는 것은 같다)
  - `lineId` number: 1 ~ TOON_SCREEN_OUTLINE_MAXIMUM_LINE_ID. (drawMesh 와 drawMeshInstanced 에 준 번호, 범위 밖이면 하지 않는다)
  - `color` Array<number>: [r, g, b, a] 의 sRGB 0 ~ 1. (a 를 빼면 1)

  예제: 가리킨 바위 하나의 선만 노랗게

  ```js
  import { ToonRenderer, ToonScreenOutline, MeshBuilder, createOctahedronGeometry, Matrix4, Vector3 } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const renderer = new ToonRenderer(webGL2RenderingContext);
  const outline = new ToonScreenOutline(webGL2RenderingContext);
  const builder = new MeshBuilder();
  builder.addPart(createOctahedronGeometry(0.3), Matrix4.createIdentity(), "#a89880", { hasOutline: false });
  const rockMesh = renderer.createMesh(builder.readArrays());
  const instances = [0, 1, 2].map((index) => ({ modelMatrix: Matrix4.createTranslation(index * 0.7, 0, 0), lineId: 10 + index }));
  outline.setLineColor(11, [1, 0.85, 0.2, 1]);
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0.7, 2, 3), Vector3.create(0.7, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  if (outline.beginLineIds(viewMatrix, projectionMatrix)) {
  	outline.drawMeshInstanced(rockMesh, instances, 10);
  	outline.endLineIds();
  }
  renderer.beginFrameWithMatrices(viewMatrix, projectionMatrix);
  renderer.drawMeshInstanced(rockMesh, instances);
  renderer.endFrame();
  outline.drawOutline();
  ```

- `removeLineColor(lineId)` (method, since 0.4.0): 번호 하나를 색 표에서 빼기. (그 번호의 선은 setColor 의 색으로 돌아간다)
  - `lineId` number: setLineColor 에 준 번호.
- `clearLineColors()` (method, since 0.4.0): 색 표 비우기. (모든 선이 setColor 의 색으로 돌아간다)

  예제: 프레임마다 강조할 번호를 다시 넣기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  outline.clearLineColors();
  outline.setLineColor(3, [1, 1, 1, 1]);
  outline.setLineColor(7, [1, 0.3, 0.2, 1]);
  const count = outline.getLineColorCount();
  ```

- `getLineColorCount()` (method, since 0.4.0): 색 표에 든 번호 수 반환.
  - returns number: setLineColor 로 넣고 빼지 않은 번호 수.
- `setWidth(width, unit = ToonOutlineWidthUnit.pixel)` (method, since 0.4.0): 바깥 선 굵기 설정. (실루엣과 다른 번호와 맞닿은 곳의 선, 반지름이 아니라 선 하나의 굵기, 0 이면 긋지 않음)
  - 꺾임 선 굵기도 이 단위로 센다.
  - `width` number: 선 하나의 굵기. (경계를 가운데로 양쪽에 반씩)
  - `unit` string: ToonOutlineWidthUnit. (기본 pixel)
- `setCrease(angleDegrees, width = 1, opacity = 1)` (method, since 0.4.0): 꺾임 선 설정. (같은 번호 안에서 이웃한 면이 이 각(도)보다 더 꺾이면 선, 굵기는 setWidth 의 단위, 진하기 0 ~ 1)
  - `angleDegrees` number: 꺾임 각. (180 이면 꺾임 선을 긋지 않는다)
  - `width` number: 꺾임 선 하나의 굵기. (기본 1, 0 이면 긋지 않는다)
  - `opacity` number: 꺾임 선의 진하기. (기본 1, 선 색의 알파에 곱한다)

  예제: 잔 면은 두고 큰 모서리만 엷게

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  outline.setCrease(75, 1.5, 0.6);
  ```

- `setFocusDistance(focusDistance)` (method, since 0.4.0): 초점 거리 설정. (원근 카메라에서 월드 굵기를 픽셀로 셀 거리, 보통 카메라에서 보는 점까지, 0 이면 그린 모형들의 평균 거리)
  - `focusDistance` number: 카메라에서의 거리. (월드 길이)
- `setDepthThreshold(depthThreshold)` (method, since 0.4.0): 깊이 문턱 설정. (같은 번호의 이웃 화소 가운데 가린 쪽이 가려진 쪽의 접평면에서 이만큼 넘게 떨어지면 가려진 실루엣으로 긋는다, 월드 길이)
  - depthThreshold 는 면에 수직인 거리이고, 화소 여유가 더해진다. (setDepthMargin 의 배율 x 잰 오차, 오차는 화소 크기와 면의 기울기를 따른다)
  - 장면의 크기에 맞춘다. (사람 크기 장면이면 0.02 ~ 0.1, 0 이면 오차 여유만으로 가른다)
  - `depthThreshold` number: 접평면 거리 문턱. (월드 길이, 면에 수직, 0 이상)

  예제: 낮은 턱까지 실루엣으로 긋기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  outline.setDepthThreshold(0.01);
  console.log(outline.getDepthThreshold());
  ```

- `getDepthThreshold()` (method, since 0.4.0): 깊이 문턱 반환. (월드 길이, 면에 수직)
  - returns number: 접평면 거리 문턱. (월드 길이)

  예제: 기본 깊이 문턱 읽기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  console.log(outline.getDepthThreshold()); // 0.05
  ```

- `setDepthMargin(depthMargin)` (method, since 0.4.0): 깊이 여유 배율 설정. (깊이 문턱에 더하는 화소 여유가 잰 오차의 몇 배인지, 기본 2)
  - 이어진 면에서도 생기는 접평면 거리의 오차를 화소마다 잰다. 노멀 8 비트 팔면체의 각 오차(0.0165 rad) x 두 화소 자리 사이 거리(면이 비스듬할수록 길다), 꺾인 줄을 사이에 둔 두 화소의 화소 한 칸 x 두 노멀이 벌어진 사인, 깊이 24 비트 한 칸.
  - 두 면이 나란한 낮은 턱은 꺾임 몫이 0 이라 화소 한 칸보다 낮아도 잡힌다. 둥근 면을 굵은 다각형으로 지어 보간 노멀이 면에서 크게 벗어나는 모형에서 깊이 문턱을 아주 낮추면 잔선이 생기니, 그때 배율을 올린다.
  - 0 이면 깊이 문턱만으로 가른다.
  - `depthMargin` number: 잰 오차에 곱할 배율. (0 이상)

  예제: 여유를 줄여 낮은 턱을 더 잡기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  outline.setDepthThreshold(0.02);
  outline.setDepthMargin(1);
  console.log(outline.getDepthMargin()); // 1
  ```

- `getDepthMargin()` (method, since 0.4.0): 깊이 여유 배율 반환.
  - returns number: 잰 오차에 곱할 배율.

  예제: 기본 깊이 여유 배율 읽기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
  console.log(outline.getDepthMargin()); // 2
  ```

- `setResolutionScale(resolutionScale)` (method, since 0.4.0): 선의 판 해상도 몫 설정. (그리는 영역에 대한 몫 0.25 ~ 1, 기본 1, 폰과 태블릿의 가벼운 길은 낮춘다)
  - 이름대로 섞기 앞의 모든 판(선의 판 = 노멀과 번호와 깊이, 가면, 씨앗, 넓히기 판)을 그리는 영역 x 몫 크기로 잡는다. 선의 판은 몫만큼 작은 해상도로 한 번 더 그려지므로 값이 몫의 제곱으로 준다.
    기본 1 은 그리는 영역과 같은 크기라 화면이 바이트까지 그대로다.
  - 몫이 1 보다 작을 때 가는 선(굵기 x 몫이 약 3.24 선의 판 픽셀 이하)은 선의 판 해상도에서 덮는 몫(진하기와 번호 색을 곱한 RGBA8)을 한 번 셈하고, 마지막 섞기가 그것을 쌍선형으로 한 번 읽는다. (읽기 9 에서 1)
    굵은 선은 선의 판 해상도에서 점프 플러딩을 하고 섞을 때 거리를 그리는 영역 픽셀로 곱해 잰다. 어느 길이든 선 굵기는 그리는 영역 픽셀(setWidth)이고 선 자리는 몫이 작을수록 가장자리가 부드러워진다.
  - getLineTarget() 의 lineTexture, depthTexture 해상도는 그리는 영역 x 몫이다. (광부의 lineTexture 약속 RGBA8, b 와 a 아래 6 비트 번호, a 위 2 비트 깃발, 굵기 0 이면 선의 판만은 그대로, 해상도만 그리는 영역과 다를 수 있다)
  - CSS 화소당 1.5 화소로 맞추려면 몫 = 1.5 / (그리는 버퍼 화소 / CSS 화소) 로 준다. 엔진 해상도 설정이 그리는 버퍼를 묶어 devicePixelRatio 와 다를 수 있어 캔버스의 width / clientWidth 로 센다.
  - `resolutionScale` number: 0.25 ~ 1. (1 이면 그리는 영역 해상도 그대로)

  예제: CSS 화소당 선의 판 1.5 화소로 맞추기

  ```js
  import { ToonScreenOutline } from "./vanilla.js";
  const canvas = document.createElement("canvas");
  canvas.width = 750;
  canvas.height = 990;
  canvas.style.width = "375px";
  const outline = new ToonScreenOutline(canvas.getContext("webgl2"));
  const bufferPixelPerCssPixel = canvas.width / 375;
  outline.setResolutionScale(1.5 / bufferPixelPerCssPixel);
  ```

- `setHdrOutput(isHdrOutput)` (method, since 0.4.0): HDR 출력 설정. (true 면 선 색을 선형으로 바꿔 섞는다, ToonRenderer.setHdrOutput 과 맞춘다)
  - `isHdrOutput` boolean: true 면 선 색을 선형으로 바꿔 섞는다.
- `getColorSpace()` (method, since 0.4.0): 색 공간 반환. (생성 때 정한 값, HDR 출력에서 선 색을 선형으로 바꾸는 곡선, gamma 는 pow 2.2, linear 는 sRGB 표준 곡선)
  - returns string: ColorSpace.gamma 나 ColorSpace.linear.

  예제: 렌더러와 같은 색 공간으로 지은 화면 바깥선

  ```js
  import { ToonRenderer, ToonScreenOutline, ColorSpace } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const renderer = new ToonRenderer(webGL2RenderingContext);
  const screenOutline = new ToonScreenOutline(webGL2RenderingContext, { colorSpace: renderer.getColorSpace() });
  if (screenOutline.getColorSpace() !== ColorSpace.gamma) {
  	throw new Error("default color space is not gamma");
  }
  ```

- `getLineWidths()` (method, since 0.4.0): 이번 프레임의 선 굵기 반환. ([바깥 선, 꺾임 선] 의 그리는 영역 픽셀, endLineIds 뒤에 정해짐)
  - returns Array<number>: [바깥 선 굵기, 꺾임 선 굵기]. (긋지 않으면 0)
- `getLineTarget()` (method, since 0.4.0): 선의 판 반환. (없으면 null)
  - returns object | null: { lineTexture (노멀과 번호 RGBA8), depthTexture (깊이 24), edgeMaskTexture (가는 선의 씨앗 쪽 판 R8UI, 아래 3 비트 바깥 선, 위 3 비트 꺾임 선의 첫 경계 쪽 0 없음, 1 오른쪽, 2 왼쪽, 3 위, 4 아래), seedTexture (온 해상도 씨앗 RGBA16UI, 굵은 선을 긋기 전에는 null), jumpFloodTextures (넓히기 판 RGBA16UI 둘, 굵은 선을 긋기 전에는 빔), jumpFloodIndex (넓히기를 마친 쪽, 점프 플러딩을 안 했으면 -1), coverageTexture (선의 판을 줄였을 때 가는 선의 덮는 몫 RGBA8 곱한 색, 줄여 가는 선을 그을 때 짓기 전에는 null), outlinePath (이번 프레임의 길 "none", "thin", "thinScaled", "jumpFlood"), width, height (선의 판 해상도, 그리는 영역 x setResolutionScale 의 몫), jumpFloodWidth, jumpFloodHeight (선의 판과 같다) }
- `beginLineIds(viewMatrix, projectionMatrix)` (method, since 0.4.0): 번호 그리기 시작. (지금 뷰포트를 그리는 영역으로 적고, 선의 판을 묶어 비운다)
  - `viewMatrix` Matrix4: 보기 행렬.
  - `projectionMatrix` Matrix4: 투영 행렬. (원근이나 직교)
  - returns boolean: 선의 판을 짓지 못했으면 false. (그때 drawMesh 는 아무것도 하지 않는다)
- `drawMesh(mesh, modelMatrix, lineId, options = {})` (method, since 0.4.0): 모형 하나의 번호 그리기. (ToonRenderer.drawMesh 와 같은 행렬과 자세, 화면 밖이면 건너뛴다)
  - `mesh` object | null: ToonRenderer.createMesh 의 결과.
  - `modelMatrix` Matrix4: 모형 행렬.
  - `lineId` number: 1 ~ TOON_SCREEN_OUTLINE_MAXIMUM_LINE_ID. (0 은 빈 곳이라 쓰지 않는다, 같은 번호끼리 맞닿은 곳은 긋지 않는다)
  - `options` object: { jointMatrices Float32Array, isCulled (기본 true), indexStart, indexCount (몸 번호 버퍼의 일부만), isMirrored (주지 않으면 행렬식으로), hasScreenOutline (기본 true, false 면 선은 내지 않고 가리기만), hasCreaseLine (기본 true) }

  예제: 바닥은 선 없이 가리기만, 결정은 꺾임 선 없이

  ```js
  import { ToonRenderer, ToonScreenOutline, MeshBuilder, createBoxGeometry, createOctahedronGeometry, Matrix4, Vector3 } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const renderer = new ToonRenderer(webGL2RenderingContext);
  const outline = new ToonScreenOutline(webGL2RenderingContext);
  const floorBuilder = new MeshBuilder();
  floorBuilder.addPart(createBoxGeometry(6, 0.2, 6), Matrix4.createTranslation(0, -0.1, 0), "#506070", { hasOutline: false });
  const floorMesh = renderer.createMesh(floorBuilder.readArrays());
  const crystalBuilder = new MeshBuilder();
  crystalBuilder.addPart(createOctahedronGeometry(0.5), Matrix4.createTranslation(0, 0.5, 0), "#90d0ff", { hasOutline: false });
  const crystalMesh = renderer.createMesh(crystalBuilder.readArrays());
  const viewMatrix = Matrix4.createLookAt(Vector3.create(3, 3, 4), Vector3.create(0, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  if (outline.beginLineIds(viewMatrix, projectionMatrix)) {
  	outline.drawMesh(floorMesh, Matrix4.createIdentity(), 1, { hasScreenOutline: false });
  	outline.drawMesh(crystalMesh, Matrix4.createIdentity(), 2, { hasCreaseLine: false });
  	outline.endLineIds();
  }
  ```

- `drawMeshInstanced(mesh, instances, lineId, options = {})` (method, since 0.4.0): 같은 모형 여럿의 번호를 한 번에 그리기. (인스턴스에 lineId 가 있으면 그 번호, 없으면 공통 번호)
  - `mesh` object | null: ToonRenderer.createMesh 의 결과.
  - `instances` Array<object>: { modelMatrix: Matrix4, lineId: number (선택) } 의 배열.
  - `lineId` number: 공통 번호.
  - `options` object: drawMesh 와 같다. (꼴은 모든 인스턴스에 같다)

  예제: 돌마다 다른 번호로 맞닿은 곳에도 선

  ```js
  import { ToonRenderer, ToonScreenOutline, MeshBuilder, createOctahedronGeometry, Matrix4, Vector3 } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const renderer = new ToonRenderer(webGL2RenderingContext);
  const outline = new ToonScreenOutline(webGL2RenderingContext);
  const builder = new MeshBuilder();
  builder.addPart(createOctahedronGeometry(0.3), Matrix4.createIdentity(), "#a89880", { hasOutline: false });
  const rockMesh = renderer.createMesh(builder.readArrays());
  const instances = [0, 1, 2].map((index) => ({ modelMatrix: Matrix4.createTranslation(index * 0.5, 0, 0), lineId: 10 + index }));
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0.5, 2, 3), Vector3.create(0.5, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  if (outline.beginLineIds(viewMatrix, projectionMatrix)) {
  	outline.drawMeshInstanced(rockMesh, instances, 10);
  	outline.endLineIds();
  }
  ```

- `drawMeshInstanceBuffer(mesh, instanceBuffer, instanceCount, lineId, options = {})` (method, since 0.4.0): 영구 인스턴스 버퍼로 번호 그리기. (몸과 같은 GPU 버퍼를 쓰고 다시 올리지 않는다, 버퍼 전체의 경계로 통째 거른다)
  - 인스턴스마다 번호는 꼴의 instanceLineId 칸, 없으면 공통 번호. 꼴에 행렬이 없으면 자리를 몰라 그리지 않는다. (정점 조각은 선의 판에서 돌지 않는다)
  - `mesh` object | null: ToonRenderer.createMesh 의 결과.
  - `instanceBuffer` object: ToonRenderer.createInstanceBuffer 의 결과. (지은 렌더러의 버텍스 어레이를 같이 쓴다)
  - `instanceCount` number: 그릴 인스턴스 수. (버퍼에 든 수를 넘으면 자른다)
  - `lineId` number: 공통 번호. (꼴에 instanceLineId 가 없을 때)
  - `options` object: drawMesh 와 같다. (꼴은 모든 인스턴스에 같다)

  예제: 돌 버퍼 하나로 몸과 선의 판을 같이 그리기

  ```js
  import { ToonRenderer, ToonScreenOutline, MeshBuilder, createOctahedronGeometry, Matrix4, Vector3 } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const renderer = new ToonRenderer(webGL2RenderingContext);
  const outline = new ToonScreenOutline(webGL2RenderingContext);
  const builder = new MeshBuilder();
  builder.addPart(createOctahedronGeometry(0.3), Matrix4.createIdentity(), "#a89880", { hasOutline: false });
  const rockMesh = renderer.createMesh(builder.readArrays());
  const instanceLayout = renderer.createInstanceLayout([{ name: "instanceMatrix", size: 16 }, { name: "instanceTint", size: 4 }, { name: "instanceLineId", size: 1 }]);
  const data = new Float32Array(21 * 3);
  for (let index = 0; index < 3; ++index) {
  	data.set(Matrix4.createTranslation(index * 0.5, 0, 0).getElements(), index * 21);
  	data.set([1, 1, 1, 1, 10 + index], index * 21 + 16);
  }
  const instanceBuffer = renderer.createInstanceBuffer(instanceLayout, data);
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0.5, 2, 3), Vector3.create(0.5, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  if (outline.beginLineIds(viewMatrix, projectionMatrix)) {
  	outline.drawMeshInstanceBuffer(rockMesh, instanceBuffer, 3, 10);
  	outline.endLineIds();
  }
  renderer.beginFrameWithMatrices(viewMatrix, projectionMatrix);
  renderer.drawMeshInstanceBuffer(rockMesh, instanceBuffer, 3);
  renderer.endFrame();
  outline.drawOutline();
  ```

- `drawWithCount(mesh, modelElements, instanceCount, lineId, lineIdSource, isMirrored, options, vertexArray)` (method, since 0.4.0): 번호 그리기 본체.
  - `mesh` object: ToonRenderer.createMesh 의 결과.
  - `modelElements` Float32Array: 모형 행렬의 수 16.
  - `instanceCount` number: 0 이면 보통 그리기.
  - `lineId` number: 공통 번호.
  - `lineIdSource` number: 번호를 읽을 곳. (0 공통 번호, 1 틴트 칸의 x, 2 인스턴스 버퍼의 instanceLineId)
  - `isMirrored` boolean: 감기를 뒤집어 그릴지.
  - `options` object: drawMesh 의 꼴.
  - `vertexArray` WebGLVertexArrayObject: 그릴 버텍스 어레이. (모형의 몸이나 모형과 인스턴스 버퍼를 묶은 것)
- `endLineIds()` (method, since 0.4.0): 번호 그리기 끝. (가는 선은 씨앗 쪽 판 하나를 고르고, 굵은 선은 씨앗을 고르고 점프 플러딩으로 넓힌 뒤, 프레임버퍼와 뷰포트와 상태를 돌린다)
  - 가는 선(굵기가 THIN_LINE_MAXIMUM_WIDTH 이하, 약 3.24 픽셀까지): 화면 전체 패스가 씨앗 쪽 판 하나다. 섞기가 둘레 3 x 3 화소의 씨앗을 본다. (씨앗, 모으기, 넓히기 없음)
  - 굵은 선: 씨앗, 모으기, 넓히기 걸음마다 한 패스. (씨앗 판과 넓히기 판은 처음 굵은 선을 그을 때 짓는다)
  - 판을 통째로 덮어쓰는 패스는 앞에 판 비우기(clearBufferuiv, clearBufferfv)를 불러 옛 내용을 읽어 들이지 않게 한다. (타일 GPU 의 판 불러오기를 없앰, invalidateFramebuffer 는 아이패드와 아이폰 사파리에서 프레임마다 약 10 ~ 25 ms 가 붙어 쓰지 않는다)
- `drawOutline()` (method, since 0.4.0): 바깥선 긋기. (가는 선은 씨앗 쪽 판, 굵은 선은 넓힌 씨앗 판으로 beginLineIds 때의 그리는 영역에 섞는다, 깊이는 보지도 쓰지도 않는다, 스텐실은 그대로)
- `readLineWidths()` (method, since 0.4.0): 이번 프레임의 선 굵기 셈. ([바깥 선, 꺾임 선] 의 그리는 영역 픽셀, 0 이 아니면 한 픽셀 밑으로 내리지 않음)
  - returns Array<number>: [바깥 선 굵기, 꺾임 선 굵기].
- `addDrawnDistance(mesh, modelElements)` (method, since 0.4.0): 그린 모형의 거리 더하기. (초점 거리를 주지 않은 원근에서 쓴다, 보기 공간의 앞쪽 거리)
  - `mesh` object: ToonRenderer.createMesh 의 결과.
  - `modelElements` Float32Array: 모형 행렬의 수 16.
- `isVisible(mesh, modelElements)` (method, since 0.4.0): 경계 구가 이번 프레임의 절두체 안에 드는지.
  - `mesh` object: ToonRenderer.createMesh 의 결과.
  - `modelElements` Float32Array: 모형 행렬의 수 16.
  - returns boolean: 절두체 안에 들면 true.
- `readLineTarget(width, height, jumpFloodWidth, jumpFloodHeight)` (method, since 0.4.0): 선의 판 읽기. (크기가 바뀌면 다시 지음, 그리는 영역 해상도의 노멀과 번호 RGBA8, 깊이 24, 씨앗 RGBA16UI 와 넓히기 판 RGBA16UI 둘, 짓지 못하면 null)
  - `width` number: 선의 판 너비. (그리는 영역 픽셀)
  - `height` number: 선의 판 높이.
  - `jumpFloodWidth` number: 넓히기 판 너비.
  - `jumpFloodHeight` number: 넓히기 판 높이.
  - returns object | null: 선의 판. (짓지 못하면 null)
- `deleteLineTarget()` (method, since 0.4.0): 선의 판 지우기.
- `saveState()` (method, since 0.4.0): 그리기 상태 적어 두기.
- `restoreState()` (method, since 0.4.0): 적어 둔 상태로 돌리기.
- `destroy()` (method, since 0.4.0): 다 쓴 뒤 지우기. (선의 판, 버텍스 어레이, 프로그램 일곱)

## Functions and constants

- `TOON_SCREEN_OUTLINE_MAXIMUM_LINE_ID = 16383` (field, since 0.4.0): 선 번호의 상한. (선의 판에 14 비트로 쓴다, 0 은 빈 곳)
  - returns number
- `readJumpFloodSteps(reach)` (method, since 0.4.0): 점프 플러딩의 걸음 목록. (씨앗을 reach 화소까지 넓히는 데 드는 걸음, 큰 것부터 반씩 줄여 1 까지)
  - 걸음 k, k/2, ..., 1 은 씨앗을 2k - 1 화소까지 옮긴다. (Rong, Tan 2006)
  - `reach` number: 씨앗을 옮길 거리. (선의 판 픽셀)
  - returns Array<number>: 큰 것부터 1 까지의 걸음.

  예제: 4 화소까지 넓히는 걸음

  ```js
  import { readJumpFloodSteps } from "./vanilla.js";
  const steps = readJumpFloodSteps(4);
  ```

## ToonOutlineWidthUnit (enum)

굵기의 단위.
- world: 월드 길이. (직교는 투영의 세로 배율로, 원근은 초점 거리에서 화면 픽셀로 셈해 멀수록 가늘어진다)
- pixel: 그리는 영역의 픽셀. (거리와 상관없이 같은 굵기)

예제: 월드 길이로 굵기 주기

```js
import { ToonScreenOutline, ToonOutlineWidthUnit } from "./vanilla.js";
const outline = new ToonScreenOutline(document.createElement("canvas").getContext("webgl2"));
outline.setWidth(0.02, ToonOutlineWidthUnit.world);
outline.setFocusDistance(6);
```

- `world` = `world`: 월드 길이. (멀수록 가늘어진다)
- `pixel` = `pixel`: 그리는 영역의 픽셀. (거리와 상관없이 같은 굵기)
