MrGarretto

mc json model loader

Feb 20th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. loadJSON: function(jsonText) {
  2.     var model = JSON.parse(jsonText);
  3.  
  4.     console.log(model);
  5.  
  6.     // Turn elements of model into segments then compile and add to scene
  7.  
  8.     var textures; // Commented out because textures are last priority in the RP model loader
  9.  
  10.     var segments = [];
  11.  
  12.     var currentElement = {};
  13.  
  14.     // Now, texrefs is an array of the names of the texture files in the blocks folder (not including the "blocks/" part)
  15.  
  16.     for (var i = 0; i < model.elements.length; i++) {
  17.  
  18.         textures = models.texFaces({
  19.             north: {
  20.                 path: model.textures[model.elements[i].faces.north.texture.replace('#', '')].replace('blocks/', ''),
  21.                 uv: []
  22.             },
  23.             east: {
  24.                 path: model.textures[model.elements[i].faces.east.texture.replace('#', '')].replace('blocks/', ''),
  25.                 uv: []
  26.             },
  27.             south: {
  28.                 path: model.textures[model.elements[i].faces.south.texture.replace('#', '')].replace('blocks/', ''),
  29.                 uv: []
  30.             },
  31.             west: {
  32.                 path: model.textures[model.elements[i].faces.west.texture.replace('#', '')].replace('blocks/', ''),
  33.                 uv: []
  34.             },
  35.             up: {
  36.                 path: model.textures[model.elements[i].faces.up.texture.replace('#', '')].replace('blocks/', ''),
  37.                 uv: []
  38.             },
  39.             down: {
  40.                 path: model.textures[model.elements[i].faces.down.texture.replace('#', '')].replace('blocks/', ''),
  41.                 uv: []
  42.             }
  43.         });
  44.  
  45.         currentElement.size = {
  46.             x: (model.elements[i].to[0] - model.elements[i].from[0]) / 16,
  47.             y: (model.elements[i].to[1] - model.elements[i].from[1]) / 16,
  48.             z: (model.elements[i].to[2] - model.elements[i].from[2]) / 16
  49.         }
  50.  
  51.         currentElement.position = {
  52.             x: (((model.elements[i].from[0] + model.elements[i].to[0]) / 2) / 16) - 0.5,
  53.             y: (((model.elements[i].from[1] + model.elements[i].to[1]) / 2) / 16) - 0.5,
  54.             z: (((model.elements[i].from[2] + model.elements[i].to[2]) / 2) / 16) - 0.5
  55.         }
  56.  
  57.         // Geometry for segment
  58.         var curGeometry = new THREE.BoxGeometry(currentElement.size.x, currentElement.size.y, currentElement.size.z);
  59.  
  60.         // MeshFaceMaterial for segment
  61.         var curMaterial = new THREE.MeshFaceMaterial(textures);
  62.  
  63.         // Apply UV mapping from JSON file
  64.  
  65.         curGeometry.faceVertexUvs[0] = [];
  66.  
  67.         var curUV = {
  68.             north: model.elements[i].faces.north.uv,
  69.             east: model.elements[i].faces.east.uv,
  70.             south: model.elements[i].faces.south.uv,
  71.             west: model.elements[i].faces.west.uv,
  72.             up: model.elements[i].faces.up.uv,
  73.             down: model.elements[i].faces.down.uv
  74.         }
  75.         // curUV is an object containing an array of UV mapping for each face
  76.  
  77.         curUVvectors = {
  78.             north: null,
  79.             east: null,
  80.             south: null,
  81.             west: null,
  82.             up: null,
  83.             down: null
  84.         };
  85.  
  86.         // Divide all curUV values by 16 so it is fitted to the 1x1x1 box geometry
  87.  
  88.         var curUVsides = ['north', 'east', 'south', 'west', 'up', 'down'];
  89.  
  90.         for (var j = 0; j < curUVsides.length; j++) {
  91.  
  92.             for (var k = 0; k < curUV[curUVsides[j]].length; k++) {
  93.  
  94.                 curUV[curUVsides[j]][k] = curUV[curUVsides[j]][k] / 16;
  95.  
  96.             }
  97.  
  98.             // Turn the 2 points into a 4-pointed square, with these being the corner coordinates:
  99.             // curUV[curUVsides[j]][0 and 2] are x (u) coordinates. curUV[curUVsides[j]][1 and 3] are y (v) coordinates
  100.  
  101.             var firstCoordinate = {x: curUV[curUVsides[j]][0], y: curUV[curUVsides[j]][1]};
  102.             var secondCoordinate = {x: curUV[curUVsides[j]][2], y: curUV[curUVsides[j]][3]};
  103.  
  104.             curUVvectors[curUVsides[j]] = [
  105.                 new THREE.Vector2(firstCoordinate.x, firstCoordinate.y),
  106.                 new THREE.Vector2(firstCoordinate.x, secondCoordinate.y),
  107.                 new THREE.Vector2(secondCoordinate.x, secondCoordinate.y),
  108.                 new THREE.Vector2(secondCoordinate.x, firstCoordinate.y)
  109.             ]
  110.  
  111.         }
  112.  
  113.         // Apply UV mapping
  114.  
  115.         curGeometry.faceVertexUvs[0][0] = [ curUVvectors.west[0], curUVvectors.west[1], curUVvectors.west[3] ];
  116.         curGeometry.faceVertexUvs[0][1] = [ curUVvectors.west[1], curUVvectors.west[2], curUVvectors.west[3] ];
  117.          
  118.         curGeometry.faceVertexUvs[0][2] = [ curUVvectors.east[0], curUVvectors.east[1], curUVvectors.east[3] ];
  119.         curGeometry.faceVertexUvs[0][3] = [ curUVvectors.east[1], curUVvectors.east[2], curUVvectors.east[3] ];
  120.          
  121.         curGeometry.faceVertexUvs[0][4] = [ curUVvectors.up[0], curUVvectors.up[1], curUVvectors.up[3] ];
  122.         curGeometry.faceVertexUvs[0][5] = [ curUVvectors.up[1], curUVvectors.up[2], curUVvectors.up[3] ];
  123.          
  124.         curGeometry.faceVertexUvs[0][6] = [ curUVvectors.down[0], curUVvectors.down[1], curUVvectors.down[3] ];
  125.         curGeometry.faceVertexUvs[0][7] = [ curUVvectors.down[1], curUVvectors.down[2], curUVvectors.down[3] ];
  126.          
  127.         curGeometry.faceVertexUvs[0][8] = [ curUVvectors.north[0], curUVvectors.north[1], curUVvectors.north[3] ];
  128.         curGeometry.faceVertexUvs[0][9] = [ curUVvectors.north[1], curUVvectors.north[2], curUVvectors.north[3] ];
  129.          
  130.         curGeometry.faceVertexUvs[0][10] = [ curUVvectors.south[0], curUVvectors.south[1], curUVvectors.south[3] ];
  131.         curGeometry.faceVertexUvs[0][11] = [ curUVvectors.south[1], curUVvectors.south[2], curUVvectors.south[3] ];
  132.  
  133.         // Create segment
  134.         segments[segments.length] = new THREE.Mesh(curGeometry, curMaterial);
  135.         var curSeg = segments[segments.length-1];
  136.  
  137.         curSeg.position.set(currentElement.position.x, currentElement.position.y, currentElement.position.z);
  138.  
  139.         // Rotation offsets for each individual segment
  140.         if (model.elements[i].rotation !== undefined) {
  141.            
  142.             var tempOffsetMesh = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), new THREE.MeshBasicMaterial({color: 0x9933ff}));
  143.             curSeg.add(tempOffsetMesh);
  144.  
  145.             var originLocWithoutRot = new THREE.Vector3();
  146.             originLocWithoutRot.x = ((model.elements[i].rotation.origin[0] / 16) - 0.5) - curSeg.position.x;
  147.             originLocWithoutRot.y = ((model.elements[i].rotation.origin[1] / 16) - 0.5) - curSeg.position.y;
  148.             originLocWithoutRot.z = ((model.elements[i].rotation.origin[2] / 16) - 0.5) - curSeg.position.z;
  149.  
  150.             tempOffsetMesh.position.set(originLocWithoutRot.x, originLocWithoutRot.y, originLocWithoutRot.z);
  151.  
  152.             // Set the withoutrot to be the matrix world, so that it is easier to compare the difference afterwards
  153.             tempOffsetMesh.updateMatrixWorld();
  154.             tempOffsetMesh.parent.updateMatrixWorld();
  155.             originLocWithoutRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
  156.  
  157.             // Now the tempOffsetMesh is at the correct location of the origin, and is a child of the segment
  158.  
  159.             // Permanently rotate the segment
  160.             curSeg.rotation[model.elements[i].rotation.axis] = model.elements[i].rotation.angle * (pi / 180);
  161.  
  162.             // Calculate the offset from the locWithoutRot to the new LocWithRot
  163.             tempOffsetMesh.updateMatrixWorld();
  164.             tempOffsetMesh.parent.updateMatrixWorld();
  165.             var originLocWithRot = new THREE.Vector3();
  166.             originLocWithRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
  167.  
  168.             // Find difference from withoutRot and WithRot
  169.             var tempDifference = new THREE.Vector3();
  170.  
  171.             tempDifference.x = originLocWithRot.x - originLocWithoutRot.x;
  172.             tempDifference.y = originLocWithRot.y - originLocWithoutRot.y;
  173.             tempDifference.z = originLocWithRot.z - originLocWithoutRot.z;
  174.  
  175.             // Now, move the object in the negated form of that difference
  176.             curSeg.position.x -= tempDifference.x;
  177.             curSeg.position.y -= tempDifference.y;
  178.             curSeg.position.z -= tempDifference.z;
  179.  
  180.             curSeg.remove(tempOffsetMesh);
  181.  
  182.         }
  183.  
  184.     }
  185.  
  186.     // Model display on head
  187.  
  188.     var display;
  189.  
  190.     if (model.display !== undefined && model.display.head !== undefined) {
  191.         display = {
  192.             rotation: {
  193.                 x: model.display.head.rotation[0],
  194.                 y: model.display.head.rotation[1],
  195.                 z: model.display.head.rotation[2]
  196.             },
  197.             translation: {
  198.                 x: model.display.head.translation[0],
  199.                 y: model.display.head.translation[1],
  200.                 z: model.display.head.translation[2]
  201.             },
  202.             scale: {
  203.                 x: model.display.head.scale[0],
  204.                 y: model.display.head.scale[1],
  205.                 z: model.display.head.scale[2]
  206.             }
  207.         }
  208.     } else {
  209.  
  210.         display = undefined;
  211.  
  212.     }
  213.  
  214.     scene.add(models.compile({seg: segments, isItem: false, modelDisplay: display})); // Compile segments then add to scene
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment