MDDLoader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {
  2. AnimationClip,
  3. BufferAttribute,
  4. FileLoader,
  5. Loader,
  6. NumberKeyframeTrack
  7. } from 'three';
  8. /**
  9. * A loader for the MDD format.
  10. *
  11. * MDD stores a position for every vertex in a model for every frame in an animation.
  12. * Similar to BVH, it can be used to transfer animation data between different 3D applications or engines.
  13. *
  14. * MDD stores its data in binary format (big endian) in the following way:
  15. *
  16. * - number of frames (a single uint32)
  17. * - number of vertices (a single uint32)
  18. * - time values for each frame (sequence of float32)
  19. * - vertex data for each frame (sequence of float32)
  20. *
  21. * ```js
  22. * const loader = new MDDLoader();
  23. * const result = await loader.loadAsync( 'models/mdd/cube.mdd' );
  24. *
  25. * const morphTargets = result.morphTargets;
  26. * const clip = result.clip;
  27. * // clip.optimize(); // optional
  28. *
  29. * const geometry = new THREE.BoxGeometry();
  30. * geometry.morphAttributes.position = morphTargets; // apply morph targets (vertex data must match)
  31. *
  32. * const material = new THREE.MeshBasicMaterial();
  33. *
  34. * const mesh = new THREE.Mesh( geometry, material );
  35. * scene.add( mesh );
  36. *
  37. * const mixer = new THREE.AnimationMixer( mesh );
  38. * mixer.clipAction( clip ).play();
  39. * ```
  40. *
  41. * @augments Loader
  42. */
  43. class MDDLoader extends Loader {
  44. /**
  45. * Constructs a new MDD loader.
  46. *
  47. * @param {LoadingManager} [manager] - The loading manager.
  48. */
  49. constructor( manager ) {
  50. super( manager );
  51. }
  52. /**
  53. * Starts loading from the given URL and passes the loaded MDD asset
  54. * to the `onLoad()` callback.
  55. *
  56. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  57. * @param {function({clip:AnimationClip, morphTargets:Array<BufferAttribute>})} onLoad - Executed when the loading process has been finished.
  58. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  59. * @param {onErrorCallback} onError - Executed when errors occur.
  60. */
  61. load( url, onLoad, onProgress, onError ) {
  62. const scope = this;
  63. const loader = new FileLoader( this.manager );
  64. loader.setPath( this.path );
  65. loader.setResponseType( 'arraybuffer' );
  66. loader.load( url, function ( data ) {
  67. onLoad( scope.parse( data ) );
  68. }, onProgress, onError );
  69. }
  70. /**
  71. * Parses the given MDD data and returns an object holding the animation clip and the respective
  72. * morph targets.
  73. *
  74. * @param {ArrayBuffer} data - The raw XYZ data as an array buffer.
  75. * @return {{clip:AnimationClip, morphTargets:Array<BufferAttribute>}} The result object.
  76. */
  77. parse( data ) {
  78. const view = new DataView( data );
  79. const totalFrames = view.getUint32( 0 );
  80. const totalPoints = view.getUint32( 4 );
  81. let offset = 8;
  82. // animation clip
  83. const times = new Float32Array( totalFrames );
  84. const values = new Float32Array( totalFrames * totalFrames ).fill( 0 );
  85. for ( let i = 0; i < totalFrames; i ++ ) {
  86. times[ i ] = view.getFloat32( offset ); offset += 4;
  87. values[ ( totalFrames * i ) + i ] = 1;
  88. }
  89. const track = new NumberKeyframeTrack( '.morphTargetInfluences', times, values );
  90. const clip = new AnimationClip( 'default', times[ times.length - 1 ], [ track ] );
  91. // morph targets
  92. const morphTargets = [];
  93. for ( let i = 0; i < totalFrames; i ++ ) {
  94. const morphTarget = new Float32Array( totalPoints * 3 );
  95. for ( let j = 0; j < totalPoints; j ++ ) {
  96. const stride = ( j * 3 );
  97. morphTarget[ stride + 0 ] = view.getFloat32( offset ); offset += 4; // x
  98. morphTarget[ stride + 1 ] = view.getFloat32( offset ); offset += 4; // y
  99. morphTarget[ stride + 2 ] = view.getFloat32( offset ); offset += 4; // z
  100. }
  101. const attribute = new BufferAttribute( morphTarget, 3 );
  102. attribute.name = 'morph_' + i;
  103. morphTargets.push( attribute );
  104. }
  105. return {
  106. morphTargets: morphTargets,
  107. clip: clip
  108. };
  109. }
  110. }
  111. export { MDDLoader };
粤ICP备19079148号