MrGarretto

JSON model rotation

Feb 20th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (model.elements[i].rotation !== undefined) {
  2.    
  3.     var tempOffsetMesh = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), new THREE.MeshBasicMaterial({color: 0x9933ff}));
  4.     curSeg.add(tempOffsetMesh);
  5.  
  6.     var originLocWithoutRot = new THREE.Vector3();
  7.     originLocWithoutRot.x = ((model.elements[i].rotation.origin[0] / 16) - 0.5) - curSeg.position.x;
  8.     originLocWithoutRot.y = ((model.elements[i].rotation.origin[1] / 16) - 0.5) - curSeg.position.y;
  9.     originLocWithoutRot.z = ((model.elements[i].rotation.origin[2] / 16) - 0.5) - curSeg.position.z;
  10.  
  11.     tempOffsetMesh.position.set(originLocWithoutRot.x, originLocWithoutRot.y, originLocWithoutRot.z);
  12.  
  13.     // Set the withoutrot to be the matrix world, so that it is easier to compare the difference afterwards
  14.     tempOffsetMesh.updateMatrixWorld();
  15.     tempOffsetMesh.parent.updateMatrixWorld();
  16.     originLocWithoutRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
  17.  
  18.     // Now the tempOffsetMesh is at the correct location of the origin, and is a child of the segment
  19.  
  20.     // Permanently rotate the segment
  21.     curSeg.rotation[model.elements[i].rotation.axis] = model.elements[i].rotation.angle * (pi / 180);
  22.  
  23.     // Calculate the offset from the locWithoutRot to the new LocWithRot
  24.     tempOffsetMesh.updateMatrixWorld();
  25.     tempOffsetMesh.parent.updateMatrixWorld();
  26.     var originLocWithRot = new THREE.Vector3();
  27.     originLocWithRot.setFromMatrixPosition(tempOffsetMesh.matrixWorld);
  28.  
  29.     // Find difference from withoutRot and WithRot
  30.     var tempDifference = new THREE.Vector3();
  31.  
  32.     tempDifference.x = originLocWithRot.x - originLocWithoutRot.x;
  33.     tempDifference.y = originLocWithRot.y - originLocWithoutRot.y;
  34.     tempDifference.z = originLocWithRot.z - originLocWithoutRot.z;
  35.  
  36.     // Now, move the object in the negated form of that difference
  37.     curSeg.position.x -= tempDifference.x;
  38.     curSeg.position.y -= tempDifference.y;
  39.     curSeg.position.z -= tempDifference.z;
  40.  
  41.     curSeg.remove(tempOffsetMesh)
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment