MrGarretto

uv mapping

Feb 21st, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. curGeometry.faceVertexUvs[0] = [];
  2.  
  3. var curUV = {
  4.     north: model.elements[i].faces.north.uv,
  5.     east: model.elements[i].faces.east.uv,
  6.     south: model.elements[i].faces.south.uv,
  7.     west: model.elements[i].faces.west.uv,
  8.     up: model.elements[i].faces.up.uv,
  9.     down: model.elements[i].faces.down.uv
  10. }
  11. // curUV is an object containing an array of UV mapping for each face
  12.  
  13. // Fill in any blank UV mapping sides
  14. for (key in curUV) {
  15.     if (curUV[key] === undefined) {
  16.         curUV[key] = [0, 0, 16, 16];
  17.     }
  18. }
  19.  
  20. curUVvectors = {
  21.     north: null,
  22.     east: null,
  23.     south: null,
  24.     west: null,
  25.     up: null,
  26.     down: null
  27. };
  28.  
  29. // Divide all curUV values by 16 so it is fitted to the 1x1x1 box geometry
  30.  
  31. var curUVsides = ['north', 'east', 'south', 'west', 'up', 'down'];
  32.  
  33. for (var j = 0; j < curUVsides.length; j++) {
  34.  
  35.     for (var k = 0; k < curUV[curUVsides[j]].length; k++) {
  36.  
  37.         curUV[curUVsides[j]][k] = curUV[curUVsides[j]][k] / 16;
  38.  
  39.     }
  40.  
  41.     // Turn the 2 points into a 4-pointed square, with these being the corner coordinates:
  42.     // curUV[curUVsides[j]][0 and 2] are x (u) coordinates. curUV[curUVsides[j]][1 and 3] are y (v) coordinates
  43.  
  44.     var firstCoordinate = {x: curUV[curUVsides[j]][0], y: curUV[curUVsides[j]][1]};
  45.     var secondCoordinate = {x: curUV[curUVsides[j]][2], y: curUV[curUVsides[j]][3]};
  46.  
  47.     curUVvectors[curUVsides[j]] = [
  48.         new THREE.Vector2(firstCoordinate.x, firstCoordinate.y),
  49.         new THREE.Vector2(firstCoordinate.x, secondCoordinate.y),
  50.         new THREE.Vector2(secondCoordinate.x, secondCoordinate.y),
  51.         new THREE.Vector2(secondCoordinate.x, firstCoordinate.y)
  52.     ]
  53.  
  54. }
  55.  
  56. // Apply UV mapping
  57.  
  58. curGeometry.faceVertexUvs[0][0] = [ curUVvectors.west[0], curUVvectors.west[1], curUVvectors.west[3] ];
  59. curGeometry.faceVertexUvs[0][1] = [ curUVvectors.west[1], curUVvectors.west[2], curUVvectors.west[3] ];
  60.  
  61. curGeometry.faceVertexUvs[0][2] = [ curUVvectors.east[0], curUVvectors.east[1], curUVvectors.east[3] ];
  62. curGeometry.faceVertexUvs[0][3] = [ curUVvectors.east[1], curUVvectors.east[2], curUVvectors.east[3] ];
  63.  
  64. curGeometry.faceVertexUvs[0][4] = [ curUVvectors.up[0], curUVvectors.up[1], curUVvectors.up[3] ];
  65. curGeometry.faceVertexUvs[0][5] = [ curUVvectors.up[1], curUVvectors.up[2], curUVvectors.up[3] ];
  66.  
  67. curGeometry.faceVertexUvs[0][6] = [ curUVvectors.down[0], curUVvectors.down[1], curUVvectors.down[3] ];
  68. curGeometry.faceVertexUvs[0][7] = [ curUVvectors.down[1], curUVvectors.down[2], curUVvectors.down[3] ];
  69.  
  70. curGeometry.faceVertexUvs[0][8] = [ curUVvectors.north[0], curUVvectors.north[1], curUVvectors.north[3] ];
  71. curGeometry.faceVertexUvs[0][9] = [ curUVvectors.north[1], curUVvectors.north[2], curUVvectors.north[3] ];
  72.  
  73. curGeometry.faceVertexUvs[0][10] = [ curUVvectors.south[0], curUVvectors.south[1], curUVvectors.south[3] ];
  74. curGeometry.faceVertexUvs[0][11] = [ curUVvectors.south[1], curUVvectors.south[2], curUVvectors.south[3] ];
Advertisement
Add Comment
Please, Sign In to add comment