Object3D.tests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**
  2. * @author simonThiele / https://github.com/simonThiele
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { Object3D } from '../../../../src/core/Object3D';
  7. import { Vector3 } from '../../../../src/math/Vector3';
  8. import { Euler } from '../../../../src/math/Euler';
  9. import { Quaternion } from '../../../../src/math/Quaternion';
  10. import { Matrix4 } from '../../../../src/math/Matrix4';
  11. import {
  12. x,
  13. y,
  14. z,
  15. w,
  16. eps
  17. } from '../math/Constants.tests';
  18. export default QUnit.module( 'Core', () => {
  19. QUnit.module( 'Object3D', () => {
  20. var RadToDeg = 180 / Math.PI;
  21. var eulerEquals = function ( a, b, tolerance ) {
  22. tolerance = tolerance || 0.0001;
  23. if ( a.order != b.order ) {
  24. return false;
  25. }
  26. return (
  27. Math.abs( a.x - b.x ) <= tolerance &&
  28. Math.abs( a.y - b.y ) <= tolerance &&
  29. Math.abs( a.z - b.z ) <= tolerance
  30. );
  31. };
  32. // INHERITANCE
  33. QUnit.todo( "Extending", ( assert ) => {
  34. assert.ok( false, "everything's gonna be alright" );
  35. } );
  36. // INSTANCING
  37. QUnit.todo( "Instancing", ( assert ) => {
  38. assert.ok( false, "everything's gonna be alright" );
  39. } );
  40. // STATIC STUFF
  41. QUnit.todo( "DefaultUp", ( assert ) => {
  42. assert.ok( false, "everything's gonna be alright" );
  43. } );
  44. QUnit.todo( "DefaultMatrixAutoUpdate", ( assert ) => {
  45. assert.ok( false, "everything's gonna be alright" );
  46. } );
  47. // PUBLIC STUFF
  48. QUnit.todo( "isObject3D", ( assert ) => {
  49. assert.ok( false, "everything's gonna be alright" );
  50. } );
  51. QUnit.todo( "onBeforeRender", ( assert ) => {
  52. assert.ok( false, "everything's gonna be alright" );
  53. } );
  54. QUnit.todo( "onAfterRender", ( assert ) => {
  55. assert.ok( false, "everything's gonna be alright" );
  56. } );
  57. QUnit.test( "applyMatrix", ( assert ) => {
  58. var a = new Object3D();
  59. var m = new Matrix4();
  60. var expectedPos = new Vector3( x, y, z );
  61. var expectedQuat = new Quaternion( 0.5 * Math.sqrt( 2 ), 0, 0, 0.5 * Math.sqrt( 2 ) );
  62. m.makeRotationX( Math.PI / 2 );
  63. m.setPosition( new Vector3( x, y, z ) );
  64. a.applyMatrix( m );
  65. assert.deepEqual( a.position, expectedPos, "Position has the expected values" );
  66. assert.ok(
  67. Math.abs( a.quaternion.x - expectedQuat.x ) <= eps &&
  68. Math.abs( a.quaternion.y - expectedQuat.y ) <= eps &&
  69. Math.abs( a.quaternion.z - expectedQuat.z ) <= eps,
  70. "Quaternion has the expected values"
  71. );
  72. } );
  73. QUnit.test( "applyQuaternion", ( assert ) => {
  74. var a = new Object3D();
  75. var sqrt = 0.5 * Math.sqrt( 2 );
  76. var quat = new Quaternion( 0, sqrt, 0, sqrt );
  77. var expected = new Quaternion( sqrt / 2, sqrt / 2, 0, 0 );
  78. a.quaternion.set( 0.25, 0.25, 0.25, 0.25 );
  79. a.applyQuaternion( quat );
  80. assert.ok(
  81. Math.abs( a.quaternion.x - expected.x ) <= eps &&
  82. Math.abs( a.quaternion.y - expected.y ) <= eps &&
  83. Math.abs( a.quaternion.z - expected.z ) <= eps,
  84. "Quaternion has the expected values"
  85. );
  86. } );
  87. QUnit.test( "setRotationFromAxisAngle", ( assert ) => {
  88. var a = new Object3D();
  89. var axis = new Vector3( 0, 1, 0 );
  90. var angle = Math.PI;
  91. var expected = new Euler( - Math.PI, 0, - Math.PI );
  92. var euler = new Euler();
  93. a.setRotationFromAxisAngle( axis, angle );
  94. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  95. assert.ok( eulerEquals( euler, expected ), "Correct values after rotation" );
  96. axis.set( 1, 0, 0 );
  97. var angle = 0;
  98. expected.set( 0, 0, 0 );
  99. a.setRotationFromAxisAngle( axis, angle );
  100. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  101. assert.ok( eulerEquals( euler, expected ), "Correct values after zeroing" );
  102. } );
  103. QUnit.test( "setRotationFromEuler", ( assert ) => {
  104. var a = new Object3D();
  105. var rotation = new Euler( ( 45 / RadToDeg ), 0, Math.PI );
  106. var expected = rotation.clone(); // bit obvious
  107. var euler = new Euler();
  108. a.setRotationFromEuler( rotation );
  109. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  110. assert.ok( eulerEquals( euler, expected ), "Correct values after rotation" );
  111. } );
  112. QUnit.test( "setRotationFromMatrix", ( assert ) => {
  113. var a = new Object3D();
  114. var m = new Matrix4();
  115. var eye = new Vector3( 0, 0, 0 );
  116. var target = new Vector3( 0, 1, - 1 );
  117. var up = new Vector3( 0, 1, 0 );
  118. var euler = new Euler();
  119. m.lookAt( eye, target, up );
  120. a.setRotationFromMatrix( m );
  121. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  122. assert.numEqual( euler.x * RadToDeg, 45, "Correct rotation angle" );
  123. } );
  124. QUnit.test( "setRotationFromQuaternion", ( assert ) => {
  125. var a = new Object3D();
  126. var rotation = new Quaternion().setFromEuler( new Euler( Math.PI, 0, - Math.PI ) );
  127. var euler = new Euler();
  128. a.setRotationFromQuaternion( rotation );
  129. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  130. assert.ok( eulerEquals( euler, new Euler( Math.PI, 0, - Math.PI ) ), "Correct values after rotation" );
  131. } );
  132. QUnit.todo( "rotateOnAxis", ( assert ) => {
  133. assert.ok( false, "everything's gonna be alright" );
  134. } );
  135. QUnit.todo( "rotateOnWorldAxis", ( assert ) => {
  136. assert.ok( false, "everything's gonna be alright" );
  137. } );
  138. QUnit.test( "rotateX", ( assert ) => {
  139. var obj = new Object3D();
  140. var angleInRad = 1.562;
  141. obj.rotateX( angleInRad );
  142. assert.numEqual( obj.rotation.x, angleInRad, "x is equal" );
  143. } );
  144. QUnit.test( "rotateY", ( assert ) => {
  145. var obj = new Object3D();
  146. var angleInRad = - 0.346;
  147. obj.rotateY( angleInRad );
  148. assert.numEqual( obj.rotation.y, angleInRad, "y is equal" );
  149. } );
  150. QUnit.test( "rotateZ", ( assert ) => {
  151. var obj = new Object3D();
  152. var angleInRad = 1;
  153. obj.rotateZ( angleInRad );
  154. assert.numEqual( obj.rotation.z, angleInRad, "z is equal" );
  155. } );
  156. QUnit.test( "translateOnAxis", ( assert ) => {
  157. var obj = new Object3D();
  158. obj.translateOnAxis( new Vector3( 1, 0, 0 ), 1 );
  159. obj.translateOnAxis( new Vector3( 0, 1, 0 ), 1.23 );
  160. obj.translateOnAxis( new Vector3( 0, 0, 1 ), - 4.56 );
  161. assert.propEqual( obj.position, {
  162. x: 1,
  163. y: 1.23,
  164. z: - 4.56
  165. } );
  166. } );
  167. QUnit.test( "translateX", ( assert ) => {
  168. var obj = new Object3D();
  169. obj.translateX( 1.234 );
  170. assert.numEqual( obj.position.x, 1.234, "x is equal" );
  171. } );
  172. QUnit.test( "translateY", ( assert ) => {
  173. var obj = new Object3D();
  174. obj.translateY( 1.234 );
  175. assert.numEqual( obj.position.y, 1.234, "y is equal" );
  176. } );
  177. QUnit.test( "translateZ", ( assert ) => {
  178. var obj = new Object3D();
  179. obj.translateZ( 1.234 );
  180. assert.numEqual( obj.position.z, 1.234, "z is equal" );
  181. } );
  182. QUnit.todo( "localToWorld", ( assert ) => {
  183. assert.ok( false, "everything's gonna be alright" );
  184. } );
  185. QUnit.todo( "worldToLocal", ( assert ) => {
  186. assert.ok( false, "everything's gonna be alright" );
  187. } );
  188. QUnit.test( "lookAt", ( assert ) => {
  189. var obj = new Object3D();
  190. obj.lookAt( new Vector3( 0, - 1, 1 ) );
  191. assert.numEqual( obj.rotation.x * RadToDeg, 45, "x is equal" );
  192. } );
  193. QUnit.test( "add/remove", ( assert ) => {
  194. var a = new Object3D();
  195. var child1 = new Object3D();
  196. var child2 = new Object3D();
  197. assert.strictEqual( a.children.length, 0, "Starts with no children" );
  198. a.add( child1 );
  199. assert.strictEqual( a.children.length, 1, "The first child was added" );
  200. assert.strictEqual( a.children[ 0 ], child1, "It's the right one" );
  201. a.add( child2 );
  202. assert.strictEqual( a.children.length, 2, "The second child was added" );
  203. assert.strictEqual( a.children[ 1 ], child2, "It's the right one" );
  204. assert.strictEqual( a.children[ 0 ], child1, "The first one is still there" );
  205. a.remove( child1 );
  206. assert.strictEqual( a.children.length, 1, "The first child was removed" );
  207. assert.strictEqual( a.children[ 0 ], child2, "The second one is still there" );
  208. a.add( child1 );
  209. a.remove( child1, child2 );
  210. assert.strictEqual( a.children.length, 0, "Both children were removed at once" );
  211. child1.add( child2 );
  212. assert.strictEqual( child1.children.length, 1, "The second child was added to the first one" );
  213. a.add( child2 );
  214. assert.strictEqual( a.children.length, 1, "The second one was added to the parent (no remove)" );
  215. assert.strictEqual( a.children[ 0 ], child2, "The second one is now the parent's child again" );
  216. assert.strictEqual( child1.children.length, 0, "The first one no longer has any children" );
  217. } );
  218. QUnit.test( "getObjectById/getObjectByName/getObjectByProperty", ( assert ) => {
  219. var parent = new Object3D();
  220. var childName = new Object3D();
  221. var childId = new Object3D(); // id = parent.id + 2
  222. var childNothing = new Object3D();
  223. parent.prop = true;
  224. childName.name = "foo";
  225. parent.add( childName, childId, childNothing );
  226. assert.strictEqual( parent.getObjectByProperty( 'prop', true ), parent, "Get parent by its own property" );
  227. assert.strictEqual( parent.getObjectByName( "foo" ), childName, "Get child by name" );
  228. assert.strictEqual( parent.getObjectById( parent.id + 2 ), childId, "Get child by Id" );
  229. assert.strictEqual(
  230. parent.getObjectByProperty( 'no-property', 'no-value' ), undefined,
  231. "Unknown property results in undefined"
  232. );
  233. } );
  234. QUnit.test( "getWorldPosition", ( assert ) => {
  235. var a = new Object3D();
  236. var b = new Object3D();
  237. var expectedSingle = new Vector3( x, y, z );
  238. var expectedParent = new Vector3( x, y, 0 );
  239. var expectedChild = new Vector3( x, y, 7 + ( z - z ) );
  240. var position = new Vector3();
  241. a.translateX( x );
  242. a.translateY( y );
  243. a.translateZ( z );
  244. assert.deepEqual( a.getWorldPosition( position ), expectedSingle, "WorldPosition as expected for single object" );
  245. // translate child and then parent
  246. b.translateZ( 7 );
  247. a.add( b );
  248. a.translateZ( - z );
  249. assert.deepEqual( a.getWorldPosition( position ), expectedParent, "WorldPosition as expected for parent" );
  250. assert.deepEqual( b.getWorldPosition( position ), expectedChild, "WorldPosition as expected for child" );
  251. } );
  252. QUnit.todo( "getWorldQuaternion", ( assert ) => {
  253. assert.ok( false, "everything's gonna be alright" );
  254. } );
  255. QUnit.test( "getWorldScale", ( assert ) => {
  256. var a = new Object3D();
  257. var m = new Matrix4().makeScale( x, y, z );
  258. var expected = new Vector3( x, y, z );
  259. a.applyMatrix( m );
  260. assert.deepEqual( a.getWorldScale( new Vector3() ), expected, "WorldScale as expected" );
  261. } );
  262. QUnit.test( "getWorldDirection", ( assert ) => {
  263. var a = new Object3D();
  264. var expected = new Vector3( 0, - 0.5 * Math.sqrt( 2 ), 0.5 * Math.sqrt( 2 ) );
  265. var direction = new Vector3();
  266. a.lookAt( new Vector3( 0, - 1, 1 ) );
  267. a.getWorldDirection( direction );
  268. assert.ok(
  269. Math.abs( direction.x - expected.x ) <= eps &&
  270. Math.abs( direction.y - expected.y ) <= eps &&
  271. Math.abs( direction.z - expected.z ) <= eps,
  272. "Direction has the expected values"
  273. );
  274. } );
  275. QUnit.todo( "raycast", ( assert ) => {
  276. assert.ok( false, "everything's gonna be alright" );
  277. } );
  278. QUnit.test( "traverse/traverseVisible/traverseAncestors", ( assert ) => {
  279. var a = new Object3D();
  280. var b = new Object3D();
  281. var c = new Object3D();
  282. var d = new Object3D();
  283. var names = [];
  284. var expectedNormal = [ "parent", "child", "childchild 1", "childchild 2" ];
  285. var expectedVisible = [ "parent", "child", "childchild 2" ];
  286. var expectedAncestors = [ "child", "parent" ];
  287. a.name = "parent";
  288. b.name = "child";
  289. c.name = "childchild 1";
  290. c.visible = false;
  291. d.name = "childchild 2";
  292. b.add( c );
  293. b.add( d );
  294. a.add( b );
  295. a.traverse( function ( obj ) {
  296. names.push( obj.name );
  297. } );
  298. assert.deepEqual( names, expectedNormal, "Traversed objects in expected order" );
  299. var names = [];
  300. a.traverseVisible( function ( obj ) {
  301. names.push( obj.name );
  302. } );
  303. assert.deepEqual( names, expectedVisible, "Traversed visible objects in expected order" );
  304. var names = [];
  305. c.traverseAncestors( function ( obj ) {
  306. names.push( obj.name );
  307. } );
  308. assert.deepEqual( names, expectedAncestors, "Traversed ancestors in expected order" );
  309. } );
  310. QUnit.todo( "updateMatrix", ( assert ) => {
  311. assert.ok( false, "everything's gonna be alright" );
  312. } );
  313. QUnit.todo( "updateMatrixWorld", ( assert ) => {
  314. assert.ok( false, "everything's gonna be alright" );
  315. } );
  316. QUnit.test( "toJSON", ( assert ) => {
  317. var a = new Object3D();
  318. var child = new Object3D();
  319. var childChild = new Object3D();
  320. a.name = "a's name";
  321. a.matrix.set( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 );
  322. a.visible = false;
  323. a.castShadow = true;
  324. a.receiveShadow = true;
  325. a.userData[ "foo" ] = "bar";
  326. child.uuid = "5D4E9AE8-DA61-4912-A575-71A5BE3D72CD";
  327. childChild.uuid = "B43854B3-E970-4E85-BD41-AAF8D7BFA189";
  328. child.add( childChild );
  329. a.add( child );
  330. var gold = {
  331. "metadata": {
  332. "version": 4.5,
  333. "type": "Object",
  334. "generator": "Object3D.toJSON"
  335. },
  336. "object": {
  337. "uuid": "0A1E4F43-CB5B-4097-8F82-DC2969C0B8C2",
  338. "type": "Object3D",
  339. "name": "a's name",
  340. "castShadow": true,
  341. "receiveShadow": true,
  342. "visible": false,
  343. "userData": { "foo": "bar" },
  344. "matrix": [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
  345. "children": [
  346. {
  347. "uuid": "5D4E9AE8-DA61-4912-A575-71A5BE3D72CD",
  348. "type": "Object3D",
  349. "matrix": [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  350. "children": [
  351. {
  352. "uuid": "B43854B3-E970-4E85-BD41-AAF8D7BFA189",
  353. "type": "Object3D",
  354. "matrix": [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]
  355. }
  356. ]
  357. }
  358. ]
  359. }
  360. };
  361. // hacks
  362. var out = a.toJSON();
  363. out.object.uuid = "0A1E4F43-CB5B-4097-8F82-DC2969C0B8C2";
  364. assert.deepEqual( out, gold, "JSON is as expected" );
  365. } );
  366. QUnit.test( "clone", ( assert ) => {
  367. var a;
  368. var b = new Object3D();
  369. assert.strictEqual( a, undefined, "Undefined pre-clone()" );
  370. a = b.clone();
  371. assert.notStrictEqual( a, b, "Defined but seperate instances post-clone()" );
  372. a.uuid = b.uuid;
  373. assert.deepEqual( a, b, "But identical properties" );
  374. } );
  375. QUnit.test( "copy", ( assert ) => {
  376. var a = new Object3D();
  377. var b = new Object3D();
  378. var child = new Object3D();
  379. var childChild = new Object3D();
  380. a.name = "original";
  381. b.name = "to-be-copied";
  382. b.position.set( x, y, z );
  383. b.quaternion.set( x, y, z, w );
  384. b.scale.set( 2, 3, 4 );
  385. // bogus QUnit.test values
  386. b.matrix.set( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 );
  387. b.matrixWorld.set( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 );
  388. b.matrixAutoUpdate = false;
  389. b.matrixWorldNeedsUpdate = true;
  390. b.layers.mask = 2;
  391. b.visible = false;
  392. b.castShadow = true;
  393. b.receiveShadow = true;
  394. b.frustumCulled = false;
  395. b.renderOrder = 1;
  396. b.userData[ "foo" ] = "bar";
  397. child.add( childChild );
  398. b.add( child );
  399. assert.notDeepEqual( a, b, "Objects are not equal pre-copy()" );
  400. a.copy( b, true );
  401. // check they're all unique instances
  402. assert.ok(
  403. a.uuid !== b.uuid &&
  404. a.children[ 0 ].uuid !== b.children[ 0 ].uuid &&
  405. a.children[ 0 ].children[ 0 ].uuid !== b.children[ 0 ].children[ 0 ].uuid,
  406. "UUIDs are all different"
  407. );
  408. // and now fix that
  409. a.uuid = b.uuid;
  410. a.children[ 0 ].uuid = b.children[ 0 ].uuid;
  411. a.children[ 0 ].children[ 0 ].uuid = b.children[ 0 ].children[ 0 ].uuid;
  412. assert.deepEqual( a, b, "Objects are equal post-copy()" );
  413. } );
  414. } );
  415. } );