# graphics3d/rendertarget (vanilla.js 0.4.0)

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

렌더 타겟. (오프스크린 프레임버퍼 + 컬러 텍스처 [+ 깊이 렌더버퍼 / 깊이 텍스처])

## RenderTarget extends Object

렌더 타겟. (오프스크린 프레임버퍼 + 컬러 텍스처 [+ 깊이 렌더버퍼 / 깊이 텍스처])
- 포스트 프로세싱 체인의 기본 단위.
- 옵션으로 다중 컬러 어태치먼트(MRT) / 반정밀 실수 컬러(HDR) / 깊이 텍스처 / 멀티샘플 / 깊이 비트를 켤 수 있다.
  { colorAttachmentCount: 1, useFloatColor: false, useDepthTexture: false, sampleCount: 0, depthBits: 16 }
- sampleCount 가 2 이상이면 멀티샘플(MSAA) 렌더버퍼에 그리고 resolve() 가 컬러 텍스처(와 깊이 텍스처)로 푼다.
  기기가 받는 수(MAX_SAMPLES, 포맷마다 받는 표본 수)를 넘으면 그 밑의 가장 큰 수로 줄이고, 못 받거나 프레임버퍼가 완성되지 않으면 멀티샘플 없이 짓는다. (getSampleCount)
  멀티샘플이면 getFramebuffer 는 그리는 멀티샘플 프레임버퍼, getResolveFramebuffer 는 텍스처가 붙은 프레임버퍼다. 텍스처를 읽기 전에 resolve() 를 부른다.
- resolve() 는 푼 뒤에도 멀티샘플 색과 깊이를 그대로 둔다. (invalidateFramebuffer 로 버리는 것은 아이패드와 아이폰 사파리에서 프레임마다 수 ~ 수십 ms 가 붙고 이득이 재어지지 않아 하지 않는다, 풀고 나서 이어 그리는 것도 된다)
- depthBits 는 깊이 렌더버퍼의 비트 수(16 이나 24)다. 깊이 텍스처는 늘 24 비트다. 멀티샘플이면서 깊이 텍스처를 쓰면 멀티샘플 깊이도 24 비트로 지어 그대로 푼다.

예제: 멀티샘플 렌더 타겟에 그리고 텍스처로 풀기

```js
import { RenderTarget } from "./build/vanilla.js";

const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const renderTarget = new RenderTarget(webGL2RenderingContext, 256, 256, true, { useDepthTexture: true, sampleCount: 4 });
renderTarget.bind();
webGL2RenderingContext.clearColor(0.2, 0.4, 0.8, 1.0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
renderTarget.resolve();
webGL2RenderingContext.bindFramebuffer(webGL2RenderingContext.FRAMEBUFFER, null);
const sampleCount = renderTarget.getSampleCount();
const colorTexture = renderTarget.getColorTexture();
const depthTexture = renderTarget.getDepthTexture();
console.log(sampleCount, colorTexture !== null, depthTexture !== null);
renderTarget.destroy();
```

- `constructor(webGL2RenderingContext, width, height, useDepth, options = null)` (constructor, since 0.3.0): 생성.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
  - `width` number: 가로 크기. (픽셀)
  - `height` number: 세로 크기. (픽셀)
  - `useDepth` boolean: 깊이 어태치먼트를 붙일지.
  - `options` object | null: { colorAttachmentCount = 1, useFloatColor = false, useDepthTexture = false, sampleCount = 0, depthBits = 16 } (null 이면 모두 기본값)

  예제: 컬러 어태치먼트 둘(MRT)과 실수 컬러

  ```js
  import { RenderTarget } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const renderTarget = new RenderTarget(webGL2RenderingContext, 320, 180, true, { colorAttachmentCount: 2, useFloatColor: true });
  const firstTexture = renderTarget.getColorTexture(0);
  const secondTexture = renderTarget.getColorTexture(1);
  console.log(firstTexture !== null, secondTexture !== null);
  renderTarget.resize(640, 360);
  const width = renderTarget.getWidth();
  const height = renderTarget.getHeight();
  console.log(width, height);
  renderTarget.destroy();
  ```

- `resize(width, height)` (method, since 0.3.0): 크기 변경. (같은 크기면 무시, 다르면 리소스 재생성)
  - `width` number: 새 가로 크기. (픽셀)
  - `height` number: 새 세로 크기. (픽셀)
- `readSupportedSampleCount(requestedSampleCount, internalFormatList)` (method, since 0.4.0): 기기가 받는 표본 수. (요청 수를 MAX_SAMPLES 와 포맷마다 받는 표본 수 밑의 가장 큰 수로, 2 밑이면 0)
  - `requestedSampleCount` number: 요청한 표본 수.
  - `internalFormatList` Array<number>: 렌더버퍼 내부 포맷 목록. (모든 포맷이 받는 수로 줄인다)
  - returns number: 기기가 받는 표본 수. (2 밑이면 0)
