animation.tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  2. import { InterpolateLinear, InterpolateSmooth, InterpolateDiscrete } from '../../../src/constants.js';
  3. export default QUnit.module( 'JSON4 Format', () => {
  4. QUnit.module( 'Animation', () => {
  5. QUnit.test( 'AnimationClip - name, duration, tracks', ( assert ) => {
  6. const json = {
  7. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  8. geometries: [ {
  9. uuid: 'geom-1',
  10. type: 'BoxGeometry',
  11. width: 1,
  12. height: 1,
  13. depth: 1
  14. } ],
  15. materials: [ {
  16. uuid: 'mat-1',
  17. type: 'MeshBasicMaterial',
  18. color: 16711680
  19. } ],
  20. animations: [ {
  21. uuid: 'anim-1',
  22. name: 'MoveRight',
  23. duration: 2,
  24. tracks: [
  25. {
  26. name: '.position',
  27. type: 'vector',
  28. times: [ 0, 1, 2 ],
  29. values: [ 0, 0, 0, 5, 0, 0, 10, 0, 0 ]
  30. }
  31. ]
  32. } ],
  33. object: {
  34. uuid: 'mesh-1',
  35. type: 'Mesh',
  36. geometry: 'geom-1',
  37. material: 'mat-1',
  38. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  39. layers: 1,
  40. animations: [ 'anim-1' ]
  41. }
  42. };
  43. const loader = new ObjectLoader();
  44. const mesh = loader.parse( json );
  45. assert.strictEqual( mesh.animations.length, 1, 'Has 1 animation' );
  46. assert.strictEqual( mesh.animations[ 0 ].name, 'MoveRight', 'Animation name' );
  47. assert.strictEqual( mesh.animations[ 0 ].duration, 2, 'Animation duration' );
  48. assert.strictEqual( mesh.animations[ 0 ].tracks.length, 1, 'Has 1 track' );
  49. } );
  50. QUnit.test( 'VectorKeyframeTrack (position)', ( assert ) => {
  51. const json = {
  52. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  53. geometries: [ {
  54. uuid: 'geom-1',
  55. type: 'BoxGeometry',
  56. width: 1,
  57. height: 1,
  58. depth: 1
  59. } ],
  60. materials: [ {
  61. uuid: 'mat-1',
  62. type: 'MeshBasicMaterial',
  63. color: 16711680
  64. } ],
  65. animations: [ {
  66. uuid: 'anim-1',
  67. name: 'Position',
  68. duration: 1,
  69. tracks: [
  70. {
  71. name: '.position',
  72. type: 'vector',
  73. times: [ 0, 0.5, 1 ],
  74. values: [ 0, 0, 0, 0, 5, 0, 0, 0, 0 ]
  75. }
  76. ]
  77. } ],
  78. object: {
  79. uuid: 'mesh-1',
  80. type: 'Mesh',
  81. geometry: 'geom-1',
  82. material: 'mat-1',
  83. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  84. layers: 1,
  85. animations: [ 'anim-1' ]
  86. }
  87. };
  88. const loader = new ObjectLoader();
  89. const mesh = loader.parse( json );
  90. const track = mesh.animations[ 0 ].tracks[ 0 ];
  91. assert.strictEqual( track.name, '.position', 'Track name' );
  92. assert.strictEqual( track.times.length, 3, 'Has 3 keyframes' );
  93. assert.strictEqual( track.values.length, 9, 'Has 9 values (3 vec3)' );
  94. } );
  95. QUnit.test( 'QuaternionKeyframeTrack (rotation)', ( assert ) => {
  96. const json = {
  97. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  98. geometries: [ {
  99. uuid: 'geom-1',
  100. type: 'BoxGeometry',
  101. width: 1,
  102. height: 1,
  103. depth: 1
  104. } ],
  105. materials: [ {
  106. uuid: 'mat-1',
  107. type: 'MeshBasicMaterial',
  108. color: 16711680
  109. } ],
  110. animations: [ {
  111. uuid: 'anim-1',
  112. name: 'Rotation',
  113. duration: 1,
  114. tracks: [
  115. {
  116. name: '.quaternion',
  117. type: 'quaternion',
  118. times: [ 0, 1 ],
  119. values: [ 0, 0, 0, 1, 0, 0.7071, 0, 0.7071 ]
  120. }
  121. ]
  122. } ],
  123. object: {
  124. uuid: 'mesh-1',
  125. type: 'Mesh',
  126. geometry: 'geom-1',
  127. material: 'mat-1',
  128. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  129. layers: 1,
  130. animations: [ 'anim-1' ]
  131. }
  132. };
  133. const loader = new ObjectLoader();
  134. const mesh = loader.parse( json );
  135. const track = mesh.animations[ 0 ].tracks[ 0 ];
  136. assert.strictEqual( track.name, '.quaternion', 'Track name' );
  137. assert.strictEqual( track.times.length, 2, 'Has 2 keyframes' );
  138. assert.strictEqual( track.values.length, 8, 'Has 8 values (2 quat)' );
  139. } );
  140. QUnit.test( 'NumberKeyframeTrack (opacity)', ( assert ) => {
  141. const json = {
  142. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  143. geometries: [ {
  144. uuid: 'geom-1',
  145. type: 'BoxGeometry',
  146. width: 1,
  147. height: 1,
  148. depth: 1
  149. } ],
  150. materials: [ {
  151. uuid: 'mat-1',
  152. type: 'MeshBasicMaterial',
  153. color: 16711680,
  154. transparent: true
  155. } ],
  156. animations: [ {
  157. uuid: 'anim-1',
  158. name: 'Fade',
  159. duration: 1,
  160. tracks: [
  161. {
  162. name: '.material.opacity',
  163. type: 'number',
  164. times: [ 0, 0.5, 1 ],
  165. values: [ 1, 0, 1 ]
  166. }
  167. ]
  168. } ],
  169. object: {
  170. uuid: 'mesh-1',
  171. type: 'Mesh',
  172. geometry: 'geom-1',
  173. material: 'mat-1',
  174. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  175. layers: 1,
  176. animations: [ 'anim-1' ]
  177. }
  178. };
  179. const loader = new ObjectLoader();
  180. const mesh = loader.parse( json );
  181. const track = mesh.animations[ 0 ].tracks[ 0 ];
  182. assert.strictEqual( track.name, '.material.opacity', 'Track name' );
  183. assert.strictEqual( track.times.length, 3, 'Has 3 keyframes' );
  184. assert.strictEqual( track.values.length, 3, 'Has 3 values' );
  185. } );
  186. QUnit.test( 'Track interpolation modes', ( assert ) => {
  187. const json = {
  188. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  189. geometries: [ {
  190. uuid: 'geom-1',
  191. type: 'BoxGeometry',
  192. width: 1,
  193. height: 1,
  194. depth: 1
  195. } ],
  196. materials: [ {
  197. uuid: 'mat-1',
  198. type: 'MeshBasicMaterial',
  199. color: 16711680
  200. } ],
  201. animations: [ {
  202. uuid: 'anim-1',
  203. name: 'Interpolation',
  204. duration: 3,
  205. tracks: [
  206. {
  207. name: '.position[x]',
  208. type: 'number',
  209. times: [ 0, 1 ],
  210. values: [ 0, 10 ],
  211. interpolation: InterpolateLinear
  212. },
  213. {
  214. name: '.position[y]',
  215. type: 'number',
  216. times: [ 0, 1 ],
  217. values: [ 0, 10 ],
  218. interpolation: InterpolateSmooth
  219. },
  220. {
  221. name: '.position[z]',
  222. type: 'number',
  223. times: [ 0, 1 ],
  224. values: [ 0, 10 ],
  225. interpolation: InterpolateDiscrete
  226. }
  227. ]
  228. } ],
  229. object: {
  230. uuid: 'mesh-1',
  231. type: 'Mesh',
  232. geometry: 'geom-1',
  233. material: 'mat-1',
  234. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  235. layers: 1,
  236. animations: [ 'anim-1' ]
  237. }
  238. };
  239. const loader = new ObjectLoader();
  240. const mesh = loader.parse( json );
  241. const tracks = mesh.animations[ 0 ].tracks;
  242. assert.strictEqual( tracks[ 0 ].getInterpolation(), InterpolateLinear, 'Linear interpolation' );
  243. assert.strictEqual( tracks[ 1 ].getInterpolation(), InterpolateSmooth, 'Smooth interpolation' );
  244. assert.strictEqual( tracks[ 2 ].getInterpolation(), InterpolateDiscrete, 'Discrete interpolation' );
  245. } );
  246. QUnit.test( 'ColorKeyframeTrack', ( assert ) => {
  247. const json = {
  248. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  249. geometries: [ {
  250. uuid: 'geom-1',
  251. type: 'BoxGeometry',
  252. width: 1,
  253. height: 1,
  254. depth: 1
  255. } ],
  256. materials: [ {
  257. uuid: 'mat-1',
  258. type: 'MeshBasicMaterial',
  259. color: 16711680
  260. } ],
  261. animations: [ {
  262. uuid: 'anim-1',
  263. name: 'ColorChange',
  264. duration: 1,
  265. tracks: [
  266. {
  267. name: '.material.color',
  268. type: 'color',
  269. times: [ 0, 1 ],
  270. values: [ 1, 0, 0, 0, 0, 1 ]
  271. }
  272. ]
  273. } ],
  274. object: {
  275. uuid: 'mesh-1',
  276. type: 'Mesh',
  277. geometry: 'geom-1',
  278. material: 'mat-1',
  279. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  280. layers: 1,
  281. animations: [ 'anim-1' ]
  282. }
  283. };
  284. const loader = new ObjectLoader();
  285. const mesh = loader.parse( json );
  286. const track = mesh.animations[ 0 ].tracks[ 0 ];
  287. assert.strictEqual( track.name, '.material.color', 'Track name' );
  288. assert.strictEqual( track.values.length, 6, 'Has 6 values (2 rgb)' );
  289. } );
  290. QUnit.test( 'BooleanKeyframeTrack', ( assert ) => {
  291. const json = {
  292. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  293. geometries: [ {
  294. uuid: 'geom-1',
  295. type: 'BoxGeometry',
  296. width: 1,
  297. height: 1,
  298. depth: 1
  299. } ],
  300. materials: [ {
  301. uuid: 'mat-1',
  302. type: 'MeshBasicMaterial',
  303. color: 16711680
  304. } ],
  305. animations: [ {
  306. uuid: 'anim-1',
  307. name: 'Visibility',
  308. duration: 2,
  309. tracks: [
  310. {
  311. name: '.visible',
  312. type: 'bool',
  313. times: [ 0, 1, 2 ],
  314. values: [ true, false, true ]
  315. }
  316. ]
  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. animations: [ 'anim-1' ]
  326. }
  327. };
  328. const loader = new ObjectLoader();
  329. const mesh = loader.parse( json );
  330. const track = mesh.animations[ 0 ].tracks[ 0 ];
  331. assert.strictEqual( track.name, '.visible', 'Track name' );
  332. assert.strictEqual( track.times.length, 3, 'Has 3 keyframes' );
  333. } );
  334. QUnit.test( 'Multiple animations on object', ( assert ) => {
  335. const json = {
  336. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  337. geometries: [ {
  338. uuid: 'geom-1',
  339. type: 'BoxGeometry',
  340. width: 1,
  341. height: 1,
  342. depth: 1
  343. } ],
  344. materials: [ {
  345. uuid: 'mat-1',
  346. type: 'MeshBasicMaterial',
  347. color: 16711680
  348. } ],
  349. animations: [
  350. {
  351. uuid: 'anim-1',
  352. name: 'Walk',
  353. duration: 1,
  354. tracks: [
  355. {
  356. name: '.position',
  357. type: 'vector',
  358. times: [ 0, 1 ],
  359. values: [ 0, 0, 0, 1, 0, 0 ]
  360. }
  361. ]
  362. },
  363. {
  364. uuid: 'anim-2',
  365. name: 'Run',
  366. duration: 0.5,
  367. tracks: [
  368. {
  369. name: '.position',
  370. type: 'vector',
  371. times: [ 0, 0.5 ],
  372. values: [ 0, 0, 0, 2, 0, 0 ]
  373. }
  374. ]
  375. }
  376. ],
  377. object: {
  378. uuid: 'mesh-1',
  379. type: 'Mesh',
  380. geometry: 'geom-1',
  381. material: 'mat-1',
  382. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  383. layers: 1,
  384. animations: [ 'anim-1', 'anim-2' ]
  385. }
  386. };
  387. const loader = new ObjectLoader();
  388. const mesh = loader.parse( json );
  389. assert.strictEqual( mesh.animations.length, 2, 'Has 2 animations' );
  390. assert.strictEqual( mesh.animations[ 0 ].name, 'Walk', 'First animation name' );
  391. assert.strictEqual( mesh.animations[ 1 ].name, 'Run', 'Second animation name' );
  392. } );
  393. QUnit.test( 'StringKeyframeTrack', ( assert ) => {
  394. const json = {
  395. metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
  396. geometries: [ {
  397. uuid: 'geom-1',
  398. type: 'BoxGeometry',
  399. width: 1,
  400. height: 1,
  401. depth: 1
  402. } ],
  403. materials: [ {
  404. uuid: 'mat-1',
  405. type: 'MeshBasicMaterial',
  406. color: 16711680
  407. } ],
  408. animations: [ {
  409. uuid: 'anim-1',
  410. name: 'NameChange',
  411. duration: 2,
  412. tracks: [
  413. {
  414. name: '.name',
  415. type: 'string',
  416. times: [ 0, 1, 2 ],
  417. values: [ 'First', 'Second', 'Third' ]
  418. }
  419. ]
  420. } ],
  421. object: {
  422. uuid: 'mesh-1',
  423. type: 'Mesh',
  424. geometry: 'geom-1',
  425. material: 'mat-1',
  426. matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
  427. layers: 1,
  428. animations: [ 'anim-1' ]
  429. }
  430. };
  431. const loader = new ObjectLoader();
  432. const mesh = loader.parse( json );
  433. const track = mesh.animations[ 0 ].tracks[ 0 ];
  434. assert.strictEqual( track.name, '.name', 'Track name' );
  435. assert.strictEqual( track.times.length, 3, 'Has 3 keyframes' );
  436. } );
  437. } );
  438. } );
粤ICP备19079148号