home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 10301 / Azera.7z / Azera.ms
Encoding:
Text File  |  2015-06-22  |  8.9 KB  |  421 lines

  1. if (heapSize < 200000) then
  2.         heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
  3. fname = getOpenFileName \
  4. caption:"Open .mesh from Mesh folder" \
  5. types:"Azera (*.model)|*.model" \
  6. historyCategory:"AzeraObjectPresets"
  7. f = fopen fname "rb"
  8.  
  9. clearlistener()
  10.  
  11. fn ReadHalfFloat fstream = 
  12.     (
  13.     hf=readshort fstream
  14.     sign = bit.get hf 16
  15.     exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
  16.     fraction = bit.and hf (bit.hexasint "03FF")
  17.     if sign==true then sign = 1 else sign = 0
  18.     exponentF = exponent + 127
  19.     outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
  20.     (bit.shift exponentF 23)) (bit.shift sign 31)
  21.     return bit.intasfloat outputasfloat*2
  22. )
  23.  
  24. fn readFixedString bstream fixedLen = (
  25. local str = ""
  26. for i = 1 to fixedLen do (
  27. str += bit.intAsChar (ReadByte bstream #unsigned))
  28. str
  29. )
  30. struct weight_data
  31. (
  32.     boneids,weights
  33. )
  34.  
  35. clearlistener()
  36.  
  37. fseek f (0x20) #seek_set
  38. ID=readlong f
  39. fseek f (0x2C) #seek_set
  40. VertSize=readshort f
  41. fseek f (0x60) #seek_set
  42. WVertexCount=readlong f
  43.  
  44. vertArray = #()
  45. faceArray = #()
  46. UV_array = #()
  47. Weight_array=#()    
  48. fseek f (0x10) #seek_set
  49. SecEnd=readlong f
  50.  
  51. fseek f (SecEnd) #seek_set
  52. fseek f (0x24) #seek_cur
  53. Sec2Size=readlong f
  54. Pos=ftell f
  55. fseek f (0x12) #seek_cur
  56. MeshNameSize=readlong f
  57. MeshName=readFixedString f MeshNameSize
  58. Print ("Mesh Name End @ 0x"+((bit.intAsHex(ftell f))as string))    
  59. fseek f (Pos) #seek_set
  60. fseek f (Sec2Size) #seek_cur    
  61. fseek f (0x10) #seek_cur    
  62. Sec3Size=readlong f
  63. fseek f (Sec3Size) #seek_cur
  64. fseek f (0x10) #seek_cur
  65. Sec4Size=readlong f
  66. fseek f (Sec4Size+12) #seek_cur
  67.  
  68. SKEL==readFixedString f 4
  69. SKELSize=readlong f
  70. Null=readshort f
  71. BoneCount=readshort f
  72.  
  73. tarr = #()
  74. rarr = #()
  75. Barr = #()
  76. Parr = #()    
  77.  
  78. for i=1 to BoneCount do (
  79.     
  80. BoneNameSize=readlong f
  81. BoneName=(readFixedString f BoneNameSize)
  82. BoneParent=readshort f+1    
  83.  
  84. getPos = ftell f + 56
  85.     
  86. c11 = ReadFloat f; c12 = ReadFloat f; c13 = ReadFloat f; c14 = ReadFloat f    
  87. c21 = ReadFloat f; c22 = ReadFloat f; c23 = ReadFloat f; c24 = ReadFloat f    
  88. c31 = ReadFloat f; c32 = ReadFloat f; c33 = ReadFloat f; c34 = ReadFloat f
  89. c41 = ReadFloat f; c42 = ReadFloat f
  90.  
  91. fseek f getPos #seek_set    
  92.             
  93. tfm = (quat c33 c34 c41 c42) as matrix3
  94. bvx=c24
  95. bvy=c31
  96. bvz=c32
  97.     
  98. append tarr [bvx,bvy,bvz]
  99. append rarr tfm
  100. append Barr BoneName        
  101. append Parr BoneParent    
  102. )    
  103.  
  104. BNArr = #()
  105. for i = 1 to BoneCount Do (
  106. if isvalidnode (getNodeByName Barr[i]) != true then (
  107. tfm = rarr[i]
  108. tfm.row4 = tarr[i]
  109. newBone = bonesys.createbone   \
  110.               tfm.row4   \
  111.               (tfm.row4 + 0.01 * (normalize tfm.row1)) \
  112.               (normalize tfm.row3)
  113.          newBone.name = Barr[i]
  114.          newBone.width  = 0.01
  115.          newBone.height = 0.01
  116.          newBone.transform = tfm
  117.          newBone.setBoneEnable false 0
  118.          newBone.wirecolor = yellow
  119.          newbone.showlinks = true
  120.          newBone.pos.controller      = TCB_position ()
  121.          newBone.rotation.controller = TCB_rotation ()
  122. if Parr[i]!=0 then (
  123. newBone.parent=BNArr[(Parr[i])]    
  124. newBone.transform *= newBone.parent.transform    
  125. )                      
  126. )
  127. if isvalidnode (getNodeByName Barr[i]) == true then (
  128. newBone = getNodeByName Barr[i]
  129. )
  130. append BNArr newBone
  131. )
  132.     
  133. Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))    
  134.  
  135. fseek f (0xE) #seek_cur
  136. WGHT=readFixedString f 4
  137. WGHTSize=readlong f    
  138. WGHTId=readlong f
  139.  
  140. for i=1 to WVertexCount do (
  141.     
  142. Bone1 = -1
  143. Bone2 = -1
  144. Bone3 = -1
  145. Bone4 = -1
  146. weight1 = 0
  147. weight2 = 0
  148. weight3 = 0
  149. weight4 = 0    
  150.     
  151. WeightCount=readshort f        
  152.     
  153. if WeightCount == 1 then (
  154. Bone1 = readshort f    
  155. weight1 = readshort f #unsigned/65535.00    
  156. )
  157. if WeightCount == 2 then (
  158. Bone1 = readshort f    
  159. weight1 = readshort f #unsigned/65535.00    
  160. Bone2 = readshort f    
  161. weight2 = readshort f #unsigned/65535.00    
  162. )
  163. if WeightCount == 3 then (
  164. Bone1 = readshort f    
  165. weight1 = readshort f #unsigned/65535.00    
  166. Bone2 = readshort f    
  167. weight2 = readshort f #unsigned/65535.00    
  168. Bone3 = readshort f    
  169. weight3 = readshort f #unsigned/65535.00            
  170. )
  171. if WeightCount == 4 then (
  172. Bone1 = readshort f    
  173. weight1 = readshort f #unsigned/65535.00    
  174. Bone2 = readshort f    
  175. weight2 = readshort f #unsigned/65535.00    
  176. Bone3 = readshort f    
  177. weight3 = readshort f #unsigned/65535.00        
  178. Bone4 = readshort f    
  179. weight4 = readshort f #unsigned/65535.00                
  180. )
  181. w = (weight_data boneids:#() weights:#())
  182. maxweight = 0
  183. if(Bone1 != -1) then
  184.     maxweight = maxweight + weight1
  185. if(Bone2 != -1) then
  186.     maxweight = maxweight + weight2
  187. if(Bone3 != -1) then
  188.     maxweight = maxweight + weight3
  189. if(Bone4 != -1) then
  190.     maxweight = maxweight + weight4
  191.  
  192. if(maxweight != 0) then (
  193.         if(Bone1 != -1) then (
  194.             w1 = weight1 as float
  195.             append w.boneids (bone1 + 1)
  196.             append w.weights w1
  197.         )
  198.         if(Bone2 != -1) then (
  199.             w2 = weight2 as float
  200.             append w.boneids (bone2 + 1)
  201.             append w.weights w2
  202.         )
  203.         if(Bone3 != -1) then (
  204.             w3 = weight3 as float
  205.             append w.boneids (bone3 + 1)
  206.             append w.weights w3
  207.         )
  208.         if(Bone4 != -1) then (
  209.             w4 = weight4 as float
  210.             append w.boneids (bone4 + 1)
  211.             append w.weights w4
  212.         )        
  213.     )
  214. append Weight_array w
  215. )
  216.  
  217. -- print Weight_array
  218.     
  219. fseek f (0x2C) #seek_set
  220. VertSize=readshort f
  221. fseek f (0x60) #seek_set
  222. VertexCount=readlong f
  223. if Id==1 then (
  224. fseek f (0xC) #seek_cur
  225. readbyte f    
  226. )    
  227. if Id!=1 then (
  228. fseek f (0x10) #seek_cur
  229. readbyte f
  230. )
  231. Print ("Face Count @ 0x"+((bit.intAsHex(ftell f))as string))    
  232. FaceCount=readlong f
  233.  
  234. if Id==1 then fseek f (0x80) #seek_set    
  235. if Id!=1 then fseek f (0x86) #seek_set
  236.     
  237. Print ("Vert Start @ 0x"+((bit.intAsHex(ftell f))as string))    
  238.  
  239. if VertSize==44 then
  240. for x=1 to VertexCount do (
  241. getPos = ftell f + VertSize
  242. vx = ReadFloat f
  243. vy = ReadFloat f
  244. vz = ReadFloat f
  245.     
  246. nx = ReadFloat f
  247. ny = ReadFloat f
  248. nz = ReadFloat f
  249.     
  250. tu =    ReadFloat f
  251. tv =  ReadFloat f*-1        
  252.  
  253. tx = ReadFloat f
  254. ty = ReadFloat f
  255. tz = ReadFloat f
  256.     
  257. fseek f getPos #seek_set    
  258. append vertArray ([vx,vy,vz])    
  259. append UV_array ([tu,tv,0])        
  260. )    
  261.     
  262. if VertSize==48 then
  263. for x=1 to VertexCount do (
  264. getPos = ftell f + VertSize
  265. vx = ReadFloat f
  266. vy = ReadFloat f
  267. vz = ReadFloat f
  268.  
  269. nx = ReadFloat f
  270. ny = ReadFloat f
  271. nz = ReadFloat f
  272.  
  273. Null=readfloat f
  274.  
  275. tu =    ReadFloat f
  276. tv =  ReadFloat f*-1    
  277.     
  278. tx = ReadFloat f
  279. ty = ReadFloat f
  280. tz = ReadFloat f    
  281.     
  282. fseek f getPos #seek_set    
  283. append vertArray ([vx,vy,vz])    
  284. append UV_array ([tu,tv,0])        
  285. )
  286.  
  287. if VertSize==52 then
  288. for x=1 to VertexCount do (
  289. getPos = ftell f + VertSize
  290. vx = ReadFloat f
  291. vy = ReadFloat f
  292. vz = ReadFloat f
  293.     
  294. nx = ReadFloat f
  295. ny = ReadFloat f
  296. nz = ReadFloat f
  297.  
  298. tu =    ReadFloat f
  299. tv =  ReadFloat f*-1    
  300.     
  301. Null=readfloat f    
  302. Null=readfloat f    
  303.     
  304. tx = ReadFloat f
  305. ty = ReadFloat f
  306. tz = ReadFloat f    
  307.     
  308. fseek f getPos #seek_set    
  309. append vertArray ([vx,vy,vz])    
  310. append UV_array ([tu,tv,0])        
  311. )
  312.  
  313. if VertSize==56 then
  314. for x=1 to VertexCount do (
  315. getPos = ftell f + VertSize
  316. vx = ReadFloat f
  317. vy = ReadFloat f
  318. vz = ReadFloat f
  319.  
  320. nx = ReadFloat f
  321. ny = ReadFloat f
  322. nz = ReadFloat f
  323.  
  324. Null=readfloat f
  325.  
  326. tu =    ReadFloat f
  327. tv =  ReadFloat f*-1    
  328.     
  329. Null=readfloat f
  330. Null=readfloat f
  331.  
  332. tx = ReadFloat f
  333. ty = ReadFloat f
  334. tz = ReadFloat f    
  335.     
  336. fseek f getPos #seek_set    
  337. append vertArray ([vx,vy,vz])    
  338. append UV_array ([tu,tv,0])        
  339. )
  340.  
  341. if VertSize==60 then
  342. for x=1 to VertexCount do (
  343. getPos = ftell f + VertSize
  344. vx = ReadFloat f
  345. vy = ReadFloat f
  346. vz = ReadFloat f
  347.  
  348. nx = ReadFloat f
  349. ny = ReadFloat f
  350. nz = ReadFloat f
  351.  
  352. Null=readfloat f
  353.  
  354. tu =    ReadFloat f
  355. tv =  ReadFloat f*-1    
  356.     
  357. Null=readfloat f
  358. Null=readfloat f
  359.  
  360. tx = ReadFloat f
  361. ty = ReadFloat f
  362. tz = ReadFloat f    
  363.     
  364. fseek f getPos #seek_set    
  365. append vertArray ([vx,vy,vz])    
  366. append UV_array ([tu,tv,0])        
  367. )
  368.  
  369. Print ("Vert End @ 0x"+((bit.intAsHex(ftell f))as string))        
  370.  
  371. for x=1 to faceCount do (
  372. fa=(readshort f #unsigned +1)
  373. fb=(readshort f #unsigned +1)
  374. fc=(readshort f #unsigned +1)    
  375. append faceArray [fa,fb,fc]
  376. )
  377.  
  378. Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))    
  379.  
  380. try (    
  381. msh = mesh vertices:vertArray faces:faceArray
  382. msh.name = (MeshName) 
  383. msh.numTVerts = UV_array.count
  384. buildTVFaces msh
  385. for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  386. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  387. msh.displayByLayer = false
  388. msh.backfacecull = false
  389. msh.wirecolor = (color 230 200 210)    
  390.  
  391. max modify mode
  392. select msh
  393. skinMod = skin ()
  394. addModifier msh skinMod
  395. for i = 1 to BNArr.count do
  396. (
  397.     maxbone = BNArr[i]
  398.     if i != BNArr.count then 
  399.         skinOps.addBone skinMod maxbone 0
  400.     else
  401.         skinOps.addBone skinMod maxbone 1
  402. )
  403. modPanel.setCurrentObject skinMod
  404. for i = 1 to vertArray.count do (
  405.    w = Weight_array[i]
  406.    bi = #() --bone index array
  407.    wv = #() --weight value array
  408.    
  409.    for j = 1 to w.boneids.count do
  410.    (
  411.       boneid = w.boneids[j]
  412.       weight = w.weights[j]
  413.       append bi boneid
  414.       append wv weight
  415.    )   
  416.    
  417.    skinOps.ReplaceVertexWeights skinMod i bi wv
  418. )    
  419. )catch(format "Wrong Skel or Messed Bones!!\n")
  420.     
  421. fclose f