---
title: "select: WGSL·GLSL 함수 뜻과 사용법, 계산 예제와 입력 범위 및 주의점"
description: "조건에 따라 거짓일 때의 값과 참일 때의 값 중 하나를 고릅니다. 벡터 조건이면 성분별로 선택합니다. 마스크에 따라 색상이나 값을 선택할 때 사용합니다. 조건은 마지막 인수입니다."
url: https://typeshade.dev/ko/reference/functions/select/
language: ko
---

# `select`

함수

조건에 따라 거짓일 때의 값과 참일 때의 값 중 하나를 고릅니다. 벡터 조건이면 성분별로 선택합니다.

## 예제 비교

조건이 거짓이면 첫 번째 값을, 참이면 두 번째 값을 선택합니다. 벡터 조건에서는 성분마다 다른 값을 선택할 수 있습니다.

**예제 1`select(10, 20, false)`**

**결과**

`10`

**예제 2`select(10, 20, true)`**

**결과**

`20`

**예제 3`select(vec2(10, 10), vec2(20, 20), vec2b(true, false))`**

**결과**

`vec2(20, 10)`

예제는 설명용 계산 모델이며 GPU를 실행하지 않습니다. 정확한 호출 형식은 아래 컴파일러 참고 자료에서 설명합니다. 소수 결과는 표시할 때 반올림했습니다. 각 예제는 명시한 전제를 따릅니다. 실제 셰이더의 결과는 대상 언어의 정밀도와 실행 규칙에 따릅니다.

## 활용

마스크에 따라 색상이나 값을 선택할 때 사용합니다.

## 입력 범위와 주의사항

조건은 마지막 인수입니다. 선택 연산은 잘못된 메모리 접근이나 정의되지 않은 식을 막는 용도로 사용할 수 없습니다.

셰이더의 입력 규칙과 정밀도는 [WGSL](https://www.w3.org/TR/WGSL/)과 [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf)에서 규정합니다.

## 관련 함수

- [`any`](https://typeshade.dev/ko/reference/functions/any/)
- [`all`](https://typeshade.dev/ko/reference/functions/all/)
- [`bool`](https://typeshade.dev/ko/reference/functions/bool/)

**WGSL**

`select(a, b, c)`

**GLSL ES 3.00**

`(c ? b : a)`

시그니처와 이름 아래 문장, 컴파일러 메시지는 커밋 af54ddad5의 컴파일러가 쓴 영어 원문입니다.

Returns `trueValue` where `cond` is true and `falseValue` where it is false, in WGSL's argument order: the condition comes last.

## 구문

```ts
declare function select<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u | vec2b | vec3b | vec4b>(a0: V, a1: V, a2: (V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2b : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3b : vec4b)): V
declare function select<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u | vec2b | vec3b | vec4b>(a0: V, a1: V, a2: bool): V
declare function select<P0 extends number | boolean, P1 extends number | boolean>(a0: P0, a1: P1, a2: bool): ScalarOf<P0, P1>
declare function select<T extends Numeric | bool | BoolVec | Vec64Any | f64>(
  falseValue: T,
  trueValue: T,
  cond: bool | BoolVec,
): T
```

매개변수나 반환 위치에 쓰인 `number`는 스칼라가 들어갈 자리입니다. 앰비언트 파일은 GPU 스칼라를 브랜드가 붙은 number로 선언하고 그 브랜드는 선택이라서, 리터럴도 브랜드가 붙은 스칼라도 이 자리에 들어갑니다.

`type f32 = number & { readonly [f32Tag]?: true }`

타입으로 쓴 `number`는 쓰이는 곳을 보고 추론하며, `f32`, `i32`, `u32`는 너비를 고정합니다. 그 행과 컴파일러가 낸 결과는 [TypeScript에서](https://typeshade.dev/ko/guide/language/from-typescript/) 페이지에 있습니다.

시그니처에는 앰비언트 파일이 스스로 쓰려고 선언한 타입이 나올 수 있습니다. 아래가 그 선언입니다.

**`BoolVec`**

`type BoolVec = vec2b | vec3b | vec4b`

**`Vec64Any`**

`type Vec64Any = vec2f64 | vec3f64 | vec4f64`

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## 매개변수

- `falseValue` `V``P0``T`
- `trueValue` `V``P1``T`
- `cond` `(V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2b : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3b : vec4b)``bool``bool | BoolVec`

## 반환값

`V``ScalarOf<P0, P1>``T`

## 설명

A call: `falseValue`, `trueValue` and `cond` are all evaluated, in that order. Write `cond ? trueValue : falseValue` to evaluate one arm.

## 대상별 출력

**WGSL**

`select(a, b, c)`

**GLSL ES 3.00**

`(c ? b : a)`

## 예제

- [`gallery/bool-select/bool-select.shade.ts`](https://typeshade.dev/ko/guide/examples/bool-select/)
- [`gallery/packing-bitcast/packing-bitcast.shade.ts`](https://typeshade.dev/ko/guide/examples/packing-bitcast/)

## 함께 보기

- [함수](https://typeshade.dev/ko/reference/functions/)
- [내장 함수 표](https://typeshade.dev/ko/guide/language/builtins/#maths)
- [`typeshade/core/ir`의 `select()`](https://typeshade.dev/ko/api/select/)
