TDSLoader.js 25 KB

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