LWO2Parser.js 9.9 KB

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