home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 9791 / emo.7z / emo.ms
Encoding:
Text File  |  2015-09-26  |  14.8 KB  |  577 lines

  1. fn ReadBEShort fstream = (
  2. short = readshort fstream #unsigned
  3. short = bit.swapBytes short 2 1
  4. b = (bit.get short 16)
  5. for i = 17 to 32 do short = bit.set short i b
  6. return short
  7. )
  8. fn floatSwap2 f = (
  9.    i = bit.floatAsInt f
  10.    h = bit.intashex i
  11.    while h.count < 8 do h = "0" + h
  12.    
  13.    s = (substring h 7 2) + (substring h 5 2) + (substring h 3 2) + (substring h 1 2)
  14.    bit.intAsFloat (bit.hexasint s)
  15. )   
  16. fn ReadBEword fstream = (
  17. return (bit.swapBytes (readshort fstream #unsigned) 1 2)
  18. )
  19. fn ReadBElong fstream = (
  20. long = readlong fstream
  21. long = bit.swapBytes long 1 4
  22. long = bit.swapBytes long 2 3
  23. return long
  24. )
  25. fn ReadBEfloat fstream = (
  26. return floatSwap2(readfloat fstream)
  27. )
  28. fn ReadFixedString bstream fixedLen = (
  29.    local str = ""
  30.    for i = 1 to fixedLen do
  31.    (
  32.       str += bit.intAsChar (ReadByte bstream #unsigned)
  33.    )
  34.    str
  35. )
  36. fn DisplayVerts msh = (
  37.     select msh
  38.     poly = Edit_Poly()
  39.     modPanel.addModToSelection poly ui:off
  40.     setselectionlevel poly #vertex
  41.     poly.select #vertex #{1}
  42. )
  43. fn convertTo32 input16 = (
  44.       inputAsInt = input16
  45.       sign = bit.get inputAsInt 16
  46.       exponent = (bit.shift (bit.and inputAsInt (bit.hexasint "7C00")) -10) as integer - 16
  47.       fraction = bit.and inputAsInt (bit.hexasint "03FF")
  48.       if sign==true then sign = 1 else sign = 0
  49.       exponentF = exponent + 127
  50.       --Ouput 32 bit integer representing a 32 bit float
  51.       outputAsFloat = bit.or (bit.or (bit.shift fraction 13) (bit.shift exponentF 23)) (bit.shift sign 31)
  52.       --Output Check   
  53.       return bit.intasfloat outputasfloat
  54. )
  55. fn ReadBEHalfFloat fstream = (
  56. return convertTo32(ReadBEword fstream)
  57. )
  58. fn ApplyNormals2 amesh nArr toggle= (
  59.     max modify mode
  60.     cui.expertModeOn()
  61.  
  62.     with redraw off (
  63.        --your import code
  64.         
  65.        for face = 1 to amesh.numfaces do setFaceSmoothGroup amesh face 1
  66.         
  67.        select amesh
  68.        addmodifier amesh (Edit_Normals ()) ui:off
  69.        amesh.Edit_Normals.MakeExplicit selection:#{1..nArr.count}
  70.        EN_convertVS = amesh.Edit_Normals.ConvertVertexSelection
  71.        EN_setNormal = amesh.Edit_Normals.SetNormal
  72.        normID = #{}
  73.         
  74.        for v = 1 to nArr.count do
  75.        (
  76.           free normID
  77.           EN_convertVS #{v} &normID
  78.           for id in normID do EN_setNormal id nArr[v]
  79.        )
  80.        
  81.        if toggle == 1 do collapseStack amesh
  82.     )
  83.  
  84.     cui.expertModeOff()
  85. )
  86. fn addHexaDecimalPadding nr = (
  87.     case of (
  88.         (nr <= 15): ("0" + ((bit.intAsHex(nr))as string))
  89.         (nr > 15): ((bit.intAsHex(nr))as string)
  90.     )
  91. )    
  92. fn ReadFixedByteString bstream fixedLen = (
  93.    local str = ""
  94.    for i = 1 to fixedLen do
  95.    (
  96.       str += addHexaDecimalPadding (ReadByte bstream #unsigned)
  97.       if mod i 1 == 0 do str += " "
  98.    )
  99.    str
  100. )
  101.  
  102.  
  103. fname = GetOpenFileName caption:"Dragon Ball Xenoverse EMO" types:"(*.emo)|*.emo"
  104. If (fname!=undefined) and ((DoesFileExist fname)==true) then (--)
  105. f = fopen fname "rb"   --open file in read only format
  106. FileName = getFileNameFile fname
  107. FileExtension = getFileNameType  fname
  108. clearlistener()
  109. fscale=50
  110. delete $*
  111.  
  112. print (FileName + FileExtension) as string
  113.  
  114. GeomOffset=#()
  115. VertCount=#()
  116. VertSize=#()
  117. VertOffset=#()
  118. ukwcount=#()
  119. NameOffset=#()
  120. NameLength=#()
  121. Name_array=#()
  122. PolyElmCount=#()
  123. FaceOffset=#()
  124. FaceOffsetTable=#()
  125.  
  126.  
  127.  
  128. FileType = ReadFixedString f 4
  129. fseek f 0x10 #seek_set
  130. BoneOffset = ReadBElong f
  131. VertStart = ReadBElong f
  132. fseek f 0x20 #seek_set
  133. GeomCount = ReadBEshort f
  134. ukw = ReadBEshort f
  135. NameTableOffset = ReadBElong f + 0x20
  136.  
  137.     -- Getting Geometry Offsets --
  138. For x=1 to GeomCount do (
  139.     GeomOffset[x] = (ReadBElong f + 0x30)
  140. --     Print ("GeomOffset @ 0x"+((bit.intAsHex(GeomOffset[x]))as string))
  141. )
  142.     -- Getting Name Offsets --
  143. fseek f NameTableOffset #seek_set
  144. For x=1 to GeomCount do (
  145.     NameOffset[x] = ReadBElong f + 0x20
  146. --     Print ("NameOffset @ 0x"+((bit.intAsHex(NameOffset[x]))as string))
  147.  
  148. )
  149.     -- Reading Names --
  150. For x=1 to GeomCount do (
  151.     fseek f NameOffset[x] #seek_set
  152.     Name_array[x] = readstring f
  153. )
  154.  
  155. -- print Name_array as string
  156.  
  157.  
  158. Struct FaceOffsetArray_Group (
  159.     FaceOffsetArray
  160. )
  161. Struct FaceOffset2Array_Group (
  162.     FaceOffset2Array
  163. )
  164. Struct FaceCountArray_Group (
  165.     FaceCountArray
  166. )
  167. Struct MeshNameArray_Group (
  168.     MeshNameArray
  169. )
  170.  
  171. FaceOffset_array=#()
  172. FaceOffset2_array=#()
  173. FaceCount_array=#()
  174. MeshName_array=#()
  175.  
  176.  
  177.     -- Getting Geometry Data --
  178. For x=1 to GeomCount do (
  179.     FaceOffset=#()
  180.     FaceOffset2=#() -- Actual face offset
  181.     FaceCount=#()
  182.     MeshName=#()
  183.     
  184.     fseek f GeomOffset[x] #seek_set
  185.     SubType = ReadFixedString f 4
  186.     ukw = ReadBEshort f
  187.     ukw2 = ReadBEshort f
  188.     DataSkip = ReadBElong f
  189.     fseek f 0x4 #seek_cur
  190.     fseek f DataSkip #seek_cur
  191. --     print (dataskip / 10) as string
  192.     VertCount[x] = ReadBEshort f
  193.     VertSize[x] = ReadBEshort f
  194.     VertOffset[x] = (ReadBElong f + GeomOffset[x] + 0x10)
  195.     PolyElmCount[x] = ReadBElong f
  196.     FaceOffsetTable = (ReadBElong f + GeomOffset[x] + 0x10)
  197.     fseek f FaceOffsetTable #seek_set
  198.     
  199.     For y=1 to PolyElmCount[x] do (
  200.         FaceOffset[y] = (ReadBElong f + GeomOffset[x] + 0x20)
  201. --         Print ("FaceOffset is 0x"+((bit.intAsHex(FaceOffset[y]))as string))
  202.     )
  203.     For y=1 to PolyElmCount[x] do (
  204.         fseek f FaceOffset[y] #seek_set
  205.         FaceBufferID = ReadBEshort f
  206.         FaceCount[y] = ReadBEshort f
  207.         BackJump = ftell f
  208.         ukw1 = ReadBEshort f
  209.         MeshName[y] = readstring f
  210. --         Print (" Read @ 0x"+((bit.intAsHex(ftell f))as string))
  211.         fseek f BackJump #seek_set
  212.         fseek f 0x22 #seek_cur
  213.         FaceOffset2[y] = ftell f
  214.     )
  215.     
  216.     
  217.     append FaceOffset_array (FaceOffsetArray_Group FaceOffsetArray:FaceOffset)
  218.     append FaceOffset2_array (FaceOffset2Array_Group FaceOffset2Array:FaceOffset2)
  219.     append FaceCount_array (FaceCountArray_Group FaceCountArray:FaceCount)
  220.     append MeshName_array (MeshNameArray_Group MeshNameArray:MeshName)
  221.     
  222. --     Print "____________________________________"
  223.  
  224.     
  225.  
  226. --     Print ("Dataskip2 is 0x"+((bit.intAsHex(dataskip2))as string))
  227. --     Print ("Vertsize is 0x"+((bit.intAsHex(VertSize[x]))as string))
  228. --     Print ("Vertcount is 0x"+((bit.intAsHex(VertCount[x]))as string))
  229. --     Print ("Facecount is 0x"+((bit.intAsHex(FaceCount[x]))as string))
  230. --     Print ("PolyElmCount is 0x"+((bit.intAsHex(PolyElmCount[x]))as string))
  231. --     Print ("FaceOffsetTable is 0x"+((bit.intAsHex(FaceOffsetTable))as string))
  232. --     Print ("FaceOffsetTable is 0x"+((bit.intAsHex(FaceOffsetTable))as string))
  233. )
  234.  
  235. -- print polyelmcount as string
  236. -- print FaceOffset2_array as string
  237. -- print FaceCount_array as string
  238. -- print MeshName_array as string
  239.  
  240. BoneNameOffset=#()
  241. BoneName=#()
  242.  
  243.     -- Reading Bones --
  244. fseek f BoneOffset #seek_set
  245. BoneCount = ReadBEshort f
  246. Ukw2 = ReadBEshort f
  247. Ukw3 = ReadBElong f
  248. BoneStart1 = (ReadBElong f + BoneOffset)    -- This bone matrix is weird
  249. BoneNameTable = (ReadBElong f + BoneOffset)
  250. fseek f 0x8 #seek_cur
  251. Ukw4 = ReadBElong f    -- Really no reason why this here
  252. BoneStart2 = (ReadBElong f + BoneOffset)
  253.  
  254.  
  255. fseek f BoneNameTable #seek_set
  256. For x=1 to BoneCount do (
  257.     BoneNameOffset[x] = (ReadBElong f + BoneOffset)
  258. )
  259. For x=1 to BoneCount do (
  260.     fseek f BoneNameOffset[x] #seek_set
  261.     BoneName[x] = readstring f
  262. --     Print BoneName[x] as string
  263. )
  264.  
  265. -- Print ("BoneOffset @ 0x"+((bit.intAsHex(BoneOffset))as string))
  266.  
  267. -- fseek f BoneStart #seek_set
  268. -- for x=1 to BoneCount do (
  269. --     read = ReadFixedByteString f 0x10
  270. --     fseek f 0x40 #seek_cur
  271. --     print read
  272. -- )
  273.  
  274. fseek f BoneStart1 #seek_set
  275. BNArr = #()
  276. For x=1 to BoneCount do (
  277.     BoneParentID = ReadBEshort f + 1
  278.     BoneID = ReadBEshort f + 1
  279.     BoneSiblingID = ReadBEshort f + 1
  280.     fseek f 0xa #seek_cur
  281.     m11 = ReadBEfloat f; m12 = ReadBEfloat f; m13 = ReadBEfloat f; m14 = ReadBEfloat f
  282.     m21 = ReadBEfloat f; m22 = ReadBEfloat f; m23 = ReadBEfloat f; m24 = ReadBEfloat f
  283.     m31 = ReadBEfloat f; m32 = ReadBEfloat f; m33 = ReadBEfloat f; m34 = ReadBEfloat f
  284.     m41 = ReadBEfloat f; m42 = ReadBEfloat f; m43 = ReadBEfloat f; m44 = ReadBEfloat f
  285.     tfm = matrix3 [m11,m21,m31,m41] [m12,m22,m32,m42] [m13,m23,m33,m43] [m14,m24,m34,m44]    
  286.     newBone = bonesys.createbone    \
  287.     tfm.row4    \
  288.     (tfm.row4 + 0.01 * (normalize tfm.row1)) \
  289.     (normalize tfm.row3)
  290.     newBone.width = 0.1
  291.     newBone.height = 0.1
  292.     newBone.wirecolor = yellow
  293.     newbone.showlinks = true
  294.     newBone.name = BoneName[x]
  295.  
  296.     
  297.     pos = [m41,m42,m43]
  298.     pos = pos * tfm
  299.  
  300.     newBone.pos.x += ((fscale)*pos.x)
  301.     newBone.pos.y += ((fscale)*pos.z)
  302.     newBone.pos.z += ((fscale)*pos.y)    
  303.  
  304.     newBone.setBoneEnable false 0
  305.     newBone.pos.controller = TCB_position ()
  306.     newBone.rotation.controller = TCB_rotation ()
  307.  
  308.     if (BoneParentID != 0) then (
  309.     newBone.parent = BNArr[BoneParentID]
  310.     newBone.transform *= newBone.parent.transform
  311.    )
  312.        append BNArr newBone
  313. )
  314.  
  315.  
  316.     -- Reading Geometry --
  317. For x=1 to GeomCount do (
  318.     Vert_array=#()
  319.     UV_array=#()
  320.     Normal_array=#()
  321.     Face_array=#()
  322.     Weight_array=#()
  323.     
  324.     fseek f VertOffset[x] #seek_set
  325.     Print ("Verts @ 0x"+((bit.intAsHex(ftell f))as string))
  326.     
  327.     If VertSize[x] == 0x14 do (
  328.         for x=1 to VertCount[x] do (
  329.         vx = ReadBEfloat f
  330.         vy = ReadBEfloat f
  331.         vz = ReadBEfloat f
  332.         fseek f 0x8 #seek_cur
  333.         append Vert_array([vx,vz,vy]*fscale)
  334.         append UV_array[0,0,0]
  335.         )
  336.     )
  337.     If VertSize[x] == 0x18 do (
  338.         for x=1 to VertCount[x] do (
  339.         vx = ReadBEfloat f
  340.         vy = ReadBEfloat f
  341.         vz = ReadBEfloat f
  342.         fseek f 0xc #seek_cur
  343.         append Vert_array([vx,vz,vy]*fscale)
  344.         append UV_array[0,0,0]
  345.         )
  346.     )
  347.     If VertSize[x] == 0x1c do (    -- Always have trouble with this guy.
  348.         for x=1 to VertCount[x] do (
  349.         vx = ReadBEfloat f
  350.         vy = ReadBEfloat f
  351.         vz = ReadBEfloat f
  352.         fseek f 0x10 #seek_cur
  353.         append Vert_array([vx,vz,vy]*fscale)
  354.         append UV_array[0,0,0]
  355.         )
  356.     )
  357.     If VertSize[x] == 0x20 do (
  358.         for x=1 to VertCount[x] do (
  359.         vx = ReadBEfloat f
  360.         vy = ReadBEfloat f
  361.         vz = ReadBEfloat f
  362.         n1 = ReadBEHalfFloat f    
  363.         n2 = ReadBEHalfFloat f
  364.         n3 = ReadBEHalfFloat f
  365.         fseek f 0xe #seek_cur
  366.         append Vert_array([vx,vz,vy]*fscale)
  367.         append UV_array[0,0,0]
  368.         append Normal_array [n1,n3,n2]
  369.         )
  370.     )
  371.     If VertSize[x] == 0x24 do (
  372.         for x=1 to VertCount[x] do (
  373.         w = (weight_data boneids:#() weights:#())
  374.         vx = ReadBEfloat f
  375.         vy = ReadBEfloat f
  376.         vz = ReadBEfloat f
  377.         n1 = ReadBEHalfFloat f    
  378.         n2 = ReadBEHalfFloat f
  379.         n3 = ReadBEHalfFloat f
  380.         null = ReadBEHalfFloat f    -- always 2
  381.         tu=ReadBEHalfFloat f
  382.         tv=ReadBEHalfFloat f
  383.         
  384.         bone4 = readbyte f #unsigned
  385.         bone3 = readbyte f #unsigned
  386.         bone2 = readbyte f #unsigned
  387.         bone1 = readbyte f #unsigned
  388.         weight1 = ReadBEHalfFloat f
  389.         weight2 = ReadBEHalfFloat f
  390.         weight3 = ReadBEHalfFloat f
  391.         weight4 = ReadBEHalfFloat f
  392.         weight4 = 1 - (weight1+weight2+weight3)
  393.             
  394.         maxweight = 0
  395.         if(bone1 != 0xFF) then
  396.             maxweight = maxweight + weight1
  397.         if(bone2 != 0xFF) then
  398.             maxweight = maxweight + weight2
  399.         if(bone3 != 0xFF) then
  400.             maxweight = maxweight + weight3
  401.         if(bone4 != 0xFF) then
  402.             maxweight = maxweight + weight4
  403.             
  404.         if(maxweight != 0) then (
  405.             mxw = 255.0
  406.             if(bone1 != 0xFF) then (
  407.                 w1 = weight1 as float
  408.                 append w.boneids (bone1+1)
  409.                 append w.weights (w1 / mxw)
  410.             )
  411.             if(bone2 != 0xFF) then (
  412.                 w2 = weight2 as float
  413.                 append w.boneids (bone2+1)
  414.                 append w.weights (w2 / mxw)
  415.             )
  416.             if(bone3 != 0xFF) then (
  417.                 w3 = weight3 as float
  418.                 append w.boneids (bone3+1)
  419.                 append w.weights (w3 / mxw)
  420.             )
  421.             if(bone4 != 0xFF) then (
  422.                 w4 = weight4 as float
  423.                 append w.boneids (bone4+1)
  424.                 append w.weights (w4 / mxw)
  425.             )        
  426.         )
  427.         
  428.         append Vert_array([vx,vz,vy]*fscale)
  429.         append UV_array([tu,(tv*-1),0]*2)
  430.         append Normal_array [n1,n3,n2]
  431.         append Weight_array w
  432.         )
  433.     )
  434.     If VertSize[x] == 0x2c do (
  435.         for x=1 to VertCount[x] do (
  436.         vx = ReadBEfloat f
  437.         vy = ReadBEfloat f
  438.         vz = ReadBEfloat f
  439.         fseek f 0x20 #seek_cur
  440.         append Vert_array([vx,vz,vy]*fscale)
  441.         append UV_array[0,0,0]
  442.         )
  443.     )
  444.     If VertSize[x] == 0x30 do (
  445.         for x=1 to VertCount[x] do (
  446.         w = (weight_data boneids:#() weights:#())
  447.         vx = ReadBEfloat f
  448.         vy = ReadBEfloat f
  449.         vz = ReadBEfloat f
  450.         n1 = ReadBEfloat f
  451.         n2 = ReadBEfloat f
  452.         n3 = ReadBEfloat f
  453.         tu=ReadBEFloat f
  454.         tv=ReadBEFloat f
  455.         bone4 = readbyte f #unsigned
  456.         bone3 = readbyte f #unsigned
  457.         bone2 = readbyte f #unsigned
  458.         bone1 = readbyte f #unsigned
  459.         weight1 = ReadBEfloat f
  460.         weight2 = ReadBEfloat f
  461.         weight3 = ReadBEfloat f
  462.         weight4 = 0
  463.         weight4 = 1 - (weight1+weight2+weight3)
  464.         maxweight = 0
  465.         if(bone1 != 0xFF) then
  466.             maxweight = maxweight + weight1
  467.         if(bone2 != 0xFF) then
  468.             maxweight = maxweight + weight2
  469.         if(bone3 != 0xFF) then
  470.             maxweight = maxweight + weight3
  471.         if(bone4 != 0xFF) then
  472.             maxweight = maxweight + weight4
  473.             
  474.         if(maxweight != 0) then (
  475.             mxw = 255.0
  476.             if(bone1 != 0xFF) then (
  477.                 w1 = weight1 as float
  478.                 append w.boneids (bone1+1)
  479.                 append w.weights (w1 / mxw)
  480.             )
  481.             if(bone2 != 0xFF) then (
  482.                 w2 = weight2 as float
  483.                 append w.boneids (bone2+1)
  484.                 append w.weights (w2 / mxw)
  485.             )
  486.             if(bone3 != 0xFF) then (
  487.                 w3 = weight3 as float
  488.                 append w.boneids (bone3+1)
  489.                 append w.weights (w3 / mxw)
  490.             )
  491.             if(bone4 != 0xFF) then (
  492.                 w4 = weight4 as float
  493.                 append w.boneids (bone4+1)
  494.                 append w.weights (w4 / mxw)
  495.             )
  496.         )
  497.             
  498. --         read = ReadFixedByteString f 0x10
  499. --         print read
  500.         append Vert_array([vx,vz,vy]*fscale)
  501.         append UV_array([tu,(tv*-1),0]*2)
  502.         append Normal_array [n1,n3,n2]
  503.         append Weight_array w
  504.         )
  505.     )
  506.     
  507.     For y=1 to PolyElmCount[x] do (
  508.         fseek f FaceOffset2_array[x].FaceOffset2Array[y] #seek_set
  509. --         Print ("Face start @ 0x"+((bit.intAsHex(ftell f))as string))
  510. --         Print ("Face count is 0x"+((bit.intAsHex(FaceCount_array[x].FaceCountArray[y]))as string))
  511.         
  512.         For z=1 to (FaceCount_array[x].FaceCountArray[y] / 3) do(
  513.             fa=ReadBEShort f +1
  514.             fb=ReadBEShort f +1
  515.             fc=ReadBEShort f +1
  516.             append Face_array[fc,fb,fa]
  517.         )
  518.         
  519.     )
  520.     msh = mesh vertices:Vert_array faces:Face_array
  521.     msh.numTVerts = UV_array.count
  522.     buildTVFaces msh
  523. --     msh.name = Name_array[x]
  524.     msh.name = (bit.intAsHex(VertSize[x]))
  525. --     msh.wirecolor = (color 255 255 255)
  526.     for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  527.     for j = 1 to Face_array.count do setTVFace msh j Face_array[j]
  528. --     ApplyNormals2 msh Normal_array 1
  529.     Loadskel = false
  530.     If LoadSkel == true do (
  531.         -- Applying Skinning --
  532.         count = msh.numverts
  533.         max modify mode
  534.         select msh
  535.         skinMod = skin ()
  536.         addModifier msh skinMod
  537.         for i = 1 to BoneCount do (
  538.             maxbone = getnodebyname BNArr[i].name
  539.             skinOps.addBone skinMod maxbone 1
  540.         )
  541.         -- Applying Weights --
  542.         select msh
  543.         modPanel.setCurrentObject skinMod
  544.         for i = 1 to weight_array.count do (
  545.             w = Weight_array[i]
  546.             bi = #() --bone index array
  547.             wv = #() --weight value array
  548.             
  549.             for j = 1 to w.boneids.count do
  550.             (
  551.                 boneid = w.boneids[j]
  552.                 weight = w.weights[j]
  553.                 append bi boneid
  554.                 append wv weight
  555.             )    
  556.             
  557.             skinOps.ReplaceVertexWeights skinMod i bi wv
  558.         )
  559.     )
  560.     
  561. )
  562.  
  563.  
  564.  
  565.  
  566. Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))
  567. )
  568.  
  569. select $*
  570. -- actionMan.executeAction 0 "310"  -- Tools: Zoom Extents Selected
  571. deselect $*
  572. /* 12/06/14
  573.     1:23am lol
  574. */
  575.  
  576. gc()
  577. fclose f