GLTFLoader.tests.js 3.1 KB

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