Object3D.tests.js 16 KB

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