LWO2Parser.js 10 KB

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