LWO2Parser.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. ( function () {
  2. class LWO2Parser {
  3. constructor( IFFParser ) {
  4. this.IFF = IFFParser;
  5. }
  6. parseBlock() {
  7. this.IFF.debugger.offset = this.IFF.reader.offset;
  8. this.IFF.debugger.closeForms();
  9. const blockID = this.IFF.reader.getIDTag();
  10. let length = this.IFF.reader.getUint32(); // size of data in bytes
  11. if ( length > this.IFF.reader.dv.byteLength - this.IFF.reader.offset ) {
  12. this.IFF.reader.offset -= 4;
  13. length = this.IFF.reader.getUint16();
  14. }
  15. this.IFF.debugger.dataOffset = this.IFF.reader.offset;
  16. this.IFF.debugger.length = length;
  17. // Data types may be found in either LWO2 OR LWO3 spec
  18. switch ( blockID ) {
  19. case 'FORM':
  20. // form blocks may consist of sub -chunks or sub-forms
  21. this.IFF.parseForm( length );
  22. break;
  23. // SKIPPED CHUNKS
  24. // if break; is called directly, the position in the lwoTree is not created
  25. // any sub chunks and forms are added to the parent form instead
  26. // MISC skipped
  27. case 'ICON': // Thumbnail Icon Image
  28. case 'VMPA': // Vertex Map Parameter
  29. case 'BBOX': // bounding box
  30. // case 'VMMD':
  31. // case 'VTYP':
  32. // normal maps can be specified, normally on models imported from other applications. Currently ignored
  33. case 'NORM':
  34. // ENVL FORM skipped
  35. case 'PRE ':
  36. case 'POST':
  37. case 'KEY ':
  38. case 'SPAN':
  39. // CLIP FORM skipped
  40. case 'TIME':
  41. case 'CLRS':
  42. case 'CLRA':
  43. case 'FILT':
  44. case 'DITH':
  45. case 'CONT':
  46. case 'BRIT':
  47. case 'SATR':
  48. case 'HUE ':
  49. case 'GAMM':
  50. case 'NEGA':
  51. case 'IFLT':
  52. case 'PFLT':
  53. // Image Map Layer skipped
  54. case 'PROJ':
  55. case 'AXIS':
  56. case 'AAST':
  57. case 'PIXB':
  58. case 'AUVO':
  59. case 'STCK':
  60. // Procedural Textures skipped
  61. case 'PROC':
  62. case 'VALU':
  63. case 'FUNC':
  64. // Gradient Textures skipped
  65. case 'PNAM':
  66. case 'INAM':
  67. case 'GRST':
  68. case 'GREN':
  69. case 'GRPT':
  70. case 'FKEY':
  71. case 'IKEY':
  72. // Texture Mapping Form skipped
  73. case 'CSYS':
  74. // Surface CHUNKs skipped
  75. case 'OPAQ': // top level 'opacity' checkbox
  76. case 'CMAP': // clip map
  77. // Surface node CHUNKS skipped
  78. // These mainly specify the node editor setup in LW
  79. case 'NLOC':
  80. case 'NZOM':
  81. case 'NVER':
  82. case 'NSRV':
  83. case 'NVSK': // unknown
  84. case 'NCRD':
  85. case 'WRPW': // image wrap w ( for cylindrical and spherical projections)
  86. case 'WRPH': // image wrap h
  87. case 'NMOD':
  88. case 'NSEL':
  89. case 'NPRW':
  90. case 'NPLA':
  91. case 'NODS':
  92. case 'VERS':
  93. case 'ENUM':
  94. case 'TAG ':
  95. case 'OPAC':
  96. // Car Material CHUNKS
  97. case 'CGMD':
  98. case 'CGTY':
  99. case 'CGST':
  100. case 'CGEN':
  101. case 'CGTS':
  102. case 'CGTE':
  103. case 'OSMP':
  104. case 'OMDE':
  105. case 'OUTR':
  106. case 'FLAG':
  107. case 'TRNL':
  108. case 'GLOW':
  109. case 'GVAL': // glow intensity
  110. case 'SHRP':
  111. case 'RFOP':
  112. case 'RSAN':
  113. case 'TROP':
  114. case 'RBLR':
  115. case 'TBLR':
  116. case 'CLRH':
  117. case 'CLRF':
  118. case 'ADTR':
  119. case 'LINE':
  120. case 'ALPH':
  121. case 'VCOL':
  122. case 'ENAB':
  123. this.IFF.debugger.skipped = true;
  124. this.IFF.reader.skip( length );
  125. break;
  126. case 'SURF':
  127. this.IFF.parseSurfaceLwo2( length );
  128. break;
  129. case 'CLIP':
  130. this.IFF.parseClipLwo2( length );
  131. break;
  132. // Texture node chunks (not in spec)
  133. case 'IPIX': // usePixelBlending
  134. case 'IMIP': // useMipMaps
  135. case 'IMOD': // imageBlendingMode
  136. case 'AMOD': // unknown
  137. case 'IINV': // imageInvertAlpha
  138. case 'INCR': // imageInvertColor
  139. case 'IAXS': // imageAxis ( for non-UV maps)
  140. case 'IFOT': // imageFallofType
  141. case 'ITIM': // timing for animated textures
  142. case 'IWRL':
  143. case 'IUTI':
  144. case 'IINX':
  145. case 'IINY':
  146. case 'IINZ':
  147. case 'IREF':
  148. // possibly a VX for reused texture nodes
  149. if ( length === 4 ) this.IFF.currentNode[ blockID ] = this.IFF.reader.getInt32(); else this.IFF.reader.skip( length );
  150. break;
  151. case 'OTAG':
  152. this.IFF.parseObjectTag();
  153. break;
  154. case 'LAYR':
  155. this.IFF.parseLayer( length );
  156. break;
  157. case 'PNTS':
  158. this.IFF.parsePoints( length );
  159. break;
  160. case 'VMAP':
  161. this.IFF.parseVertexMapping( length );
  162. break;
  163. case 'AUVU':
  164. case 'AUVN':
  165. this.IFF.reader.skip( length - 1 );
  166. this.IFF.reader.getVariableLengthIndex(); // VX
  167. break;
  168. case 'POLS':
  169. this.IFF.parsePolygonList( length );
  170. break;
  171. case 'TAGS':
  172. this.IFF.parseTagStrings( length );
  173. break;
  174. case 'PTAG':
  175. this.IFF.parsePolygonTagMapping( length );
  176. break;
  177. case 'VMAD':
  178. this.IFF.parseVertexMapping( length, true );
  179. break;
  180. // Misc CHUNKS
  181. case 'DESC':
  182. // Description Line
  183. this.IFF.currentForm.description = this.IFF.reader.getString();
  184. break;
  185. case 'TEXT':
  186. case 'CMNT':
  187. case 'NCOM':
  188. this.IFF.currentForm.comment = this.IFF.reader.getString();
  189. break;
  190. // Envelope Form
  191. case 'NAME':
  192. this.IFF.currentForm.channelName = this.IFF.reader.getString();
  193. break;
  194. // Image Map Layer
  195. case 'WRAP':
  196. this.IFF.currentForm.wrap = {
  197. w: this.IFF.reader.getUint16(),
  198. h: this.IFF.reader.getUint16()
  199. };
  200. break;
  201. case 'IMAG':
  202. const index = this.IFF.reader.getVariableLengthIndex();
  203. this.IFF.currentForm.imageIndex = index;
  204. break;
  205. // Texture Mapping Form
  206. case 'OREF':
  207. this.IFF.currentForm.referenceObject = this.IFF.reader.getString();
  208. break;
  209. case 'ROID':
  210. this.IFF.currentForm.referenceObjectID = this.IFF.reader.getUint32();
  211. break;
  212. // Surface Blocks
  213. case 'SSHN':
  214. this.IFF.currentSurface.surfaceShaderName = this.IFF.reader.getString();
  215. break;
  216. case 'AOVN':
  217. this.IFF.currentSurface.surfaceCustomAOVName = this.IFF.reader.getString();
  218. break;
  219. // Nodal Blocks
  220. case 'NSTA':
  221. this.IFF.currentForm.disabled = this.IFF.reader.getUint16();
  222. break;
  223. case 'NRNM':
  224. this.IFF.currentForm.realName = this.IFF.reader.getString();
  225. break;
  226. case 'NNME':
  227. this.IFF.currentForm.refName = this.IFF.reader.getString();
  228. this.IFF.currentSurface.nodes[ this.IFF.currentForm.refName ] = this.IFF.currentForm;
  229. break;
  230. // Nodal Blocks : connections
  231. case 'INME':
  232. if ( ! this.IFF.currentForm.nodeName ) this.IFF.currentForm.nodeName = [];
  233. this.IFF.currentForm.nodeName.push( this.IFF.reader.getString() );
  234. break;
  235. case 'IINN':
  236. if ( ! this.IFF.currentForm.inputNodeName ) this.IFF.currentForm.inputNodeName = [];
  237. this.IFF.currentForm.inputNodeName.push( this.IFF.reader.getString() );
  238. break;
  239. case 'IINM':
  240. if ( ! this.IFF.currentForm.inputName ) this.IFF.currentForm.inputName = [];
  241. this.IFF.currentForm.inputName.push( this.IFF.reader.getString() );
  242. break;
  243. case 'IONM':
  244. if ( ! this.IFF.currentForm.inputOutputName ) this.IFF.currentForm.inputOutputName = [];
  245. this.IFF.currentForm.inputOutputName.push( this.IFF.reader.getString() );
  246. break;
  247. case 'FNAM':
  248. this.IFF.currentForm.fileName = this.IFF.reader.getString();
  249. break;
  250. case 'CHAN':
  251. // NOTE: ENVL Forms may also have CHAN chunk, however ENVL is currently ignored
  252. if ( length === 4 ) this.IFF.currentForm.textureChannel = this.IFF.reader.getIDTag(); else this.IFF.reader.skip( length );
  253. break;
  254. // LWO2 Spec chunks: these are needed since the SURF FORMs are often in LWO2 format
  255. case 'SMAN':
  256. const maxSmoothingAngle = this.IFF.reader.getFloat32();
  257. this.IFF.currentSurface.attributes.smooth = maxSmoothingAngle < 0 ? false : true;
  258. break;
  259. // LWO2: Basic Surface Parameters
  260. case 'COLR':
  261. this.IFF.currentSurface.attributes.Color = {
  262. value: this.IFF.reader.getFloat32Array( 3 )
  263. };
  264. this.IFF.reader.skip( 2 ); // VX: envelope
  265. break;
  266. case 'LUMI':
  267. this.IFF.currentSurface.attributes.Luminosity = {
  268. value: this.IFF.reader.getFloat32()
  269. };
  270. this.IFF.reader.skip( 2 );
  271. break;
  272. case 'SPEC':
  273. this.IFF.currentSurface.attributes.Specular = {
  274. value: this.IFF.reader.getFloat32()
  275. };
  276. this.IFF.reader.skip( 2 );
  277. break;
  278. case 'DIFF':
  279. this.IFF.currentSurface.attributes.Diffuse = {
  280. value: this.IFF.reader.getFloat32()
  281. };
  282. this.IFF.reader.skip( 2 );
  283. break;
  284. case 'REFL':
  285. this.IFF.currentSurface.attributes.Reflection = {
  286. value: this.IFF.reader.getFloat32()
  287. };
  288. this.IFF.reader.skip( 2 );
  289. break;
  290. case 'GLOS':
  291. this.IFF.currentSurface.attributes.Glossiness = {
  292. value: this.IFF.reader.getFloat32()
  293. };
  294. this.IFF.reader.skip( 2 );
  295. break;
  296. case 'TRAN':
  297. this.IFF.currentSurface.attributes.opacity = this.IFF.reader.getFloat32();
  298. this.IFF.reader.skip( 2 );
  299. break;
  300. case 'BUMP':
  301. this.IFF.currentSurface.attributes.bumpStrength = this.IFF.reader.getFloat32();
  302. this.IFF.reader.skip( 2 );
  303. break;
  304. case 'SIDE':
  305. this.IFF.currentSurface.attributes.side = this.IFF.reader.getUint16();
  306. break;
  307. case 'RIMG':
  308. this.IFF.currentSurface.attributes.reflectionMap = this.IFF.reader.getVariableLengthIndex();
  309. break;
  310. case 'RIND':
  311. this.IFF.currentSurface.attributes.refractiveIndex = this.IFF.reader.getFloat32();
  312. this.IFF.reader.skip( 2 );
  313. break;
  314. case 'TIMG':
  315. this.IFF.currentSurface.attributes.refractionMap = this.IFF.reader.getVariableLengthIndex();
  316. break;
  317. case 'IMAP':
  318. this.IFF.reader.skip( 2 );
  319. break;
  320. case 'TMAP':
  321. this.IFF.debugger.skipped = true;
  322. this.IFF.reader.skip( length ); // needs implementing
  323. break;
  324. case 'IUVI':
  325. // uv channel name
  326. this.IFF.currentNode.UVChannel = this.IFF.reader.getString( length );
  327. break;
  328. case 'IUTL':
  329. // widthWrappingMode: 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
  330. this.IFF.currentNode.widthWrappingMode = this.IFF.reader.getUint32();
  331. break;
  332. case 'IVTL':
  333. // heightWrappingMode
  334. this.IFF.currentNode.heightWrappingMode = this.IFF.reader.getUint32();
  335. break;
  336. // LWO2 USE
  337. case 'BLOK':
  338. // skip
  339. break;
  340. default:
  341. this.IFF.parseUnknownCHUNK( blockID, length );
  342. }
  343. if ( blockID != 'FORM' ) {
  344. this.IFF.debugger.node = 1;
  345. this.IFF.debugger.nodeID = blockID;
  346. this.IFF.debugger.log();
  347. }
  348. if ( this.IFF.reader.offset >= this.IFF.currentFormEnd ) {
  349. this.IFF.currentForm = this.IFF.parentForm;
  350. }
  351. }
  352. }
  353. THREE.LWO2Parser = LWO2Parser;
  354. } )();