GLTFLoader.tests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* global QUnit */
  2. import { GLTFExporter } from '../../../../examples/jsm/exporters/GLTFExporter.js';
  3. import { GLTFLoader } from '../../../../examples/jsm/loaders/GLTFLoader.js';
  4. import { AnimationClip } from '../../../../src/animation/AnimationClip.js';
  5. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  6. import { BufferGeometry } from '../../../../src/core/BufferGeometry.js';
  7. import { BoxBufferGeometry } from '../../../../src/geometries/BoxGeometry.js';
  8. import { Mesh } from '../../../../src/objects/Mesh.js';
  9. import { MeshStandardMaterial } from '../../../../src/materials/MeshStandardMaterial.js';
  10. import { Object3D } from '../../../../src/core/Object3D.js';
  11. import { Scene } from '../../../../src/scenes/Scene.js';
  12. import { DataTexture } from '../../../../src/textures/DataTexture.js';
  13. import { VectorKeyframeTrack } from '../../../../src/animation/tracks/VectorKeyframeTrack.js';
  14. export default QUnit.module( 'Loaders', () => {
  15. QUnit.module( 'GLTFLoader', () => {
  16. QUnit.test( 'constructor', ( assert ) => {
  17. assert.ok( new GLTFLoader(), 'Can instantiate a loader.' );
  18. } );
  19. } );
  20. QUnit.module( 'GLTFLoader-webonly', () => {
  21. QUnit.test( 'parse - basic', ( assert ) => {
  22. var done = assert.async();
  23. var geometry = new BufferGeometry();
  24. var array = new Float32Array( [
  25. - 1, - 1, - 1,
  26. 1, 1, 1,
  27. 4, 4, 4
  28. ] );
  29. geometry.setAttribute( 'position', new BufferAttribute( array, 3 ) );
  30. var meshIn = new Mesh( geometry, new MeshStandardMaterial( { color: 0xFF0000 } ) );
  31. meshIn.name = 'test_mesh';
  32. var exporter = new GLTFExporter();
  33. var loader = new GLTFLoader();
  34. exporter.parse( meshIn, function ( binary ) {
  35. loader.parse( binary, './', function ( gltf ) {
  36. var meshOut = gltf.scene.children[ 0 ];
  37. var attrsIn = meshIn.geometry.attributes;
  38. var attrsOut = meshOut.geometry.attributes;
  39. assert.equal( meshIn.name, meshOut.name, 'loads names' );
  40. assert.equal( meshIn.material.color.getHex(), meshOut.material.color.getHex(), 'loads color' );
  41. assert.smartEqual( attrsIn.position.array, attrsOut.position.array, 'loads positions' );
  42. assert.equal( undefined, attrsOut.normal, 'ignores missing attributes' );
  43. done();
  44. }, undefined, function ( e ) {
  45. console.error( e );
  46. } );
  47. }, { binary: true } );
  48. } );
  49. QUnit.test( 'parse - animation', ( assert ) => {
  50. var done = assert.async();
  51. var node1 = new Object3D();
  52. node1.name = 'node1';
  53. var node2 = new Object3D();
  54. node2.name = 'node2';
  55. var scene = new Scene();
  56. scene.add( node1, node2 );
  57. var clip = new AnimationClip( 'clip', undefined, [
  58. new VectorKeyframeTrack( 'node1.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] )
  59. ] );
  60. var exporter = new GLTFExporter();
  61. var loader = new GLTFLoader();
  62. exporter.parse( scene, function ( binary ) {
  63. loader.parse( binary, './', function ( gltf ) {
  64. var clipOut = gltf.animations[ 0 ];
  65. assert.equal( 'node1.position', clipOut.tracks[ 0 ].name, 'track name' );
  66. assert.smartEqual( clip.tracks[ 0 ].times, clipOut.tracks[ 0 ].times, 'track times' );
  67. assert.smartEqual( clip.tracks[ 0 ].values, clipOut.tracks[ 0 ].values, 'track values' );
  68. done();
  69. }, undefined, function ( e ) {
  70. console.error( e );
  71. } );
  72. }, { binary: true, animations: [ clip ] } );
  73. } );
  74. QUnit.test( 'parser - associations', ( assert ) => {
  75. var done = assert.async();
  76. var scene = new Scene();
  77. scene.add( new Mesh(
  78. new BoxBufferGeometry(),
  79. new MeshStandardMaterial( { map: new DataTexture( new Uint8ClampedArray( [ 0, 0, 0, 0 ] ), 1, 1 ) } )
  80. ) );
  81. var exporter = new GLTFExporter();
  82. var loader = new GLTFLoader();
  83. exporter.parse( scene, function ( binary ) {
  84. loader.parse( binary, './', function ( gltf ) {
  85. var parser = gltf.parser;
  86. var associations = parser.associations;
  87. gltf.scene.traverse( function ( object ) {
  88. if ( object.isMesh ) {
  89. assert.smartEqual( associations.get( object ), {
  90. meshes: 0,
  91. nodes: 0,
  92. primitives: 0
  93. }, 'Mesh has a proper association' );
  94. assert.smartEqual( associations.get( object.material ), {
  95. materials: 0
  96. }, 'Material has a proper association' );
  97. assert.smartEqual( associations.get( object.material.map ), {
  98. textures: 0
  99. }, 'Texture has a proper association' );
  100. }
  101. } );
  102. done();
  103. }, undefined, function ( e ) {
  104. console.error( e );
  105. } );
  106. }, { binary: true } );
  107. } );
  108. } );
  109. } );