---
title: "normalize: WGSL·GLSL 함수 뜻과 사용법, 계산 예제와 입력 범위 및 주의점"
description: "정규화는 영이 아닌 벡터의 방향을 유지하면서 길이를 단위 길이로 바꾸는 연산입니다. 벡터의 크기와 무관하게 방향만 필요한 계산에 사용합니다. 영 벡터에는 하나로 정해지는 방향이 없습니다. 이 예제에서는 세로 성분을 영이 아닌 값으로 유지합니다."
url: https://typeshade.dev/ko/reference/functions/normalize/
language: ko
---

# `normalize`

함수

정규화는 영이 아닌 벡터의 방향을 유지하면서 길이를 단위 길이로 바꾸는 연산입니다.

## 함수 그래프

`y = normalize(vec2(x, 1.)).x`

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

`y` = 1.25 `y` = -1.25 `x` = -3 `x` = 3

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

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

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

입력 변화 관찰

- 이 그래프는 세로 성분이 고정된 벡터를 정규화한 뒤 가로 성분만 표시합니다.

**전체 셰이더 코드**

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

normalize-plot.shade.ts

```ts
"use typeshade";

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

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

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

  return normalize(vec2(x, 1.)).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`.

## 활용

벡터의 크기와 무관하게 방향만 필요한 계산에 사용합니다.

계산 예제: `normalize(vec2(1.0, 1.)).x ≈ 0.707107`

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

## 입력 범위와 주의사항

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

영 벡터에는 하나로 정해지는 방향이 없습니다. 이 예제에서는 세로 성분을 영이 아닌 값으로 유지합니다.

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

## 관련 함수

- [`length`](https://typeshade.dev/ko/reference/functions/length/)
- [`distance`](https://typeshade.dev/ko/reference/functions/distance/)
- [`dot`](https://typeshade.dev/ko/reference/functions/dot/)

**WGSL**

`normalize(a)`

**GLSL ES 3.00**

`normalize(a)`

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

Returns a unit vector in the same direction as the input, computed by dividing each component by the vector's length.

## 구문

```ts
declare function normalize<V extends vec2 | vec3 | vec4>(a0: V): V
declare function normalize<T extends Numeric | Vec64Any>(a: T): T
```

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

**`Vec64Any`**

`type Vec64Any = vec2f64 | vec3f64 | vec4f64`

**`Numeric`**

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

## 매개변수

- `a` `V``T`

## 반환값

`V``T`

## 설명

For the zero vector the GPU result is undefined, and the CPU oracle returns NaN in each component. Also accepts `f64` operands.

## 대상별 출력

**WGSL**

`normalize(a)`

**GLSL ES 3.00**

`normalize(a)`

## 예제

- [`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/path-tracer/path-tracer.shade.ts`](https://typeshade.dev/ko/guide/examples/path-tracer/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/ko/guide/examples/rt-renderer-class/)
- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/ko/guide/examples/bit-bump/)
- [`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/optional-fields/optional-fields.shade.ts`](https://typeshade.dev/ko/guide/examples/optional-fields/)
- [`gallery/optional-values/optional-values.shade.ts`](https://typeshade.dev/ko/guide/examples/optional-values/)
- [`gallery/ray-class/ray-class.shade.ts`](https://typeshade.dev/ko/guide/examples/ray-class/)

## 함께 보기

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