TDSLoader.js 24 KB

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