Skip to content

Instantly share code, notes, and snippets.

@hacksalot
Created March 16, 2015 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacksalot/30e071961b6dabf3c1be to your computer and use it in GitHub Desktop.
Save hacksalot/30e071961b6dabf3c1be to your computer and use it in GitHub Desktop.
Assign UVs in three.js.
function assignUVs( geometry ) {
geometry.computeBoundingBox();
var max = geometry.boundingBox.max;
var min = geometry.boundingBox.min;
var offset = new THREE.Vector2(0 - min.x, 0 - min.y);
var range = new THREE.Vector2(max.x - min.x, max.y - min.y);
var faces = geometry.faces;
geometry.faceVertexUvs[0] = [];
for (i = 0; i < geometry.faces.length ; i++) {
var v1 = geometry.vertices[ faces[i].a ];
var v2 = geometry.vertices[ faces[i].b ];
var v3 = geometry.vertices[ faces[i].c ];
geometry.faceVertexUvs[0].push([
new THREE.Vector2( ( v1.x + offset.x ) / range.x , ( v1.y + offset.y ) / range.y ),
new THREE.Vector2( ( v2.x + offset.x ) / range.x , ( v2.y + offset.y ) / range.y ),
new THREE.Vector2( ( v3.x + offset.x ) / range.x , ( v3.y + offset.y ) / range.y )
]);
}
geometry.uvsNeedUpdate = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment