normalize
이 페이지에서

normalize

함수

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

함수 그래프

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

normalize 그래프입니다. 곡선이 출력을 나타내며, 강조된 점은 선택한 입력을 따라 움직입니다.
y = 1.25 y = -1.25 x = -3 x = 3
파란 곡선은 출력이고, 주황색 점은 선택한 입력의 위치입니다.

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

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

입력 변화 관찰

  • 이 그래프는 세로 성분이 고정된 벡터를 정규화한 뒤 가로 성분만 표시합니다.
전체 셰이더 코드
Playground에서 열기
normalize-plot.shade.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.));
}
function-plot.shade.ts
function-plot.shade.ts
…
font.shade.ts
font.shade.ts
…
palette.shade.ts
palette.shade.ts
…

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

출력 결과

컴파일러가 본 모듈 전체입니다. 버텍스 진입점과 그것이 돌려주는 VsOut 구조체는 페이지가 붙였고, 나머지는 위 파일입니다.

WGSL
…
GLSL ES 3.00 버텍스
…
GLSL ES 3.00 프래그먼트
…
유니폼 블록
필드타입오프셋
resolutionvec2<f32>0
plotCentervec2<f32>8
plotSpanvec2<f32>16
plotRotationvec2<f32>24
rotationBasisvec4<f32>32
plotPanvec2<f32>48
plotBlendf3256
appearancevec4<f32>64
decorationsu3280
labelStylevec4<f32>96
decorationOpacityvec3<f32>112
modelmat4x4<f32>128
inverseModelmat4x4<f32>192
axesModelmat4x4<f32>256
inverseAxesModelmat4x4<f32>320
sceneOpacityvec2<f32>384
cameraLensvec2<f32>392
plotCenterZf32400
probef32404
parameterFocusSeedf32408
view3du32412
parameterBoundsvec2<f32>416
parameterModeu32424
curveQualityvec2<u32>432
curvePatternvec4<f32>448
curveTilesvec4<u32>464
surfaceBoundsvec4<f32>480
surfaceDomainModeu32496
surfaceRangevec2<f32>504
surfaceHeightvec2<f32>512
surfaceDepthvec2<f32>520
surfaceRendervec4<f32>528
surfaceFlagsu32544

활용

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

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

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

입력 범위와 주의사항

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

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

셰이더의 입력 규칙과 정밀도는 WGSL과 GLSL ES 3.00에서 규정합니다.

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.

구문

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 VT

반환값

VT

설명

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)

예제

함께 보기

이 페이지 편집 문제 보고