geometries.tests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. export default QUnit.module( 'JSON4 Format', () => {
  3. QUnit.module( 'Geometries', () => {
  4. QUnit.test( 'BufferGeometry with position attribute', ( assert ) => {
  5. const json = {
  6. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  7. geometries: [ {
  8. uuid: 'geom-1',
  9. type: 'BufferGeometry',
  10. data: {
  11. attributes: {
  12. position: {
  13. itemSize: 3,
  14. type: 'Float32Array',
  15. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  16. normalized: false
  17. }
  18. }
  19. }
  20. } ],
  21. materials: [ {
  22. uuid: 'mat-1',
  23. type: 'MeshBasicMaterial',
  24. color: 16711680
  25. } ],
  26. object: {
  27. uuid: 'scene-1',
  28. type: 'Scene',
  29. children: [ {
  30. uuid: 'mesh-1',
  31. type: 'Mesh',
  32. geometry: 'geom-1',
  33. material: 'mat-1',
  34. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  35. layers: 1
  36. } ]
  37. }
  38. };
  39. const loader = new ObjectLoader();
  40. const scene = loader.parse( json );
  41. assert.ok( scene.isScene, 'Parsed Scene' );
  42. const mesh = scene.children[ 0 ];
  43. assert.ok( mesh.isMesh, 'Child is Mesh' );
  44. assert.ok( mesh.geometry.isBufferGeometry, 'Has BufferGeometry' );
  45. assert.strictEqual(
  46. mesh.geometry.attributes.position.count,
  47. 3,
  48. 'Position has 3 vertices'
  49. );
  50. } );
  51. QUnit.test( 'BufferGeometry with position, normal, uv attributes', ( assert ) => {
  52. const json = {
  53. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  54. geometries: [ {
  55. uuid: 'geom-1',
  56. type: 'BufferGeometry',
  57. data: {
  58. attributes: {
  59. position: {
  60. itemSize: 3,
  61. type: 'Float32Array',
  62. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  63. normalized: false
  64. },
  65. normal: {
  66. itemSize: 3,
  67. type: 'Float32Array',
  68. array: [ 0, 0, 1, 0, 0, 1, 0, 0, 1 ],
  69. normalized: false
  70. },
  71. uv: {
  72. itemSize: 2,
  73. type: 'Float32Array',
  74. array: [ 0, 0, 1, 0, 0.5, 1 ],
  75. normalized: false
  76. }
  77. }
  78. }
  79. } ],
  80. materials: [ {
  81. uuid: 'mat-1',
  82. type: 'MeshBasicMaterial',
  83. color: 16711680
  84. } ],
  85. object: {
  86. uuid: 'mesh-1',
  87. type: 'Mesh',
  88. geometry: 'geom-1',
  89. material: 'mat-1',
  90. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  91. layers: 1
  92. }
  93. };
  94. const loader = new ObjectLoader();
  95. const mesh = loader.parse( json );
  96. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  97. assert.ok( mesh.geometry.attributes.position, 'Has position attribute' );
  98. assert.ok( mesh.geometry.attributes.normal, 'Has normal attribute' );
  99. assert.ok( mesh.geometry.attributes.uv, 'Has uv attribute' );
  100. assert.strictEqual( mesh.geometry.attributes.position.itemSize, 3, 'Position itemSize is 3' );
  101. assert.strictEqual( mesh.geometry.attributes.normal.itemSize, 3, 'Normal itemSize is 3' );
  102. assert.strictEqual( mesh.geometry.attributes.uv.itemSize, 2, 'UV itemSize is 2' );
  103. } );
  104. QUnit.test( 'Indexed BufferGeometry', ( assert ) => {
  105. const json = {
  106. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  107. geometries: [ {
  108. uuid: 'geom-1',
  109. type: 'BufferGeometry',
  110. data: {
  111. attributes: {
  112. position: {
  113. itemSize: 3,
  114. type: 'Float32Array',
  115. array: [ 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 ],
  116. normalized: false
  117. }
  118. },
  119. index: {
  120. type: 'Uint16Array',
  121. array: [ 0, 1, 2, 0, 2, 3 ]
  122. }
  123. }
  124. } ],
  125. materials: [ {
  126. uuid: 'mat-1',
  127. type: 'MeshBasicMaterial',
  128. color: 16711680
  129. } ],
  130. object: {
  131. uuid: 'mesh-1',
  132. type: 'Mesh',
  133. geometry: 'geom-1',
  134. material: 'mat-1',
  135. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  136. layers: 1
  137. }
  138. };
  139. const loader = new ObjectLoader();
  140. const mesh = loader.parse( json );
  141. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  142. assert.ok( mesh.geometry.index, 'Has index buffer' );
  143. assert.strictEqual( mesh.geometry.index.count, 6, 'Index count is 6' );
  144. assert.strictEqual( mesh.geometry.attributes.position.count, 4, 'Position has 4 vertices' );
  145. } );
  146. QUnit.test( 'Geometry groups (materialIndex)', ( assert ) => {
  147. const json = {
  148. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  149. geometries: [ {
  150. uuid: 'geom-1',
  151. type: 'BufferGeometry',
  152. data: {
  153. attributes: {
  154. position: {
  155. itemSize: 3,
  156. type: 'Float32Array',
  157. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 ],
  158. normalized: false
  159. }
  160. },
  161. groups: [
  162. { start: 0, count: 3, materialIndex: 0 },
  163. { start: 3, count: 3, materialIndex: 1 }
  164. ]
  165. }
  166. } ],
  167. materials: [
  168. { uuid: 'mat-1', type: 'MeshBasicMaterial', color: 16711680 },
  169. { uuid: 'mat-2', type: 'MeshBasicMaterial', color: 65280 }
  170. ],
  171. object: {
  172. uuid: 'mesh-1',
  173. type: 'Mesh',
  174. geometry: 'geom-1',
  175. material: [ 'mat-1', 'mat-2' ],
  176. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  177. layers: 1
  178. }
  179. };
  180. const loader = new ObjectLoader();
  181. const mesh = loader.parse( json );
  182. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  183. assert.strictEqual( mesh.geometry.groups.length, 2, 'Has 2 groups' );
  184. assert.strictEqual( mesh.geometry.groups[ 0 ].materialIndex, 0, 'First group materialIndex is 0' );
  185. assert.strictEqual( mesh.geometry.groups[ 1 ].materialIndex, 1, 'Second group materialIndex is 1' );
  186. assert.ok( Array.isArray( mesh.material ), 'Material is an array' );
  187. assert.strictEqual( mesh.material.length, 2, 'Has 2 materials' );
  188. } );
  189. QUnit.test( 'BoundingSphere preservation', ( assert ) => {
  190. const json = {
  191. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  192. geometries: [ {
  193. uuid: 'geom-1',
  194. type: 'BufferGeometry',
  195. data: {
  196. attributes: {
  197. position: {
  198. itemSize: 3,
  199. type: 'Float32Array',
  200. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  201. normalized: false
  202. }
  203. },
  204. boundingSphere: {
  205. center: [ 0.5, 0.5, 0 ],
  206. radius: 0.7071067811865476
  207. }
  208. }
  209. } ],
  210. materials: [ {
  211. uuid: 'mat-1',
  212. type: 'MeshBasicMaterial',
  213. color: 16711680
  214. } ],
  215. object: {
  216. uuid: 'mesh-1',
  217. type: 'Mesh',
  218. geometry: 'geom-1',
  219. material: 'mat-1',
  220. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  221. layers: 1
  222. }
  223. };
  224. const loader = new ObjectLoader();
  225. const mesh = loader.parse( json );
  226. assert.ok( mesh.geometry.boundingSphere, 'Has boundingSphere' );
  227. assert.strictEqual( mesh.geometry.boundingSphere.center.x, 0.5, 'BoundingSphere center.x' );
  228. assert.strictEqual( mesh.geometry.boundingSphere.center.y, 0.5, 'BoundingSphere center.y' );
  229. assert.ok( Math.abs( mesh.geometry.boundingSphere.radius - 0.7071067811865476 ) < 0.0001, 'BoundingSphere radius' );
  230. } );
  231. QUnit.test( 'BoxGeometry via parameters', ( assert ) => {
  232. const json = {
  233. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  234. geometries: [ {
  235. uuid: 'geom-1',
  236. type: 'BoxGeometry',
  237. width: 2,
  238. height: 3,
  239. depth: 4,
  240. widthSegments: 2,
  241. heightSegments: 3,
  242. depthSegments: 4
  243. } ],
  244. materials: [ {
  245. uuid: 'mat-1',
  246. type: 'MeshBasicMaterial',
  247. color: 16711680
  248. } ],
  249. object: {
  250. uuid: 'mesh-1',
  251. type: 'Mesh',
  252. geometry: 'geom-1',
  253. material: 'mat-1',
  254. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  255. layers: 1
  256. }
  257. };
  258. const loader = new ObjectLoader();
  259. const mesh = loader.parse( json );
  260. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  261. assert.strictEqual( mesh.geometry.type, 'BoxGeometry', 'Geometry type is BoxGeometry' );
  262. assert.strictEqual( mesh.geometry.parameters.width, 2, 'Width parameter' );
  263. assert.strictEqual( mesh.geometry.parameters.height, 3, 'Height parameter' );
  264. assert.strictEqual( mesh.geometry.parameters.depth, 4, 'Depth parameter' );
  265. } );
  266. QUnit.test( 'SphereGeometry via parameters', ( assert ) => {
  267. const json = {
  268. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  269. geometries: [ {
  270. uuid: 'geom-1',
  271. type: 'SphereGeometry',
  272. radius: 5,
  273. widthSegments: 32,
  274. heightSegments: 16,
  275. phiStart: 0,
  276. phiLength: Math.PI * 2,
  277. thetaStart: 0,
  278. thetaLength: Math.PI
  279. } ],
  280. materials: [ {
  281. uuid: 'mat-1',
  282. type: 'MeshBasicMaterial',
  283. color: 16711680
  284. } ],
  285. object: {
  286. uuid: 'mesh-1',
  287. type: 'Mesh',
  288. geometry: 'geom-1',
  289. material: 'mat-1',
  290. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  291. layers: 1
  292. }
  293. };
  294. const loader = new ObjectLoader();
  295. const mesh = loader.parse( json );
  296. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  297. assert.strictEqual( mesh.geometry.type, 'SphereGeometry', 'Geometry type is SphereGeometry' );
  298. assert.strictEqual( mesh.geometry.parameters.radius, 5, 'Radius parameter' );
  299. assert.strictEqual( mesh.geometry.parameters.widthSegments, 32, 'widthSegments parameter' );
  300. assert.strictEqual( mesh.geometry.parameters.heightSegments, 16, 'heightSegments parameter' );
  301. } );
  302. QUnit.test( 'PlaneGeometry via parameters', ( assert ) => {
  303. const json = {
  304. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  305. geometries: [ {
  306. uuid: 'geom-1',
  307. type: 'PlaneGeometry',
  308. width: 10,
  309. height: 20,
  310. widthSegments: 5,
  311. heightSegments: 10
  312. } ],
  313. materials: [ {
  314. uuid: 'mat-1',
  315. type: 'MeshBasicMaterial',
  316. color: 16711680
  317. } ],
  318. object: {
  319. uuid: 'mesh-1',
  320. type: 'Mesh',
  321. geometry: 'geom-1',
  322. material: 'mat-1',
  323. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  324. layers: 1
  325. }
  326. };
  327. const loader = new ObjectLoader();
  328. const mesh = loader.parse( json );
  329. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  330. assert.strictEqual( mesh.geometry.type, 'PlaneGeometry', 'Geometry type is PlaneGeometry' );
  331. assert.strictEqual( mesh.geometry.parameters.width, 10, 'Width parameter' );
  332. assert.strictEqual( mesh.geometry.parameters.height, 20, 'Height parameter' );
  333. } );
  334. QUnit.test( 'CylinderGeometry via parameters', ( assert ) => {
  335. const json = {
  336. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  337. geometries: [ {
  338. uuid: 'geom-1',
  339. type: 'CylinderGeometry',
  340. radiusTop: 1,
  341. radiusBottom: 2,
  342. height: 5,
  343. radialSegments: 16,
  344. heightSegments: 4,
  345. openEnded: false,
  346. thetaStart: 0,
  347. thetaLength: Math.PI * 2
  348. } ],
  349. materials: [ {
  350. uuid: 'mat-1',
  351. type: 'MeshBasicMaterial',
  352. color: 16711680
  353. } ],
  354. object: {
  355. uuid: 'mesh-1',
  356. type: 'Mesh',
  357. geometry: 'geom-1',
  358. material: 'mat-1',
  359. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  360. layers: 1
  361. }
  362. };
  363. const loader = new ObjectLoader();
  364. const mesh = loader.parse( json );
  365. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  366. assert.strictEqual( mesh.geometry.type, 'CylinderGeometry', 'Geometry type is CylinderGeometry' );
  367. assert.strictEqual( mesh.geometry.parameters.radiusTop, 1, 'radiusTop parameter' );
  368. assert.strictEqual( mesh.geometry.parameters.radiusBottom, 2, 'radiusBottom parameter' );
  369. assert.strictEqual( mesh.geometry.parameters.height, 5, 'height parameter' );
  370. } );
  371. QUnit.test( 'TorusGeometry via parameters', ( assert ) => {
  372. const json = {
  373. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  374. geometries: [ {
  375. uuid: 'geom-1',
  376. type: 'TorusGeometry',
  377. radius: 10,
  378. tube: 3,
  379. radialSegments: 16,
  380. tubularSegments: 100,
  381. arc: Math.PI * 2
  382. } ],
  383. materials: [ {
  384. uuid: 'mat-1',
  385. type: 'MeshBasicMaterial',
  386. color: 16711680
  387. } ],
  388. object: {
  389. uuid: 'mesh-1',
  390. type: 'Mesh',
  391. geometry: 'geom-1',
  392. material: 'mat-1',
  393. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  394. layers: 1
  395. }
  396. };
  397. const loader = new ObjectLoader();
  398. const mesh = loader.parse( json );
  399. assert.ok( mesh.isMesh, 'Parsed Mesh' );
  400. assert.strictEqual( mesh.geometry.type, 'TorusGeometry', 'Geometry type is TorusGeometry' );
  401. assert.strictEqual( mesh.geometry.parameters.radius, 10, 'radius parameter' );
  402. assert.strictEqual( mesh.geometry.parameters.tube, 3, 'tube parameter' );
  403. } );
  404. QUnit.test( 'Geometry name and userData', ( assert ) => {
  405. const json = {
  406. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  407. geometries: [ {
  408. uuid: 'geom-1',
  409. type: 'BufferGeometry',
  410. name: 'MyGeometry',
  411. userData: { customProp: 'customValue' },
  412. data: {
  413. attributes: {
  414. position: {
  415. itemSize: 3,
  416. type: 'Float32Array',
  417. array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
  418. normalized: false
  419. }
  420. }
  421. }
  422. } ],
  423. materials: [ {
  424. uuid: 'mat-1',
  425. type: 'MeshBasicMaterial',
  426. color: 16711680
  427. } ],
  428. object: {
  429. uuid: 'mesh-1',
  430. type: 'Mesh',
  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.geometry.name, 'MyGeometry', 'Geometry name' );
  440. assert.strictEqual( mesh.geometry.userData.customProp, 'customValue', 'Geometry userData' );
  441. } );
  442. } );
  443. } );
粤ICP备19079148号