GLTFLoader.tests.js 3.2 KB

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