- `resolve(isAttachmentKept = false)` (method, since 0.4.0): 멀티샘플을 텍스처로 풀기. (컬러 어태치먼트마다와 깊이 텍스처, 묶여 있던 프레임버퍼로 돌림)
  - 푼 뒤에도 멀티샘플 색과 깊이는 그대로 남는다. (invalidateFramebuffer 로 버리지 않는다, 아이패드와 아이폰 사파리에서 프레임마다 수 ~ 수십 ms 가 붙고 이득이 재어지지 않았다)
  - `isAttachmentKept` boolean: 더는 쓰지 않는다. (옛 꼴과 맞추려고 남김, 어느 쪽이든 푼 뒤에 멀티샘플 색과 깊이를 버리지 않는다)

  예제: 풀고 나서 같은 판에 이어 그리기

  ```js
  import { RenderTarget } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const renderTarget = new RenderTarget(webGL2RenderingContext, 128, 128, true, { sampleCount: 4 });
  renderTarget.bind();
  webGL2RenderingContext.clearColor(0.1, 0.2, 0.3, 1.0);
  webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
  renderTarget.resolve(true);
  renderTarget.bind();
  renderTarget.resolve();
  webGL2RenderingContext.bindFramebuffer(webGL2RenderingContext.FRAMEBUFFER, null);
  console.log(renderTarget.getSampleCount());
  ```

- `bind()` (method, since 0.3.0): 렌더링 대상으로 바인드. (프레임버퍼 + 뷰포트)
- `destroy()` (method, since 0.3.0): 파괴. (GL 리소스 해제)
- `destroyMultisample()` (method, since 0.4.0): 멀티샘플 프레임버퍼와 렌더버퍼 지우기.
- `getWebGL2RenderingContext()` (method, since 0.3.0): 렌더링 컨텍스트 반환.
  - returns WebGL2RenderingContext: 렌더링 컨텍스트.
- `getFramebuffer()` (method, since 0.3.0): 프레임버퍼 반환. (그리는 프레임버퍼, 멀티샘플이면 멀티샘플 프레임버퍼)
  - returns WebGLFramebuffer | null: 그리는 프레임버퍼. (멀티샘플이면 멀티샘플 프레임버퍼, 파괴 뒤면 null)
- `getResolveFramebuffer()` (method, since 0.4.0): 텍스처가 붙은 프레임버퍼 반환. (멀티샘플이 아니면 getFramebuffer 와 같음)
  - returns WebGLFramebuffer | null: 컬러 텍스처가 붙은 프레임버퍼. (파괴 뒤면 null)
- `getSampleCount()` (method, since 0.4.0): 실제로 지은 표본 수 반환. (0 이면 멀티샘플 없음)
  - returns number: 실제로 지은 표본 수. (0 이면 멀티샘플 없음)
- `getDepthBits()` (method, since 0.4.0): 깊이 렌더버퍼 비트 수 반환. (16 이나 24)
  - returns number: 깊이 렌더버퍼 비트 수. (16 이나 24)
- `getColorTexture(attachmentIndex = 0)` (method, since 0.3.0): 컬러 텍스처 반환. (어태치먼트 인덱스, 기본 0)
  - `attachmentIndex` number: 컬러 어태치먼트 번호. (기본 0)
  - returns WebGLTexture | null: 컬러 텍스처. (번호가 범위 밖이면 null)
- `getDepthTexture()` (method, since 0.3.0): 깊이 텍스처 반환. (useDepthTexture 옵션일 때만 존재)
  - returns WebGLTexture | null: 깊이 텍스처. (useDepth 와 useDepthTexture 를 함께 켰을 때만, 아니면 null)
- `getColorAttachmentCount()` (method, since 0.3.0): 컬러 어태치먼트 개수 반환.
  - returns number: 컬러 어태치먼트 개수.
- `getUseFloatColor()` (method, since 0.3.0): 실수 컬러 사용 여부 반환.
  - returns boolean: 컬러가 RGBA16F 면 참.
- `getUseDepthTexture()` (method, since 0.3.0): 깊이 텍스처 사용 여부 반환.
  - returns boolean: 깊이 텍스처를 쓰면 참.
- `getWidth()` (method, since 0.3.0): 가로 크기 반환.
  - returns number: 가로 크기. (픽셀, 파괴 뒤면 0)
- `getHeight()` (method, since 0.3.0): 세로 크기 반환.
  - returns number: 세로 크기. (픽셀, 파괴 뒤면 0)
