GLTFExporter.tests.js 11 KB

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