1
0

objects.tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. export default QUnit.module( 'JSON4 Format', () => {
  3. QUnit.module( 'Objects', () => {
  4. QUnit.test( 'Mesh with geometry + material', ( assert ) => {
  5. const json = {
  6. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  7. geometries: [ {
  8. uuid: 'geom-1',
  9. type: 'BoxGeometry',
  10. width: 1,
  11. height: 1,
  12. depth: 1
  13. } ],
  14. materials: [ {
  15. uuid: 'mat-1',
  16. type: 'MeshBasicMaterial',
  17. color: 16711680
  18. } ],
  19. object: {
  20. uuid: 'mesh-1',
  21. type: 'Mesh',
  22. geometry: 'geom-1',
  23. material: 'mat-1',
  24. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  25. layers: 1
  26. }
  27. };
  28. const loader = new ObjectLoader();
  29. const mesh = loader.parse( json );
  30. assert.ok( mesh.isMesh, 'Is Mesh' );
  31. assert.ok( mesh.geometry.isBufferGeometry, 'Has geometry' );
  32. assert.ok( mesh.material.isMaterial, 'Has material' );
  33. } );
  34. QUnit.test( 'Mesh with material array (multi-material)', ( assert ) => {
  35. const json = {
  36. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  37. geometries: [ {
  38. uuid: 'geom-1',
  39. type: 'BufferGeometry',
  40. data: {
  41. attributes: {
  42. position: {
  43. itemSize: 3,
  44. type: 'Float32Array',
  45. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 ],
  46. normalized: false
  47. }
  48. },
  49. groups: [
  50. { start: 0, count: 3, materialIndex: 0 },
  51. { start: 3, count: 3, materialIndex: 1 }
  52. ]
  53. }
  54. } ],
  55. materials: [
  56. { uuid: 'mat-1', type: 'MeshBasicMaterial', color: 16711680 },
  57. { uuid: 'mat-2', type: 'MeshBasicMaterial', color: 65280 }
  58. ],
  59. object: {
  60. uuid: 'mesh-1',
  61. type: 'Mesh',
  62. geometry: 'geom-1',
  63. material: [ 'mat-1', 'mat-2' ],
  64. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  65. layers: 1
  66. }
  67. };
  68. const loader = new ObjectLoader();
  69. const mesh = loader.parse( json );
  70. assert.ok( Array.isArray( mesh.material ), 'Material is array' );
  71. assert.strictEqual( mesh.material.length, 2, 'Has 2 materials' );
  72. assert.strictEqual( mesh.material[ 0 ].color.getHex(), 0xff0000, 'First material is red' );
  73. assert.strictEqual( mesh.material[ 1 ].color.getHex(), 0x00ff00, 'Second material is green' );
  74. } );
  75. QUnit.test( 'Group container', ( assert ) => {
  76. const json = {
  77. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  78. geometries: [ {
  79. uuid: 'geom-1',
  80. type: 'BoxGeometry',
  81. width: 1,
  82. height: 1,
  83. depth: 1
  84. } ],
  85. materials: [ {
  86. uuid: 'mat-1',
  87. type: 'MeshBasicMaterial',
  88. color: 16711680
  89. } ],
  90. object: {
  91. uuid: 'group-1',
  92. type: 'Group',
  93. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  94. layers: 1,
  95. children: [
  96. {
  97. uuid: 'mesh-1',
  98. type: 'Mesh',
  99. geometry: 'geom-1',
  100. material: 'mat-1',
  101. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  102. layers: 1
  103. }
  104. ]
  105. }
  106. };
  107. const loader = new ObjectLoader();
  108. const group = loader.parse( json );
  109. assert.ok( group.isGroup, 'Is Group' );
  110. assert.strictEqual( group.children.length, 1, 'Has 1 child' );
  111. assert.ok( group.children[ 0 ].isMesh, 'Child is Mesh' );
  112. } );
  113. QUnit.test( 'Object3D transforms (position, rotation, scale via matrix)', ( assert ) => {
  114. const json = {
  115. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  116. geometries: [ {
  117. uuid: 'geom-1',
  118. type: 'BoxGeometry',
  119. width: 1,
  120. height: 1,
  121. depth: 1
  122. } ],
  123. materials: [ {
  124. uuid: 'mat-1',
  125. type: 'MeshBasicMaterial',
  126. color: 16711680
  127. } ],
  128. object: {
  129. uuid: 'mesh-1',
  130. type: 'Mesh',
  131. geometry: 'geom-1',
  132. material: 'mat-1',
  133. matrix: [ 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 5, 10, 15, 1 ],
  134. layers: 1
  135. }
  136. };
  137. const loader = new ObjectLoader();
  138. const mesh = loader.parse( json );
  139. assert.strictEqual( mesh.position.x, 5, 'Position.x is 5' );
  140. assert.strictEqual( mesh.position.y, 10, 'Position.y is 10' );
  141. assert.strictEqual( mesh.position.z, 15, 'Position.z is 15' );
  142. assert.strictEqual( mesh.scale.x, 2, 'Scale.x is 2' );
  143. assert.strictEqual( mesh.scale.y, 2, 'Scale.y is 2' );
  144. assert.strictEqual( mesh.scale.z, 2, 'Scale.z is 2' );
  145. } );
  146. QUnit.test( 'Visibility, frustumCulled, renderOrder, layers', ( assert ) => {
  147. const json = {
  148. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  149. geometries: [ {
  150. uuid: 'geom-1',
  151. type: 'BoxGeometry',
  152. width: 1,
  153. height: 1,
  154. depth: 1
  155. } ],
  156. materials: [ {
  157. uuid: 'mat-1',
  158. type: 'MeshBasicMaterial',
  159. color: 16711680
  160. } ],
  161. object: {
  162. uuid: 'mesh-1',
  163. type: 'Mesh',
  164. geometry: 'geom-1',
  165. material: 'mat-1',
  166. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  167. visible: false,
  168. frustumCulled: false,
  169. renderOrder: 5,
  170. layers: 3
  171. }
  172. };
  173. const loader = new ObjectLoader();
  174. const mesh = loader.parse( json );
  175. assert.strictEqual( mesh.visible, false, 'Visible is false' );
  176. assert.strictEqual( mesh.frustumCulled, false, 'FrustumCulled is false' );
  177. assert.strictEqual( mesh.renderOrder, 5, 'RenderOrder is 5' );
  178. assert.strictEqual( mesh.layers.mask, 3, 'Layers mask is 3' );
  179. } );
  180. QUnit.test( 'castShadow, receiveShadow', ( assert ) => {
  181. const json = {
  182. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  183. geometries: [ {
  184. uuid: 'geom-1',
  185. type: 'BoxGeometry',
  186. width: 1,
  187. height: 1,
  188. depth: 1
  189. } ],
  190. materials: [ {
  191. uuid: 'mat-1',
  192. type: 'MeshBasicMaterial',
  193. color: 16711680
  194. } ],
  195. object: {
  196. uuid: 'mesh-1',
  197. type: 'Mesh',
  198. geometry: 'geom-1',
  199. material: 'mat-1',
  200. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  201. layers: 1,
  202. castShadow: true,
  203. receiveShadow: true
  204. }
  205. };
  206. const loader = new ObjectLoader();
  207. const mesh = loader.parse( json );
  208. assert.strictEqual( mesh.castShadow, true, 'CastShadow is true' );
  209. assert.strictEqual( mesh.receiveShadow, true, 'ReceiveShadow is true' );
  210. } );
  211. QUnit.test( 'userData preservation', ( assert ) => {
  212. const json = {
  213. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  214. geometries: [ {
  215. uuid: 'geom-1',
  216. type: 'BoxGeometry',
  217. width: 1,
  218. height: 1,
  219. depth: 1
  220. } ],
  221. materials: [ {
  222. uuid: 'mat-1',
  223. type: 'MeshBasicMaterial',
  224. color: 16711680
  225. } ],
  226. object: {
  227. uuid: 'mesh-1',
  228. type: 'Mesh',
  229. geometry: 'geom-1',
  230. material: 'mat-1',
  231. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  232. layers: 1,
  233. userData: {
  234. customString: 'hello',
  235. customNumber: 42,
  236. customArray: [ 1, 2, 3 ],
  237. customObject: { nested: true }
  238. }
  239. }
  240. };
  241. const loader = new ObjectLoader();
  242. const mesh = loader.parse( json );
  243. assert.strictEqual( mesh.userData.customString, 'hello', 'userData string' );
  244. assert.strictEqual( mesh.userData.customNumber, 42, 'userData number' );
  245. assert.deepEqual( mesh.userData.customArray, [ 1, 2, 3 ], 'userData array' );
  246. assert.deepEqual( mesh.userData.customObject, { nested: true }, 'userData object' );
  247. } );
  248. QUnit.test( 'Points with PointsMaterial', ( assert ) => {
  249. const json = {
  250. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  251. geometries: [ {
  252. uuid: 'geom-1',
  253. type: 'BufferGeometry',
  254. data: {
  255. attributes: {
  256. position: {
  257. itemSize: 3,
  258. type: 'Float32Array',
  259. array: [ 0, 0, 0, 1, 1, 1, 2, 2, 2 ],
  260. normalized: false
  261. }
  262. }
  263. }
  264. } ],
  265. materials: [ {
  266. uuid: 'mat-1',
  267. type: 'PointsMaterial',
  268. color: 16711680,
  269. size: 5
  270. } ],
  271. object: {
  272. uuid: 'points-1',
  273. type: 'Points',
  274. geometry: 'geom-1',
  275. material: 'mat-1',
  276. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  277. layers: 1
  278. }
  279. };
  280. const loader = new ObjectLoader();
  281. const points = loader.parse( json );
  282. assert.ok( points.isPoints, 'Is Points' );
  283. assert.ok( points.material.isPointsMaterial, 'Has PointsMaterial' );
  284. } );
  285. QUnit.test( 'Line', ( assert ) => {
  286. const json = {
  287. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  288. geometries: [ {
  289. uuid: 'geom-1',
  290. type: 'BufferGeometry',
  291. data: {
  292. attributes: {
  293. position: {
  294. itemSize: 3,
  295. type: 'Float32Array',
  296. array: [ 0, 0, 0, 1, 1, 0, 2, 0, 0 ],
  297. normalized: false
  298. }
  299. }
  300. }
  301. } ],
  302. materials: [ {
  303. uuid: 'mat-1',
  304. type: 'LineBasicMaterial',
  305. color: 65280
  306. } ],
  307. object: {
  308. uuid: 'line-1',
  309. type: 'Line',
  310. geometry: 'geom-1',
  311. material: 'mat-1',
  312. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  313. layers: 1
  314. }
  315. };
  316. const loader = new ObjectLoader();
  317. const line = loader.parse( json );
  318. assert.ok( line.isLine, 'Is Line' );
  319. assert.ok( line.material.isLineBasicMaterial, 'Has LineBasicMaterial' );
  320. } );
  321. QUnit.test( 'LineSegments', ( assert ) => {
  322. const json = {
  323. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  324. geometries: [ {
  325. uuid: 'geom-1',
  326. type: 'BufferGeometry',
  327. data: {
  328. attributes: {
  329. position: {
  330. itemSize: 3,
  331. type: 'Float32Array',
  332. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0 ],
  333. normalized: false
  334. }
  335. }
  336. }
  337. } ],
  338. materials: [ {
  339. uuid: 'mat-1',
  340. type: 'LineBasicMaterial',
  341. color: 65280
  342. } ],
  343. object: {
  344. uuid: 'lines-1',
  345. type: 'LineSegments',
  346. geometry: 'geom-1',
  347. material: 'mat-1',
  348. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  349. layers: 1
  350. }
  351. };
  352. const loader = new ObjectLoader();
  353. const lines = loader.parse( json );
  354. assert.ok( lines.isLineSegments, 'Is LineSegments' );
  355. } );
  356. QUnit.test( 'LineLoop', ( assert ) => {
  357. const json = {
  358. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  359. geometries: [ {
  360. uuid: 'geom-1',
  361. type: 'BufferGeometry',
  362. data: {
  363. attributes: {
  364. position: {
  365. itemSize: 3,
  366. type: 'Float32Array',
  367. array: [ 0, 0, 0, 1, 0, 0, 0.5, 1, 0 ],
  368. normalized: false
  369. }
  370. }
  371. }
  372. } ],
  373. materials: [ {
  374. uuid: 'mat-1',
  375. type: 'LineBasicMaterial',
  376. color: 65280
  377. } ],
  378. object: {
  379. uuid: 'loop-1',
  380. type: 'LineLoop',
  381. geometry: 'geom-1',
  382. material: 'mat-1',
  383. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  384. layers: 1
  385. }
  386. };
  387. const loader = new ObjectLoader();
  388. const loop = loader.parse( json );
  389. assert.ok( loop.isLineLoop, 'Is LineLoop' );
  390. } );
  391. QUnit.test( 'Sprite', ( assert ) => {
  392. const json = {
  393. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  394. materials: [ {
  395. uuid: 'mat-1',
  396. type: 'SpriteMaterial',
  397. color: 16711680
  398. } ],
  399. object: {
  400. uuid: 'sprite-1',
  401. type: 'Sprite',
  402. material: 'mat-1',
  403. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  404. layers: 1
  405. }
  406. };
  407. const loader = new ObjectLoader();
  408. const sprite = loader.parse( json );
  409. assert.ok( sprite.isSprite, 'Is Sprite' );
  410. assert.ok( sprite.material.isSpriteMaterial, 'Has SpriteMaterial' );
  411. } );
  412. QUnit.test( 'Object name', ( assert ) => {
  413. const json = {
  414. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  415. geometries: [ {
  416. uuid: 'geom-1',
  417. type: 'BoxGeometry',
  418. width: 1,
  419. height: 1,
  420. depth: 1
  421. } ],
  422. materials: [ {
  423. uuid: 'mat-1',
  424. type: 'MeshBasicMaterial',
  425. color: 16711680
  426. } ],
  427. object: {
  428. uuid: 'mesh-1',
  429. type: 'Mesh',
  430. name: 'MyMesh',
  431. geometry: 'geom-1',
  432. material: 'mat-1',
  433. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  434. layers: 1
  435. }
  436. };
  437. const loader = new ObjectLoader();
  438. const mesh = loader.parse( json );
  439. assert.strictEqual( mesh.name, 'MyMesh', 'Object name' );
  440. } );
  441. } );
  442. } );
粤ICP备19079148号