home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 12648 / SWTOR.7z / SWTOR.ms
Encoding:
Text File  |  2017-03-17  |  9.4 KB  |  381 lines

  1. if (heapSize < 20000000) then
  2.     heapSize = 200000000
  3. fname = getOpenFileName \
  4. caption:"Open .gr2 from Mesh folder" \
  5. types:"SWTOR (*.gr2)|*.gr2" \
  6. historyCategory:"SWTORObjectPresets"
  7. f = fopen fname "rb"
  8.  
  9.  
  10. fn readHalfFloat fstream = (
  11.     hf=readshort fstream #unsigned
  12.     sign = bit.get hf 16
  13.     exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
  14.     fraction = bit.and hf (bit.hexasint "03FF")
  15.     if sign==true then sign = 1 else sign = 0
  16.     exponentF = exponent + 127
  17.     outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
  18.     (bit.shift exponentF 23)) (bit.shift sign 31)
  19.     return bit.intasfloat outputasfloat*2
  20.     )
  21.  
  22. fn ReadFixedString bstream fixedLen =
  23. (
  24.     local str = ""
  25.     for i = 1 to fixedLen do
  26.     (
  27.         str += bit.intAsChar (ReadByte bstream #unsigned)
  28.     )
  29.     str
  30. )    
  31.  
  32. clearlistener()
  33.  
  34. struct weight_data
  35. (
  36.    boneids,weights
  37. )
  38. struct mesh_info_struct
  39. (
  40. BCount,FaceCount,VertCount,VertPos,FacePos,BonePos,VSize
  41. )
  42.  
  43. clearlistener()
  44.  
  45. Mesh_InfoArray=#()
  46.  
  47. fseek f 0x8 #seek_set
  48. Pos=ftell f
  49. fseek f 0x14 #seek_set
  50. unk_1=readlong f
  51. MeshCount=readshort f
  52. TexCount=readshort f
  53. fseek f 0x34 #seek_cur
  54. UnkOff=readlong f
  55. MeshInfoOff=readlong f
  56. NamesOff=readlong f
  57. fseek f 0x14 #seek_cur
  58. MeshNameOff=readlong f
  59. fseek f (Pos+MeshInfoOff) #seek_set
  60. Print ("Mesh Info @ 0x"+((bit.intAsHex(ftell f))as string))    
  61. for i=1 to MeshCount do (
  62. meshcount2=readshort f
  63. BCount=readshort f
  64. unk=readshort f    
  65. VSize=readshort f
  66. VertCount=readlong f
  67. FaceCount=readlong f
  68. VertPos=readlong f    
  69. unk1=readlong f
  70. FacePos=readlong f
  71. BonePos=readlong f
  72. unk2=readlong f
  73. unk3=readlong f    
  74. append Mesh_InfoArray (mesh_info_struct BCount:BCount FaceCount:FaceCount VertCount:VertCount VertPos:VertPos FacePos:FacePos BonePos:BonePos VSize:VSize)    
  75. )
  76.  
  77. print Mesh_InfoArray
  78.  
  79. for i=1 to MeshCount do (
  80.     
  81. fseek f Mesh_InfoArray[i].VertPos #seek_set
  82.     
  83. Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))    
  84.     
  85. BNArr = #()
  86. vertArray=#()
  87. faceArray =#()
  88. UVarray=#()
  89. Weight_array=#()
  90. BoneNamesArray=#()    
  91.  
  92. if (Mesh_InfoArray[i].VSize)==12 then
  93. for x =1 to Mesh_InfoArray[i].VertCount do (
  94. getPos = ftell f + (Mesh_InfoArray[i].VSize)        
  95. vx = readFloat f
  96. vy = readFloat f
  97. vz = readFloat f    
  98. fseek f getPos #seek_set    
  99. append vertArray ([vx,vy,vz]*1000)    
  100. append UVarray [tu,tv,0]        
  101. )    
  102. if (Mesh_InfoArray[i].VSize)==24 then
  103. for x =1 to Mesh_InfoArray[i].VertCount do (
  104. getPos = ftell f + (Mesh_InfoArray[i].VSize)        
  105. vx = readFloat f
  106. vy = readFloat f
  107. vz = readFloat f    
  108. unk1 = readFloat f
  109. unk2 = readFloat f        
  110. tu = readHalfFloat f
  111. tv = readHalfFloat f*-1    
  112. fseek f getPos #seek_set        
  113. append vertArray ([vx,vy,vz]*1000)    
  114. append UVarray [tu,tv,0]        
  115. )
  116. if (Mesh_InfoArray[i].VSize)==32 then
  117. for x =1 to Mesh_InfoArray[i].VertCount do (
  118. getPos = ftell f + (Mesh_InfoArray[i].VSize)        
  119. vx = readFloat f
  120. vy = readFloat f
  121. vz = readFloat f    
  122. weight1 = readbyte f #unsigned
  123. weight2 = readbyte f #unsigned
  124. weight3 = readbyte f #unsigned
  125. weight4 =readbyte f #unsigned    
  126. bone1 = readbyte f #unsigned
  127. bone2 = readbyte f #unsigned
  128. bone3 = readbyte f #unsigned
  129. bone4 = readbyte f #unsigned
  130. unk1 = readFloat f
  131. unk2 = readFloat f        
  132. tu = readHalfFloat f
  133. tv = readHalfFloat f*-1    
  134.     
  135. w = (weight_data boneids:#() weights:#())
  136. maxweight = 0
  137.     
  138. if(weight1 != 0) then
  139.    maxweight = maxweight + weight1
  140. if(weight2 != 0) then
  141.    maxweight = maxweight + weight2
  142. if(weight3 != 0) then
  143.    maxweight = maxweight + weight3
  144. if(weight4 != 0) then
  145.    maxweight = maxweight + weight4
  146.  
  147. if(maxweight != 0) then (
  148.       if(weight1 != 0) then (
  149.          w1 = weight1 as float
  150.          append w.boneids (bone1 + 1)
  151.          append w.weights (w1 / 255.0)
  152.       )
  153.       if(weight2 != 0) then (
  154.          w2 = weight2 as float
  155.          append w.boneids (bone2 + 1)
  156.          append w.weights (w2 / 255.0)
  157.       )
  158.       if(weight3 != 0) then (
  159.          w3 = weight3 as float
  160.          append w.boneids (bone3 + 1)
  161.          append w.weights (w3 / 255.0)
  162.       )
  163.       if(weight4 != 0) then (
  164.          w4 = weight4 as float
  165.          append w.boneids (bone4 + 1)
  166.          append w.weights (w4 / 255.0)
  167.       )      
  168.    )
  169. append Weight_array w 
  170. fseek f getPos #seek_set        
  171. append vertArray ([vx,vy,vz]*1000)    
  172. append UVarray [tu,tv,0]        
  173. )
  174.  
  175. if (Mesh_InfoArray[i].VSize)==36 then
  176. for x =1 to Mesh_InfoArray[i].VertCount do (
  177. getPos = ftell f + (Mesh_InfoArray[i].VSize)        
  178. vx = readFloat f
  179. vy = readFloat f
  180. vz = readFloat f    
  181. weight1 = readbyte f #unsigned
  182. weight2 = readbyte f #unsigned
  183. weight3 = readbyte f #unsigned
  184. weight4 =readbyte f #unsigned    
  185. bone1 = readbyte f #unsigned
  186. bone2 = readbyte f #unsigned
  187. bone3 = readbyte f #unsigned
  188. bone4 = readbyte f #unsigned
  189. unk1 = readFloat f
  190. unk2 = readFloat f        
  191. unk3 = readFloat f        
  192. tu = readHalfFloat f
  193. tv = readHalfFloat f*-1    
  194.     
  195. w = (weight_data boneids:#() weights:#())
  196. maxweight = 0
  197.     
  198. if(weight1 != 0) then
  199.    maxweight = maxweight + weight1
  200. if(weight2 != 0) then
  201.    maxweight = maxweight + weight2
  202. if(weight3 != 0) then
  203.    maxweight = maxweight + weight3
  204. if(weight4 != 0) then
  205.    maxweight = maxweight + weight4
  206.  
  207. if(maxweight != 0) then (
  208.       if(weight1 != 0) then (
  209.          w1 = weight1 as float
  210.          append w.boneids (bone1 + 1)
  211.          append w.weights (w1 / 255.0)
  212.       )
  213.       if(weight2 != 0) then (
  214.          w2 = weight2 as float
  215.          append w.boneids (bone2 + 1)
  216.          append w.weights (w2 / 255.0)
  217.       )
  218.       if(weight3 != 0) then (
  219.          w3 = weight3 as float
  220.          append w.boneids (bone3 + 1)
  221.          append w.weights (w3 / 255.0)
  222.       )
  223.       if(weight4 != 0) then (
  224.          w4 = weight4 as float
  225.          append w.boneids (bone4 + 1)
  226.          append w.weights (w4 / 255.0)
  227.       )      
  228.    )
  229. append Weight_array w 
  230. fseek f getPos #seek_set        
  231. append vertArray ([vx,vy,vz]*1000)    
  232. append UVarray [tu,tv,0]        
  233. )
  234.  
  235. if (Mesh_InfoArray[i].VSize)==40 then
  236. for x =1 to Mesh_InfoArray[i].VertCount do (
  237. getPos = ftell f + (Mesh_InfoArray[i].VSize)        
  238. vx = readFloat f
  239. vy = readFloat f
  240. vz = readFloat f    
  241. weight1 = readbyte f #unsigned
  242. weight2 = readbyte f #unsigned
  243. weight3 = readbyte f #unsigned
  244. weight4 =readbyte f #unsigned    
  245. bone1 = readbyte f #unsigned
  246. bone2 = readbyte f #unsigned
  247. bone3 = readbyte f #unsigned
  248. bone4 = readbyte f #unsigned
  249. unk1 = readFloat f
  250. unk2 = readFloat f
  251. unk3 = readFloat f    
  252. tu = readHalfFloat f
  253. tv = readHalfFloat f*-1    
  254.     
  255. w = (weight_data boneids:#() weights:#())
  256. maxweight = 0
  257.     
  258. if(weight1 != 0) then
  259.    maxweight = maxweight + weight1
  260. if(weight2 != 0) then
  261.    maxweight = maxweight + weight2
  262. if(weight3 != 0) then
  263.    maxweight = maxweight + weight3
  264. if(weight4 != 0) then
  265.    maxweight = maxweight + weight4
  266.  
  267. if(maxweight != 0) then (
  268.       if(weight1 != 0) then (
  269.          w1 = weight1 as float
  270.          append w.boneids (bone1 + 1)
  271.          append w.weights (w1 / 255.0)
  272.       )
  273.       if(weight2 != 0) then (
  274.          w2 = weight2 as float
  275.          append w.boneids (bone2 + 1)
  276.          append w.weights (w2 / 255.0)
  277.       )
  278.       if(weight3 != 0) then (
  279.          w3 = weight3 as float
  280.          append w.boneids (bone3 + 1)
  281.          append w.weights (w3 / 255.0)
  282.       )
  283.       if(weight4 != 0) then (
  284.          w4 = weight4 as float
  285.          append w.boneids (bone4 + 1)
  286.          append w.weights (w4 / 255.0)
  287.       )      
  288.    )
  289. append Weight_array w 
  290. fseek f getPos #seek_set        
  291. append vertArray ([vx,vy,vz]*1000)    
  292. append UVarray [tu,tv,0]        
  293. )
  294. Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))    
  295.  
  296. fseek f Mesh_InfoArray[i].FacePos #seek_set    
  297.     
  298. for x = 1 to (Mesh_InfoArray[i].FaceCount)/3 do (
  299. fa=readshort f + 1
  300. fb=readshort f + 1
  301. fc=readshort f + 1
  302. append faceArray [fa,fb,fc]     
  303. )    
  304.  
  305. Print ("Index End @ 0x"+((bit.intAsHex(ftell f))as string))    
  306.  
  307. fseek f Mesh_InfoArray[i].BonePos #seek_set
  308.     
  309. BonePos=ftell f    
  310. for a = 1 to Mesh_InfoArray[i].BCount do (
  311. getPos = ftell f + 28    
  312. fseek f getPos #seek_set        
  313. )    
  314.  
  315. MeshName=#()
  316. if TexCount!=0 then
  317. for x=1 to 1 do (    
  318. fseek f (MeshNameOff) #seek_set
  319. append MeshName (readstring f)
  320. fseek f (NamesOff) #seek_set
  321. NameStart=readlong f
  322. fseek f (NameStart) #seek_set
  323. for x=1 to TexCount do (
  324. MatInfo=readstring f
  325. )
  326. for a = 1 to Mesh_InfoArray[i].BCount do (
  327. BoneName=readstring f
  328. append BoneNamesArray BoneName    
  329. )    
  330. )
  331.  
  332. if TexCount==0 then
  333. fseek f (MeshNameOff) #seek_set
  334. append MeshName (readstring f)
  335. for a = 1 to Mesh_InfoArray[i].BCount do (
  336. BoneName=readstring f
  337. append BoneNamesArray BoneName    
  338. )    
  339.  
  340. Print ("Bone Name End @ 0x"+((bit.intAsHex(ftell f))as string))    
  341.     
  342. msh = mesh vertices:vertArray faces:faceArray
  343. msh.name=MeshName[i]
  344. msh.numTVerts = UVarray.count
  345. buildTVFaces msh
  346. for j = 1 to UVarray.count do setTVert msh j UVarray[j]
  347. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  348.  
  349. try(
  350. max modify mode
  351. select msh
  352. skinMod = skin ()
  353. addModifier msh skinMod
  354. for i = 1 to Mesh_InfoArray[i].BCount do
  355. (
  356.     maxbone = getnodebyname BoneNamesArray[i]
  357.     if i != BNArr.count then 
  358.         skinOps.addBone skinMod maxbone 0
  359.     else
  360.         skinOps.addBone skinMod maxbone 1
  361. )
  362. modPanel.setCurrentObject skinMod
  363. for i = 1 to Mesh_InfoArray[i].VertCount do (
  364.    w = Weight_array[i]
  365.    bi = #() --bone index array
  366.    wv = #() --weight value array
  367.    
  368.    for j = 1 to w.boneids.count do
  369.    (
  370.       boneid = w.boneids[j]
  371.       weight = w.weights[j]
  372.       append bi boneid
  373.       append wv weight
  374.    )   
  375.    
  376.    skinOps.ReplaceVertexWeights skinMod i bi wv
  377. )
  378. )catch(format "Wrong Skeleton!!!\n")
  379. )
  380.  
  381. fclose f