GLTFExporter.tests.js 11 KB

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