LWO3Parser.js 9.1 KB

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