---
title: "textureSample / texture: WGSL·GLSL 함수 사용법과 계산 예제, 입력 범위와 주의점"
description: "정규화된 좌표로 텍스처를 읽습니다. 샘플러가 보간과 주소 처리를 정하고, 인접 프래그먼트의 변화량으로 밉 레벨을 고릅니다. 표면에 이미지를 입힐 때 사용합니다. 화면에서 이미지가 작아지면 세부 수준을 낮춰 계단 현상을 줄입니다."
url: https://typeshade.dev/ko/reference/functions/textureSample/
language: ko
---

# `textureSample`

함수

정규화된 좌표로 텍스처를 읽습니다. 샘플러가 보간과 주소 처리를 정하고, 인접 프래그먼트의 변화량으로 밉 레벨을 고릅니다.

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

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

- GLSL: `texture`
- GLSL: `texture2D`

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

## 예제 비교

텍셀 중심에서는 그 텍셀의 값이, 격자 중심에서는 주변 텍셀을 섞은 값이 나옵니다.

**모델의 전제**`filter = linear; address = clamp-to-edge; level = 0`

**예제 1`uv = vec2(0.25, 0.25)`**

`0.75` `1` `0` `0.25`

텍셀 격자입니다. 위쪽 행의 세로 텍스처 좌표가 더 큽니다. 값은 한 채널을 나타내며 비교 격자는 통과 여부를 보여줍니다.

**결과**

`0`

**예제 2`uv = vec2(0.5, 0.5)`**

`0.75` `1` `0` `0.25`

텍셀 격자입니다. 위쪽 행의 세로 텍스처 좌표가 더 큽니다. 값은 한 채널을 나타내며 비교 격자는 통과 여부를 보여줍니다.

**결과**

`0.5`

**예제 3`uv = vec2(0.75, 0.75)`**

`0.75` `1` `0` `0.25`

텍셀 격자입니다. 위쪽 행의 세로 텍스처 좌표가 더 큽니다. 값은 한 채널을 나타내며 비교 격자는 통과 여부를 보여줍니다.

**결과**

`1`

예제는 설명용 계산 모델이며 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)에서 규정합니다.

## 관련 함수

- [`textureLoad`](https://typeshade.dev/ko/reference/functions/textureLoad/)
- [`textureSampleLevel`](https://typeshade.dev/ko/reference/functions/textureSampleLevel/)
- [`textureSampleBias`](https://typeshade.dev/ko/reference/functions/textureSampleBias/)
- [`textureSampleGrad`](https://typeshade.dev/ko/reference/functions/textureSampleGrad/)
- [`textureGather`](https://typeshade.dev/ko/reference/functions/textureGather/)

**WGSL**

`textureSample(a, b, c)`

**GLSL ES 3.00**

`texture(a, c)`

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

Samples a float texture through the sampler `smp` with the implicit level of detail, in the fragment stage only: at `uv` on a `texture_2d`, by a `vec3` direction on a `texture_cube`, by a `vec3` coordinate on a `texture_3d`, by one `f32` on a `texture_1d`, and on an array texture the argument after the coordinate picks the layer.

## 구문

```ts
declare function textureSample(tex: texture_2d<f32>, smp: sampler, uv: vec2): vec4
declare function textureSample(
  tex: texture_2d_array<f32>,
  smp: sampler,
  uv: vec2,
  layer: number,
): vec4
declare function textureSample(tex: texture_cube<f32>, smp: sampler, dir: vec3): vec4
declare function textureSample(tex: texture_3d<f32>, smp: sampler, coord: vec3): vec4
declare function textureSample(tex: texture_1d<f32>, smp: sampler, coord: number): vec4
declare function textureSample(
  tex: texture_cube_array<f32>,
  smp: sampler,
  dir: vec3,
  layer: number,
): vec4
```

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

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

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

## 매개변수

- `tex` `texture_2d<f32>``texture_2d_array<f32>``texture_cube<f32>``texture_3d<f32>``texture_1d<f32>``texture_cube_array<f32>`
- `smp` `sampler`
- `uv` 선택 사항 `vec2`
- `dir` 선택 사항 `vec3`
- `coord` 선택 사항 `vec3``number`
- `layer` 선택 사항 `number`

## 반환값

`vec4`

## 설명

Compiles to `textureSample` on WGSL and `texture` on GLSL, where the layer is folded into a `vec3` coordinate; the GLSL lowering of a `texture_1d` or a `texture_cube_array` is not supported yet ([Rule 10.5](https://typeshade.dev/ko/reference/rules/10-5/)), so a module using one emits WGSL alone and is not supported yet on the WebGL2 tier.

CPU 오라클에는 텍스처 메모리도 이웃 프래그먼트도 없습니다. `{ gpuStubs: true }`로 컴파일한 모듈이 아니면 호출이 예외를 던집니다. 그 옵션을 주면 자리를 채우는 값이 돌아옵니다.

## 대상별 출력

**WGSL**

`textureSample(a, b, c)`

**GLSL ES 3.00**

`texture(a, c)`

## 예제

- [`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/sample-branch/sample-branch.shade.ts`](https://typeshade.dev/ko/guide/examples/sample-branch/)
- [`gallery/textured-quad/textured-quad.shade.ts`](https://typeshade.dev/ko/guide/examples/textured-quad/)

## 함께 보기

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