LWO2Parser.js 9.9 KB

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