MrGarretto

uv mapping newest

Feb 21st, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. for (var i = 0; i < models.curUVsides.length; i++) {
  2.     if (model.elements[i][models.curUVsides[i]] === undefined) {
  3.         model.elements[i][models.curUVsides[i]] = 'nothing'; // Transparent 1x1 texture
  4.     }
  5. }
  6.  
  7. for (var i = 0; i < model.elements.length; i++) {
  8.  
  9.     textures = models.texFaces({
  10.         north: {
  11.             path: models.getParentTexture(model.elements[i].faces.north.texture)
  12.         },
  13.         east: {
  14.             path: models.getParentTexture(model.elements[i].faces.east.texture)
  15.         },
  16.         south: {
  17.             path: models.getParentTexture(model.elements[i].faces.south.texture)
  18.         },
  19.         west: {
  20.             path: models.getParentTexture(model.elements[i].faces.west.texture)
  21.         },
  22.         up: {
  23.             path: models.getParentTexture(model.elements[i].faces.up.texture)
  24.         },
  25.         down: {
  26.             path: models.getParentTexture(model.elements[i].faces.down.texture)
  27.         }
  28.     });
  29.  
  30.     currentElement.size = {
  31.         x: (model.elements[i].to[0] - model.elements[i].from[0]) / 16,
  32.         y: (model.elements[i].to[1] - model.elements[i].from[1]) / 16,
  33.         z: (model.elements[i].to[2] - model.elements[i].from[2]) / 16
  34.     }
  35.  
  36.     currentElement.position = {
  37.         x: (((model.elements[i].from[0] + model.elements[i].to[0]) / 2) / 16) - 0.5,
  38.         y: (((model.elements[i].from[1] + model.elements[i].to[1]) / 2) / 16) - 0.5,
  39.         z: (((model.elements[i].from[2] + model.elements[i].to[2]) / 2) / 16) - 0.5
  40.     }
  41.  
  42.     // Geometry for segment
  43.     var curGeometry = new THREE.BoxGeometry(currentElement.size.x, currentElement.size.y, currentElement.size.z);
  44.  
  45.     // MeshFaceMaterial for segment
  46.     var curMaterial = new THREE.MeshFaceMaterial(textures);
  47.  
  48.     // Apply UV mapping from JSON file
  49.  
  50.     curGeometry.faceVertexUvs[0] = [];
  51.  
  52.     var curUV = {
  53.         north: model.elements[i].faces.north.uv,
  54.         east: model.elements[i].faces.east.uv,
  55.         south: model.elements[i].faces.south.uv,
  56.         west: model.elements[i].faces.west.uv,
  57.         up: model.elements[i].faces.up.uv,
  58.         down: model.elements[i].faces.down.uv
  59.     }
  60.     // curUV is an object containing an array of UV mapping for each face
  61.  
  62.     // Fill in any blank UV mapping sides
  63.     for (key in curUV) {
  64.         if (curUV[key] === undefined) {
  65.             curUV[key] = [0, 0, 16, 16];
  66.         }
  67.     }
  68.  
  69.     curUVvectors = {
  70.         north: null,
  71.         east: null,
  72.         south: null,
  73.         west: null,
  74.         up: null,
  75.         down: null
  76.     };
  77.  
  78.     // Divide all curUV values by 16 so it is fitted to the 1x1x1 box geometry
  79.  
  80.     for (var j = 0; j < models.curUVsides.length; j++) {
  81.  
  82.         for (var k = 0; k < curUV[models.curUVsides[j]].length; k++) {
  83.  
  84.             curUV[models.curUVsides[j]][k] = curUV[models.curUVsides[j]][k] / 16;
  85.  
  86.         }
  87.  
  88.         // Turn the 2 points into a 4-pointed square, with these being the corner coordinates:
  89.         // curUV[models.curUVsides[j]][0 and 2] are x (u) coordinates. curUV[models.curUVsides[j]][1 and 3] are y (v) coordinates
  90.  
  91.         var firstCoordinate = {x: curUV[models.curUVsides[j]][0], y: curUV[models.curUVsides[j]][1]};
  92.         var secondCoordinate = {x: curUV[models.curUVsides[j]][2], y: curUV[models.curUVsides[j]][3]};
  93.  
  94.         curUVvectors[models.curUVsides[j]] = [
  95.             new THREE.Vector2(firstCoordinate.x, secondCoordinate.y),
  96.             new THREE.Vector2(firstCoordinate.x, firstCoordinate.y),
  97.             new THREE.Vector2(secondCoordinate.x, firstCoordinate.y),
  98.             new THREE.Vector2(secondCoordinate.x, secondCoordinate.y),
  99.         ]
  100.  
  101.     }
  102.  
  103.     // Apply UV mapping
  104.  
  105.     curGeometry.faceVertexUvs[0][0] = [ curUVvectors.west[0], curUVvectors.west[1], curUVvectors.west[3] ];
  106.     curGeometry.faceVertexUvs[0][1] = [ curUVvectors.west[1], curUVvectors.west[2], curUVvectors.west[3] ];
  107.      
  108.     curGeometry.faceVertexUvs[0][2] = [ curUVvectors.east[0], curUVvectors.east[1], curUVvectors.east[3] ];
  109.     curGeometry.faceVertexUvs[0][3] = [ curUVvectors.east[1], curUVvectors.east[2], curUVvectors.east[3] ];
  110.      
  111.     curGeometry.faceVertexUvs[0][4] = [ curUVvectors.up[0], curUVvectors.up[1], curUVvectors.up[3] ];
  112.     curGeometry.faceVertexUvs[0][5] = [ curUVvectors.up[1], curUVvectors.up[2], curUVvectors.up[3] ];
  113.      
  114.     curGeometry.faceVertexUvs[0][6] = [ curUVvectors.down[0], curUVvectors.down[1], curUVvectors.down[3] ];
  115.     curGeometry.faceVertexUvs[0][7] = [ curUVvectors.down[1], curUVvectors.down[2], curUVvectors.down[3] ];
  116.      
  117.     curGeometry.faceVertexUvs[0][8] = [ curUVvectors.north[0], curUVvectors.north[1], curUVvectors.north[3] ];
  118.     curGeometry.faceVertexUvs[0][9] = [ curUVvectors.north[1], curUVvectors.north[2], curUVvectors.north[3] ];
  119.      
  120.     curGeometry.faceVertexUvs[0][10] = [ curUVvectors.south[0], curUVvectors.south[1], curUVvectors.south[3] ];
  121.     curGeometry.faceVertexUvs[0][11] = [ curUVvectors.south[1], curUVvectors.south[2], curUVvectors.south[3] ];
  122. }
Advertisement
Add Comment
Please, Sign In to add comment