home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 7300 / Survarium.7z / Survarium.ms
Encoding:
Text File  |  2014-05-05  |  10.9 KB  |  471 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. hname = getOpenFileName \
  4. caption:"Open .* from Mesh folder" \
  5. types:"Survarium export_properties Info (*.*)|*.*" \
  6. historyCategory:"SurvariumInfoObjectPresets"
  7. h = fopen hname "rb"        
  8. gname = getOpenFileName \
  9. caption:"Open .* from Bones folder" \
  10. types:"Survarium bind_pose Bones (*.*)|*.*" \
  11. historyCategory:"SurvariumBonesObjectPresets"
  12. g = fopen gname "rb"
  13. fname = getOpenFileName \
  14. caption:"Open .* from Mesh folder" \
  15. types:"Survarium converted_model Meshes (*.*)|*.*" \
  16. historyCategory:"SurvariumInfoObjectPresets"
  17. f = fopen fname "rb"
  18.  
  19. clearlistener()
  20. fn readHalfFloat fstream = (
  21.     hf=readshort fstream #unsigned
  22.     sign = bit.get hf 16
  23.     exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
  24.     fraction = bit.and hf (bit.hexasint "03FF")
  25.     if sign==true then sign = 1 else sign = 0
  26.     exponentF = exponent + 127
  27.     outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
  28.     (bit.shift exponentF 23)) (bit.shift sign 31)
  29.     return bit.intasfloat outputasfloat*2
  30.     )
  31. fn readFixedString bstream fixedLen = (
  32. local str = ""
  33. for i = 1 to fixedLen do (
  34. str += bit.intAsChar (ReadByte bstream #unsigned))
  35. str
  36. )
  37. struct weight_data
  38. (
  39.     boneids,weights
  40. )
  41.  
  42. clearlistener()
  43.  
  44. fseek h 0x30 #seek_set
  45. Version = readlong h
  46. fseek h 0xA8 #seek_set
  47. MeshNameOff = readlong h
  48. fseek h MeshNameOff #seek_set
  49. MeshName = readstring h
  50.  
  51. BoneCount = #()
  52.  
  53. fseek g 0x0 #seek_set
  54. BoneCount = readshort g
  55.  
  56. BNArr = #()
  57.  
  58. for i = 1 to BoneCount do (
  59.  
  60. BoneName=readString g
  61.                 
  62. c11 = ReadFloat g; c12 = ReadFloat g; c13 = ReadFloat g
  63. c21 = ReadFloat g; c22 = ReadFloat g; c23 = ReadFloat g
  64. c31 = ReadFloat g; c32 = ReadFloat g; c33 = ReadFloat g
  65.         
  66. tfm = (quat 0 0 0 1) as matrix3
  67. tfm.row4 = ([c11,c12,c13]*100)
  68.     
  69. newBone = bonesys.createbone    \
  70.                   tfm.row4    \
  71.                   (tfm.row4 + 0.01 * (normalize tfm.row1)) \
  72.                   (normalize tfm.row3)
  73.             newBone.name = BoneName
  74.             newBone.width  = 0.01
  75.             newBone.height = 0.01
  76.             newBone.transform = inverse tfm
  77.             newBone.setBoneEnable false 0
  78.             newBone.wirecolor = white
  79.             newbone.showlinks = true
  80.             newBone.pos.controller      = TCB_position ()
  81.             newBone.rotation.controller = TCB_rotation ()                  
  82. append BNArr newBone                  
  83. )
  84.  
  85. rootRT = dummy name:"Main_Root" boxsize:[.1,.1,.1]
  86. for b in BNArr where b.parent==undefined do b.parent = rootRT
  87. rootRT.transform = (matrix3 [1,0,0] [0,0,-1] [0,1,0] [0,0,0])
  88.  
  89.  
  90. fseek f 0x0 #seek_set
  91. VertexId = ReadLong f
  92. VertexSecSize = ReadLong f
  93. numVerts = ReadLong f
  94.  
  95. vertArray = #()
  96. Facearray = #()    
  97. UV_array = #()
  98. Weight_array =#()
  99.  
  100. Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))    
  101.  
  102. if Version==44 then    
  103. for x = 1 to numVerts Do (
  104. vx = Readfloat f
  105. vy = Readfloat f
  106. vz = Readfloat f
  107.  
  108. bone1 = -1    
  109. bone2 = -1    
  110. bone3 = -1
  111. bone4 = -1
  112.     
  113. weight1 = 0
  114. weight2 = 0
  115. weight3 = 0
  116. weight4 = 0
  117.     
  118. n1 = ReadLong f
  119. n2 = ReadLong f
  120. n3 = ReadLong f    
  121.     
  122. tu=ReadFloat f    
  123. tv=ReadFloat f*-1    
  124.  
  125. w = (weight_data boneids:#() weights:#())
  126. maxweight = 0
  127. if(weight1 != 0) then
  128.    maxweight = maxweight + weight1
  129. if(weight2 != 0) then
  130.    maxweight = maxweight + weight2
  131. if(weight3 != 0) then
  132.    maxweight = maxweight + weight3
  133. if(weight4 != 0) then
  134.    maxweight = maxweight + weight4
  135.  
  136.  
  137. if(maxweight != 0) then (
  138.       if(weight1 != 0) then (
  139.          w1 = weight1 as float
  140.          append w.boneids (bone1 + 1)
  141.          append w.weights (w1)
  142.       )
  143.       if(weight2 != 0) then (
  144.          w2 = weight2 as float
  145.          append w.boneids (bone2 + 1)
  146.          append w.weights (w2)
  147.       )
  148.       if(weight3 != 0) then (
  149.          w3 = weight3 as float
  150.          append w.boneids (bone3 + 1)
  151.          append w.weights (w3)
  152.       )
  153.       if(weight4 != 0) then (
  154.          w4 = weight4 as float
  155.          append w.boneids (bone4 + 1)
  156.         append w.weights (w4)
  157.       )      
  158.    )
  159. append Weight_array w               
  160. append vertArray ([-vx,-vz,vy]*100)
  161. append UV_array [tu,tv,0]     
  162. )
  163.     
  164. if Version==45 then    
  165. for x = 1 to numVerts Do (
  166. vx = Readfloat f
  167. vy = Readfloat f
  168. vz = Readfloat f
  169.  
  170. bone1 = readbyte f #unsigned    
  171. bone2 = readbyte f #unsigned    
  172. bone3 = readbyte f #unsigned    
  173. bone4 = readbyte f #unsigned    
  174.     
  175. weight1 = Readfloat f
  176. weight2 = 0
  177. weight3 = 0
  178. weight4 = 0
  179.     
  180. n1 = ReadLong f
  181. n2 = ReadLong f
  182. n3 = ReadLong f    
  183.     
  184. tu=ReadFloat f    
  185. tv=ReadFloat f*-1    
  186.  
  187. w = (weight_data boneids:#() weights:#())
  188. maxweight = 0
  189. if(weight1 != 0) then
  190.    maxweight = maxweight + weight1
  191. if(weight2 != 0) then
  192.    maxweight = maxweight + weight2
  193. if(weight3 != 0) then
  194.    maxweight = maxweight + weight3
  195. if(weight4 != 0) then
  196.    maxweight = maxweight + weight4
  197.  
  198.  
  199. if(maxweight != 0) then (
  200.       if(weight1 != 0) then (
  201.          w1 = weight1 as float
  202.          append w.boneids (bone1 + 1)
  203.          append w.weights (w1)
  204.       )
  205.       if(weight2 != 0) then (
  206.          w2 = weight2 as float
  207.          append w.boneids (bone2 + 1)
  208.          append w.weights (w2)
  209.       )
  210.       if(weight3 != 0) then (
  211.          w3 = weight3 as float
  212.          append w.boneids (bone3 + 1)
  213.          append w.weights (w3)
  214.       )
  215.       if(weight4 != 0) then (
  216.          w4 = weight4 as float
  217.          append w.boneids (bone4 + 1)
  218.         append w.weights (w4)
  219.       )      
  220.    )
  221. append Weight_array w               
  222. append vertArray ([-vx,-vz,vy]*100)
  223. append UV_array [tu,tv,0]     
  224. )        
  225.     
  226. if Version==46 then    
  227. for x = 1 to numVerts Do (
  228. vx = Readfloat f
  229. vy = Readfloat f
  230. vz = Readfloat f
  231.  
  232. bone1 = readbyte f #unsigned    
  233. bone2 = readbyte f #unsigned    
  234. bone3 = readbyte f #unsigned    
  235. bone4 = readbyte f #unsigned    
  236.     
  237. weight1 = Readfloat f
  238. weight2 = Readfloat f
  239. weight3 = 0
  240. weight4 = 0
  241.     
  242. n1 = ReadLong f
  243. n2 = ReadLong f
  244. n3 = ReadLong f    
  245.     
  246. tu=ReadFloat f    
  247. tv=ReadFloat f*-1    
  248.  
  249. w = (weight_data boneids:#() weights:#())
  250. maxweight = 0
  251. if(weight1 != 0) then
  252.    maxweight = maxweight + weight1
  253. if(weight2 != 0) then
  254.    maxweight = maxweight + weight2
  255. if(weight3 != 0) then
  256.    maxweight = maxweight + weight3
  257. if(weight4 != 0) then
  258.    maxweight = maxweight + weight4
  259.  
  260.  
  261. if(maxweight != 0) then (
  262.       if(weight1 != 0) then (
  263.          w1 = weight1 as float
  264.          append w.boneids (bone1 + 1)
  265.          append w.weights (w1)
  266.       )
  267.       if(weight2 != 0) then (
  268.          w2 = weight2 as float
  269.          append w.boneids (bone2 + 1)
  270.          append w.weights (w2)
  271.       )
  272.       if(weight3 != 0) then (
  273.          w3 = weight3 as float
  274.          append w.boneids (bone3 + 1)
  275.          append w.weights (w3)
  276.       )
  277.       if(weight4 != 0) then (
  278.          w4 = weight4 as float
  279.          append w.boneids (bone4 + 1)
  280.         append w.weights (w4)
  281.       )      
  282.    )
  283. append Weight_array w               
  284. append vertArray ([-vx,-vz,vy]*100)
  285. append UV_array [tu,tv,0]     
  286. )    
  287.  
  288. if Version==47 then    
  289. for x = 1 to numVerts Do (
  290. vx = Readfloat f
  291. vy = Readfloat f
  292. vz = Readfloat f
  293.  
  294. bone1 = readbyte f #unsigned    
  295. bone2 = readbyte f #unsigned    
  296. bone3 = readbyte f #unsigned    
  297. bone4 = readbyte f #unsigned    
  298.     
  299. weight1 = Readfloat f
  300. weight2 = Readfloat f
  301. weight3 = Readfloat f
  302. weight4 = 0
  303.     
  304. n1 = ReadLong f
  305. n2 = ReadLong f
  306. n3 = ReadLong f    
  307.     
  308. tu=ReadFloat f    
  309. tv=ReadFloat f*-1    
  310.  
  311. w = (weight_data boneids:#() weights:#())
  312. maxweight = 0
  313. if(weight1 != 0) then
  314.    maxweight = maxweight + weight1
  315. if(weight2 != 0) then
  316.    maxweight = maxweight + weight2
  317. if(weight3 != 0) then
  318.    maxweight = maxweight + weight3
  319. if(weight4 != 0) then
  320.    maxweight = maxweight + weight4
  321.  
  322.  
  323. if(maxweight != 0) then (
  324.       if(weight1 != 0) then (
  325.          w1 = weight1 as float
  326.          append w.boneids (bone1 + 1)
  327.          append w.weights (w1)
  328.       )
  329.       if(weight2 != 0) then (
  330.          w2 = weight2 as float
  331.          append w.boneids (bone2 + 1)
  332.          append w.weights (w2)
  333.       )
  334.       if(weight3 != 0) then (
  335.          w3 = weight3 as float
  336.          append w.boneids (bone3 + 1)
  337.          append w.weights (w3)
  338.       )
  339.       if(weight4 != 0) then (
  340.          w4 = weight4 as float
  341.          append w.boneids (bone4 + 1)
  342.         append w.weights (w4)
  343.       )      
  344.    )
  345. append Weight_array w               
  346. append vertArray ([-vx,-vz,vy]*100)
  347. append UV_array [tu,tv,0]     
  348. )
  349.  
  350. if Version==48 then    
  351. for x = 1 to numVerts Do (
  352. vx = Readfloat f
  353. vy = Readfloat f
  354. vz = Readfloat f
  355.  
  356. bone1 = readbyte f #unsigned    
  357. bone2 = readbyte f #unsigned    
  358. bone3 = readbyte f #unsigned    
  359. bone4 = readbyte f #unsigned    
  360.     
  361. weight1 = Readfloat f
  362. weight2 = Readfloat f
  363. weight3 = Readfloat f
  364. weight4 = Readfloat f
  365.     
  366. n1 = ReadLong f
  367. n2 = ReadLong f
  368. n3 = ReadLong f    
  369.     
  370. tu=ReadFloat f    
  371. tv=ReadFloat f*-1    
  372.  
  373. w = (weight_data boneids:#() weights:#())
  374. maxweight = 0
  375. if(weight1 != 0) then
  376.    maxweight = maxweight + weight1
  377. if(weight2 != 0) then
  378.    maxweight = maxweight + weight2
  379. if(weight3 != 0) then
  380.    maxweight = maxweight + weight3
  381. if(weight4 != 0) then
  382.    maxweight = maxweight + weight4
  383.  
  384.  
  385. if(maxweight != 0) then (
  386.       if(weight1 != 0) then (
  387.          w1 = weight1 as float
  388.          append w.boneids (bone1 + 1)
  389.          append w.weights (w1)
  390.       )
  391.       if(weight2 != 0) then (
  392.          w2 = weight2 as float
  393.          append w.boneids (bone2 + 1)
  394.          append w.weights (w2)
  395.       )
  396.       if(weight3 != 0) then (
  397.          w3 = weight3 as float
  398.          append w.boneids (bone3 + 1)
  399.          append w.weights (w3)
  400.       )
  401.       if(weight4 != 0) then (
  402.          w4 = weight4 as float
  403.          append w.boneids (bone4 + 1)
  404.         append w.weights (w4)
  405.       )      
  406.    )
  407. append Weight_array w               
  408. append vertArray ([-vx,-vz,vy]*100)
  409. append UV_array [tu,tv,0]     
  410. )
  411.  
  412. Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))
  413.  
  414. FaceId = ReadLong f
  415. FaceSecSize = ReadLong f
  416. numFaces = ReadLong f
  417.  
  418. for x = 1 to (numFaces/3) do 
  419. (
  420. f1 = (Readshort f #unsigned + 1) 
  421. f2 = (Readshort f #unsigned + 1) 
  422. f3 = (Readshort f #unsigned + 1) 
  423. append Facearray [f3,f2,f1]
  424. )
  425.  
  426. try(
  427. msh = mesh vertices:vertArray faces:faceArray
  428. msh.numTVerts = UV_array.count
  429. buildTVFaces msh
  430. msh.name = MeshName
  431. for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  432. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  433. msh.displayByLayer = false
  434. msh.backfacecull = true
  435. msh.wirecolor = (color 230 200 210)
  436.             
  437.  
  438. max modify mode
  439. select msh
  440. skinMod = skin ()
  441. addModifier msh skinMod
  442. for i = 1 to BoneCount do
  443. (
  444.     maxbone = BNArr[i] 
  445.     if i != BoneCount then 
  446.         skinOps.addBone skinMod maxbone 0
  447.     else
  448.         skinOps.addBone skinMod maxbone 1
  449. )
  450. modPanel.setCurrentObject skinMod
  451. for i = 1 to (numVerts) do (
  452.    w = Weight_array[i]
  453.    bi = #() --bone index array
  454.    wv = #() --weight value array
  455.    
  456.    for j = 1 to w.boneids.count do
  457.    (
  458.       boneid = w.boneids[j]
  459.       weight = w.weights[j]
  460.       append bi boneid
  461.       append wv weight
  462.    )   
  463.    
  464.    skinOps.ReplaceVertexWeights skinMod i bi wv
  465.  
  466. )
  467. )catch(format "Failed!!!\n")
  468.  
  469. fclose h
  470. fclose g
  471. fclose f