LWO2Parser.js 10 KB

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