Created
March 16, 2015 04:00
-
-
Save hacksalot/30e071961b6dabf3c1be to your computer and use it in GitHub Desktop.
Assign UVs in three.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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