MD2Loader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import {
  2. AnimationClip,
  3. BufferGeometry,
  4. FileLoader,
  5. Float32BufferAttribute,
  6. Loader,
  7. Vector3
  8. } from 'three';
  9. const _normalData = [
  10. [ - 0.525731, 0.000000, 0.850651 ], [ - 0.442863, 0.238856, 0.864188 ],
  11. [ - 0.295242, 0.000000, 0.955423 ], [ - 0.309017, 0.500000, 0.809017 ],
  12. [ - 0.162460, 0.262866, 0.951056 ], [ 0.000000, 0.000000, 1.000000 ],
  13. [ 0.000000, 0.850651, 0.525731 ], [ - 0.147621, 0.716567, 0.681718 ],
  14. [ 0.147621, 0.716567, 0.681718 ], [ 0.000000, 0.525731, 0.850651 ],
  15. [ 0.309017, 0.500000, 0.809017 ], [ 0.525731, 0.000000, 0.850651 ],
  16. [ 0.295242, 0.000000, 0.955423 ], [ 0.442863, 0.238856, 0.864188 ],
  17. [ 0.162460, 0.262866, 0.951056 ], [ - 0.681718, 0.147621, 0.716567 ],
  18. [ - 0.809017, 0.309017, 0.500000 ], [ - 0.587785, 0.425325, 0.688191 ],
  19. [ - 0.850651, 0.525731, 0.000000 ], [ - 0.864188, 0.442863, 0.238856 ],
  20. [ - 0.716567, 0.681718, 0.147621 ], [ - 0.688191, 0.587785, 0.425325 ],
  21. [ - 0.500000, 0.809017, 0.309017 ], [ - 0.238856, 0.864188, 0.442863 ],
  22. [ - 0.425325, 0.688191, 0.587785 ], [ - 0.716567, 0.681718, - 0.147621 ],
  23. [ - 0.500000, 0.809017, - 0.309017 ], [ - 0.525731, 0.850651, 0.000000 ],
  24. [ 0.000000, 0.850651, - 0.525731 ], [ - 0.238856, 0.864188, - 0.442863 ],
  25. [ 0.000000, 0.955423, - 0.295242 ], [ - 0.262866, 0.951056, - 0.162460 ],
  26. [ 0.000000, 1.000000, 0.000000 ], [ 0.000000, 0.955423, 0.295242 ],
  27. [ - 0.262866, 0.951056, 0.162460 ], [ 0.238856, 0.864188, 0.442863 ],
  28. [ 0.262866, 0.951056, 0.162460 ], [ 0.500000, 0.809017, 0.309017 ],
  29. [ 0.238856, 0.864188, - 0.442863 ], [ 0.262866, 0.951056, - 0.162460 ],
  30. [ 0.500000, 0.809017, - 0.309017 ], [ 0.850651, 0.525731, 0.000000 ],
  31. [ 0.716567, 0.681718, 0.147621 ], [ 0.716567, 0.681718, - 0.147621 ],
  32. [ 0.525731, 0.850651, 0.000000 ], [ 0.425325, 0.688191, 0.587785 ],
  33. [ 0.864188, 0.442863, 0.238856 ], [ 0.688191, 0.587785, 0.425325 ],
  34. [ 0.809017, 0.309017, 0.500000 ], [ 0.681718, 0.147621, 0.716567 ],
  35. [ 0.587785, 0.425325, 0.688191 ], [ 0.955423, 0.295242, 0.000000 ],
  36. [ 1.000000, 0.000000, 0.000000 ], [ 0.951056, 0.162460, 0.262866 ],
  37. [ 0.850651, - 0.525731, 0.000000 ], [ 0.955423, - 0.295242, 0.000000 ],
  38. [ 0.864188, - 0.442863, 0.238856 ], [ 0.951056, - 0.162460, 0.262866 ],
  39. [ 0.809017, - 0.309017, 0.500000 ], [ 0.681718, - 0.147621, 0.716567 ],
  40. [ 0.850651, 0.000000, 0.525731 ], [ 0.864188, 0.442863, - 0.238856 ],
  41. [ 0.809017, 0.309017, - 0.500000 ], [ 0.951056, 0.162460, - 0.262866 ],
  42. [ 0.525731, 0.000000, - 0.850651 ], [ 0.681718, 0.147621, - 0.716567 ],
  43. [ 0.681718, - 0.147621, - 0.716567 ], [ 0.850651, 0.000000, - 0.525731 ],
  44. [ 0.809017, - 0.309017, - 0.500000 ], [ 0.864188, - 0.442863, - 0.238856 ],
  45. [ 0.951056, - 0.162460, - 0.262866 ], [ 0.147621, 0.716567, - 0.681718 ],
  46. [ 0.309017, 0.500000, - 0.809017 ], [ 0.425325, 0.688191, - 0.587785 ],
  47. [ 0.442863, 0.238856, - 0.864188 ], [ 0.587785, 0.425325, - 0.688191 ],
  48. [ 0.688191, 0.587785, - 0.425325 ], [ - 0.147621, 0.716567, - 0.681718 ],
  49. [ - 0.309017, 0.500000, - 0.809017 ], [ 0.000000, 0.525731, - 0.850651 ],
  50. [ - 0.525731, 0.000000, - 0.850651 ], [ - 0.442863, 0.238856, - 0.864188 ],
  51. [ - 0.295242, 0.000000, - 0.955423 ], [ - 0.162460, 0.262866, - 0.951056 ],
  52. [ 0.000000, 0.000000, - 1.000000 ], [ 0.295242, 0.000000, - 0.955423 ],
  53. [ 0.162460, 0.262866, - 0.951056 ], [ - 0.442863, - 0.238856, - 0.864188 ],
  54. [ - 0.309017, - 0.500000, - 0.809017 ], [ - 0.162460, - 0.262866, - 0.951056 ],
  55. [ 0.000000, - 0.850651, - 0.525731 ], [ - 0.147621, - 0.716567, - 0.681718 ],
  56. [ 0.147621, - 0.716567, - 0.681718 ], [ 0.000000, - 0.525731, - 0.850651 ],
  57. [ 0.309017, - 0.500000, - 0.809017 ], [ 0.442863, - 0.238856, - 0.864188 ],
  58. [ 0.162460, - 0.262866, - 0.951056 ], [ 0.238856, - 0.864188, - 0.442863 ],
  59. [ 0.500000, - 0.809017, - 0.309017 ], [ 0.425325, - 0.688191, - 0.587785 ],
  60. [ 0.716567, - 0.681718, - 0.147621 ], [ 0.688191, - 0.587785, - 0.425325 ],
  61. [ 0.587785, - 0.425325, - 0.688191 ], [ 0.000000, - 0.955423, - 0.295242 ],
  62. [ 0.000000, - 1.000000, 0.000000 ], [ 0.262866, - 0.951056, - 0.162460 ],
  63. [ 0.000000, - 0.850651, 0.525731 ], [ 0.000000, - 0.955423, 0.295242 ],
  64. [ 0.238856, - 0.864188, 0.442863 ], [ 0.262866, - 0.951056, 0.162460 ],
  65. [ 0.500000, - 0.809017, 0.309017 ], [ 0.716567, - 0.681718, 0.147621 ],
  66. [ 0.525731, - 0.850651, 0.000000 ], [ - 0.238856, - 0.864188, - 0.442863 ],
  67. [ - 0.500000, - 0.809017, - 0.309017 ], [ - 0.262866, - 0.951056, - 0.162460 ],
  68. [ - 0.850651, - 0.525731, 0.000000 ], [ - 0.716567, - 0.681718, - 0.147621 ],
  69. [ - 0.716567, - 0.681718, 0.147621 ], [ - 0.525731, - 0.850651, 0.000000 ],
  70. [ - 0.500000, - 0.809017, 0.309017 ], [ - 0.238856, - 0.864188, 0.442863 ],
  71. [ - 0.262866, - 0.951056, 0.162460 ], [ - 0.864188, - 0.442863, 0.238856 ],
  72. [ - 0.809017, - 0.309017, 0.500000 ], [ - 0.688191, - 0.587785, 0.425325 ],
  73. [ - 0.681718, - 0.147621, 0.716567 ], [ - 0.442863, - 0.238856, 0.864188 ],
  74. [ - 0.587785, - 0.425325, 0.688191 ], [ - 0.309017, - 0.500000, 0.809017 ],
  75. [ - 0.147621, - 0.716567, 0.681718 ], [ - 0.425325, - 0.688191, 0.587785 ],
  76. [ - 0.162460, - 0.262866, 0.951056 ], [ 0.442863, - 0.238856, 0.864188 ],
  77. [ 0.162460, - 0.262866, 0.951056 ], [ 0.309017, - 0.500000, 0.809017 ],
  78. [ 0.147621, - 0.716567, 0.681718 ], [ 0.000000, - 0.525731, 0.850651 ],
  79. [ 0.425325, - 0.688191, 0.587785 ], [ 0.587785, - 0.425325, 0.688191 ],
  80. [ 0.688191, - 0.587785, 0.425325 ], [ - 0.955423, 0.295242, 0.000000 ],
  81. [ - 0.951056, 0.162460, 0.262866 ], [ - 1.000000, 0.000000, 0.000000 ],
  82. [ - 0.850651, 0.000000, 0.525731 ], [ - 0.955423, - 0.295242, 0.000000 ],
  83. [ - 0.951056, - 0.162460, 0.262866 ], [ - 0.864188, 0.442863, - 0.238856 ],
  84. [ - 0.951056, 0.162460, - 0.262866 ], [ - 0.809017, 0.309017, - 0.500000 ],
  85. [ - 0.864188, - 0.442863, - 0.238856 ], [ - 0.951056, - 0.162460, - 0.262866 ],
  86. [ - 0.809017, - 0.309017, - 0.500000 ], [ - 0.681718, 0.147621, - 0.716567 ],
  87. [ - 0.681718, - 0.147621, - 0.716567 ], [ - 0.850651, 0.000000, - 0.525731 ],
  88. [ - 0.688191, 0.587785, - 0.425325 ], [ - 0.587785, 0.425325, - 0.688191 ],
  89. [ - 0.425325, 0.688191, - 0.587785 ], [ - 0.425325, - 0.688191, - 0.587785 ],
  90. [ - 0.587785, - 0.425325, - 0.688191 ], [ - 0.688191, - 0.587785, - 0.425325 ]
  91. ];
  92. class MD2Loader extends Loader {
  93. constructor( manager ) {
  94. super( manager );
  95. }
  96. load( url, onLoad, onProgress, onError ) {
  97. const scope = this;
  98. const loader = new FileLoader( scope.manager );
  99. loader.setPath( scope.path );
  100. loader.setResponseType( 'arraybuffer' );
  101. loader.setRequestHeader( scope.requestHeader );
  102. loader.setWithCredentials( scope.withCredentials );
  103. loader.load( url, function ( buffer ) {
  104. try {
  105. onLoad( scope.parse( buffer ) );
  106. } catch ( e ) {
  107. if ( onError ) {
  108. onError( e );
  109. } else {
  110. console.error( e );
  111. }
  112. scope.manager.itemError( url );
  113. }
  114. }, onProgress, onError );
  115. }
  116. parse( buffer ) {
  117. const data = new DataView( buffer );
  118. // http://tfc.duke.free.fr/coding/md2-specs-en.html
  119. const header = {};
  120. const headerNames = [
  121. 'ident', 'version',
  122. 'skinwidth', 'skinheight',
  123. 'framesize',
  124. 'num_skins', 'num_vertices', 'num_st', 'num_tris', 'num_glcmds', 'num_frames',
  125. 'offset_skins', 'offset_st', 'offset_tris', 'offset_frames', 'offset_glcmds', 'offset_end'
  126. ];
  127. for ( let i = 0; i < headerNames.length; i ++ ) {
  128. header[ headerNames[ i ] ] = data.getInt32( i * 4, true );
  129. }
  130. if ( header.ident !== 844121161 || header.version !== 8 ) {
  131. console.error( 'Not a valid MD2 file' );
  132. return;
  133. }
  134. if ( header.offset_end !== data.byteLength ) {
  135. console.error( 'Corrupted MD2 file' );
  136. return;
  137. }
  138. //
  139. const geometry = new BufferGeometry();
  140. // uvs
  141. const uvsTemp = [];
  142. let offset = header.offset_st;
  143. for ( let i = 0, l = header.num_st; i < l; i ++ ) {
  144. const u = data.getInt16( offset + 0, true );
  145. const v = data.getInt16( offset + 2, true );
  146. uvsTemp.push( u / header.skinwidth, 1 - ( v / header.skinheight ) );
  147. offset += 4;
  148. }
  149. // triangles
  150. offset = header.offset_tris;
  151. const vertexIndices = [];
  152. const uvIndices = [];
  153. for ( let i = 0, l = header.num_tris; i < l; i ++ ) {
  154. vertexIndices.push(
  155. data.getUint16( offset + 0, true ),
  156. data.getUint16( offset + 2, true ),
  157. data.getUint16( offset + 4, true )
  158. );
  159. uvIndices.push(
  160. data.getUint16( offset + 6, true ),
  161. data.getUint16( offset + 8, true ),
  162. data.getUint16( offset + 10, true )
  163. );
  164. offset += 12;
  165. }
  166. // frames
  167. const translation = new Vector3();
  168. const scale = new Vector3();
  169. const frames = [];
  170. offset = header.offset_frames;
  171. for ( let i = 0, l = header.num_frames; i < l; i ++ ) {
  172. scale.set(
  173. data.getFloat32( offset + 0, true ),
  174. data.getFloat32( offset + 4, true ),
  175. data.getFloat32( offset + 8, true )
  176. );
  177. translation.set(
  178. data.getFloat32( offset + 12, true ),
  179. data.getFloat32( offset + 16, true ),
  180. data.getFloat32( offset + 20, true )
  181. );
  182. offset += 24;
  183. const string = [];
  184. for ( let j = 0; j < 16; j ++ ) {
  185. const character = data.getUint8( offset + j );
  186. if ( character === 0 ) break;
  187. string[ j ] = character;
  188. }
  189. const frame = {
  190. name: String.fromCharCode.apply( null, string ),
  191. vertices: [],
  192. normals: []
  193. };
  194. offset += 16;
  195. for ( let j = 0; j < header.num_vertices; j ++ ) {
  196. let x = data.getUint8( offset ++ );
  197. let y = data.getUint8( offset ++ );
  198. let z = data.getUint8( offset ++ );
  199. const n = _normalData[ data.getUint8( offset ++ ) ];
  200. x = x * scale.x + translation.x;
  201. y = y * scale.y + translation.y;
  202. z = z * scale.z + translation.z;
  203. frame.vertices.push( x, z, y ); // convert to Y-up
  204. frame.normals.push( n[ 0 ], n[ 2 ], n[ 1 ] ); // convert to Y-up
  205. }
  206. frames.push( frame );
  207. }
  208. // static
  209. const positions = [];
  210. const normals = [];
  211. const uvs = [];
  212. const verticesTemp = frames[ 0 ].vertices;
  213. const normalsTemp = frames[ 0 ].normals;
  214. for ( let i = 0, l = vertexIndices.length; i < l; i ++ ) {
  215. const vertexIndex = vertexIndices[ i ];
  216. let stride = vertexIndex * 3;
  217. //
  218. const x = verticesTemp[ stride ];
  219. const y = verticesTemp[ stride + 1 ];
  220. const z = verticesTemp[ stride + 2 ];
  221. positions.push( x, y, z );
  222. //
  223. const nx = normalsTemp[ stride ];
  224. const ny = normalsTemp[ stride + 1 ];
  225. const nz = normalsTemp[ stride + 2 ];
  226. normals.push( nx, ny, nz );
  227. //
  228. const uvIndex = uvIndices[ i ];
  229. stride = uvIndex * 2;
  230. const u = uvsTemp[ stride ];
  231. const v = uvsTemp[ stride + 1 ];
  232. uvs.push( u, v );
  233. }
  234. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  235. geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  236. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  237. // animation
  238. const morphPositions = [];
  239. const morphNormals = [];
  240. for ( let i = 0, l = frames.length; i < l; i ++ ) {
  241. const frame = frames[ i ];
  242. const attributeName = frame.name;
  243. if ( frame.vertices.length > 0 ) {
  244. const positions = [];
  245. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  246. const vertexIndex = vertexIndices[ j ];
  247. const stride = vertexIndex * 3;
  248. const x = frame.vertices[ stride ];
  249. const y = frame.vertices[ stride + 1 ];
  250. const z = frame.vertices[ stride + 2 ];
  251. positions.push( x, y, z );
  252. }
  253. const positionAttribute = new Float32BufferAttribute( positions, 3 );
  254. positionAttribute.name = attributeName;
  255. morphPositions.push( positionAttribute );
  256. }
  257. if ( frame.normals.length > 0 ) {
  258. const normals = [];
  259. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  260. const vertexIndex = vertexIndices[ j ];
  261. const stride = vertexIndex * 3;
  262. const nx = frame.normals[ stride ];
  263. const ny = frame.normals[ stride + 1 ];
  264. const nz = frame.normals[ stride + 2 ];
  265. normals.push( nx, ny, nz );
  266. }
  267. const normalAttribute = new Float32BufferAttribute( normals, 3 );
  268. normalAttribute.name = attributeName;
  269. morphNormals.push( normalAttribute );
  270. }
  271. }
  272. geometry.morphAttributes.position = morphPositions;
  273. geometry.morphAttributes.normal = morphNormals;
  274. geometry.morphTargetsRelative = false;
  275. geometry.animations = AnimationClip.CreateClipsFromMorphTargetSequences( frames, 10 );
  276. return geometry;
  277. }
  278. }
  279. export { MD2Loader };