# Repeat Tile From Texture Atlas

**URL:** <https://community.khronos.org/t/repeat-tile-from-texture-atlas/104500>\
**Category:** OpenGL\
**Tags:** glsl\
**Created:** [September 1, 2019, 5:50pm UTC](https://community.khronos.org/t/repeat-tile-from-texture-atlas/104500 "2019-09-01T17:50:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GClements](https://community.khronos.org/letter_avatar_proxy/v4/letter/g/e0b2c6/32.png) [@GClements](https://community.khronos.org/u/GClements)\
**Post date:** [September 1, 2019, 6:27pm UTC](https://community.khronos.org/t/repeat-tile-from-texture-atlas/104500/2 "2019-09-01T18:27:45Z")

</div>

> [@BenSwolo](#):
>
> I’ve got a question regarding Texture Atlases and tiling specific tiles from that atlas. Currently I have my project set up so that I calculate the tile UV texture coordinates for each vertex before passing into glsl shader. I was wondering if it was possible to instead having one large texture of the tile on my quad that I may have it smaller (Preferably I would like to pass in a uniform to indicate the amount of times to tile) and tiled multiple times instead.

So you want the same effect as setting the wrap mode to `GL_REPEAT`, but for a tile rather than the entire texture, right?

You need to pass in the size of the tile and its position within the atlas, both as fractions of the texture’s size (i.e. in the range [0,1]). E.g.

```auto
uniform vec2 position;
uniform vec2 size;
...
texcoord = position + size * frac(texcoord);

```

If you’re using mipmaps, there’s the added complication that you can’t use the implicit derivatives generated by `texture()`, as those will be incorrect when you wrap around the edge of a tile. Instead, you need to calculate derivatives from the original texture coordinates and use `textureGrad()`, e.g.:

```auto
uniform vec2 position;
uniform vec2 size;
...
vec2 dx = dFdx(texcoord);
vec2 dy = dFdy(texcoord);
texcoord = position + size * frac(texcoord);
vec4 color = textureGrad(tex, texcoord, dx, dy);

```

> [@BenSwolo](#):
>
> I’ve also heard using texture arrays is another solution, but I am unfamiliar with them and their performance.

A 2D texture array is similar to a 3D texture in that it has a third dimension. But unlike a 3D texture, mipmaps are only in 2 dimensions (so for e.g. a 64x64x16 2D texture array, the next mipmap level would be 32x32x16, not 32x32x8), filtering is only performed in 2D dimensions, and the third texture coordinate is unnormalised (it’s just rounded to the nearest integer to select the layer). 2D texture arrays are created using `glTexImage3D` with the target `GL_TEXTURE_2D_ARRAY` and accessed via a uniform of type `sampler2DArray` in the shader.

If you’re using a 2D texture array, each tile would be the full width and height of the texture so you can just use `GL_REPEAT`. Texture arrays require OpenGL 3.0 or the [EXT\_texture\_array](http://www.opengl.org/registry/specs/EXT/texture_array.txt) extension.

---

_[View the full topic](https://community.khronos.org/t/repeat-tile-from-texture-atlas/104500)._
