TDSLoader.js 25 KB

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