MD2Loader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ( function () {
  2. const _normalData = [[ - 0.525731, 0.000000, 0.850651 ], [ - 0.442863, 0.238856, 0.864188 ], [ - 0.295242, 0.000000, 0.955423 ], [ - 0.309017, 0.500000, 0.809017 ], [ - 0.162460, 0.262866, 0.951056 ], [ 0.000000, 0.000000, 1.000000 ], [ 0.000000, 0.850651, 0.525731 ], [ - 0.147621, 0.716567, 0.681718 ], [ 0.147621, 0.716567, 0.681718 ], [ 0.000000, 0.525731, 0.850651 ], [ 0.309017, 0.500000, 0.809017 ], [ 0.525731, 0.000000, 0.850651 ], [ 0.295242, 0.000000, 0.955423 ], [ 0.442863, 0.238856, 0.864188 ], [ 0.162460, 0.262866, 0.951056 ], [ - 0.681718, 0.147621, 0.716567 ], [ - 0.809017, 0.309017, 0.500000 ], [ - 0.587785, 0.425325, 0.688191 ], [ - 0.850651, 0.525731, 0.000000 ], [ - 0.864188, 0.442863, 0.238856 ], [ - 0.716567, 0.681718, 0.147621 ], [ - 0.688191, 0.587785, 0.425325 ], [ - 0.500000, 0.809017, 0.309017 ], [ - 0.238856, 0.864188, 0.442863 ], [ - 0.425325, 0.688191, 0.587785 ], [ - 0.716567, 0.681718, - 0.147621 ], [ - 0.500000, 0.809017, - 0.309017 ], [ - 0.525731, 0.850651, 0.000000 ], [ 0.000000, 0.850651, - 0.525731 ], [ - 0.238856, 0.864188, - 0.442863 ], [ 0.000000, 0.955423, - 0.295242 ], [ - 0.262866, 0.951056, - 0.162460 ], [ 0.000000, 1.000000, 0.000000 ], [ 0.000000, 0.955423, 0.295242 ], [ - 0.262866, 0.951056, 0.162460 ], [ 0.238856, 0.864188, 0.442863 ], [ 0.262866, 0.951056, 0.162460 ], [ 0.500000, 0.809017, 0.309017 ], [ 0.238856, 0.864188, - 0.442863 ], [ 0.262866, 0.951056, - 0.162460 ], [ 0.500000, 0.809017, - 0.309017 ], [ 0.850651, 0.525731, 0.000000 ], [ 0.716567, 0.681718, 0.147621 ], [ 0.716567, 0.681718, - 0.147621 ], [ 0.525731, 0.850651, 0.000000 ], [ 0.425325, 0.688191, 0.587785 ], [ 0.864188, 0.442863, 0.238856 ], [ 0.688191, 0.587785, 0.425325 ], [ 0.809017, 0.309017, 0.500000 ], [ 0.681718, 0.147621, 0.716567 ], [ 0.587785, 0.425325, 0.688191 ], [ 0.955423, 0.295242, 0.000000 ], [ 1.000000, 0.000000, 0.000000 ], [ 0.951056, 0.162460, 0.262866 ], [ 0.850651, - 0.525731, 0.000000 ], [ 0.955423, - 0.295242, 0.000000 ], [ 0.864188, - 0.442863, 0.238856 ], [ 0.951056, - 0.162460, 0.262866 ], [ 0.809017, - 0.309017, 0.500000 ], [ 0.681718, - 0.147621, 0.716567 ], [ 0.850651, 0.000000, 0.525731 ], [ 0.864188, 0.442863, - 0.238856 ], [ 0.809017, 0.309017, - 0.500000 ], [ 0.951056, 0.162460, - 0.262866 ], [ 0.525731, 0.000000, - 0.850651 ], [ 0.681718, 0.147621, - 0.716567 ], [ 0.681718, - 0.147621, - 0.716567 ], [ 0.850651, 0.000000, - 0.525731 ], [ 0.809017, - 0.309017, - 0.500000 ], [ 0.864188, - 0.442863, - 0.238856 ], [ 0.951056, - 0.162460, - 0.262866 ], [ 0.147621, 0.716567, - 0.681718 ], [ 0.309017, 0.500000, - 0.809017 ], [ 0.425325, 0.688191, - 0.587785 ], [ 0.442863, 0.238856, - 0.864188 ], [ 0.587785, 0.425325, - 0.688191 ], [ 0.688191, 0.587785, - 0.425325 ], [ - 0.147621, 0.716567, - 0.681718 ], [ - 0.309017, 0.500000, - 0.809017 ], [ 0.000000, 0.525731, - 0.850651 ], [ - 0.525731, 0.000000, - 0.850651 ], [ - 0.442863, 0.238856, - 0.864188 ], [ - 0.295242, 0.000000, - 0.955423 ], [ - 0.162460, 0.262866, - 0.951056 ], [ 0.000000, 0.000000, - 1.000000 ], [ 0.295242, 0.000000, - 0.955423 ], [ 0.162460, 0.262866, - 0.951056 ], [ - 0.442863, - 0.238856, - 0.864188 ], [ - 0.309017, - 0.500000, - 0.809017 ], [ - 0.162460, - 0.262866, - 0.951056 ], [ 0.000000, - 0.850651, - 0.525731 ], [ - 0.147621, - 0.716567, - 0.681718 ], [ 0.147621, - 0.716567, - 0.681718 ], [ 0.000000, - 0.525731, - 0.850651 ], [ 0.309017, - 0.500000, - 0.809017 ], [ 0.442863, - 0.238856, - 0.864188 ], [ 0.162460, - 0.262866, - 0.951056 ], [ 0.238856, - 0.864188, - 0.442863 ], [ 0.500000, - 0.809017, - 0.309017 ], [ 0.425325, - 0.688191, - 0.587785 ], [ 0.716567, - 0.681718, - 0.147621 ], [ 0.688191, - 0.587785, - 0.425325 ], [ 0.587785, - 0.425325, - 0.688191 ], [ 0.000000, - 0.955423, - 0.295242 ], [ 0.000000, - 1.000000, 0.000000 ], [ 0.262866, - 0.951056, - 0.162460 ], [ 0.000000, - 0.850651, 0.525731 ], [ 0.000000, - 0.955423, 0.295242 ], [ 0.238856, - 0.864188, 0.442863 ], [ 0.262866, - 0.951056, 0.162460 ], [ 0.500000, - 0.809017, 0.309017 ], [ 0.716567, - 0.681718, 0.147621 ], [ 0.525731, - 0.850651, 0.000000 ], [ - 0.238856, - 0.864188, - 0.442863 ], [ - 0.500000, - 0.809017, - 0.309017 ], [ - 0.262866, - 0.951056, - 0.162460 ], [ - 0.850651, - 0.525731, 0.000000 ], [ - 0.716567, - 0.681718, - 0.147621 ], [ - 0.716567, - 0.681718, 0.147621 ], [ - 0.525731, - 0.850651, 0.000000 ], [ - 0.500000, - 0.809017, 0.309017 ], [ - 0.238856, - 0.864188, 0.442863 ], [ - 0.262866, - 0.951056, 0.162460 ], [ - 0.864188, - 0.442863, 0.238856 ], [ - 0.809017, - 0.309017, 0.500000 ], [ - 0.688191, - 0.587785, 0.425325 ], [ - 0.681718, - 0.147621, 0.716567 ], [ - 0.442863, - 0.238856, 0.864188 ], [ - 0.587785, - 0.425325, 0.688191 ], [ - 0.309017, - 0.500000, 0.809017 ], [ - 0.147621, - 0.716567, 0.681718 ], [ - 0.425325, - 0.688191, 0.587785 ], [ - 0.162460, - 0.262866, 0.951056 ], [ 0.442863, - 0.238856, 0.864188 ], [ 0.162460, - 0.262866, 0.951056 ], [ 0.309017, - 0.500000, 0.809017 ], [ 0.147621, - 0.716567, 0.681718 ], [ 0.000000, - 0.525731, 0.850651 ], [ 0.425325, - 0.688191, 0.587785 ], [ 0.587785, - 0.425325, 0.688191 ], [ 0.688191, - 0.587785, 0.425325 ], [ - 0.955423, 0.295242, 0.000000 ], [ - 0.951056, 0.162460, 0.262866 ], [ - 1.000000, 0.000000, 0.000000 ], [ - 0.850651, 0.000000, 0.525731 ], [ - 0.955423, - 0.295242, 0.000000 ], [ - 0.951056, - 0.162460, 0.262866 ], [ - 0.864188, 0.442863, - 0.238856 ], [ - 0.951056, 0.162460, - 0.262866 ], [ - 0.809017, 0.309017, - 0.500000 ], [ - 0.864188, - 0.442863, - 0.238856 ], [ - 0.951056, - 0.162460, - 0.262866 ], [ - 0.809017, - 0.309017, - 0.500000 ], [ - 0.681718, 0.147621, - 0.716567 ], [ - 0.681718, - 0.147621, - 0.716567 ], [ - 0.850651, 0.000000, - 0.525731 ], [ - 0.688191, 0.587785, - 0.425325 ], [ - 0.587785, 0.425325, - 0.688191 ], [ - 0.425325, 0.688191, - 0.587785 ], [ - 0.425325, - 0.688191, - 0.587785 ], [ - 0.587785, - 0.425325, - 0.688191 ], [ - 0.688191, - 0.587785, - 0.425325 ]];
  3. class MD2Loader extends THREE.Loader {
  4. constructor( manager ) {
  5. super( manager );
  6. }
  7. load( url, onLoad, onProgress, onError ) {
  8. const scope = this;
  9. const loader = new THREE.FileLoader( scope.manager );
  10. loader.setPath( scope.path );
  11. loader.setResponseType( 'arraybuffer' );
  12. loader.setRequestHeader( scope.requestHeader );
  13. loader.setWithCredentials( scope.withCredentials );
  14. loader.load( url, function ( buffer ) {
  15. try {
  16. onLoad( scope.parse( buffer ) );
  17. } catch ( e ) {
  18. if ( onError ) {
  19. onError( e );
  20. } else {
  21. console.error( e );
  22. }
  23. scope.manager.itemError( url );
  24. }
  25. }, onProgress, onError );
  26. }
  27. parse( buffer ) {
  28. const data = new DataView( buffer ); // http://tfc.duke.free.fr/coding/md2-specs-en.html
  29. const header = {};
  30. const headerNames = [ 'ident', 'version', 'skinwidth', 'skinheight', 'framesize', 'num_skins', 'num_vertices', 'num_st', 'num_tris', 'num_glcmds', 'num_frames', 'offset_skins', 'offset_st', 'offset_tris', 'offset_frames', 'offset_glcmds', 'offset_end' ];
  31. for ( let i = 0; i < headerNames.length; i ++ ) {
  32. header[ headerNames[ i ] ] = data.getInt32( i * 4, true );
  33. }
  34. if ( header.ident !== 844121161 || header.version !== 8 ) {
  35. console.error( 'Not a valid MD2 file' );
  36. return;
  37. }
  38. if ( header.offset_end !== data.byteLength ) {
  39. console.error( 'Corrupted MD2 file' );
  40. return;
  41. } //
  42. const geometry = new THREE.BufferGeometry(); // uvs
  43. const uvsTemp = [];
  44. let offset = header.offset_st;
  45. for ( let i = 0, l = header.num_st; i < l; i ++ ) {
  46. const u = data.getInt16( offset + 0, true );
  47. const v = data.getInt16( offset + 2, true );
  48. uvsTemp.push( u / header.skinwidth, 1 - v / header.skinheight );
  49. offset += 4;
  50. } // triangles
  51. offset = header.offset_tris;
  52. const vertexIndices = [];
  53. const uvIndices = [];
  54. for ( let i = 0, l = header.num_tris; i < l; i ++ ) {
  55. vertexIndices.push( data.getUint16( offset + 0, true ), data.getUint16( offset + 2, true ), data.getUint16( offset + 4, true ) );
  56. uvIndices.push( data.getUint16( offset + 6, true ), data.getUint16( offset + 8, true ), data.getUint16( offset + 10, true ) );
  57. offset += 12;
  58. } // frames
  59. const translation = new THREE.Vector3();
  60. const scale = new THREE.Vector3();
  61. const frames = [];
  62. offset = header.offset_frames;
  63. for ( let i = 0, l = header.num_frames; i < l; i ++ ) {
  64. scale.set( data.getFloat32( offset + 0, true ), data.getFloat32( offset + 4, true ), data.getFloat32( offset + 8, true ) );
  65. translation.set( data.getFloat32( offset + 12, true ), data.getFloat32( offset + 16, true ), data.getFloat32( offset + 20, true ) );
  66. offset += 24;
  67. const string = [];
  68. for ( let j = 0; j < 16; j ++ ) {
  69. const character = data.getUint8( offset + j, true );
  70. if ( character === 0 ) break;
  71. string[ j ] = character;
  72. }
  73. const frame = {
  74. name: String.fromCharCode.apply( null, string ),
  75. vertices: [],
  76. normals: []
  77. };
  78. offset += 16;
  79. for ( let j = 0; j < header.num_vertices; j ++ ) {
  80. let x = data.getUint8( offset ++, true );
  81. let y = data.getUint8( offset ++, true );
  82. let z = data.getUint8( offset ++, true );
  83. const n = _normalData[ data.getUint8( offset ++, true ) ];
  84. x = x * scale.x + translation.x;
  85. y = y * scale.y + translation.y;
  86. z = z * scale.z + translation.z;
  87. frame.vertices.push( x, z, y ); // convert to Y-up
  88. frame.normals.push( n[ 0 ], n[ 2 ], n[ 1 ] ); // convert to Y-up
  89. }
  90. frames.push( frame );
  91. } // static
  92. const positions = [];
  93. const normals = [];
  94. const uvs = [];
  95. const verticesTemp = frames[ 0 ].vertices;
  96. const normalsTemp = frames[ 0 ].normals;
  97. for ( let i = 0, l = vertexIndices.length; i < l; i ++ ) {
  98. const vertexIndex = vertexIndices[ i ];
  99. let stride = vertexIndex * 3; //
  100. const x = verticesTemp[ stride ];
  101. const y = verticesTemp[ stride + 1 ];
  102. const z = verticesTemp[ stride + 2 ];
  103. positions.push( x, y, z ); //
  104. const nx = normalsTemp[ stride ];
  105. const ny = normalsTemp[ stride + 1 ];
  106. const nz = normalsTemp[ stride + 2 ];
  107. normals.push( nx, ny, nz ); //
  108. const uvIndex = uvIndices[ i ];
  109. stride = uvIndex * 2;
  110. const u = uvsTemp[ stride ];
  111. const v = uvsTemp[ stride + 1 ];
  112. uvs.push( u, v );
  113. }
  114. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  115. geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  116. geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) ); // animation
  117. const morphPositions = [];
  118. const morphNormals = [];
  119. for ( let i = 0, l = frames.length; i < l; i ++ ) {
  120. const frame = frames[ i ];
  121. const attributeName = frame.name;
  122. if ( frame.vertices.length > 0 ) {
  123. const positions = [];
  124. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  125. const vertexIndex = vertexIndices[ j ];
  126. const stride = vertexIndex * 3;
  127. const x = frame.vertices[ stride ];
  128. const y = frame.vertices[ stride + 1 ];
  129. const z = frame.vertices[ stride + 2 ];
  130. positions.push( x, y, z );
  131. }
  132. const positionAttribute = new THREE.Float32BufferAttribute( positions, 3 );
  133. positionAttribute.name = attributeName;
  134. morphPositions.push( positionAttribute );
  135. }
  136. if ( frame.normals.length > 0 ) {
  137. const normals = [];
  138. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  139. const vertexIndex = vertexIndices[ j ];
  140. const stride = vertexIndex * 3;
  141. const nx = frame.normals[ stride ];
  142. const ny = frame.normals[ stride + 1 ];
  143. const nz = frame.normals[ stride + 2 ];
  144. normals.push( nx, ny, nz );
  145. }
  146. const normalAttribute = new THREE.Float32BufferAttribute( normals, 3 );
  147. normalAttribute.name = attributeName;
  148. morphNormals.push( normalAttribute );
  149. }
  150. }
  151. geometry.morphAttributes.position = morphPositions;
  152. geometry.morphAttributes.normal = morphNormals;
  153. geometry.morphTargetsRelative = false;
  154. geometry.animations = THREE.AnimationClip.CreateClipsFromMorphTargetSequences( frames, 10 );
  155. return geometry;
  156. }
  157. }
  158. THREE.MD2Loader = MD2Loader;
  159. } )();