Object3D.tests.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /* global QUnit */
  2. import { Object3D } from '../../../../src/core/Object3D.js';
  3. import { Vector3 } from '../../../../src/math/Vector3.js';
  4. import { Euler } from '../../../../src/math/Euler.js';
  5. import { Quaternion } from '../../../../src/math/Quaternion.js';
  6. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  7. import {
  8. x,
  9. y,
  10. z,
  11. w,
  12. eps
  13. } from '../../utils/math-constants.js';
  14. import { EventDispatcher } from '../../../../src/core/EventDispatcher.js';
  15. const matrixEquals4 = ( a, b ) => {
  16. for ( let i = 0; i < 16; i ++ ) {
  17. if ( Math.abs( a.elements[ i ] - b.elements[ i ] ) >= eps ) {
  18. return false;
  19. }
  20. }
  21. return true;
  22. };
  23. export default QUnit.module( 'Core', () => {
  24. QUnit.module( 'Object3D', () => {
  25. const RadToDeg = 180 / Math.PI;
  26. const eulerEquals = function ( a, b, tolerance ) {
  27. tolerance = tolerance || 0.0001;
  28. if ( a.order != b.order ) {
  29. return false;
  30. }
  31. return (
  32. Math.abs( a.x - b.x ) <= tolerance &&
  33. Math.abs( a.y - b.y ) <= tolerance &&
  34. Math.abs( a.z - b.z ) <= tolerance
  35. );
  36. };
  37. // INHERITANCE
  38. QUnit.test( 'Extending', ( assert ) => {
  39. const object = new Object3D();
  40. assert.strictEqual(
  41. object instanceof EventDispatcher, true,
  42. 'Object3D extends from EventDispatcher'
  43. );
  44. } );
  45. // INSTANCING
  46. QUnit.test( 'Instancing', ( assert ) => {
  47. const object = new Object3D();
  48. assert.ok( object, 'Can instantiate an Object3D.' );
  49. } );
  50. // PROPERTIES
  51. QUnit.todo( 'id', ( assert ) => {
  52. assert.ok( false, 'everything\'s gonna be alright' );
  53. } );
  54. QUnit.todo( 'uuid', ( assert ) => {
  55. assert.ok( false, 'everything\'s gonna be alright' );
  56. } );
  57. QUnit.todo( 'name', ( assert ) => {
  58. assert.ok( false, 'everything\'s gonna be alright' );
  59. } );
  60. QUnit.test( 'type', ( assert ) => {
  61. const object = new Object3D();
  62. assert.ok(
  63. object.type === 'Object3D',
  64. 'Object3D.type should be Object3D'
  65. );
  66. } );
  67. QUnit.todo( 'parent', ( assert ) => {
  68. assert.ok( false, 'everything\'s gonna be alright' );
  69. } );
  70. QUnit.todo( 'children', ( assert ) => {
  71. assert.ok( false, 'everything\'s gonna be alright' );
  72. } );
  73. QUnit.todo( 'up', ( assert ) => {
  74. assert.ok( false, 'everything\'s gonna be alright' );
  75. } );
  76. QUnit.todo( 'position', ( assert ) => {
  77. assert.ok( false, 'everything\'s gonna be alright' );
  78. } );
  79. QUnit.todo( 'rotation', ( assert ) => {
  80. assert.ok( false, 'everything\'s gonna be alright' );
  81. } );
  82. QUnit.todo( 'quaternion', ( assert ) => {
  83. assert.ok( false, 'everything\'s gonna be alright' );
  84. } );
  85. QUnit.todo( 'scale', ( assert ) => {
  86. assert.ok( false, 'everything\'s gonna be alright' );
  87. } );
  88. QUnit.todo( 'modelViewMatrix', ( assert ) => {
  89. assert.ok( false, 'everything\'s gonna be alright' );
  90. } );
  91. QUnit.todo( 'normalMatrix', ( assert ) => {
  92. assert.ok( false, 'everything\'s gonna be alright' );
  93. } );
  94. QUnit.todo( 'matrix', ( assert ) => {
  95. assert.ok( false, 'everything\'s gonna be alright' );
  96. } );
  97. QUnit.todo( 'matrixWorld', ( assert ) => {
  98. assert.ok( false, 'everything\'s gonna be alright' );
  99. } );
  100. QUnit.todo( 'matrixAutoUpdate', ( assert ) => {
  101. assert.ok( false, 'everything\'s gonna be alright' );
  102. } );
  103. QUnit.todo( 'matrixWorldNeedsUpdate', ( assert ) => {
  104. assert.ok( false, 'everything\'s gonna be alright' );
  105. } );
  106. QUnit.todo( 'matrixWorldAutoUpdate', ( assert ) => {
  107. assert.ok( false, 'everything\'s gonna be alright' );
  108. } );
  109. QUnit.todo( 'layers', ( assert ) => {
  110. assert.ok( false, 'everything\'s gonna be alright' );
  111. } );
  112. QUnit.todo( 'visible', ( assert ) => {
  113. assert.ok( false, 'everything\'s gonna be alright' );
  114. } );
  115. QUnit.todo( 'castShadow', ( assert ) => {
  116. assert.ok( false, 'everything\'s gonna be alright' );
  117. } );
  118. QUnit.todo( 'receiveShadow', ( assert ) => {
  119. assert.ok( false, 'everything\'s gonna be alright' );
  120. } );
  121. QUnit.todo( 'frustumCulled', ( assert ) => {
  122. assert.ok( false, 'everything\'s gonna be alright' );
  123. } );
  124. QUnit.todo( 'renderOrder', ( assert ) => {
  125. assert.ok( false, 'everything\'s gonna be alright' );
  126. } );
  127. QUnit.todo( 'animations', ( assert ) => {
  128. assert.ok( false, 'everything\'s gonna be alright' );
  129. } );
  130. QUnit.todo( 'userData', ( assert ) => {
  131. assert.ok( false, 'everything\'s gonna be alright' );
  132. } );
  133. // STATIC
  134. QUnit.test( 'DEFAULT_UP', ( assert ) => {
  135. const currentDefaultUp = new Vector3().copy( Object3D.DEFAULT_UP );
  136. const v = new Vector3();
  137. try {
  138. assert.deepEqual( Object3D.DEFAULT_UP, v.set( 0, 1, 0 ), 'default DEFAULT_UP is Y-up' );
  139. const object = new Object3D();
  140. assert.deepEqual( object.up, v.set( 0, 1, 0 ), '.up of a new object inherits Object3D.DEFAULT_UP = Y-up' );
  141. Object3D.DEFAULT_UP.set( 0, 0, 1 );
  142. const object2 = new Object3D();
  143. assert.deepEqual( object2.up, v.set( 0, 0, 1 ), '.up of a new object inherits Object3D.DEFAULT_UP = Z-up' );
  144. } finally {
  145. Object3D.DEFAULT_UP.copy( currentDefaultUp );
  146. }
  147. } );
  148. QUnit.test( 'DEFAULT_MATRIX_AUTO_UPDATE', ( assert ) => {
  149. const currentDefaultMatrixAutoUpdate = Object3D.DEFAULT_MATRIX_AUTO_UPDATE;
  150. try {
  151. assert.equal( currentDefaultMatrixAutoUpdate, true, 'default DEFAULT_MATRIX_AUTO_UPDATE is true' );
  152. const object = new Object3D();
  153. assert.equal(
  154. object.matrixAutoUpdate, true,
  155. '.matrixAutoUpdate of a new object inherits Object3D.DEFAULT_MATRIX_AUTO_UPDATE = true'
  156. );
  157. Object3D.DEFAULT_MATRIX_AUTO_UPDATE = false;
  158. const object2 = new Object3D();
  159. assert.equal(
  160. object2.matrixAutoUpdate, false,
  161. '.matrixAutoUpdate of a new object inherits Object3D.DEFAULT_MATRIX_AUTO_UPDATE = false'
  162. );
  163. } finally {
  164. Object3D.DEFAULT_MATRIX_AUTO_UPDATE = currentDefaultMatrixAutoUpdate;
  165. }
  166. } );
  167. // PUBLIC
  168. QUnit.test( 'isObject3D', ( assert ) => {
  169. const object = new Object3D();
  170. assert.ok(
  171. object.isObject3D,
  172. 'Object3D.isObject3D should be true'
  173. );
  174. const object2 = {};
  175. assert.ok(
  176. object2.isObject3D === undefined,
  177. 'other object isObject3D should be undefined'
  178. );
  179. } );
  180. QUnit.todo( 'onBeforeRender', ( assert ) => {
  181. assert.ok( false, 'everything\'s gonna be alright' );
  182. } );
  183. QUnit.todo( 'onAfterRender', ( assert ) => {
  184. assert.ok( false, 'everything\'s gonna be alright' );
  185. } );
  186. QUnit.test( 'applyMatrix4', ( assert ) => {
  187. const a = new Object3D();
  188. const m = new Matrix4();
  189. const expectedPos = new Vector3( x, y, z );
  190. const expectedQuat = new Quaternion( 0.5 * Math.sqrt( 2 ), 0, 0, 0.5 * Math.sqrt( 2 ) );
  191. m.makeRotationX( Math.PI / 2 );
  192. m.setPosition( new Vector3( x, y, z ) );
  193. a.applyMatrix4( m );
  194. assert.deepEqual( a.position, expectedPos, 'Position has the expected values' );
  195. assert.ok(
  196. Math.abs( a.quaternion.x - expectedQuat.x ) <= eps &&
  197. Math.abs( a.quaternion.y - expectedQuat.y ) <= eps &&
  198. Math.abs( a.quaternion.z - expectedQuat.z ) <= eps,
  199. 'Quaternion has the expected values'
  200. );
  201. } );
  202. QUnit.test( 'applyQuaternion', ( assert ) => {
  203. const a = new Object3D();
  204. const sqrt = 0.5 * Math.sqrt( 2 );
  205. const quat = new Quaternion( 0, sqrt, 0, sqrt );
  206. const expected = new Quaternion( sqrt / 2, sqrt / 2, 0, 0 );
  207. a.quaternion.set( 0.25, 0.25, 0.25, 0.25 );
  208. a.applyQuaternion( quat );
  209. assert.ok(
  210. Math.abs( a.quaternion.x - expected.x ) <= eps &&
  211. Math.abs( a.quaternion.y - expected.y ) <= eps &&
  212. Math.abs( a.quaternion.z - expected.z ) <= eps,
  213. 'Quaternion has the expected values'
  214. );
  215. } );
  216. QUnit.test( 'setRotationFromAxisAngle', ( assert ) => {
  217. const a = new Object3D();
  218. const axis = new Vector3( 0, 1, 0 );
  219. let angle = Math.PI;
  220. const expected = new Euler( - Math.PI, 0, - Math.PI );
  221. const euler = new Euler();
  222. a.setRotationFromAxisAngle( axis, angle );
  223. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  224. assert.ok( eulerEquals( euler, expected ), 'Correct values after rotation' );
  225. axis.set( 1, 0, 0 );
  226. angle = 0;
  227. expected.set( 0, 0, 0 );
  228. a.setRotationFromAxisAngle( axis, angle );
  229. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  230. assert.ok( eulerEquals( euler, expected ), 'Correct values after zeroing' );
  231. } );
  232. QUnit.test( 'setRotationFromEuler', ( assert ) => {
  233. const a = new Object3D();
  234. const rotation = new Euler( ( 45 / RadToDeg ), 0, Math.PI );
  235. const expected = rotation.clone(); // bit obvious
  236. const euler = new Euler();
  237. a.setRotationFromEuler( rotation );
  238. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  239. assert.ok( eulerEquals( euler, expected ), 'Correct values after rotation' );
  240. } );
  241. QUnit.test( 'setRotationFromMatrix', ( assert ) => {
  242. const a = new Object3D();
  243. const m = new Matrix4();
  244. const eye = new Vector3( 0, 0, 0 );
  245. const target = new Vector3( 0, 1, - 1 );
  246. const up = new Vector3( 0, 1, 0 );
  247. const euler = new Euler();
  248. m.lookAt( eye, target, up );
  249. a.setRotationFromMatrix( m );
  250. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  251. assert.numEqual( euler.x * RadToDeg, 45, 'Correct rotation angle' );
  252. } );
  253. QUnit.test( 'setRotationFromQuaternion', ( assert ) => {
  254. const a = new Object3D();
  255. const rotation = new Quaternion().setFromEuler( new Euler( Math.PI, 0, - Math.PI ) );
  256. const euler = new Euler();
  257. a.setRotationFromQuaternion( rotation );
  258. euler.setFromQuaternion( a.getWorldQuaternion( new Quaternion() ) );
  259. assert.ok( eulerEquals( euler, new Euler( Math.PI, 0, - Math.PI ) ), 'Correct values after rotation' );
  260. } );
  261. QUnit.todo( 'rotateOnAxis', ( assert ) => {
  262. assert.ok( false, 'everything\'s gonna be alright' );
  263. } );
  264. QUnit.todo( 'rotateOnWorldAxis', ( assert ) => {
  265. assert.ok( false, 'everything\'s gonna be alright' );
  266. } );
  267. QUnit.test( 'rotateX', ( assert ) => {
  268. const obj = new Object3D();
  269. const angleInRad = 1.562;
  270. obj.rotateX( angleInRad );
  271. assert.numEqual( obj.rotation.x, angleInRad, 'x is equal' );
  272. } );
  273. QUnit.test( 'rotateY', ( assert ) => {
  274. const obj = new Object3D();
  275. const angleInRad = - 0.346;
  276. obj.rotateY( angleInRad );
  277. assert.numEqual( obj.rotation.y, angleInRad, 'y is equal' );
  278. } );
  279. QUnit.test( 'rotateZ', ( assert ) => {
  280. const obj = new Object3D();
  281. const angleInRad = 1;
  282. obj.rotateZ( angleInRad );
  283. assert.numEqual( obj.rotation.z, angleInRad, 'z is equal' );
  284. } );
  285. QUnit.test( 'translateOnAxis', ( assert ) => {
  286. const obj = new Object3D();
  287. obj.translateOnAxis( new Vector3( 1, 0, 0 ), 1 );
  288. obj.translateOnAxis( new Vector3( 0, 1, 0 ), 1.23 );
  289. obj.translateOnAxis( new Vector3( 0, 0, 1 ), - 4.56 );
  290. assert.propEqual( obj.position, {
  291. x: 1,
  292. y: 1.23,
  293. z: - 4.56,
  294. } );
  295. } );
  296. QUnit.test( 'translateX', ( assert ) => {
  297. const obj = new Object3D();
  298. obj.translateX( 1.234 );
  299. assert.numEqual( obj.position.x, 1.234, 'x is equal' );
  300. } );
  301. QUnit.test( 'translateY', ( assert ) => {
  302. const obj = new Object3D();
  303. obj.translateY( 1.234 );
  304. assert.numEqual( obj.position.y, 1.234, 'y is equal' );
  305. } );
  306. QUnit.test( 'translateZ', ( assert ) => {
  307. const obj = new Object3D();
  308. obj.translateZ( 1.234 );
  309. assert.numEqual( obj.position.z, 1.234, 'z is equal' );
  310. } );
  311. QUnit.test( 'localToWorld', ( assert ) => {
  312. const v = new Vector3();
  313. const expectedPosition = new Vector3( 5, - 1, - 4 );
  314. const parent = new Object3D();
  315. const child = new Object3D();
  316. parent.position.set( 1, 0, 0 );
  317. parent.rotation.set( 0, Math.PI / 2, 0 );
  318. parent.scale.set( 2, 1, 1 );
  319. child.position.set( 0, 1, 0 );
  320. child.rotation.set( Math.PI / 2, 0, 0 );
  321. child.scale.set( 1, 2, 1 );
  322. parent.add( child );
  323. parent.updateMatrixWorld();
  324. child.localToWorld( v.set( 2, 2, 2 ) );
  325. assert.ok(
  326. Math.abs( v.x - expectedPosition.x ) <= eps &&
  327. Math.abs( v.y - expectedPosition.y ) <= eps &&
  328. Math.abs( v.z - expectedPosition.z ) <= eps,
  329. 'local vector is converted to world'
  330. );
  331. } );
  332. QUnit.test( 'worldToLocal', ( assert ) => {
  333. const v = new Vector3();
  334. const expectedPosition = new Vector3( - 1, 0.5, - 1 );
  335. const parent = new Object3D();
  336. const child = new Object3D();
  337. parent.position.set( 1, 0, 0 );
  338. parent.rotation.set( 0, Math.PI / 2, 0 );
  339. parent.scale.set( 2, 1, 1 );
  340. child.position.set( 0, 1, 0 );
  341. child.rotation.set( Math.PI / 2, 0, 0 );
  342. child.scale.set( 1, 2, 1 );
  343. parent.add( child );
  344. parent.updateMatrixWorld();
  345. child.worldToLocal( v.set( 2, 2, 2 ) );
  346. assert.ok(
  347. Math.abs( v.x - expectedPosition.x ) <= eps &&
  348. Math.abs( v.y - expectedPosition.y ) <= eps &&
  349. Math.abs( v.z - expectedPosition.z ) <= eps,
  350. 'world vector is converted to local'
  351. );
  352. } );
  353. QUnit.test( 'lookAt', ( assert ) => {
  354. const obj = new Object3D();
  355. obj.lookAt( new Vector3( 0, - 1, 1 ) );
  356. assert.numEqual( obj.rotation.x * RadToDeg, 45, 'x is equal' );
  357. } );
  358. QUnit.test( 'add/remove/removeFromParent/clear', ( assert ) => {
  359. const a = new Object3D();
  360. const child1 = new Object3D();
  361. const child2 = new Object3D();
  362. assert.strictEqual( a.children.length, 0, 'Starts with no children' );
  363. a.add( child1 );
  364. assert.strictEqual( a.children.length, 1, 'The first child was added' );
  365. assert.strictEqual( a.children[ 0 ], child1, 'It\'s the right one' );
  366. a.add( child2 );
  367. assert.strictEqual( a.children.length, 2, 'The second child was added' );
  368. assert.strictEqual( a.children[ 1 ], child2, 'It\'s the right one' );
  369. assert.strictEqual( a.children[ 0 ], child1, 'The first one is still there' );
  370. a.remove( child1 );
  371. assert.strictEqual( a.children.length, 1, 'The first child was removed' );
  372. assert.strictEqual( a.children[ 0 ], child2, 'The second one is still there' );
  373. a.add( child1 );
  374. a.remove( child1, child2 );
  375. assert.strictEqual( a.children.length, 0, 'Both children were removed at once' );
  376. child1.add( child2 );
  377. assert.strictEqual( child1.children.length, 1, 'The second child was added to the first one' );
  378. a.add( child2 );
  379. assert.strictEqual( a.children.length, 1, 'The second one was added to the parent (no remove)' );
  380. assert.strictEqual( a.children[ 0 ], child2, 'The second one is now the parent\'s child again' );
  381. assert.strictEqual( child1.children.length, 0, 'The first one no longer has any children' );
  382. a.add( child1 );
  383. assert.strictEqual( a.children.length, 2, 'The first child was added to the parent' );
  384. a.clear();
  385. assert.strictEqual( a.children.length, 0, 'All childrens were removed' );
  386. assert.strictEqual( child1.parent, null, 'First child has no parent' );
  387. assert.strictEqual( child2.parent, null, 'Second child has no parent' );
  388. a.add( child1 );
  389. assert.strictEqual( a.children.length, 1, 'The child was added to the parent' );
  390. child1.removeFromParent();
  391. assert.strictEqual( a.children.length, 0, 'The child was removed' );
  392. assert.strictEqual( child1.parent, null, 'Child has no parent' );
  393. } );
  394. QUnit.test( 'attach', ( assert ) => {
  395. const object = new Object3D();
  396. const oldParent = new Object3D();
  397. const newParent = new Object3D();
  398. const expectedMatrixWorld = new Matrix4();
  399. // Attach to a parent
  400. object.position.set( 1, 2, 3 );
  401. object.rotation.set( Math.PI / 2, Math.PI / 3, Math.PI / 4 );
  402. object.scale.set( 2, 3, 4 );
  403. newParent.position.set( 4, 5, 6 );
  404. newParent.rotation.set( Math.PI / 5, Math.PI / 6, Math.PI / 7 );
  405. newParent.scale.set( 5, 5, 5 );
  406. object.updateMatrixWorld();
  407. newParent.updateMatrixWorld();
  408. expectedMatrixWorld.copy( object.matrixWorld );
  409. newParent.attach( object );
  410. assert.ok( object.parent && object.parent == newParent &&
  411. oldParent.children.indexOf( object ) === - 1,
  412. 'object is a child of a new parent' );
  413. assert.ok( matrixEquals4( expectedMatrixWorld, object.matrixWorld ), 'object\'s world matrix is maintained' );
  414. // Attach to a new parent from an old parent
  415. object.position.set( 1, 2, 3 );
  416. object.rotation.set( Math.PI / 2, Math.PI / 3, Math.PI / 4 );
  417. object.scale.set( 2, 3, 4 );
  418. oldParent.position.set( 4, 5, 6 );
  419. oldParent.rotation.set( Math.PI / 5, Math.PI / 6, Math.PI / 7 );
  420. oldParent.scale.set( 5, 5, 5 );
  421. newParent.position.set( 7, 8, 9 );
  422. newParent.rotation.set( Math.PI / 8, Math.PI / 9, Math.PI / 10 );
  423. newParent.scale.set( 6, 6, 6 );
  424. oldParent.add( object );
  425. oldParent.updateMatrixWorld();
  426. newParent.updateMatrixWorld();
  427. expectedMatrixWorld.copy( object.matrixWorld );
  428. newParent.attach( object );
  429. assert.ok( object.parent && object.parent == newParent &&
  430. newParent.children.indexOf( object ) !== - 1 &&
  431. oldParent.children.indexOf( object ) === - 1,
  432. 'object is no longer a child of an old parent and is a child of a new parent now' );
  433. assert.ok( matrixEquals4( expectedMatrixWorld, object.matrixWorld ),
  434. 'object\'s world matrix is maintained even it had a parent' );
  435. } );
  436. QUnit.test( 'getObjectById/getObjectByName/getObjectByProperty', ( assert ) => {
  437. const parent = new Object3D();
  438. const childName = new Object3D();
  439. const childId = new Object3D(); // id = parent.id + 2
  440. const childNothing = new Object3D();
  441. parent.prop = true;
  442. childName.name = 'foo';
  443. parent.add( childName, childId, childNothing );
  444. assert.strictEqual( parent.getObjectByProperty( 'prop', true ), parent, 'Get parent by its own property' );
  445. assert.strictEqual( parent.getObjectByName( 'foo' ), childName, 'Get child by name' );
  446. assert.strictEqual( parent.getObjectById( parent.id + 2 ), childId, 'Get child by Id' );
  447. assert.strictEqual(
  448. parent.getObjectByProperty( 'no-property', 'no-value' ), undefined,
  449. 'Unknown property results in undefined'
  450. );
  451. } );
  452. QUnit.test( 'getObjectsByProperty', ( assert ) => {
  453. const parent = new Object3D();
  454. const childName = new Object3D();
  455. const childNothing = new Object3D();
  456. const childName2 = new Object3D();
  457. const childName3 = new Object3D();
  458. parent.prop = true;
  459. childName.name = 'foo';
  460. childName2.name = 'foo';
  461. childName3.name = 'foo';
  462. childName2.add( childName3 );
  463. childName.add( childName2 );
  464. parent.add( childName, childNothing );
  465. assert.strictEqual( parent.getObjectsByProperty( 'name', 'foo' ).length, 3, 'Get amount of all childs by name "foo"' );
  466. assert.strictEqual( parent.getObjectsByProperty( 'name', 'foo' ).some( obj => obj.name !== 'foo' ), false, 'Get all childs by name "foo"' );
  467. } );
  468. QUnit.test( 'getWorldPosition', ( assert ) => {
  469. const a = new Object3D();
  470. const b = new Object3D();
  471. const expectedSingle = new Vector3( x, y, z );
  472. const expectedParent = new Vector3( x, y, 0 );
  473. const expectedChild = new Vector3( x, y, 7 );
  474. const position = new Vector3();
  475. a.translateX( x );
  476. a.translateY( y );
  477. a.translateZ( z );
  478. assert.deepEqual( a.getWorldPosition( position ), expectedSingle, 'WorldPosition as expected for single object' );
  479. // translate child and then parent
  480. b.translateZ( 7 );
  481. a.add( b );
  482. a.translateZ( - z );
  483. assert.deepEqual( a.getWorldPosition( position ), expectedParent, 'WorldPosition as expected for parent' );
  484. assert.deepEqual( b.getWorldPosition( position ), expectedChild, 'WorldPosition as expected for child' );
  485. } );
  486. QUnit.todo( 'getWorldQuaternion', ( assert ) => {
  487. assert.ok( false, 'everything\'s gonna be alright' );
  488. } );
  489. QUnit.test( 'getWorldScale', ( assert ) => {
  490. const a = new Object3D();
  491. const m = new Matrix4().makeScale( x, y, z );
  492. const expected = new Vector3( x, y, z );
  493. a.applyMatrix4( m );
  494. assert.deepEqual( a.getWorldScale( new Vector3() ), expected, 'WorldScale as expected' );
  495. } );
  496. QUnit.test( 'getWorldDirection', ( assert ) => {
  497. const a = new Object3D();
  498. const expected = new Vector3( 0, - 0.5 * Math.sqrt( 2 ), 0.5 * Math.sqrt( 2 ) );
  499. const direction = new Vector3();
  500. a.lookAt( new Vector3( 0, - 1, 1 ) );
  501. a.getWorldDirection( direction );
  502. assert.ok(
  503. Math.abs( direction.x - expected.x ) <= eps &&
  504. Math.abs( direction.y - expected.y ) <= eps &&
  505. Math.abs( direction.z - expected.z ) <= eps,
  506. 'Direction has the expected values'
  507. );
  508. } );
  509. QUnit.test( 'localTransformVariableInstantiation', ( assert ) => {
  510. const a = new Object3D();
  511. const b = new Object3D();
  512. const c = new Object3D();
  513. const d = new Object3D();
  514. a.getWorldDirection( new Vector3() );
  515. a.lookAt( new Vector3( 0, - 1, 1 ) );
  516. assert.ok( true, 'Calling lookAt after getWorldDirection does not create errors' );
  517. b.getWorldPosition( new Vector3() );
  518. b.lookAt( new Vector3( 0, - 1, 1 ) );
  519. assert.ok( true, 'Calling lookAt after getWorldPosition does not create errors' );
  520. c.getWorldQuaternion( new Quaternion() );
  521. c.lookAt( new Vector3( 0, - 1, 1 ) );
  522. assert.ok( true, 'Calling lookAt after getWorldQuaternion does not create errors' );
  523. d.getWorldScale( new Vector3() );
  524. d.lookAt( new Vector3( 0, - 1, 1 ) );
  525. assert.ok( true, 'Calling lookAt after getWorldScale does not create errors' );
  526. } );
  527. QUnit.todo( 'raycast', ( assert ) => {
  528. assert.ok( false, 'everything\'s gonna be alright' );
  529. } );
  530. QUnit.test( 'traverse/traverseVisible/traverseAncestors', ( assert ) => {
  531. const a = new Object3D();
  532. const b = new Object3D();
  533. const c = new Object3D();
  534. const d = new Object3D();
  535. let names = [];
  536. const expectedNormal = [ 'parent', 'child', 'childchild 1', 'childchild 2' ];
  537. const expectedVisible = [ 'parent', 'child', 'childchild 2' ];
  538. const expectedAncestors = [ 'child', 'parent' ];
  539. a.name = 'parent';
  540. b.name = 'child';
  541. c.name = 'childchild 1';
  542. c.visible = false;
  543. d.name = 'childchild 2';
  544. b.add( c );
  545. b.add( d );
  546. a.add( b );
  547. a.traverse( function ( obj ) {
  548. names.push( obj.name );
  549. } );
  550. assert.deepEqual( names, expectedNormal, 'Traversed objects in expected order' );
  551. names = [];
  552. a.traverseVisible( function ( obj ) {
  553. names.push( obj.name );
  554. } );
  555. assert.deepEqual( names, expectedVisible, 'Traversed visible objects in expected order' );
  556. names = [];
  557. c.traverseAncestors( function ( obj ) {
  558. names.push( obj.name );
  559. } );
  560. assert.deepEqual( names, expectedAncestors, 'Traversed ancestors in expected order' );
  561. } );
  562. QUnit.test( 'updateMatrix', ( assert ) => {
  563. const a = new Object3D();
  564. a.position.set( 2, 3, 4 );
  565. a.quaternion.set( 5, 6, 7, 8 );
  566. a.scale.set( 9, 10, 11 );
  567. assert.deepEqual( a.matrix.elements, [
  568. 1, 0, 0, 0,
  569. 0, 1, 0, 0,
  570. 0, 0, 1, 0,
  571. 0, 0, 0, 1
  572. ], 'Updating position, quaternion, or scale has no effect to matrix until calling updateMatrix()' );
  573. a.updateMatrix();
  574. assert.deepEqual( a.matrix.elements, [
  575. - 1521, 1548, - 234, 0,
  576. - 520, - 1470, 1640, 0,
  577. 1826, 44, - 1331, 0,
  578. 2, 3, 4, 1
  579. ], 'matrix is calculated from position, quaternion, and scale' );
  580. assert.equal( a.matrixWorldNeedsUpdate, true, 'The flag indicating world matrix needs to be updated should be true' );
  581. } );
  582. QUnit.test( 'updateMatrixWorld', ( assert ) => {
  583. const parent = new Object3D();
  584. const child = new Object3D();
  585. // -- Standard usage test
  586. parent.position.set( 1, 2, 3 );
  587. child.position.set( 4, 5, 6 );
  588. parent.add( child );
  589. parent.updateMatrixWorld();
  590. assert.deepEqual( parent.matrix.elements, [
  591. 1, 0, 0, 0,
  592. 0, 1, 0, 0,
  593. 0, 0, 1, 0,
  594. 1, 2, 3, 1
  595. ], 'updateMatrixWorld() updates local matrix' );
  596. assert.deepEqual( parent.matrixWorld.elements, [
  597. 1, 0, 0, 0,
  598. 0, 1, 0, 0,
  599. 0, 0, 1, 0,
  600. 1, 2, 3, 1
  601. ], 'updateMatrixWorld() updates world matrix' );
  602. assert.deepEqual( child.matrix.elements, [
  603. 1, 0, 0, 0,
  604. 0, 1, 0, 0,
  605. 0, 0, 1, 0,
  606. 4, 5, 6, 1
  607. ], 'updateMatrixWorld() updates children\'s local matrix' );
  608. assert.deepEqual( child.matrixWorld.elements, [
  609. 1, 0, 0, 0,
  610. 0, 1, 0, 0,
  611. 0, 0, 1, 0,
  612. 5, 7, 9, 1
  613. ], 'updateMatrixWorld() updates children\'s world matrices from their parent world matrix and their local matrices' );
  614. assert.equal( parent.matrixWorldNeedsUpdate || child.matrixWorldNeedsUpdate, false, 'The flag indicating world matrix needs to be updated should be false after updating world matrix' );
  615. // -- No sync between local position/quaternion/scale/matrix and world matrix test
  616. parent.position.set( 0, 0, 0 );
  617. parent.updateMatrix();
  618. assert.deepEqual( parent.matrixWorld.elements, [
  619. 1, 0, 0, 0,
  620. 0, 1, 0, 0,
  621. 0, 0, 1, 0,
  622. 1, 2, 3, 1
  623. ], 'Updating position, quaternion, scale, or local matrix has no effect to world matrix until calling updateWorldMatrix()' );
  624. // -- matrixAutoUpdate = false test
  625. // Resetting local and world matrices to the origin
  626. child.position.set( 0, 0, 0 );
  627. parent.updateMatrixWorld();
  628. parent.position.set( 1, 2, 3 );
  629. parent.matrixAutoUpdate = false;
  630. child.matrixAutoUpdate = false;
  631. parent.updateMatrixWorld();
  632. assert.deepEqual( parent.matrix.elements, [
  633. 1, 0, 0, 0,
  634. 0, 1, 0, 0,
  635. 0, 0, 1, 0,
  636. 0, 0, 0, 1
  637. ], 'updateMatrixWorld() doesn\'t update local matrix if matrixAutoUpdate is false' );
  638. assert.deepEqual( parent.matrixWorld.elements, [
  639. 1, 0, 0, 0,
  640. 0, 1, 0, 0,
  641. 0, 0, 1, 0,
  642. 0, 0, 0, 1
  643. ], 'World matrix isn\'t updated because local matrix isn\'t updated and the flag indicating world matrix needs to be updated didn\'t rise' );
  644. assert.deepEqual( child.matrixWorld.elements, [
  645. 1, 0, 0, 0,
  646. 0, 1, 0, 0,
  647. 0, 0, 1, 0,
  648. 0, 0, 0, 1
  649. ], 'No effect to child world matrix if parent local and world matrices and child local matrix are not updated' );
  650. // -- matrixWorldAutoUpdate = false test
  651. parent.position.set( 3, 2, 1 );
  652. parent.updateMatrix();
  653. parent.matrixAutoUpdate = true;
  654. child.matrixAutoUpdate = true;
  655. parent.matrixWorldNeedsUpdate = true;
  656. child.matrixWorldAutoUpdate = false;
  657. parent.updateMatrixWorld();
  658. assert.deepEqual( child.matrixWorld.elements, [
  659. 1, 0, 0, 0,
  660. 0, 1, 0, 0,
  661. 0, 0, 1, 0,
  662. 0, 0, 0, 1
  663. ], 'No effect to child world matrix when matrixWorldAutoUpdate is set to false' );
  664. // -- Propagation to children world matrices test
  665. child.position.set( 0, 0, 0 );
  666. parent.position.set( 1, 2, 3 );
  667. child.matrixWorldAutoUpdate = true;
  668. parent.updateMatrixWorld();
  669. assert.deepEqual( child.matrixWorld.elements, [
  670. 1, 0, 0, 0,
  671. 0, 1, 0, 0,
  672. 0, 0, 1, 0,
  673. 1, 2, 3, 1
  674. ], 'Updating parent world matrix has effect to children world matrices even if children local matrices aren\'t changed' );
  675. // -- force argument test
  676. // Resetting the local and world matrices to the origin
  677. child.position.set( 0, 0, 0 );
  678. child.matrixAutoUpdate = true;
  679. parent.updateMatrixWorld();
  680. parent.position.set( 1, 2, 3 );
  681. parent.updateMatrix();
  682. parent.matrixAutoUpdate = false;
  683. parent.matrixWorldNeedsUpdate = false;
  684. parent.updateMatrixWorld( true );
  685. assert.deepEqual( parent.matrixWorld.elements, [
  686. 1, 0, 0, 0,
  687. 0, 1, 0, 0,
  688. 0, 0, 1, 0,
  689. 1, 2, 3, 1
  690. ], 'force = true forces to update world matrix even if local matrix is not changed' );
  691. // -- Restriction test: No effect to parent matrices
  692. // Resetting the local and world matrices to the origin
  693. parent.position.set( 0, 0, 0 );
  694. child.position.set( 0, 0, 0 );
  695. parent.matrixAutoUpdate = true;
  696. child.matrixAutoUpdate = true;
  697. parent.updateMatrixWorld();
  698. parent.position.set( 1, 2, 3 );
  699. child.position.set( 4, 5, 6 );
  700. child.updateMatrixWorld();
  701. assert.deepEqual( parent.matrix.elements, [
  702. 1, 0, 0, 0,
  703. 0, 1, 0, 0,
  704. 0, 0, 1, 0,
  705. 0, 0, 0, 1
  706. ], 'updateMatrixWorld() doesn\'t update parent local matrix' );
  707. assert.deepEqual( parent.matrixWorld.elements, [
  708. 1, 0, 0, 0,
  709. 0, 1, 0, 0,
  710. 0, 0, 1, 0,
  711. 0, 0, 0, 1
  712. ], 'updateMatrixWorld() doesn\'t update parent world matrix' );
  713. assert.deepEqual( child.matrixWorld.elements, [
  714. 1, 0, 0, 0,
  715. 0, 1, 0, 0,
  716. 0, 0, 1, 0,
  717. 4, 5, 6, 1
  718. ], 'updateMatrixWorld() calculates world matrix from the current parent world matrix' );
  719. } );
  720. QUnit.test( 'updateWorldMatrix', ( assert ) => {
  721. const object = new Object3D();
  722. const parent = new Object3D();
  723. const child = new Object3D();
  724. const m = new Matrix4();
  725. const v = new Vector3();
  726. parent.add( object );
  727. object.add( child );
  728. parent.position.set( 1, 2, 3 );
  729. object.position.set( 4, 5, 6 );
  730. child.position.set( 7, 8, 9 );
  731. // Update the world matrix of an object
  732. object.updateWorldMatrix();
  733. assert.deepEqual( parent.matrix.elements,
  734. m.elements,
  735. 'No effect to parents\' local matrices' );
  736. assert.deepEqual( parent.matrixWorld.elements,
  737. m.elements,
  738. 'No effect to parents\' world matrices' );
  739. assert.deepEqual( object.matrix.elements,
  740. m.setPosition( object.position ).elements,
  741. 'Object\'s local matrix is updated' );
  742. assert.deepEqual( object.matrixWorld.elements,
  743. m.setPosition( object.position ).elements,
  744. 'Object\'s world matrix is updated' );
  745. assert.deepEqual( child.matrix.elements,
  746. m.identity().elements,
  747. 'No effect to children\'s local matrices' );
  748. assert.deepEqual( child.matrixWorld.elements,
  749. m.elements,
  750. 'No effect to children\'s world matrices' );
  751. // Update the world matrices of an object and its parents
  752. object.matrix.identity();
  753. object.matrixWorld.identity();
  754. object.updateWorldMatrix( true, false );
  755. assert.deepEqual( parent.matrix.elements,
  756. m.setPosition( parent.position ).elements,
  757. 'Parents\' local matrices are updated' );
  758. assert.deepEqual( parent.matrixWorld.elements,
  759. m.setPosition( parent.position ).elements,
  760. 'Parents\' world matrices are updated' );
  761. assert.deepEqual( object.matrix.elements,
  762. m.setPosition( object.position ).elements,
  763. 'Object\'s local matrix is updated' );
  764. assert.deepEqual( object.matrixWorld.elements,
  765. m.setPosition( v.copy( parent.position ).add( object.position ) ).elements,
  766. 'Object\'s world matrix is updated' );
  767. assert.deepEqual( child.matrix.elements,
  768. m.identity().elements,
  769. 'No effect to children\'s local matrices' );
  770. assert.deepEqual( child.matrixWorld.elements,
  771. m.identity().elements,
  772. 'No effect to children\'s world matrices' );
  773. // Update the world matrices of an object and its children
  774. parent.matrix.identity();
  775. parent.matrixWorld.identity();
  776. object.matrix.identity();
  777. object.matrixWorld.identity();
  778. object.updateWorldMatrix( false, true );
  779. assert.deepEqual( parent.matrix.elements,
  780. m.elements,
  781. 'No effect to parents\' local matrices' );
  782. assert.deepEqual( parent.matrixWorld.elements,
  783. m.elements,
  784. 'No effect to parents\' world matrices' );
  785. assert.deepEqual( object.matrix.elements,
  786. m.setPosition( object.position ).elements,
  787. 'Object\'s local matrix is updated' );
  788. assert.deepEqual( object.matrixWorld.elements,
  789. m.setPosition( object.position ).elements,
  790. 'Object\'s world matrix is updated' );
  791. assert.deepEqual( child.matrix.elements,
  792. m.setPosition( child.position ).elements,
  793. 'Children\'s local matrices are updated' );
  794. assert.deepEqual( child.matrixWorld.elements,
  795. m.setPosition( v.copy( object.position ).add( child.position ) ).elements,
  796. 'Children\'s world matrices are updated' );
  797. // Update the world matrices of an object and its parents and children
  798. object.matrix.identity();
  799. object.matrixWorld.identity();
  800. child.matrix.identity();
  801. child.matrixWorld.identity();
  802. object.updateWorldMatrix( true, true );
  803. assert.deepEqual( parent.matrix.elements,
  804. m.setPosition( parent.position ).elements,
  805. 'Parents\' local matrices are updated' );
  806. assert.deepEqual( parent.matrixWorld.elements,
  807. m.setPosition( parent.position ).elements,
  808. 'Parents\' world matrices are updated' );
  809. assert.deepEqual( object.matrix.elements,
  810. m.setPosition( object.position ).elements,
  811. 'Object\'s local matrix is updated' );
  812. assert.deepEqual( object.matrixWorld.elements,
  813. m.setPosition( v.copy( parent.position ).add( object.position ) ).elements,
  814. 'Object\'s world matrix is updated' );
  815. assert.deepEqual( child.matrix.elements,
  816. m.setPosition( child.position ).elements,
  817. 'Children\'s local matrices are updated' );
  818. assert.deepEqual( child.matrixWorld.elements,
  819. m.setPosition( v.copy( parent.position ).add( object.position ).add( child.position ) ).elements,
  820. 'Children\'s world matrices are updated' );
  821. // object.matrixAutoUpdate = false test
  822. object.matrix.identity();
  823. object.matrixWorld.identity();
  824. object.matrixAutoUpdate = false;
  825. object.updateWorldMatrix( true, false );
  826. assert.deepEqual( object.matrix.elements,
  827. m.identity().elements,
  828. 'No effect to object\'s local matrix if matrixAutoUpdate is false' );
  829. assert.deepEqual( object.matrixWorld.elements,
  830. m.setPosition( parent.position ).elements,
  831. 'object\'s world matrix is updated even if matrixAutoUpdate is false' );
  832. // object.matrixWorldAutoUpdate = false test
  833. parent.matrixWorldAutoUpdate = false;
  834. child.matrixWorldAutoUpdate = false;
  835. child.matrixWorld.identity();
  836. parent.matrixWorld.identity();
  837. child.updateWorldMatrix( true, true );
  838. assert.deepEqual( child.matrixWorld.elements,
  839. m.identity().elements,
  840. 'No effect to child\'s world matrix if matrixWorldAutoUpdate is false' );
  841. assert.deepEqual( parent.matrixWorld.elements,
  842. m.identity().elements,
  843. 'No effect to parent\'s world matrix if matrixWorldAutoUpdate is false' );
  844. } );
  845. QUnit.test( 'toJSON', ( assert ) => {
  846. const a = new Object3D();
  847. const child = new Object3D();
  848. const childChild = new Object3D();
  849. a.name = 'a\'s name';
  850. a.matrix.set( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 );
  851. a.visible = false;
  852. a.castShadow = true;
  853. a.receiveShadow = true;
  854. a.userData[ 'foo' ] = 'bar';
  855. a.up.set( 1, 0, 0 );
  856. child.uuid = '5D4E9AE8-DA61-4912-A575-71A5BE3D72CD';
  857. childChild.uuid = 'B43854B3-E970-4E85-BD41-AAF8D7BFA189';
  858. child.add( childChild );
  859. a.add( child );
  860. const gold = {
  861. 'metadata': {
  862. 'version': 4.6,
  863. 'type': 'Object',
  864. 'generator': 'Object3D.toJSON'
  865. },
  866. 'object': {
  867. 'uuid': '0A1E4F43-CB5B-4097-8F82-DC2969C0B8C2',
  868. 'type': 'Object3D',
  869. 'name': 'a\'s name',
  870. 'castShadow': true,
  871. 'receiveShadow': true,
  872. 'visible': false,
  873. 'userData': { 'foo': 'bar' },
  874. 'layers': 1,
  875. 'matrix': [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
  876. 'children': [
  877. {
  878. 'uuid': '5D4E9AE8-DA61-4912-A575-71A5BE3D72CD',
  879. 'type': 'Object3D',
  880. 'layers': 1,
  881. 'matrix': [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  882. 'children': [
  883. {
  884. 'uuid': 'B43854B3-E970-4E85-BD41-AAF8D7BFA189',
  885. 'type': 'Object3D',
  886. 'layers': 1,
  887. 'matrix': [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  888. 'up': [ 0, 1, 0 ]
  889. }
  890. ],
  891. 'up': [ 0, 1, 0 ]
  892. }
  893. ],
  894. 'up': [ 1, 0, 0 ]
  895. }
  896. };
  897. // hacks
  898. const out = a.toJSON();
  899. out.object.uuid = '0A1E4F43-CB5B-4097-8F82-DC2969C0B8C2';
  900. assert.deepEqual( out, gold, 'JSON is as expected' );
  901. } );
  902. QUnit.test( 'clone', ( assert ) => {
  903. let a;
  904. const b = new Object3D();
  905. assert.strictEqual( a, undefined, 'Undefined pre-clone()' );
  906. a = b.clone();
  907. assert.notStrictEqual( a, b, 'Defined but seperate instances post-clone()' );
  908. a.uuid = b.uuid;
  909. assert.deepEqual( a, b, 'But identical properties' );
  910. } );
  911. QUnit.test( 'copy', ( assert ) => {
  912. const a = new Object3D();
  913. const b = new Object3D();
  914. const child = new Object3D();
  915. const childChild = new Object3D();
  916. a.name = 'original';
  917. b.name = 'to-be-copied';
  918. b.position.set( x, y, z );
  919. b.quaternion.set( x, y, z, w );
  920. b.scale.set( 2, 3, 4 );
  921. // bogus QUnit.test values
  922. b.matrix.set( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 );
  923. b.matrixWorld.set( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 );
  924. b.matrixAutoUpdate = false;
  925. b.matrixWorldNeedsUpdate = true;
  926. b.layers.mask = 2;
  927. b.visible = false;
  928. b.castShadow = true;
  929. b.receiveShadow = true;
  930. b.frustumCulled = false;
  931. b.renderOrder = 1;
  932. b.userData[ 'foo' ] = 'bar';
  933. child.add( childChild );
  934. b.add( child );
  935. assert.notDeepEqual( a, b, 'Objects are not equal pre-copy()' );
  936. a.copy( b, true );
  937. // check they're all unique instances
  938. assert.ok(
  939. a.uuid !== b.uuid &&
  940. a.children[ 0 ].uuid !== b.children[ 0 ].uuid &&
  941. a.children[ 0 ].children[ 0 ].uuid !== b.children[ 0 ].children[ 0 ].uuid,
  942. 'UUIDs are all different'
  943. );
  944. // and now fix that
  945. a.uuid = b.uuid;
  946. a.children[ 0 ].uuid = b.children[ 0 ].uuid;
  947. a.children[ 0 ].children[ 0 ].uuid = b.children[ 0 ].children[ 0 ].uuid;
  948. assert.deepEqual( a, b, 'Objects are equal post-copy()' );
  949. } );
  950. } );
  951. } );