---
title: "Calculate sprite sheet UVs in three.js"
description: "Calculate three.js sprite sheet UVs: repeat 1/columns by 1/rows, then offset.x = column / columns and offset.y = 1 - (row + 1) / rows for each frame."
canonical: https://appliedmanifold.com/guides/threejs-sprite-sheet-uv/
site: "Applied Manifold: The spatial compute cloud for 3D reality"
---

1.  [Home](https://appliedmanifold.com/)
2.  [Guides](https://appliedmanifold.com/guides/)
3.  Sprite sheet UV math

GUIDE // THREE.JS

# How to calculate UV coordinates for a sprite sheet in three.js

For your grid of C columns and R rows, set texture.repeat to 1/C by 1/R. Frame n sits in column n mod C and row n / C, rounded down. Set offset.x to column / C and offset.y to 1 - (row + 1) / R, since three.js flips loaded images so the top row stays on top.

[Get Free API Key](https://console.appliedmanifold.com/sign-in?next=/api-keys) [See the examples →](https://appliedmanifold.com/docs/pyrofx/examples/)

-   Godot 4 Available
-   Unity Manual setup
-   Unreal Engine Manual setup
-   Roblox Studio Manual setup
-   **three.js** Available

A draft compile is from $0.05, and new accounts start with $5 of usage.

**FIRE\_WHIRLFINAL**

![First frame of the fire\_whirl bake](/bakes/fire_whirl/poster.webp)

Fire whirl, 4.4 x 7.1 m, compiled with Spatial Compiler. [Spec →](https://appliedmanifold.com/docs/pyrofx/examples/#ex-fire_whirl)

01 // HOW TO

## From frame number to offset

1.  Read the grid from the sheet, or from `layout.json`: 8 columns and 8 rows for a default compile.
2.  Set `texture.repeat.set(1 / 8, 1 / 8)` once, so the sprite shows one cell.
3.  For each frame, find its column with `frame % 8` and its row with `Math.floor(frame / 8)`, counting rows from the top.
4.  Call `setFrame` whenever the frame changes, so `texture.offset` moves to the new cell.

setFrame.js

```
export function setFrame(texture, frame, cols = 8, rows = 8) {
  const col = frame % cols;
  const row = Math.floor(frame / cols);
  texture.offset.set(col / cols, 1 - (row + 1) / rows);
}
```

On an 8 x 8 sheet, frame 0 lands at (0, 0.875), frame 7 at (0.875, 0.875), frame 8 at (0, 0.75) and frame 63 at (0.875, 0).

02 // IN A SHADER

## The same math in GLSL

With a `ShaderMaterial` on a plane, do the lookup in the fragment shader and pass the frame as a uniform.

flipbook.frag

```
uniform sampler2D map;
uniform float frame; // whole number, 0 to 63
varying vec2 vUv;

void main() {
  vec2 cell = vec2(mod(frame, 8.0), 7.0 - floor(frame / 8.0));
  gl_FragColor = texture2D(map, (vUv + cell) / 8.0);
}
```

Textures loaded with `flipY` set to false, such as those inside a glTF file, keep the top row at the bottom of UV space. For those, offset.y is row / rows.

03 // FRAME ORDER

## Check the order before you trust the math

The formula assumes frames run from the top-left cell across each row and then down. Spatial Compiler records the frame order and times in `layout.json`, so read them there rather than guessing from the image.

Godot 4 gets a ready scene that relights the smoke. three.js uses the documented snippet. A native six-way material is not built yet.

04 // RELATED

## Related pages

-   [Flipbook on GPUParticles3D Godot 4Set up a GPUParticles3D, ParticleProcessMaterial and Particle Billboard material so each particle in Godot 4 plays a flipbook sheet.](https://appliedmanifold.com/guides/godot-flipbook-gpuparticles3d/)
-   [Roblox flipbook layout Roblox StudioChoose the right FlipbookLayout for a Roblox ParticleEmitter, and re-tile sheets whose grid Roblox does not accept.](https://appliedmanifold.com/guides/roblox-flipbook-layout/)
-   [Loop a sprite animation three.jsLoop a fire or smoke sprite sheet in three.js from elapsed time, so it plays at the same speed on every screen.](https://appliedmanifold.com/guides/threejs-loop-sprite-animation/)
-   [three.js fire whirl three.jsA fire whirl that spins flames into a 7 m column and loops every 2 seconds, played in three.js with a documented snippet.](https://appliedmanifold.com/effects/threejs-fire-whirl/)
-   [three.js VFX three.jsFire, smoke, explosion and dust effects in three.js: sprite sheets you play with a documented snippet, next to Godot 4's ready scene.](https://appliedmanifold.com/effects/threejs-vfx/)
-   [Flipbook textureA flipbook texture packs an animation's frames into one grid image that your engine plays cell by cell on a quad.](https://appliedmanifold.com/learn/flipbook-texture/)
-   [OutputsWith the default export, each view writes four sheets, `<view>_pos.png`, `_neg.png`, `_colour.png` and `_aux.png`, plus `layout.json`, two still previews, the animated `preview_anim.png` and `bake_report.json`. Sheets are 8 x 8 tiles of 256 px by default.](https://appliedmanifold.com/docs/pyrofx/outputs/)

[All pages in Guides →](https://appliedmanifold.com/guides/)

[← Load a sprite sheet](https://appliedmanifold.com/guides/threejs-load-sprite-sheet/)[Loop a sprite animation →](https://appliedmanifold.com/guides/threejs-loop-sprite-animation/)

05 // FAQ

## three.js sprite sheet UV questions

[All questions →](https://appliedmanifold.com/faq/)

**README.THREEJS\_SPRITE\_SHEET\_UV**

### How do I calculate UV coordinates for a sprite sheet with rows and columns in three.js?

Set `texture.repeat` to `1/C` by `1/R`. Compute column `n % C` and row `Math.floor(n / C)`, then offset X by `column / C` and Y from the top.

### How do I set the number of rows and columns for a three.js flipbook texture?

Set `texture.repeat.set(1 / columns, 1 / rows)`. three.js has no built in grid setting, so your code tracks the column and row counts.

### How does a three.js shader know how many frames are in a sprite sheet?

You pass the counts through uniforms or constants. Spatial Compiler records the grid and frame count in `layout.json` so your code can read them instead of hardcoding.

### Is there a simple three.js sprite sheet example?

Yes. Load the texture, set repeat to `1/8` by `1/8` and update `texture.offset` in your animation loop using the column and row of the current frame.

## Compile your first effect

You pay per compile and see the price before it runs. New accounts start with $5 of usage.

[Get Free API Key](https://console.appliedmanifold.com/sign-in?next=/api-keys) [Outputs →](https://appliedmanifold.com/docs/pyrofx/outputs/)

[All effects, guides and explainers →](https://appliedmanifold.com/effects/)
