home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 7132 / SWTOR.7z / SWTOR.ms < prev   
Encoding:
Text File  |  2014-03-25  |  6.5 KB  |  265 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 [0,0,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. Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))    
  175.  
  176. fseek f Mesh_InfoArray[i].FacePos #seek_set    
  177.     
  178. for x = 1 to (Mesh_InfoArray[i].FaceCount)/3 do (
  179. fa=readshort f #unsigned + 1 
  180. fb=readshort f #unsigned+ 1
  181. fc=readshort f #unsigned+ 1
  182. append faceArray [fa,fb,fc]     
  183. )    
  184.  
  185. Print ("Index End @ 0x"+((bit.intAsHex(ftell f))as string))    
  186.  
  187. fseek f Mesh_InfoArray[i].BonePos #seek_set
  188.     
  189. BonePos=ftell f    
  190. for a = 1 to Mesh_InfoArray[i].BCount do (
  191. getPos = ftell f + 28    
  192. fseek f getPos #seek_set        
  193. )    
  194.  
  195. MeshName=#()
  196. Print ("Bone Name Start @ 0x"+((bit.intAsHex(ftell f))as string))    
  197. fseek f (MeshNameOff) #seek_set        
  198. for x=1 to meshcount do (    
  199. append MeshName (readstring f)
  200. Print ("Mesh Name @ 0x"+((bit.intAsHex(ftell f))as string))        
  201. )    
  202. if TexCount!=0 then
  203. for x=1 to 1 do (    
  204. fseek f (NamesOff) #seek_set
  205. NameStart=readlong f
  206. fseek f (NameStart) #seek_set
  207. for x=1 to TexCount do (
  208. MatInfo=readstring f
  209. )
  210. for a = 1 to Mesh_InfoArray[i].BCount do (
  211. BoneName=readstring f
  212. append BoneNamesArray BoneName    
  213. )    
  214. )
  215.  
  216. if TexCount==0 then
  217. fseek f (MeshNameOff) #seek_set
  218. append MeshName (readstring f)
  219. for a = 1 to Mesh_InfoArray[i].BCount do (
  220. BoneName=readstring f
  221. append BoneNamesArray BoneName    
  222. )    
  223.  
  224. Print ("Bone Name End @ 0x"+((bit.intAsHex(ftell f))as string))    
  225.     
  226. msh = mesh vertices:vertArray faces:faceArray
  227. msh.name=MeshName[i]
  228. msh.numTVerts = UVarray.count
  229. buildTVFaces msh
  230. for j = 1 to UVarray.count do setTVert msh j UVarray[j]
  231. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  232.  
  233. try(
  234. max modify mode
  235. select msh
  236. skinMod = skin ()
  237. addModifier msh skinMod
  238. for i = 1 to Mesh_InfoArray[i].BCount do
  239. (
  240.     maxbone = getnodebyname BoneNamesArray[i]
  241.     if i != BNArr.count then 
  242.         skinOps.addBone skinMod maxbone 0
  243.     else
  244.         skinOps.addBone skinMod maxbone 1
  245. )
  246. modPanel.setCurrentObject skinMod
  247. for i = 1 to Mesh_InfoArray[i].VertCount do (
  248.    w = Weight_array[i]
  249.    bi = #() --bone index array
  250.    wv = #() --weight value array
  251.    
  252.    for j = 1 to w.boneids.count do
  253.    (
  254.       boneid = w.boneids[j]
  255.       weight = w.weights[j]
  256.       append bi boneid
  257.       append wv weight
  258.    )   
  259.    
  260.    skinOps.ReplaceVertexWeights skinMod i bi wv
  261. )
  262. )catch(format "Wrong Skeleton!!!\n")
  263. )
  264.  
  265. fclose f