MDDLoader.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ( function () {
  2. /**
  3. * MDD is a special format that stores a position for every vertex in a model for every frame in an animation.
  4. * Similar to BVH, it can be used to transfer animation data between different 3D applications or engines.
  5. *
  6. * MDD stores its data in binary format (big endian) in the following way:
  7. *
  8. * number of frames (a single uint32)
  9. * number of vertices (a single uint32)
  10. * time values for each frame (sequence of float32)
  11. * vertex data for each frame (sequence of float32)
  12. */
  13. class MDDLoader extends THREE.Loader {
  14. constructor( manager ) {
  15. super( manager );
  16. }
  17. load( url, onLoad, onProgress, onError ) {
  18. const scope = this;
  19. const loader = new THREE.FileLoader( this.manager );
  20. loader.setPath( this.path );
  21. loader.setResponseType( 'arraybuffer' );
  22. loader.load( url, function ( data ) {
  23. onLoad( scope.parse( data ) );
  24. }, onProgress, onError );
  25. }
  26. parse( data ) {
  27. const view = new DataView( data );
  28. const totalFrames = view.getUint32( 0 );
  29. const totalPoints = view.getUint32( 4 );
  30. let offset = 8;
  31. // animation clip
  32. const times = new Float32Array( totalFrames );
  33. const values = new Float32Array( totalFrames * totalFrames ).fill( 0 );
  34. for ( let i = 0; i < totalFrames; i ++ ) {
  35. times[ i ] = view.getFloat32( offset );
  36. offset += 4;
  37. values[ totalFrames * i + i ] = 1;
  38. }
  39. const track = new THREE.NumberKeyframeTrack( '.morphTargetInfluences', times, values );
  40. const clip = new THREE.AnimationClip( 'default', times[ times.length - 1 ], [ track ] );
  41. // morph targets
  42. const morphTargets = [];
  43. for ( let i = 0; i < totalFrames; i ++ ) {
  44. const morphTarget = new Float32Array( totalPoints * 3 );
  45. for ( let j = 0; j < totalPoints; j ++ ) {
  46. const stride = j * 3;
  47. morphTarget[ stride + 0 ] = view.getFloat32( offset );
  48. offset += 4; // x
  49. morphTarget[ stride + 1 ] = view.getFloat32( offset );
  50. offset += 4; // y
  51. morphTarget[ stride + 2 ] = view.getFloat32( offset );
  52. offset += 4; // z
  53. }
  54. const attribute = new THREE.BufferAttribute( morphTarget, 3 );
  55. attribute.name = 'morph_' + i;
  56. morphTargets.push( attribute );
  57. }
  58. return {
  59. morphTargets: morphTargets,
  60. clip: clip
  61. };
  62. }
  63. }
  64. THREE.MDDLoader = MDDLoader;
  65. } )();