Skip to content

Instantly share code, notes, and snippets.

@rnixik
Created January 29, 2016 15:16
Show Gist options
  • Save rnixik/97cbd24f95a28001de16 to your computer and use it in GitHub Desktop.
Save rnixik/97cbd24f95a28001de16 to your computer and use it in GitHub Desktop.
UV coordinates of horizontal cross atlas for CubeGeometry in three.js
var assignUVs = function( geometry ){
var tileUsize = 0.25;
var tileVsize = 0.333;
/*
* lookup-tables for tiles: the number - is offset in size of the tile.
* ..., 1, 2, ... means new THREE.Vector2(tileUsize, 2 * tileVsize)
*
* Each tile in atlas is
* (x3, y3) -- (x2, y2)
* | |
* | |
* (x0, y0) -- (x1, y1)
*
* Format of offsets: x0, y0, x1, y1, x2, y2, x3, y3
*/
var cubeTiles = {};
/* horizontal cross */
cubeTiles.negx = [0, 1, 1, 1, 1, 2, 0, 2];
cubeTiles.posz = [1, 1, 2, 1, 2, 2, 1, 2];
cubeTiles.posx = [2, 1, 3, 1, 3, 2, 2, 2];
cubeTiles.negz = [3, 1, 4, 1, 4, 2, 3, 2];
cubeTiles.posy = [1, 2, 2, 2, 2, 3, 1, 3];
cubeTiles.negy = [1, 0, 2, 0, 2, 1, 1, 1];
var facesUVs = {};
for (var tile in cubeTiles) {
if (cubeTiles.hasOwnProperty(tile)) {
var to = cubeTiles[tile];
facesUVs[tile] = [
new THREE.Vector2(tileUsize * to[0], tileVsize * to[1]),
new THREE.Vector2(tileUsize * to[2], tileVsize * to[3]),
new THREE.Vector2(tileUsize * to[4], tileVsize * to[5]),
new THREE.Vector2(tileUsize * to[6], tileVsize * to[7])
];
}
}
geometry.faceVertexUvs[0][0] = [facesUVs.negx[3], facesUVs.negx[0], facesUVs.negx[2]];
geometry.faceVertexUvs[0][1] = [facesUVs.negx[0], facesUVs.negx[1], facesUVs.negx[2]];
geometry.faceVertexUvs[0][2] = [facesUVs.posx[3], facesUVs.posx[0], facesUVs.posx[2]];
geometry.faceVertexUvs[0][3] = [facesUVs.posx[0], facesUVs.posx[1], facesUVs.posx[2]];
geometry.faceVertexUvs[0][4] = [facesUVs.posy[1], facesUVs.posy[2], facesUVs.posy[0]];
geometry.faceVertexUvs[0][5] = [facesUVs.posy[2], facesUVs.posy[3], facesUVs.posy[0]];
geometry.faceVertexUvs[0][6] = [facesUVs.negy[1], facesUVs.negy[2], facesUVs.negy[0]];
geometry.faceVertexUvs[0][7] = [facesUVs.negy[2], facesUVs.negy[3], facesUVs.negy[0]];
geometry.faceVertexUvs[0][8] = [facesUVs.negz[3], facesUVs.negz[0], facesUVs.negz[2]];
geometry.faceVertexUvs[0][9] = [facesUVs.negz[0], facesUVs.negz[1], facesUVs.negz[2]];
geometry.faceVertexUvs[0][10] = [facesUVs.posz[3], facesUVs.posz[0], facesUVs.posz[2]];
geometry.faceVertexUvs[0][11] = [facesUVs.posz[0], facesUVs.posz[1], facesUVs.posz[2]];
geometry.uvsNeedUpdate = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment