TDSLoader.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. import {
  2. AdditiveBlending,
  3. BufferGeometry,
  4. Color,
  5. DoubleSide,
  6. FileLoader,
  7. Float32BufferAttribute,
  8. Group,
  9. Loader,
  10. LoaderUtils,
  11. Matrix4,
  12. Mesh,
  13. MeshPhongMaterial,
  14. TextureLoader
  15. } from 'three';
  16. /**
  17. * A loader for the 3DS format, based on lib3ds.
  18. *
  19. * Loads geometry with uv and materials basic properties with texture support.
  20. *
  21. * ```js
  22. * const loader = new TDSLoader();
  23. * loader.setResourcePath( 'models/3ds/portalgun/textures/' );
  24. * const object = await loader.loadAsync( 'models/3ds/portalgun/portalgun.3ds' );
  25. * scene.add( object );
  26. *
  27. * @augments Loader
  28. */
  29. class TDSLoader extends Loader {
  30. /**
  31. * Constructs a new 3DS loader.
  32. *
  33. * @param {LoadingManager} [manager] - The loading manager.
  34. */
  35. constructor( manager ) {
  36. super( manager );
  37. /**
  38. * Whether debug mode should be enabled or not.
  39. *
  40. * @type {boolean}
  41. * @default false
  42. */
  43. this.debug = false;
  44. // internals
  45. this.group = null;
  46. this.materials = [];
  47. this.meshes = [];
  48. }
  49. /**
  50. * Starts loading from the given URL and passes the loaded 3DS asset
  51. * to the `onLoad()` callback.
  52. *
  53. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  54. * @param {function(Group)} onLoad - Executed when the loading process has been finished.
  55. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  56. * @param {onErrorCallback} onError - Executed when errors occur.
  57. */
  58. load( url, onLoad, onProgress, onError ) {
  59. const scope = this;
  60. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  61. const loader = new FileLoader( this.manager );
  62. loader.setPath( this.path );
  63. loader.setResponseType( 'arraybuffer' );
  64. loader.setRequestHeader( this.requestHeader );
  65. loader.setWithCredentials( this.withCredentials );
  66. loader.load( url, function ( data ) {
  67. try {
  68. onLoad( scope.parse( data, path ) );
  69. } catch ( e ) {
  70. if ( onError ) {
  71. onError( e );
  72. } else {
  73. console.error( e );
  74. }
  75. scope.manager.itemError( url );
  76. }
  77. }, onProgress, onError );
  78. }
  79. /**
  80. * Parses the given 3DS data and returns the resulting data.
  81. *
  82. * @param {ArrayBuffer} arraybuffer - The raw 3DS data as an array buffer.
  83. * @param {string} path - The asset path.
  84. * @return {Group} The parsed asset represented as a group.
  85. */
  86. parse( arraybuffer, path ) {
  87. this.group = new Group();
  88. this.materials = [];
  89. this.meshes = [];
  90. this.readFile( arraybuffer, path );
  91. for ( let i = 0; i < this.meshes.length; i ++ ) {
  92. this.group.add( this.meshes[ i ] );
  93. }
  94. return this.group;
  95. }
  96. /**
  97. * Decode file content to read 3ds data.
  98. *
  99. * @private
  100. * @param {ArrayBuffer} arraybuffer - Arraybuffer data to be loaded.
  101. * @param {string} path - Path for external resources.
  102. */
  103. readFile( arraybuffer, path ) {
  104. const data = new DataView( arraybuffer );
  105. const chunk = new Chunk( data, 0, this.debugMessage );
  106. if ( chunk.id === MLIBMAGIC || chunk.id === CMAGIC || chunk.id === M3DMAGIC ) {
  107. let next = chunk.readChunk();
  108. while ( next ) {
  109. if ( next.id === M3D_VERSION ) {
  110. const version = next.readDWord();
  111. this.debugMessage( '3DS file version: ' + version );
  112. } else if ( next.id === MDATA ) {
  113. this.readMeshData( next, path );
  114. } else {
  115. this.debugMessage( 'Unknown main chunk: ' + next.hexId );
  116. }
  117. next = chunk.readChunk();
  118. }
  119. }
  120. this.debugMessage( 'Parsed ' + this.meshes.length + ' meshes' );
  121. }
  122. /**
  123. * Read mesh data chunk.
  124. *
  125. * @private
  126. * @param {Chunk} chunk - to read mesh from
  127. * @param {string} path - Path for external resources.
  128. */
  129. readMeshData( chunk, path ) {
  130. let next = chunk.readChunk();
  131. while ( next ) {
  132. if ( next.id === MESH_VERSION ) {
  133. const version = + next.readDWord();
  134. this.debugMessage( 'Mesh Version: ' + version );
  135. } else if ( next.id === MASTER_SCALE ) {
  136. const scale = next.readFloat();
  137. this.debugMessage( 'Master scale: ' + scale );
  138. this.group.scale.set( scale, scale, scale );
  139. } else if ( next.id === NAMED_OBJECT ) {
  140. this.debugMessage( 'Named Object' );
  141. this.readNamedObject( next );
  142. } else if ( next.id === MAT_ENTRY ) {
  143. this.debugMessage( 'Material' );
  144. this.readMaterialEntry( next, path );
  145. } else {
  146. this.debugMessage( 'Unknown MDATA chunk: ' + next.hexId );
  147. }
  148. next = chunk.readChunk();
  149. }
  150. }
  151. /**
  152. * Read named object chunk.
  153. *
  154. * @private
  155. * @param {Chunk} chunk - Chunk in use.
  156. */
  157. readNamedObject( chunk ) {
  158. const name = chunk.readString();
  159. let next = chunk.readChunk();
  160. while ( next ) {
  161. if ( next.id === N_TRI_OBJECT ) {
  162. const mesh = this.readMesh( next );
  163. mesh.name = name;
  164. this.meshes.push( mesh );
  165. } else {
  166. this.debugMessage( 'Unknown named object chunk: ' + next.hexId );
  167. }
  168. next = chunk.readChunk( );
  169. }
  170. }
  171. /**
  172. * Read material data chunk and add it to the material list.
  173. *
  174. * @private
  175. * @param {Chunk} chunk - Chunk in use.
  176. * @param {string} path - Path for external resources.
  177. */
  178. readMaterialEntry( chunk, path ) {
  179. let next = chunk.readChunk();
  180. const material = new MeshPhongMaterial();
  181. while ( next ) {
  182. if ( next.id === MAT_NAME ) {
  183. material.name = next.readString();
  184. this.debugMessage( ' Name: ' + material.name );
  185. } else if ( next.id === MAT_WIRE ) {
  186. this.debugMessage( ' Wireframe' );
  187. material.wireframe = true;
  188. } else if ( next.id === MAT_WIRE_SIZE ) {
  189. const value = next.readByte();
  190. material.wireframeLinewidth = value;
  191. this.debugMessage( ' Wireframe Thickness: ' + value );
  192. } else if ( next.id === MAT_TWO_SIDE ) {
  193. material.side = DoubleSide;
  194. this.debugMessage( ' DoubleSided' );
  195. } else if ( next.id === MAT_ADDITIVE ) {
  196. this.debugMessage( ' Additive Blending' );
  197. material.blending = AdditiveBlending;
  198. } else if ( next.id === MAT_DIFFUSE ) {
  199. this.debugMessage( ' Diffuse Color' );
  200. material.color = this.readColor( next );
  201. } else if ( next.id === MAT_SPECULAR ) {
  202. this.debugMessage( ' Specular Color' );
  203. material.specular = this.readColor( next );
  204. } else if ( next.id === MAT_AMBIENT ) {
  205. this.debugMessage( ' Ambient color' );
  206. material.color = this.readColor( next );
  207. } else if ( next.id === MAT_SHININESS ) {
  208. const shininess = this.readPercentage( next );
  209. material.shininess = shininess * 100;
  210. this.debugMessage( ' Shininess : ' + shininess );
  211. } else if ( next.id === MAT_TRANSPARENCY ) {
  212. const transparency = this.readPercentage( next );
  213. material.opacity = 1 - transparency;
  214. this.debugMessage( ' Transparency : ' + transparency );
  215. material.transparent = material.opacity < 1 ? true : false;
  216. } else if ( next.id === MAT_TEXMAP ) {
  217. this.debugMessage( ' ColorMap' );
  218. material.map = this.readMap( next, path );
  219. } else if ( next.id === MAT_BUMPMAP ) {
  220. this.debugMessage( ' BumpMap' );
  221. material.bumpMap = this.readMap( next, path );
  222. } else if ( next.id === MAT_OPACMAP ) {
  223. this.debugMessage( ' OpacityMap' );
  224. material.alphaMap = this.readMap( next, path );
  225. } else if ( next.id === MAT_SPECMAP ) {
  226. this.debugMessage( ' SpecularMap' );
  227. material.specularMap = this.readMap( next, path );
  228. } else {
  229. this.debugMessage( ' Unknown material chunk: ' + next.hexId );
  230. }
  231. next = chunk.readChunk();
  232. }
  233. this.materials[ material.name ] = material;
  234. }
  235. /**
  236. * Read mesh data chunk.
  237. *
  238. * @private
  239. * @param {Chunk} chunk - Chunk in use.
  240. * @return {Mesh} - The parsed mesh.
  241. */
  242. readMesh( chunk ) {
  243. let next = chunk.readChunk( );
  244. const geometry = new BufferGeometry();
  245. const material = new MeshPhongMaterial();
  246. const mesh = new Mesh( geometry, material );
  247. mesh.name = 'mesh';
  248. while ( next ) {
  249. if ( next.id === POINT_ARRAY ) {
  250. const points = next.readWord( );
  251. this.debugMessage( ' Vertex: ' + points );
  252. //BufferGeometry
  253. const vertices = [];
  254. for ( let i = 0; i < points; i ++ ) {
  255. vertices.push( next.readFloat( ) );
  256. vertices.push( next.readFloat( ) );
  257. vertices.push( next.readFloat( ) );
  258. }
  259. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  260. } else if ( next.id === FACE_ARRAY ) {
  261. this.readFaceArray( next, mesh );
  262. } else if ( next.id === TEX_VERTS ) {
  263. const texels = next.readWord( );
  264. this.debugMessage( ' UV: ' + texels );
  265. //BufferGeometry
  266. const uvs = [];
  267. for ( let i = 0; i < texels; i ++ ) {
  268. uvs.push( next.readFloat( ) );
  269. uvs.push( next.readFloat( ) );
  270. }
  271. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  272. } else if ( next.id === MESH_MATRIX ) {
  273. this.debugMessage( ' Transformation Matrix (TODO)' );
  274. const values = [];
  275. for ( let i = 0; i < 12; i ++ ) {
  276. values[ i ] = next.readFloat( );
  277. }
  278. const matrix = new Matrix4();
  279. //X Line
  280. matrix.elements[ 0 ] = values[ 0 ];
  281. matrix.elements[ 1 ] = values[ 6 ];
  282. matrix.elements[ 2 ] = values[ 3 ];
  283. matrix.elements[ 3 ] = values[ 9 ];
  284. //Y Line
  285. matrix.elements[ 4 ] = values[ 2 ];
  286. matrix.elements[ 5 ] = values[ 8 ];
  287. matrix.elements[ 6 ] = values[ 5 ];
  288. matrix.elements[ 7 ] = values[ 11 ];
  289. //Z Line
  290. matrix.elements[ 8 ] = values[ 1 ];
  291. matrix.elements[ 9 ] = values[ 7 ];
  292. matrix.elements[ 10 ] = values[ 4 ];
  293. matrix.elements[ 11 ] = values[ 10 ];
  294. //W Line
  295. matrix.elements[ 12 ] = 0;
  296. matrix.elements[ 13 ] = 0;
  297. matrix.elements[ 14 ] = 0;
  298. matrix.elements[ 15 ] = 1;
  299. matrix.transpose();
  300. const inverse = new Matrix4();
  301. inverse.copy( matrix ).invert();
  302. geometry.applyMatrix4( inverse );
  303. matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
  304. } else {
  305. this.debugMessage( ' Unknown mesh chunk: ' + next.hexId );
  306. }
  307. next = chunk.readChunk( );
  308. }
  309. geometry.computeVertexNormals();
  310. return mesh;
  311. }
  312. /**
  313. * Read face array data chunk.
  314. *
  315. * @private
  316. * @param {Chunk} chunk - Chunk in use.
  317. * @param {Mesh} mesh - Mesh to be filled with the data read.
  318. */
  319. readFaceArray( chunk, mesh ) {
  320. const faces = chunk.readWord( );
  321. this.debugMessage( ' Faces: ' + faces );
  322. const index = [];
  323. for ( let i = 0; i < faces; ++ i ) {
  324. index.push( chunk.readWord( ), chunk.readWord( ), chunk.readWord( ) );
  325. chunk.readWord( ); // visibility
  326. }
  327. mesh.geometry.setIndex( index );
  328. //The rest of the FACE_ARRAY chunk is subchunks
  329. let materialIndex = 0;
  330. let start = 0;
  331. while ( ! chunk.endOfChunk ) {
  332. const subchunk = chunk.readChunk( );
  333. if ( subchunk.id === MSH_MAT_GROUP ) {
  334. this.debugMessage( ' Material Group' );
  335. const group = this.readMaterialGroup( subchunk );
  336. const count = group.index.length * 3; // assuming successive indices
  337. mesh.geometry.addGroup( start, count, materialIndex );
  338. start += count;
  339. materialIndex ++;
  340. const material = this.materials[ group.name ];
  341. if ( Array.isArray( mesh.material ) === false ) mesh.material = [];
  342. if ( material !== undefined ) {
  343. mesh.material.push( material );
  344. }
  345. } else {
  346. this.debugMessage( ' Unknown face array chunk: ' + subchunk.hexId );
  347. }
  348. }
  349. if ( mesh.material.length === 1 ) mesh.material = mesh.material[ 0 ]; // for backwards compatibility
  350. }
  351. /**
  352. * Read texture map data chunk.
  353. *
  354. * @private
  355. * @param {Chunk} chunk - Chunk in use.
  356. * @param {string} path - Path for external resources.
  357. * @return {Texture} Texture read from this data chunk.
  358. */
  359. readMap( chunk, path ) {
  360. let next = chunk.readChunk( );
  361. let texture = {};
  362. const loader = new TextureLoader( this.manager );
  363. loader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  364. while ( next ) {
  365. if ( next.id === MAT_MAPNAME ) {
  366. const name = next.readString();
  367. texture = loader.load( name );
  368. this.debugMessage( ' File: ' + path + name );
  369. } else if ( next.id === MAT_MAP_UOFFSET ) {
  370. texture.offset.x = next.readFloat( );
  371. this.debugMessage( ' OffsetX: ' + texture.offset.x );
  372. } else if ( next.id === MAT_MAP_VOFFSET ) {
  373. texture.offset.y = next.readFloat( );
  374. this.debugMessage( ' OffsetY: ' + texture.offset.y );
  375. } else if ( next.id === MAT_MAP_USCALE ) {
  376. texture.repeat.x = next.readFloat( );
  377. this.debugMessage( ' RepeatX: ' + texture.repeat.x );
  378. } else if ( next.id === MAT_MAP_VSCALE ) {
  379. texture.repeat.y = next.readFloat( );
  380. this.debugMessage( ' RepeatY: ' + texture.repeat.y );
  381. } else {
  382. this.debugMessage( ' Unknown map chunk: ' + next.hexId );
  383. }
  384. next = chunk.readChunk( );
  385. }
  386. return texture;
  387. }
  388. /**
  389. * Read material group data chunk.
  390. *
  391. * @private
  392. * @param {Chunk} chunk - Chunk in use.
  393. * @return {Object} Object with name and index of the object.
  394. */
  395. readMaterialGroup( chunk ) {
  396. const name = chunk.readString();
  397. const numFaces = chunk.readWord();
  398. this.debugMessage( ' Name: ' + name );
  399. this.debugMessage( ' Faces: ' + numFaces );
  400. const index = [];
  401. for ( let i = 0; i < numFaces; ++ i ) {
  402. index.push( chunk.readWord( ) );
  403. }
  404. return { name: name, index: index };
  405. }
  406. /**
  407. * Read a color value.
  408. *
  409. * @private
  410. * @param {Chunk} chunk - Chunk.
  411. * @return {Color} Color value read.
  412. */
  413. readColor( chunk ) {
  414. const subChunk = chunk.readChunk( );
  415. const color = new Color();
  416. if ( subChunk.id === COLOR_24 || subChunk.id === LIN_COLOR_24 ) {
  417. const r = subChunk.readByte( );
  418. const g = subChunk.readByte( );
  419. const b = subChunk.readByte( );
  420. color.setRGB( r / 255, g / 255, b / 255 );
  421. this.debugMessage( ' Color: ' + color.r + ', ' + color.g + ', ' + color.b );
  422. } else if ( subChunk.id === COLOR_F || subChunk.id === LIN_COLOR_F ) {
  423. const r = subChunk.readFloat( );
  424. const g = subChunk.readFloat( );
  425. const b = subChunk.readFloat( );
  426. color.setRGB( r, g, b );
  427. this.debugMessage( ' Color: ' + color.r + ', ' + color.g + ', ' + color.b );
  428. } else {
  429. this.debugMessage( ' Unknown color chunk: ' + subChunk.hexId );
  430. }
  431. return color;
  432. }
  433. /**
  434. * Read percentage value.
  435. *
  436. * @private
  437. * @param {Chunk} chunk - Chunk to read data from.
  438. * @return {number} Data read from the dataview.
  439. */
  440. readPercentage( chunk ) {
  441. const subChunk = chunk.readChunk( );
  442. switch ( subChunk.id ) {
  443. case INT_PERCENTAGE:
  444. return ( subChunk.readShort( ) / 100 );
  445. break;
  446. case FLOAT_PERCENTAGE:
  447. return subChunk.readFloat( );
  448. break;
  449. default:
  450. this.debugMessage( ' Unknown percentage chunk: ' + subChunk.hexId );
  451. return 0;
  452. }
  453. }
  454. /**
  455. * Print debug message to the console.
  456. *
  457. * Is controlled by a flag to show or hide debug messages.
  458. *
  459. * @private
  460. * @param {Object} message - Debug message to print to the console.
  461. */
  462. debugMessage( message ) {
  463. if ( this.debug ) {
  464. console.log( message );
  465. }
  466. }
  467. }
  468. /**
  469. * Read data/sub-chunks from chunk.
  470. *
  471. * @private
  472. */
  473. class Chunk {
  474. /**
  475. * Create a new chunk
  476. *
  477. * @private
  478. * @param {DataView} data - DataView to read from.
  479. * @param {number} position - In data.
  480. * @param {Function} debugMessage - Logging callback.
  481. */
  482. constructor( data, position, debugMessage ) {
  483. this.data = data;
  484. // the offset to the begin of this chunk
  485. this.offset = position;
  486. // the current reading position
  487. this.position = position;
  488. this.debugMessage = debugMessage;
  489. if ( this.debugMessage instanceof Function ) {
  490. this.debugMessage = function () {};
  491. }
  492. this.id = this.readWord();
  493. this.size = this.readDWord();
  494. this.end = this.offset + this.size;
  495. if ( this.end > data.byteLength ) {
  496. this.debugMessage( 'Bad chunk size for chunk at ' + position );
  497. }
  498. }
  499. /**
  500. * Reads a sub cchunk.
  501. *
  502. * @private
  503. * @return {Chunk | null} next sub chunk.
  504. */
  505. readChunk() {
  506. if ( this.endOfChunk ) {
  507. return null;
  508. }
  509. try {
  510. const next = new Chunk( this.data, this.position, this.debugMessage );
  511. this.position += next.size;
  512. return next;
  513. } catch ( e ) {
  514. this.debugMessage( 'Unable to read chunk at ' + this.position );
  515. return null;
  516. }
  517. }
  518. /**
  519. * Returns the ID of this chunk as Hex
  520. *
  521. * @private
  522. * @return {string} hex-string of id
  523. */
  524. get hexId() {
  525. return this.id.toString( 16 );
  526. }
  527. get endOfChunk() {
  528. return this.position >= this.end;
  529. }
  530. /**
  531. * Read byte value.
  532. *
  533. * @private
  534. * @return {number} Data read from the dataview.
  535. */
  536. readByte() {
  537. const v = this.data.getUint8( this.position, true );
  538. this.position += 1;
  539. return v;
  540. }
  541. /**
  542. * Read 32 bit float value.
  543. *
  544. * @private
  545. * @return {number} Data read from the dataview.
  546. */
  547. readFloat() {
  548. try {
  549. const v = this.data.getFloat32( this.position, true );
  550. this.position += 4;
  551. return v;
  552. } catch ( e ) {
  553. this.debugMessage( e + ' ' + this.position + ' ' + this.data.byteLength );
  554. return 0;
  555. }
  556. }
  557. /**
  558. * Read 32 bit signed integer value.
  559. *
  560. * @private
  561. * @return {number} Data read from the dataview.
  562. */
  563. readInt() {
  564. const v = this.data.getInt32( this.position, true );
  565. this.position += 4;
  566. return v;
  567. }
  568. /**
  569. * Read 16 bit signed integer value.
  570. *
  571. * @private
  572. * @return {number} Data read from the dataview.
  573. */
  574. readShort() {
  575. const v = this.data.getInt16( this.position, true );
  576. this.position += 2;
  577. return v;
  578. }
  579. /**
  580. * Read 64 bit unsigned integer value.
  581. *
  582. * @private
  583. * @return {number} Data read from the dataview.
  584. */
  585. readDWord() {
  586. const v = this.data.getUint32( this.position, true );
  587. this.position += 4;
  588. return v;
  589. }
  590. /**
  591. * Read 32 bit unsigned integer value.
  592. *
  593. * @private
  594. * @return {number} Data read from the dataview.
  595. */
  596. readWord() {
  597. const v = this.data.getUint16( this.position, true );
  598. this.position += 2;
  599. return v;
  600. }
  601. /**
  602. * Read NULL terminated ASCII string value from chunk-pos.
  603. *
  604. * @private
  605. * @return {string} Data read from the dataview.
  606. */
  607. readString() {
  608. let s = '';
  609. let c = this.readByte();
  610. while ( c ) {
  611. s += String.fromCharCode( c );
  612. c = this.readByte();
  613. }
  614. return s;
  615. }
  616. }
  617. // const NULL_CHUNK = 0x0000;
  618. const M3DMAGIC = 0x4D4D;
  619. // const SMAGIC = 0x2D2D;
  620. // const LMAGIC = 0x2D3D;
  621. const MLIBMAGIC = 0x3DAA;
  622. // const MATMAGIC = 0x3DFF;
  623. const CMAGIC = 0xC23D;
  624. const M3D_VERSION = 0x0002;
  625. // const M3D_KFVERSION = 0x0005;
  626. const COLOR_F = 0x0010;
  627. const COLOR_24 = 0x0011;
  628. const LIN_COLOR_24 = 0x0012;
  629. const LIN_COLOR_F = 0x0013;
  630. const INT_PERCENTAGE = 0x0030;
  631. const FLOAT_PERCENTAGE = 0x0031;
  632. const MDATA = 0x3D3D;
  633. const MESH_VERSION = 0x3D3E;
  634. const MASTER_SCALE = 0x0100;
  635. // const LO_SHADOW_BIAS = 0x1400;
  636. // const HI_SHADOW_BIAS = 0x1410;
  637. // const SHADOW_MAP_SIZE = 0x1420;
  638. // const SHADOW_SAMPLES = 0x1430;
  639. // const SHADOW_RANGE = 0x1440;
  640. // const SHADOW_FILTER = 0x1450;
  641. // const RAY_BIAS = 0x1460;
  642. // const O_CONSTS = 0x1500;
  643. // const AMBIENT_LIGHT = 0x2100;
  644. // const BIT_MAP = 0x1100;
  645. // const SOLID_BGND = 0x1200;
  646. // const V_GRADIENT = 0x1300;
  647. // const USE_BIT_MAP = 0x1101;
  648. // const USE_SOLID_BGND = 0x1201;
  649. // const USE_V_GRADIENT = 0x1301;
  650. // const FOG = 0x2200;
  651. // const FOG_BGND = 0x2210;
  652. // const LAYER_FOG = 0x2302;
  653. // const DISTANCE_CUE = 0x2300;
  654. // const DCUE_BGND = 0x2310;
  655. // const USE_FOG = 0x2201;
  656. // const USE_LAYER_FOG = 0x2303;
  657. // const USE_DISTANCE_CUE = 0x2301;
  658. const MAT_ENTRY = 0xAFFF;
  659. const MAT_NAME = 0xA000;
  660. const MAT_AMBIENT = 0xA010;
  661. const MAT_DIFFUSE = 0xA020;
  662. const MAT_SPECULAR = 0xA030;
  663. const MAT_SHININESS = 0xA040;
  664. // const MAT_SHIN2PCT = 0xA041;
  665. const MAT_TRANSPARENCY = 0xA050;
  666. // const MAT_XPFALL = 0xA052;
  667. // const MAT_USE_XPFALL = 0xA240;
  668. // const MAT_REFBLUR = 0xA053;
  669. // const MAT_SHADING = 0xA100;
  670. // const MAT_USE_REFBLUR = 0xA250;
  671. // const MAT_SELF_ILLUM = 0xA084;
  672. const MAT_TWO_SIDE = 0xA081;
  673. // const MAT_DECAL = 0xA082;
  674. const MAT_ADDITIVE = 0xA083;
  675. const MAT_WIRE = 0xA085;
  676. // const MAT_FACEMAP = 0xA088;
  677. // const MAT_TRANSFALLOFF_IN = 0xA08A;
  678. // const MAT_PHONGSOFT = 0xA08C;
  679. // const MAT_WIREABS = 0xA08E;
  680. const MAT_WIRE_SIZE = 0xA087;
  681. const MAT_TEXMAP = 0xA200;
  682. // const MAT_SXP_TEXT_DATA = 0xA320;
  683. // const MAT_TEXMASK = 0xA33E;
  684. // const MAT_SXP_TEXTMASK_DATA = 0xA32A;
  685. // const MAT_TEX2MAP = 0xA33A;
  686. // const MAT_SXP_TEXT2_DATA = 0xA321;
  687. // const MAT_TEX2MASK = 0xA340;
  688. // const MAT_SXP_TEXT2MASK_DATA = 0xA32C;
  689. const MAT_OPACMAP = 0xA210;
  690. // const MAT_SXP_OPAC_DATA = 0xA322;
  691. // const MAT_OPACMASK = 0xA342;
  692. // const MAT_SXP_OPACMASK_DATA = 0xA32E;
  693. const MAT_BUMPMAP = 0xA230;
  694. // const MAT_SXP_BUMP_DATA = 0xA324;
  695. // const MAT_BUMPMASK = 0xA344;
  696. // const MAT_SXP_BUMPMASK_DATA = 0xA330;
  697. const MAT_SPECMAP = 0xA204;
  698. // const MAT_SXP_SPEC_DATA = 0xA325;
  699. // const MAT_SPECMASK = 0xA348;
  700. // const MAT_SXP_SPECMASK_DATA = 0xA332;
  701. // const MAT_SHINMAP = 0xA33C;
  702. // const MAT_SXP_SHIN_DATA = 0xA326;
  703. // const MAT_SHINMASK = 0xA346;
  704. // const MAT_SXP_SHINMASK_DATA = 0xA334;
  705. // const MAT_SELFIMAP = 0xA33D;
  706. // const MAT_SXP_SELFI_DATA = 0xA328;
  707. // const MAT_SELFIMASK = 0xA34A;
  708. // const MAT_SXP_SELFIMASK_DATA = 0xA336;
  709. // const MAT_REFLMAP = 0xA220;
  710. // const MAT_REFLMASK = 0xA34C;
  711. // const MAT_SXP_REFLMASK_DATA = 0xA338;
  712. // const MAT_ACUBIC = 0xA310;
  713. const MAT_MAPNAME = 0xA300;
  714. // const MAT_MAP_TILING = 0xA351;
  715. // const MAT_MAP_TEXBLUR = 0xA353;
  716. const MAT_MAP_USCALE = 0xA354;
  717. const MAT_MAP_VSCALE = 0xA356;
  718. const MAT_MAP_UOFFSET = 0xA358;
  719. const MAT_MAP_VOFFSET = 0xA35A;
  720. // const MAT_MAP_ANG = 0xA35C;
  721. // const MAT_MAP_COL1 = 0xA360;
  722. // const MAT_MAP_COL2 = 0xA362;
  723. // const MAT_MAP_RCOL = 0xA364;
  724. // const MAT_MAP_GCOL = 0xA366;
  725. // const MAT_MAP_BCOL = 0xA368;
  726. const NAMED_OBJECT = 0x4000;
  727. // const N_DIRECT_LIGHT = 0x4600;
  728. // const DL_OFF = 0x4620;
  729. // const DL_OUTER_RANGE = 0x465A;
  730. // const DL_INNER_RANGE = 0x4659;
  731. // const DL_MULTIPLIER = 0x465B;
  732. // const DL_EXCLUDE = 0x4654;
  733. // const DL_ATTENUATE = 0x4625;
  734. // const DL_SPOTLIGHT = 0x4610;
  735. // const DL_SPOT_ROLL = 0x4656;
  736. // const DL_SHADOWED = 0x4630;
  737. // const DL_LOCAL_SHADOW2 = 0x4641;
  738. // const DL_SEE_CONE = 0x4650;
  739. // const DL_SPOT_RECTANGULAR = 0x4651;
  740. // const DL_SPOT_ASPECT = 0x4657;
  741. // const DL_SPOT_PROJECTOR = 0x4653;
  742. // const DL_SPOT_OVERSHOOT = 0x4652;
  743. // const DL_RAY_BIAS = 0x4658;
  744. // const DL_RAYSHAD = 0x4627;
  745. // const N_CAMERA = 0x4700;
  746. // const CAM_SEE_CONE = 0x4710;
  747. // const CAM_RANGES = 0x4720;
  748. // const OBJ_HIDDEN = 0x4010;
  749. // const OBJ_VIS_LOFTER = 0x4011;
  750. // const OBJ_DOESNT_CAST = 0x4012;
  751. // const OBJ_DONT_RECVSHADOW = 0x4017;
  752. // const OBJ_MATTE = 0x4013;
  753. // const OBJ_FAST = 0x4014;
  754. // const OBJ_PROCEDURAL = 0x4015;
  755. // const OBJ_FROZEN = 0x4016;
  756. const N_TRI_OBJECT = 0x4100;
  757. const POINT_ARRAY = 0x4110;
  758. // const POINT_FLAG_ARRAY = 0x4111;
  759. const FACE_ARRAY = 0x4120;
  760. const MSH_MAT_GROUP = 0x4130;
  761. // const SMOOTH_GROUP = 0x4150;
  762. // const MSH_BOXMAP = 0x4190;
  763. const TEX_VERTS = 0x4140;
  764. const MESH_MATRIX = 0x4160;
  765. // const MESH_COLOR = 0x4165;
  766. // const MESH_TEXTURE_INFO = 0x4170;
  767. // const KFDATA = 0xB000;
  768. // const KFHDR = 0xB00A;
  769. // const KFSEG = 0xB008;
  770. // const KFCURTIME = 0xB009;
  771. // const AMBIENT_NODE_TAG = 0xB001;
  772. // const OBJECT_NODE_TAG = 0xB002;
  773. // const CAMERA_NODE_TAG = 0xB003;
  774. // const TARGET_NODE_TAG = 0xB004;
  775. // const LIGHT_NODE_TAG = 0xB005;
  776. // const L_TARGET_NODE_TAG = 0xB006;
  777. // const SPOTLIGHT_NODE_TAG = 0xB007;
  778. // const NODE_ID = 0xB030;
  779. // const NODE_HDR = 0xB010;
  780. // const PIVOT = 0xB013;
  781. // const INSTANCE_NAME = 0xB011;
  782. // const MORPH_SMOOTH = 0xB015;
  783. // const BOUNDBOX = 0xB014;
  784. // const POS_TRACK_TAG = 0xB020;
  785. // const COL_TRACK_TAG = 0xB025;
  786. // const ROT_TRACK_TAG = 0xB021;
  787. // const SCL_TRACK_TAG = 0xB022;
  788. // const MORPH_TRACK_TAG = 0xB026;
  789. // const FOV_TRACK_TAG = 0xB023;
  790. // const ROLL_TRACK_TAG = 0xB024;
  791. // const HOT_TRACK_TAG = 0xB027;
  792. // const FALL_TRACK_TAG = 0xB028;
  793. // const HIDE_TRACK_TAG = 0xB029;
  794. // const POLY_2D = 0x5000;
  795. // const SHAPE_OK = 0x5010;
  796. // const SHAPE_NOT_OK = 0x5011;
  797. // const SHAPE_HOOK = 0x5020;
  798. // const PATH_3D = 0x6000;
  799. // const PATH_MATRIX = 0x6005;
  800. // const SHAPE_2D = 0x6010;
  801. // const M_SCALE = 0x6020;
  802. // const M_TWIST = 0x6030;
  803. // const M_TEETER = 0x6040;
  804. // const M_FIT = 0x6050;
  805. // const M_BEVEL = 0x6060;
  806. // const XZ_CURVE = 0x6070;
  807. // const YZ_CURVE = 0x6080;
  808. // const INTERPCT = 0x6090;
  809. // const DEFORM_LIMIT = 0x60A0;
  810. // const USE_CONTOUR = 0x6100;
  811. // const USE_TWEEN = 0x6110;
  812. // const USE_SCALE = 0x6120;
  813. // const USE_TWIST = 0x6130;
  814. // const USE_TEETER = 0x6140;
  815. // const USE_FIT = 0x6150;
  816. // const USE_BEVEL = 0x6160;
  817. // const DEFAULT_VIEW = 0x3000;
  818. // const VIEW_TOP = 0x3010;
  819. // const VIEW_BOTTOM = 0x3020;
  820. // const VIEW_LEFT = 0x3030;
  821. // const VIEW_RIGHT = 0x3040;
  822. // const VIEW_FRONT = 0x3050;
  823. // const VIEW_BACK = 0x3060;
  824. // const VIEW_USER = 0x3070;
  825. // const VIEW_CAMERA = 0x3080;
  826. // const VIEW_WINDOW = 0x3090;
  827. // const VIEWPORT_LAYOUT_OLD = 0x7000;
  828. // const VIEWPORT_DATA_OLD = 0x7010;
  829. // const VIEWPORT_LAYOUT = 0x7001;
  830. // const VIEWPORT_DATA = 0x7011;
  831. // const VIEWPORT_DATA_3 = 0x7012;
  832. // const VIEWPORT_SIZE = 0x7020;
  833. // const NETWORK_VIEW = 0x7030;
  834. export { TDSLoader };
粤ICP备19079148号