> For the complete documentation index, see [llms.txt](https://docs.hology.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hology.app/zh-tw/shaders/typescript-shaders/textures.md).

# Textures

In order to use a texture as the color, the texture data needs to be provided as a uniform to the shader and the color needs to be sampled from the texture on each fragment.

```ts
import { texture2d, uniformSampler2d, float, varyingVec2, Sampler2D, Vec2Node, attributes }  from "@hology/core/shader-nodes"
import * as THREE from 'three'

const sampler: Sampler2D = uniformSampler2d('image')
const coord: Vec2Node = varyingVec2(attributes.uv)
const color: RgbaNode = texture2d(sampler, coord)

const texture = new THREE.TextureLoader().load( "path/to/texture.jpg" )

const material = new NodeShaderMaterial({
    color: color,
    uniforms: { image: { value: texture } }
})
```

If the texture is already available when creating the shader such as when it is passed in to a shader material via a parameter, then you can use the `textureSampler2d` function to create a sampler. By passing in the texture directly to this function, the texture does not also need to be added as a uniform.

```typescript
import { texture2d, textureSampler2d, float, varyingVec2, Sampler2D, Vec2Node, attributes }  from "@hology/core/shader-nodes"
import * as THREE from 'three'

const texture = new THREE.TextureLoader().load( "path/to/texture.jpg" )

const sampler: Sampler2D = textureSampler2d(texture)
const coord: Vec2Node = varyingVec2(attributes.uv)
const color: RgbaNode = sampler.sample(coord)

const material = new NodeShaderMaterial({
    color: color
})
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hology.app/zh-tw/shaders/typescript-shaders/textures.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
