Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- loadJSON: function(jsonText) {
- var model = JSON.parse(jsonText);
- console.log(model);
- // Turn elements of model into segments then compile and add to scene
- var textures; // Commented out because textures are last priority in the RP model loader
- var segments = [];
- var currentElement = {};
- // Now, texrefs is an array of the names of the texture files in the blocks folder (not including the "blocks/" part)
- for (var i = 0; i < model.elements.length; i++) {
- textures = models.texFaces({
- north: {
- path: model.textures[model.elements[i].faces.north.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- },
- east: {
- path: model.textures[model.elements[i].faces.east.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- },
- south: {
- path: model.textures[model.elements[i].faces.south.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- },
- west: {
- path: model.textures[model.elements[i].faces.west.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- },
- up: {
- path: model.textures[model.elements[i].faces.up.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- },
- down: {
- path: model.textures[model.elements[i].faces.down.texture.replace('#', '')].replace('blocks/', ''),
- uv: []
- }
- });
- currentElement.size = {
- x: (model.elements[i].to[0] - model.elements[i].from[0]) / 16,
- y: (model.elements[i].to[1] - model.elements[i].from[1]) / 16,
- z: (model.elements[i].to[2] - model.elements[i].from[2]) / 16
- }
- currentElement.position = {
- x: (((model.elements[i].from[0] + model.elements[i].to[0]) / 2) / 16) - 0.5,
- y: (((model.elements[i].from[1] + model.elements[i].to[1]) / 2) / 16) - 0.5,
- z: (((model.elements[i].from[2] + model.elements[i].to[2]) / 2) / 16) - 0.5
- }
- // Geometry for segment
- var curGeometry = new THREE.BoxGeometry(currentElement.size.x, currentElement.size.y, currentElement.size.z);
- // MeshFaceMaterial for segment
- var curMaterial = new THREE.MeshFaceMaterial(textures);
- // Apply UV mapping from JSON file
- curGeometry.faceVertexUvs[0] = [];
- var curUV = {
- north: model.elements[i].faces.north.uv,
- east: model.elements[i].faces.east.uv,
- south: model.elements[i].faces.south.uv,
- west: model.elements[i].faces.west.uv,
- up: model.elements[i].faces.up.uv,
- down: model.elements[i].faces.down.uv
- }
- // curUV is an object containing an array of UV mapping for each face
- curUVvectors = {
- north: null,
- east: null,
- south: null,
- west: null,
- up: null,
- down: null
- };
- // Divide all curUV values by 16 so it is fitted to the 1x1x1 box geometry
- var curUVsides = ['north', 'east', 'south', 'west', 'up', 'down'];
- for (var j = 0; j < curUVsides.length; j++) {
- for (var k = 0; k < curUV[curUVsides[j]].length; k++) {
- curUV[curUVsides[j]][k] = curUV[curUVsides[j]][k] / 16;
- }
- // Turn the 2 points into a 4-pointed square, with these being the corner coordinates:
- // curUV[curUVsides[j]][0 and 2] are x (u) coordinates. curUV[curUVsides[j]][1 and 3] are y (v) coordinates
- var firstCoordinate = {x: curUV[curUVsides[j]][0], y: curUV[curUVsides[j]][1]};
- var secondCoordinate = {x: curUV[curUVsides[j]][2], y: curUV[curUVsides[j]][3]};
- curUVvectors[curUVsides[j]] = [
- new THREE.Vector2(firstCoordinate.x, firstCoordinate.y),
- new THREE.Vector2(firstCoordinate.x, secondCoordinate.y),
- new THREE.Vector2(secondCoordinate.x, secondCoordinate.y),
- new THREE.Vector2(secondCoordinate.x, firstCoordinate.y)
- ]
- }
- // Apply UV mapping
- curGeometry.faceVertexUvs[0][0] = [ curUVvectors.west[0], curUVvectors.west[1], curUVvectors.west[3] ];
- curGeometry.faceVertexUvs[0][1] = [ curUVvectors.west[1], curUVvectors.west[2], curUVvectors.west[3] ];
- curGeometry.faceVertexUvs[0][2] = [ curUVvectors.east[0], curUVvectors.east[1], curUVvectors.east[3] ];
- curGeometry.faceVertexUvs[0][3] = [ curUVvectors.east[1], curUVvectors.east[2], curUVvectors.east[3] ];
- curGeometry.faceVertexUvs[0][4] = [ curUVvectors.up[0], curUVvectors.up[1], curUVvectors.up[3] ];
- curGeometry.faceVertexUvs[0][5] = [ curUVvectors.up[1], curUVvectors.up[2], curUVvectors.up[3] ];
- curGeometry.faceVertexUvs[0][6] = [ curUVvectors.down[0], curUVvectors.down[1], curUVvectors.down[3] ];
- curGeometry.faceVertexUvs[0][7] = [ curUVvectors.down[1], curUVvectors.down[2], curUVvectors.down[3] ];
- curGeometry.faceVertexUvs[0][8] = [ curUVvectors.north[0], curUVvectors.north[1], curUVvectors.north[3] ];
- curGeometry.faceVertexUvs[0][9] = [ curUVvectors.north[1], curUVvectors.north[2], curUVvectors.north[3] ];
- curGeometry.faceVertexUvs[0][10] = [ curUVvectors.south[0], curUVvectors.south[1], curUVvectors.south[3] ];
- curGeometry.faceVertexUvs[0][11] = [ curUVvectors.south[1], curUVvectors.south[2], curUVvectors.south[3] ];
- // Create segment
- segments[segments.length] = new THREE.Mesh(curGeometry, curMaterial);
- var curSeg = segments[segments.length-1];
- curSeg.position.set(currentElement.position.x, currentElement.position.y, currentElement.position.z);
- // Rotation offsets for each individual segment
- if (model.elements[i].rotation !== undefined) {
- var tempOffsetMesh = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), new THREE.MeshBasicMaterial({color: 0x9933ff}));
- curSeg.add(tempOffsetMesh);
- var originLocWithoutRot = new THREE.Vector3();
- originLocWithoutRot.x = ((model.elements[i].rotation.origin[0] / 16) - 0.5) - curSeg.position.x;
- originLocWithoutRot.y = ((model.elements[i].rotation.origin[1] / 16) - 0.5) - curSeg.position.y;
- originLocWithoutRot.z = ((model.elements[i].rotation.origin[2] / 16) - 0.5) - curSeg.position.z;
- tempOffsetMesh.position.set(originLocWithoutRot.x, originLocWithoutRot.y, originLocWithoutRot.z);
- // Set the withoutrot to be the matrix world, so that it is easier to compare the difference afterwards
- tempOffsetMesh.updateMatrixWorld();
- tempOffsetMesh.parent.updateMatrixWorld();
- originLocWithoutRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
- // Now the tempOffsetMesh is at the correct location of the origin, and is a child of the segment
- // Permanently rotate the segment
- curSeg.rotation[model.elements[i].rotation.axis] = model.elements[i].rotation.angle * (pi / 180);
- // Calculate the offset from the locWithoutRot to the new LocWithRot
- tempOffsetMesh.updateMatrixWorld();
- tempOffsetMesh.parent.updateMatrixWorld();
- var originLocWithRot = new THREE.Vector3();
- originLocWithRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
- // Find difference from withoutRot and WithRot
- var tempDifference = new THREE.Vector3();
- tempDifference.x = originLocWithRot.x - originLocWithoutRot.x;
- tempDifference.y = originLocWithRot.y - originLocWithoutRot.y;
- tempDifference.z = originLocWithRot.z - originLocWithoutRot.z;
- // Now, move the object in the negated form of that difference
- curSeg.position.x -= tempDifference.x;
- curSeg.position.y -= tempDifference.y;
- curSeg.position.z -= tempDifference.z;
- curSeg.remove(tempOffsetMesh);
- }
- }
- // Model display on head
- var display;
- if (model.display !== undefined && model.display.head !== undefined) {
- display = {
- rotation: {
- x: model.display.head.rotation[0],
- y: model.display.head.rotation[1],
- z: model.display.head.rotation[2]
- },
- translation: {
- x: model.display.head.translation[0],
- y: model.display.head.translation[1],
- z: model.display.head.translation[2]
- },
- scale: {
- x: model.display.head.scale[0],
- y: model.display.head.scale[1],
- z: model.display.head.scale[2]
- }
- }
- } else {
- display = undefined;
- }
- scene.add(models.compile({seg: segments, isItem: false, modelDisplay: display})); // Compile segments then add to scene
- }
Advertisement
Add Comment
Please, Sign In to add comment