MD2Loader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /**
  93. * A loader for the MD2 format.
  94. *
  95. * The loader represents the animations of the MD2 asset as an array of animation
  96. * clips and stores them in the `animations` property of the geometry.
  97. *
  98. * ```js
  99. * const loader = new MD2Loader();
  100. * const geometry = await loader.loadAsync( './models/md2/ogro/ogro.md2' );
  101. *
  102. * const animations = geometry.animations;
  103. * ```
  104. *
  105. * @augments Loader
  106. * @three_import import { MD2Loader } from 'three/addons/loaders/MD2Loader.js';
  107. */
  108. class MD2Loader extends Loader {
  109. /**
  110. * Constructs a new MD2 loader.
  111. *
  112. * @param {LoadingManager} [manager] - The loading manager.
  113. */
  114. constructor( manager ) {
  115. super( manager );
  116. }
  117. /**
  118. * Starts loading from the given URL and passes the loaded MD2 asset
  119. * to the `onLoad()` callback.
  120. *
  121. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  122. * @param {function(BufferGeometry)} onLoad - Executed when the loading process has been finished.
  123. * @param {onProgressCallback} [onProgress] - Executed while the loading is in progress.
  124. * @param {onErrorCallback} [onError] - Executed when errors occur.
  125. */
  126. load( url, onLoad, onProgress, onError ) {
  127. const scope = this;
  128. const loader = new FileLoader( scope.manager );
  129. loader.setPath( scope.path );
  130. loader.setResponseType( 'arraybuffer' );
  131. loader.setRequestHeader( scope.requestHeader );
  132. loader.setWithCredentials( scope.withCredentials );
  133. loader.load( url, function ( buffer ) {
  134. try {
  135. onLoad( scope.parse( buffer ) );
  136. } catch ( e ) {
  137. if ( onError ) {
  138. onError( e );
  139. } else {
  140. console.error( e );
  141. }
  142. scope.manager.itemError( url );
  143. }
  144. }, onProgress, onError );
  145. }
  146. /**
  147. * Parses the given MD2 data and returns a geometry.
  148. *
  149. * @param {ArrayBuffer} buffer - The raw MD2 data as an array buffer.
  150. * @return {BufferGeometry} The parsed geometry data.
  151. */
  152. parse( buffer ) {
  153. const data = new DataView( buffer );
  154. // http://tfc.duke.free.fr/coding/md2-specs-en.html
  155. const header = {};
  156. const headerNames = [
  157. 'ident', 'version',
  158. 'skinwidth', 'skinheight',
  159. 'framesize',
  160. 'num_skins', 'num_vertices', 'num_st', 'num_tris', 'num_glcmds', 'num_frames',
  161. 'offset_skins', 'offset_st', 'offset_tris', 'offset_frames', 'offset_glcmds', 'offset_end'
  162. ];
  163. for ( let i = 0; i < headerNames.length; i ++ ) {
  164. header[ headerNames[ i ] ] = data.getInt32( i * 4, true );
  165. }
  166. if ( header.ident !== 844121161 || header.version !== 8 ) {
  167. console.error( 'Not a valid MD2 file' );
  168. return;
  169. }
  170. if ( header.offset_end !== data.byteLength ) {
  171. console.error( 'Corrupted MD2 file' );
  172. return;
  173. }
  174. //
  175. const geometry = new BufferGeometry();
  176. // uvs
  177. const uvsTemp = [];
  178. let offset = header.offset_st;
  179. for ( let i = 0, l = header.num_st; i < l; i ++ ) {
  180. const u = data.getInt16( offset + 0, true );
  181. const v = data.getInt16( offset + 2, true );
  182. uvsTemp.push( u / header.skinwidth, 1 - ( v / header.skinheight ) );
  183. offset += 4;
  184. }
  185. // triangles
  186. offset = header.offset_tris;
  187. const vertexIndices = [];
  188. const uvIndices = [];
  189. for ( let i = 0, l = header.num_tris; i < l; i ++ ) {
  190. vertexIndices.push(
  191. data.getUint16( offset + 0, true ),
  192. data.getUint16( offset + 2, true ),
  193. data.getUint16( offset + 4, true )
  194. );
  195. uvIndices.push(
  196. data.getUint16( offset + 6, true ),
  197. data.getUint16( offset + 8, true ),
  198. data.getUint16( offset + 10, true )
  199. );
  200. offset += 12;
  201. }
  202. // frames
  203. const translation = new Vector3();
  204. const scale = new Vector3();
  205. const frames = [];
  206. offset = header.offset_frames;
  207. for ( let i = 0, l = header.num_frames; i < l; i ++ ) {
  208. scale.set(
  209. data.getFloat32( offset + 0, true ),
  210. data.getFloat32( offset + 4, true ),
  211. data.getFloat32( offset + 8, true )
  212. );
  213. translation.set(
  214. data.getFloat32( offset + 12, true ),
  215. data.getFloat32( offset + 16, true ),
  216. data.getFloat32( offset + 20, true )
  217. );
  218. offset += 24;
  219. const string = [];
  220. for ( let j = 0; j < 16; j ++ ) {
  221. const character = data.getUint8( offset + j );
  222. if ( character === 0 ) break;
  223. string[ j ] = character;
  224. }
  225. const frame = {
  226. name: String.fromCharCode.apply( null, string ),
  227. vertices: [],
  228. normals: []
  229. };
  230. offset += 16;
  231. for ( let j = 0; j < header.num_vertices; j ++ ) {
  232. let x = data.getUint8( offset ++ );
  233. let y = data.getUint8( offset ++ );
  234. let z = data.getUint8( offset ++ );
  235. const n = _normalData[ data.getUint8( offset ++ ) ];
  236. x = x * scale.x + translation.x;
  237. y = y * scale.y + translation.y;
  238. z = z * scale.z + translation.z;
  239. frame.vertices.push( x, z, y ); // convert to Y-up
  240. frame.normals.push( n[ 0 ], n[ 2 ], n[ 1 ] ); // convert to Y-up
  241. }
  242. frames.push( frame );
  243. }
  244. // static
  245. const positions = [];
  246. const normals = [];
  247. const uvs = [];
  248. const verticesTemp = frames[ 0 ].vertices;
  249. const normalsTemp = frames[ 0 ].normals;
  250. for ( let i = 0, l = vertexIndices.length; i < l; i ++ ) {
  251. const vertexIndex = vertexIndices[ i ];
  252. let stride = vertexIndex * 3;
  253. //
  254. const x = verticesTemp[ stride ];
  255. const y = verticesTemp[ stride + 1 ];
  256. const z = verticesTemp[ stride + 2 ];
  257. positions.push( x, y, z );
  258. //
  259. const nx = normalsTemp[ stride ];
  260. const ny = normalsTemp[ stride + 1 ];
  261. const nz = normalsTemp[ stride + 2 ];
  262. normals.push( nx, ny, nz );
  263. //
  264. const uvIndex = uvIndices[ i ];
  265. stride = uvIndex * 2;
  266. const u = uvsTemp[ stride ];
  267. const v = uvsTemp[ stride + 1 ];
  268. uvs.push( u, v );
  269. }
  270. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  271. geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  272. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  273. // animation
  274. const morphPositions = [];
  275. const morphNormals = [];
  276. for ( let i = 0, l = frames.length; i < l; i ++ ) {
  277. const frame = frames[ i ];
  278. const attributeName = frame.name;
  279. if ( frame.vertices.length > 0 ) {
  280. const positions = [];
  281. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  282. const vertexIndex = vertexIndices[ j ];
  283. const stride = vertexIndex * 3;
  284. const x = frame.vertices[ stride ];
  285. const y = frame.vertices[ stride + 1 ];
  286. const z = frame.vertices[ stride + 2 ];
  287. positions.push( x, y, z );
  288. }
  289. const positionAttribute = new Float32BufferAttribute( positions, 3 );
  290. positionAttribute.name = attributeName;
  291. morphPositions.push( positionAttribute );
  292. }
  293. if ( frame.normals.length > 0 ) {
  294. const normals = [];
  295. for ( let j = 0, jl = vertexIndices.length; j < jl; j ++ ) {
  296. const vertexIndex = vertexIndices[ j ];
  297. const stride = vertexIndex * 3;
  298. const nx = frame.normals[ stride ];
  299. const ny = frame.normals[ stride + 1 ];
  300. const nz = frame.normals[ stride + 2 ];
  301. normals.push( nx, ny, nz );
  302. }
  303. const normalAttribute = new Float32BufferAttribute( normals, 3 );
  304. normalAttribute.name = attributeName;
  305. morphNormals.push( normalAttribute );
  306. }
  307. }
  308. geometry.morphAttributes.position = morphPositions;
  309. geometry.morphAttributes.normal = morphNormals;
  310. geometry.morphTargetsRelative = false;
  311. geometry.animations = AnimationClip.CreateClipsFromMorphTargetSequences( frames, 10, false );
  312. return geometry;
  313. }
  314. }
  315. export { MD2Loader };