LWO2Parser.js 10 KB

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