GLTFLoader.tests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author Don McCurdy / https://www.donmccurdy.com
  3. */
  4. /* global QUnit */
  5. import * as GLTFExporter from '../../../../examples/js/exporters/GLTFExporter';
  6. import * as GLTFLoader from '../../../../examples/js/loaders/GLTFLoader';
  7. export default QUnit.module( 'Loaders', () => {
  8. QUnit.module( 'GLTFLoader', () => {
  9. QUnit.test( 'constructor', ( assert ) => {
  10. assert.ok( new THREE.GLTFLoader(), 'Can instantiate a loader.' );
  11. } );
  12. QUnit.test( 'parse - basic', ( assert ) => {
  13. var done = assert.async();
  14. var geometry = new THREE.BufferGeometry();
  15. var array = new Float32Array( [
  16. - 1, - 1, - 1,
  17. 1, 1, 1,
  18. 4, 4, 4
  19. ] );
  20. geometry.addAttribute( 'position', new THREE.BufferAttribute( array, 3 ) );
  21. var meshIn = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { color: 0xFF0000 } ) );
  22. meshIn.name = 'test_mesh';
  23. var exporter = new THREE.GLTFExporter();
  24. var loader = new THREE.GLTFLoader();
  25. exporter.parse( meshIn, function ( binary ) {
  26. loader.parse( binary, './', function ( gltf ) {
  27. var meshOut = gltf.scene.children[ 0 ];
  28. var attrsIn = meshIn.geometry.attributes;
  29. var attrsOut = meshOut.geometry.attributes;
  30. assert.equal( meshIn.name, meshOut.name, 'loads names' );
  31. assert.equal( meshIn.material.color.getHex(), meshOut.material.color.getHex(), 'loads color' );
  32. assert.smartEqual( attrsIn.position.array, attrsOut.position.array, 'loads positions' );
  33. assert.equal( undefined, attrsOut.normal, 'ignores missing attributes' );
  34. done();
  35. }, undefined, function ( e ) {
  36. console.error(e);
  37. } );
  38. }, { binary: true } );
  39. } );
  40. QUnit.test( 'parse - animation', ( assert ) => {
  41. var done = assert.async();
  42. var node1 = new THREE.Object3D();
  43. node1.name = 'node1';
  44. var node2 = new THREE.Object3D();
  45. node2.name = 'node2';
  46. var scene = new THREE.Scene();
  47. scene.add( node1, node2 );
  48. var clip = new THREE.AnimationClip( 'clip', undefined, [
  49. new THREE.VectorKeyframeTrack( 'node1.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] )
  50. ] );
  51. var exporter = new THREE.GLTFExporter();
  52. var loader = new THREE.GLTFLoader();
  53. exporter.parse( scene, function ( binary ) {
  54. loader.parse( binary, './', function ( gltf ) {
  55. var clipOut = gltf.animations[ 0 ];
  56. assert.equal( 'node1.position', clipOut.tracks[ 0 ].name, 'track name' );
  57. assert.smartEqual( clip.tracks[ 0 ].times, clipOut.tracks[ 0 ].times, 'track times' );
  58. assert.smartEqual( clip.tracks[ 0 ].values, clipOut.tracks[ 0 ].values, 'track values' );
  59. done();
  60. }, undefined, function ( e ) {
  61. console.error(e);
  62. } );
  63. }, { binary: true, animations: [ clip ] } );
  64. } );
  65. } );
  66. } );