2
0

Object3D.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @author simonThiele / https://github.com/simonThiele
  3. */
  4. module( "Object3D" );
  5. var RadToDeg = 180 / Math.PI;
  6. test( "rotateX", function() {
  7. var obj = new THREE.Object3D();
  8. var angleInRad = 1.562;
  9. obj.rotateX(angleInRad);
  10. // should calculate the correct rotation on x
  11. checkIfFloatsAreEqual(obj.rotation.x, angleInRad);
  12. });
  13. test( "rotateY", function() {
  14. var obj = new THREE.Object3D();
  15. var angleInRad = -0.346;
  16. obj.rotateY(angleInRad);
  17. // should calculate the correct rotation on y
  18. checkIfFloatsAreEqual(obj.rotation.y, angleInRad);
  19. });
  20. test( "rotateZ", function() {
  21. var obj = new THREE.Object3D();
  22. var angleInRad = 1;
  23. obj.rotateZ(angleInRad);
  24. // should calculate the correct rotation on y
  25. checkIfFloatsAreEqual(obj.rotation.z, angleInRad);
  26. });
  27. test( "translateOnAxis", function() {
  28. var obj = new THREE.Object3D();
  29. // get a reference object for comparing
  30. var reference = {x: 1, y: 1.23, z: -4.56};
  31. obj.translateOnAxis(new THREE.Vector3(1, 0, 0), 1);
  32. obj.translateOnAxis(new THREE.Vector3(0, 1, 0), 1.23);
  33. obj.translateOnAxis(new THREE.Vector3(0, 0, 1), -4.56);
  34. checkIfPropsAreEqual(reference, obj.position);
  35. });
  36. test( "translateX", function() {
  37. var obj = new THREE.Object3D();
  38. obj.translateX(1.234);
  39. ok( obj.position.x === 1.234 , "x is equal" );
  40. });
  41. test( "translateY", function() {
  42. var obj = new THREE.Object3D();
  43. obj.translateY(1.234);
  44. ok( obj.position.y === 1.234 , "y is equal" );
  45. });
  46. test( "translateZ", function() {
  47. var obj = new THREE.Object3D();
  48. obj.translateZ(1.234);
  49. ok( obj.position.z === 1.234 , "z is equal" );
  50. });
  51. test( "lookAt", function() {
  52. var obj = new THREE.Object3D();
  53. obj.lookAt(new THREE.Vector3(0, -1, 1));
  54. ok( obj.rotation.x * RadToDeg === 45 , "x is equal" );
  55. });
  56. test( "getWorldRotation", function() {
  57. var obj = new THREE.Object3D();
  58. obj.lookAt(new THREE.Vector3(0, -1, 1));
  59. ok( obj.getWorldRotation().x * RadToDeg === 45 , "x is equal" );
  60. obj.lookAt(new THREE.Vector3(1, 0, 0));
  61. ok( obj.getWorldRotation().y * RadToDeg === 90 , "y is equal" );
  62. });
  63. function checkIfPropsAreEqual(reference, obj) {
  64. ok( obj.x === reference.x , "x is equal" );
  65. ok( obj.y === reference.y , "y is equal!" );
  66. ok( obj.z === reference.z , "z is equal!" );
  67. }
  68. // since float equal checking is a mess in js, one solution is to cut off
  69. // decimal places
  70. function checkIfFloatsAreEqual(f1, f2) {
  71. var f1Rounded = ((f1 * 1000) | 0) / 1000;
  72. var f2Rounded = ((f2 * 1000) | 0) / 1000;
  73. ok( f1Rounded === f2Rounded, "passed" );
  74. }