GLTFExporter.tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* global QUnit */
  2. import { GLTFExporter } from '../../../../examples/jsm/exporters/GLTFExporter';
  3. import { AnimationClip } from '../../../../src/animation/AnimationClip';
  4. import { BoxBufferGeometry } from '../../../../src/geometries/BoxBufferGeometry';
  5. import { BufferAttribute } from '../../../../src/core/BufferAttribute';
  6. import { BufferGeometry } from '../../../../src/core/BufferGeometry';
  7. import { Mesh } from '../../../../src/objects/Mesh';
  8. import { MeshBasicMaterial } from '../../../../src/materials/MeshBasicMaterial';
  9. import { MeshStandardMaterial } from '../../../../src/materials/MeshStandardMaterial';
  10. import { Object3D } from '../../../../src/core/Object3D';
  11. import { NumberKeyframeTrack } from '../../../../src/animation/tracks/NumberKeyframeTrack';
  12. import { Scene } from '../../../../src/scenes/Scene';
  13. import { VectorKeyframeTrack } from '../../../../src/animation/tracks/VectorKeyframeTrack';
  14. import {
  15. DoubleSide,
  16. InterpolateLinear,
  17. InterpolateDiscrete
  18. } from '../../../../src/constants.js';
  19. export default QUnit.module( 'Exporters', () => {
  20. QUnit.module( 'GLTFExporter', () => {
  21. QUnit.test( 'constructor', ( assert ) => {
  22. assert.ok( new GLTFExporter(), 'Can instantiate an exporter.' );
  23. } );
  24. QUnit.test( 'parse - metadata', ( assert ) => {
  25. var done = assert.async();
  26. var object = new Object3D();
  27. var exporter = new GLTFExporter();
  28. exporter.parse( object, function ( gltf ) {
  29. console.log( gltf );
  30. assert.equal( '2.0', gltf.asset.version, 'asset.version' );
  31. assert.equal( 'GLTFExporter', gltf.asset.generator, 'asset.generator' );
  32. done();
  33. } );
  34. } );
  35. QUnit.test( 'parse - basic', ( assert ) => {
  36. var done = assert.async();
  37. var box = new Mesh(
  38. new BoxBufferGeometry( 1, 1, 1 ),
  39. new MeshStandardMaterial( { color: 0xFF0000 } )
  40. );
  41. var exporter = new GLTFExporter();
  42. exporter.parse( box, function ( gltf ) {
  43. assert.equal( 1, gltf.nodes.length, 'correct number of nodes' );
  44. assert.equal( 0, gltf.nodes[ 0 ].mesh, 'node references mesh' );
  45. assert.equal( 1, gltf.meshes[ 0 ].primitives.length, 'correct number of primitives' );
  46. var primitive = gltf.meshes[ 0 ].primitives[ 0 ];
  47. var material = gltf.materials[ primitive.material ];
  48. assert.equal( 4, primitive.mode, 'mesh uses TRIANGLES mode' );
  49. assert.ok( primitive.attributes.POSITION !== undefined, 'mesh contains position data' );
  50. assert.ok( primitive.attributes.NORMAL !== undefined, 'mesh contains normal data' );
  51. assert.smartEqual( {
  52. baseColorFactor: [ 1, 0, 0, 1 ],
  53. metallicFactor: 0.0,
  54. roughnessFactor: 1.0
  55. }, material.pbrMetallicRoughness, 'material' );
  56. done();
  57. } );
  58. } );
  59. QUnit.test( 'parse - animation', ( assert ) => {
  60. var done = assert.async();
  61. var mesh1 = new Mesh();
  62. mesh1.name = 'mesh1';
  63. var mesh2 = new Mesh();
  64. mesh2.name = 'mesh2';
  65. var mesh3 = new Mesh();
  66. mesh3.name = 'mesh3';
  67. var scene = new Scene();
  68. scene.add( mesh1, mesh2, mesh3 );
  69. var clip1 = new AnimationClip( 'clip1', undefined, [
  70. new VectorKeyframeTrack( 'mesh1.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] )
  71. ] );
  72. var clip2 = new AnimationClip( 'clip2', undefined, [
  73. new VectorKeyframeTrack( 'mesh3.scale', [ 0, 1, 2 ], [ 1, 1, 1, 2, 2, 2, 1, 1, 1 ] )
  74. ] );
  75. var exporter = new GLTFExporter();
  76. exporter.parse( scene, function ( gltf ) {
  77. assert.equal( 2, gltf.animations.length, 'one animation per clip' );
  78. var target1 = gltf.animations[ 0 ].channels[ 0 ].target;
  79. var target2 = gltf.animations[ 1 ].channels[ 0 ].target;
  80. assert.equal( 'mesh1', gltf.nodes[ target1.node ].name, 'clip1 node' );
  81. assert.equal( 'translation', target1.path, 'clip1 property' );
  82. assert.equal( 'mesh3', gltf.nodes[ target2.node ].name, 'clip2 node' );
  83. assert.equal( 'scale', target2.path, 'clip2 property' );
  84. done();
  85. }, { animations: [ clip1, clip2 ] } );
  86. } );
  87. QUnit.test( 'parse - empty buffergeometry', ( assert ) => {
  88. var done = assert.async();
  89. var scene = new Scene();
  90. var geometry = new BufferGeometry();
  91. var numElements = 6;
  92. var positions = new Float32Array( ( numElements ) * 3 );
  93. var colors = new Float32Array( ( numElements ) * 3 );
  94. geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
  95. geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
  96. geometry.setDrawRange( 0, 0 );
  97. var empty = new Mesh( geometry, new MeshBasicMaterial( { side: DoubleSide, vertexColors: true } ) );
  98. empty.name = 'Custom buffered empty (drawrange)';
  99. scene.add( empty );
  100. var exporter = new GLTFExporter();
  101. exporter.parse( scene, function ( gltf ) {
  102. assert.equal( gltf.meshes, undefined, 'empty meshes' );
  103. assert.equal( gltf.materials, undefined, 'empty materials' );
  104. assert.equal( gltf.bufferViews, undefined, 'empty bufferViews' );
  105. assert.equal( gltf.buffers, undefined, 'buffers' );
  106. assert.equal( gltf.accessors, undefined, 'accessors' );
  107. assert.equal( gltf.nodes[ 0 ].mesh, undefined, 'nodes[0].mesh' );
  108. done();
  109. } );
  110. } );
  111. QUnit.test( 'parse - individual morph targets', ( assert ) => {
  112. var done = assert.async();
  113. // Creates a geometry with four (4) morph targets, three (3) of which are
  114. // animated by an animation clip. Because glTF requires all morph targets
  115. // to be animated in unison, the exporter should write an empty track for
  116. // the fourth target.
  117. var geometry = new BufferGeometry();
  118. var position = new BufferAttribute( new Float32Array( [ 0, 0, 0, 0, 0, 1, 1, 0, 1 ] ), 3 );
  119. geometry.setAttribute( 'position', position );
  120. geometry.morphAttributes.position = [ position, position, position, position ];
  121. var mesh = new Mesh( geometry );
  122. mesh.morphTargetDictionary.a = 0;
  123. mesh.morphTargetDictionary.b = 1;
  124. mesh.morphTargetDictionary.c = 2;
  125. mesh.morphTargetDictionary.d = 3;
  126. var timesA = [ 0, 1, 2 ];
  127. var timesB = [ 2, 3, 4 ];
  128. var timesC = [ 4, 5, 6 ];
  129. var valuesA = [ 0, 1, 0 ];
  130. var valuesB = [ 0, 1, 0 ];
  131. var valuesC = [ 0, 1, 0 ];
  132. var trackA = new VectorKeyframeTrack( '.morphTargetInfluences[a]', timesA, valuesA, InterpolateLinear );
  133. var trackB = new VectorKeyframeTrack( '.morphTargetInfluences[b]', timesB, valuesB, InterpolateLinear );
  134. var trackC = new VectorKeyframeTrack( '.morphTargetInfluences[c]', timesC, valuesC, InterpolateLinear );
  135. var clip = new AnimationClip( 'clip1', undefined, [ trackA, trackB, trackC ] );
  136. var exporter = new GLTFExporter();
  137. exporter.parse( mesh, function ( gltf ) {
  138. assert.equal( 1, gltf.animations.length, 'one animation' );
  139. assert.equal( 1, gltf.animations[ 0 ].channels.length, 'one channel' );
  140. assert.equal( 1, gltf.animations[ 0 ].samplers.length, 'one sampler' );
  141. var channel = gltf.animations[ 0 ].channels[ 0 ];
  142. var sampler = gltf.animations[ 0 ].samplers[ 0 ];
  143. assert.smartEqual( channel, { sampler: 0, target: { node: 0, path: 'weights' } } );
  144. assert.equal( sampler.interpolation, 'LINEAR' );
  145. var input = gltf.accessors[ sampler.input ];
  146. var output = gltf.accessors[ sampler.output ];
  147. assert.equal( input.count, 7 );
  148. assert.equal( input.type, 'SCALAR' );
  149. assert.smartEqual( input.min, [ 0 ] );
  150. assert.smartEqual( input.max, [ 6 ] );
  151. assert.equal( output.count, 28 ); // 4 targets * 7 frames
  152. assert.equal( output.type, 'SCALAR' );
  153. assert.smartEqual( output.min, [ 0 ] );
  154. assert.smartEqual( output.max, [ 1 ] );
  155. done();
  156. }, { animations: [ clip ] } );
  157. } );
  158. QUnit.test( 'utils - insertKeyframe', ( assert ) => {
  159. var track;
  160. var index;
  161. function createTrack() {
  162. return new VectorKeyframeTrack(
  163. 'foo.bar',
  164. [ 5, 10, 15, 20, 25, 30 ],
  165. [ 0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0 ],
  166. InterpolateLinear
  167. );
  168. }
  169. track = createTrack();
  170. index = GLTFExporter.Utils.insertKeyframe( track, 0 );
  171. assert.equal( index, 0, 'prepend - index' );
  172. assert.smartEqual( Array.from( track.times ), [ 0, 5, 10, 15, 20, 25, 30 ], 'prepend - time' );
  173. assert.smartEqual( Array.from( track.values ), [ 0, 5, 0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0 ], 'prepend - value' );
  174. track = createTrack();
  175. index = GLTFExporter.Utils.insertKeyframe( track, 7.5 );
  176. assert.equal( index, 1, 'insert - index (linear)' );
  177. assert.smartEqual( Array.from( track.times ), [ 5, 7.5, 10, 15, 20, 25, 30 ], 'insert - time (linear)' );
  178. assert.smartEqual( Array.from( track.values ), [ 0, 5, 0.5, 4.5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0 ], 'insert - value (linear)' );
  179. track = createTrack();
  180. track.setInterpolation( InterpolateDiscrete );
  181. index = GLTFExporter.Utils.insertKeyframe( track, 16 );
  182. assert.equal( index, 3, 'insert - index (linear)' );
  183. assert.smartEqual( Array.from( track.times ), [ 5, 10, 15, 16, 20, 25, 30 ], 'insert - time (discrete)' );
  184. assert.smartEqual( Array.from( track.values ), [ 0, 5, 1, 4, 2, 3, 2, 3, 3, 2, 4, 1, 5, 0 ], 'insert - value (discrete)' );
  185. track = createTrack();
  186. index = GLTFExporter.Utils.insertKeyframe( track, 100 );
  187. assert.equal( index, 6, 'append - index' );
  188. assert.smartEqual( Array.from( track.times ), [ 5, 10, 15, 20, 25, 30, 100 ], 'append time' );
  189. assert.smartEqual( Array.from( track.values ), [ 0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0, 5, 0 ], 'append value' );
  190. track = createTrack();
  191. index = GLTFExporter.Utils.insertKeyframe( track, 15 );
  192. assert.equal( index, 2, 'existing - index' );
  193. assert.smartEqual( Array.from( track.times ), [ 5, 10, 15, 20, 25, 30 ], 'existing - time' );
  194. assert.smartEqual( Array.from( track.values ), [ 0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0 ], 'existing - value' );
  195. track = createTrack();
  196. index = GLTFExporter.Utils.insertKeyframe( track, 20.000005 );
  197. assert.equal( index, 3, 'tolerance - index' );
  198. assert.smartEqual( Array.from( track.times ), [ 5, 10, 15, 20, 25, 30 ], 'tolerance - time' );
  199. assert.smartEqual( Array.from( track.values ), [ 0, 5, 1, 4, 2, 3, 3, 2, 4, 1, 5, 0 ], 'tolerance - value' );
  200. } );
  201. QUnit.test( 'utils - mergeMorphTargetTracks', ( assert ) => {
  202. var trackA = new NumberKeyframeTrack(
  203. 'foo.morphTargetInfluences[a]',
  204. [ 5, 10, 15, 20, 25, 30 ],
  205. [ 0, 0.2, 0.4, 0.6, 0.8, 1.0 ],
  206. InterpolateLinear
  207. );
  208. var trackB = new NumberKeyframeTrack(
  209. 'foo.morphTargetInfluences[b]',
  210. [ 10, 50 ],
  211. [ 0.25, 0.75 ],
  212. InterpolateLinear
  213. );
  214. var geometry = new BufferGeometry();
  215. var position = new BufferAttribute( new Float32Array( [ 0, 0, 0, 0, 0, 1, 1, 0, 1 ] ), 3 );
  216. geometry.setAttribute( 'position', position );
  217. geometry.morphAttributes.position = [ position, position ];
  218. var mesh = new Mesh( geometry );
  219. mesh.name = 'foo';
  220. mesh.morphTargetDictionary.a = 0;
  221. mesh.morphTargetDictionary.b = 1;
  222. var root = new Object3D();
  223. root.add( mesh );
  224. var clip = new AnimationClip( 'waltz', undefined, [ trackA, trackB ] );
  225. clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip, root );
  226. assert.equal( clip.tracks.length, 1, 'tracks are merged' );
  227. var track = clip.tracks[ 0 ];
  228. assert.smartEqual( Array.from( track.times ), [ 5, 10, 15, 20, 25, 30, 50 ], 'all keyframes are present' );
  229. var expectedValues = [ 0, 0.25, 0.2, 0.25, 0.4, 0.3125, 0.6, 0.375, 0.8, 0.4375, 1.0, 0.5, 1.0, 0.75 ];
  230. for ( var i = 0; i < track.values.length; i ++ ) {
  231. assert.numEqual( track.values[ i ], expectedValues[ i ], 'all values are merged or interpolated - ' + i );
  232. }
  233. } );
  234. } );
  235. } );