GLTFLoader.tests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. } );
  18. QUnit.module( 'GLTFLoader-webonly', () => {
  19. QUnit.test( 'parse - basic', ( assert ) => {
  20. var done = assert.async();
  21. var geometry = new BufferGeometry();
  22. var array = new Float32Array( [
  23. - 1, - 1, - 1,
  24. 1, 1, 1,
  25. 4, 4, 4
  26. ] );
  27. geometry.setAttribute( 'position', new BufferAttribute( array, 3 ) );
  28. var meshIn = new Mesh( geometry, new MeshStandardMaterial( { color: 0xFF0000 } ) );
  29. meshIn.name = 'test_mesh';
  30. var exporter = new GLTFExporter();
  31. var loader = new GLTFLoader();
  32. exporter.parse( meshIn, function ( binary ) {
  33. loader.parse( binary, './', function ( gltf ) {
  34. var meshOut = gltf.scene.children[ 0 ];
  35. var attrsIn = meshIn.geometry.attributes;
  36. var attrsOut = meshOut.geometry.attributes;
  37. assert.equal( meshIn.name, meshOut.name, 'loads names' );
  38. assert.equal( meshIn.material.color.getHex(), meshOut.material.color.getHex(), 'loads color' );
  39. assert.smartEqual( attrsIn.position.array, attrsOut.position.array, 'loads positions' );
  40. assert.equal( undefined, attrsOut.normal, 'ignores missing attributes' );
  41. done();
  42. }, undefined, function ( e ) {
  43. console.error( e );
  44. } );
  45. }, { binary: true } );
  46. } );
  47. QUnit.test( 'parse - animation', ( assert ) => {
  48. var done = assert.async();
  49. var node1 = new Object3D();
  50. node1.name = 'node1';
  51. var node2 = new Object3D();
  52. node2.name = 'node2';
  53. var scene = new Scene();
  54. scene.add( node1, node2 );
  55. var clip = new AnimationClip( 'clip', undefined, [
  56. new VectorKeyframeTrack( 'node1.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] )
  57. ] );
  58. var exporter = new GLTFExporter();
  59. var loader = new GLTFLoader();
  60. exporter.parse( scene, function ( binary ) {
  61. loader.parse( binary, './', function ( gltf ) {
  62. var clipOut = gltf.animations[ 0 ];
  63. assert.equal( 'node1.position', clipOut.tracks[ 0 ].name, 'track name' );
  64. assert.smartEqual( clip.tracks[ 0 ].times, clipOut.tracks[ 0 ].times, 'track times' );
  65. assert.smartEqual( clip.tracks[ 0 ].values, clipOut.tracks[ 0 ].values, 'track values' );
  66. done();
  67. }, undefined, function ( e ) {
  68. console.error( e );
  69. } );
  70. }, { binary: true, animations: [ clip ] } );
  71. } );
  72. } );
  73. } );