MD2Loader.js 13 KB

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