---
title: "mix / lerp: WGSL·GLSL 함수 뜻과 사용법, 계산 예제와 입력 범위 및 주의점"
description: "선형 보간은 가중치에 따라 두 끝값 사이의 값을 구하는 연산입니다. 값, 색, 위치를 혼합하는 데 사용합니다. 가중치가 [0,1] 밖이면 끝값 너머로 외삽합니다. 이를 막으려면 가중치를 명시적으로 제한해야 합니다."
url: https://typeshade.dev/ko/reference/functions/mix/
language: ko
---

# `mix`

함수

선형 보간은 가중치에 따라 두 끝값 사이의 값을 구하는 연산입니다.

**다른 언어의 관련 이름**

관련 연산은 인수, 지원 범위와 의미가 다를 수 있습니다. 다른 언어의 이름을 TypeShade 별칭으로 사용할 수는 없습니다. 지원하는 형태와 제한은 대상 언어에 따라 다릅니다.

- HLSL: `lerp`

[대상별 출력](https://typeshade.dev/ko/reference/functions/mix/#targets)

## 함수 그래프

`y = mix(0., k, x)`

![mix 그래프입니다. 곡선이 출력을 나타내며, 강조된 점은 선택한 입력을 따라 움직입니다.](https://typeshade.dev/stills/reference-function-mix.webp)

`y` = 2.25 `y` = -0.25 `x` = 0 `x` = 1

파란 곡선은 출력이고, 주황색 점은 선택한 입력의 위치입니다.

휠로 확대·축소하고 드래그로 이동한다. 3D에서는 드래그로 회전한다. 키보드: 방향키, +/-, Home으로 초기화.

축 옆에 표시한 범위만 그립니다. 이 범위 밖의 값은 화면에 나타나지 않습니다.

입력 변화 관찰

- x는 보간 가중치이고 k는 두 번째 끝값입니다. k가 커지면 그래프의 기울기도 커집니다.

**전체 셰이더 코드**

[Playground에서 열기](https://typeshade.dev/ko/playground/)

mix-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms { k: f32; }
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return mix(0., u.k, x);
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, false);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, false);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

페이지가 매 프레임 채우는 값이라 컨트롤이 없습니다: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## 활용

값, 색, 위치를 혼합하는 데 사용합니다.

계산 예제: `mix(0., 1.0, 0.25) ≈ 0.25`

참고 계산의 결과는 읽기 쉽게 반올림했습니다. 실제 셰이더 출력은 대상 언어의 정밀도 규칙을 따릅니다.

## 입력 범위와 주의사항

이 예제의 입력 조건: x ∈ ℝ

가중치가 \[ 0,1 \] 밖이면 끝값 너머로 외삽합니다. 이를 막으려면 가중치를 명시적으로 제한해야 합니다.

셰이더의 입력 규칙과 정밀도는 [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)에서 규정합니다.

## 관련 함수

- [`min`](https://typeshade.dev/ko/reference/functions/min/)
- [`max`](https://typeshade.dev/ko/reference/functions/max/)
- [`clamp`](https://typeshade.dev/ko/reference/functions/clamp/)
- [`saturate`](https://typeshade.dev/ko/reference/functions/saturate/)

**WGSL**

`mix(a, b, c)`

**GLSL ES 3.00**

`mix(a, b, c)`

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

Returns `a * (1 - t) + b * t`.

## 구문

```ts
declare function mix<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: (f32)): V
declare function mix<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: V): V
declare function mix<P0 extends number, P1 extends number, P2 extends number>(a0: P0, a1: P1, a2: P2): ScalarOf<P0, P1, P2>
declare function mix(a: vec2, b: vec2, t: number): vec2
declare function mix(a: vec3, b: vec3, t: number): vec3
declare function mix(a: vec4, b: vec4, t: number): vec4
declare function mix(a: vec2f64, b: vec2f64, t: number): vec2f64
declare function mix(a: vec3f64, b: vec3f64, t: number): vec3f64
declare function mix(a: vec4f64, b: vec4f64, t: number): vec4f64
declare function mix<T extends Numeric>(a: T, b: T, t: T): T
```

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

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

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

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

**`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`

## 매개변수

- `a` `V``P0``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`
- `b` `V``P1``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`
- `t` `(f32)``V``P2``number``T`

## 반환값

`V``ScalarOf<P0, P1, P2>``vec2``vec3``vec4``vec2f64``vec3f64``vec4f64``T`

## 설명

`t` is a scalar or the same shape as `a` and `b`. `f64` vectors accept an `f32` blend factor.

CPU 오라클이 `f64`로 직접 계산합니다.

## 대상별 출력

**WGSL**

`mix(a, b, c)`

**GLSL ES 3.00**

`mix(a, b, c)`

## 예제

- [`path/palette/palette.shade.ts`](https://typeshade.dev/ko/guide/examples/palette/)
- [`path/ray-tracer/ray-tracer.shade.ts`](https://typeshade.dev/ko/guide/examples/ray-tracer/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/ko/guide/examples/capsule-corp-namek-class/)
- [`gallery/domain-warp-twin/domain-warp-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/domain-warp-twin/)
- [`gallery/hillshade-twin/hillshade-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/hillshade-twin/)
- [`gallery/julia-twin/julia-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/julia-twin/)
- [`gallery/kaleidoscope-twin/kaleidoscope-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/kaleidoscope-twin/)
- [`gallery/ocean-twin/ocean-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/ocean-twin/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/ko/guide/examples/rt-renderer-class/)
- [`gallery/starfield-twin/starfield-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/starfield-twin/)
- [`gallery/tunnel-twin/tunnel-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/tunnel-twin/)
- [`gallery/fp64-burning-ship-twin/fp64-burning-ship-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-burning-ship-twin/)
- [`gallery/fp64-cancellation-twin/fp64-cancellation-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-cancellation-twin/)
- [`gallery/fp64-checker-plane-twin/fp64-checker-plane-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-checker-plane-twin/)
- [`gallery/fp64-clock-twin/fp64-clock-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-clock-twin/)
- [`gallery/fp64-julia-twin/fp64-julia-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-julia-twin/)
- [`gallery/fp64-loran-twin/fp64-loran-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-loran-twin/)
- [`gallery/fp64-newton-twin/fp64-newton-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-newton-twin/)
- [`gallery/fp64-rtc-twin/fp64-rtc-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-rtc-twin/)
- [`gallery/fp64-sine-sweep-twin/fp64-sine-sweep-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/fp64-sine-sweep-twin/)
- [`gallery/array-methods/array-methods.shade.ts`](https://typeshade.dev/ko/guide/examples/array-methods/)
- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/ko/guide/examples/bit-bump/)
- [`gallery/block-scope/block-scope.shade.ts`](https://typeshade.dev/ko/guide/examples/block-scope/)
- [`gallery/class-builder/class-builder.shade.ts`](https://typeshade.dev/ko/guide/examples/class-builder/)
- [`gallery/class-parts/class-parts.shade.ts`](https://typeshade.dev/ko/guide/examples/class-parts/)
- [`gallery/class-syntax/class-syntax.shade.ts`](https://typeshade.dev/ko/guide/examples/class-syntax/)
- [`gallery/closures/closures.shade.ts`](https://typeshade.dev/ko/guide/examples/closures/)
- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/ko/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/ko/guide/examples/cube-env/)
- [`gallery/cutout/cutout.shade.ts`](https://typeshade.dev/ko/guide/examples/cutout/)
- [`gallery/default-args/default-args.shade.ts`](https://typeshade.dev/ko/guide/examples/default-args/)
- [`gallery/gradient-twin/gradient-twin.shade.ts`](https://typeshade.dev/ko/guide/examples/gradient-twin/)
- [`gallery/higher-order/higher-order.shade.ts`](https://typeshade.dev/ko/guide/examples/higher-order/)
- [`gallery/imported-noise/imported-noise.shade.ts`](https://typeshade.dev/ko/guide/examples/imported-noise/)
- [`gallery/pick-composite/pick-composite.shade.ts`](https://typeshade.dev/ko/guide/examples/pick-composite/)
- [`gallery/reference-parameters/reference-parameters.shade.ts`](https://typeshade.dev/ko/guide/examples/reference-parameters/)
- [`gallery/rng-method/rng-method.shade.ts`](https://typeshade.dev/ko/guide/examples/rng-method/)
- [`gallery/shadow-compare/shadow-compare.shade.ts`](https://typeshade.dev/ko/guide/examples/shadow-compare/)
- [`gallery/textured-quad/textured-quad.shade.ts`](https://typeshade.dev/ko/guide/examples/textured-quad/)
- [`gallery/uniform-array/uniform-array.shade.ts`](https://typeshade.dev/ko/guide/examples/uniform-array/)

## 함께 보기

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