Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 27.68 KB | None | 0 0
  1. """
  2.     Megaman Legends 2 Noesis Addon
  3.     Copyright 2017, 2018 Satoh
  4.    
  5.    Permission is hereby granted, free of charge, to any person obtaining
  6.    a copy of this software and associated documentation files (the
  7.    "Software"), to deal in the Software without restriction, including
  8.    without limitation the rights to use, copy, modify, merge, publish,
  9.    distribute, sublicense, and/or sell copies of the Software, and to
  10.    permit persons to whom the Software is furnished to do so, subject to
  11.    the following conditions:
  12.    
  13.    The above copyright notice and this permission notice shall be included    
  14.    in all copies or substantial portions of the Software.
  15.    
  16.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19.    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20.    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21.    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22.    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. """
  25.  
  26. #MML2 PC DAT models.
  27. #step one: load an extracted X01 file
  28. #step two: discover and load X01s inside a DAT
  29. #step three: discover and load texture and palette data inside a DAT
  30. #step four: apply textures to loaded models
  31. #Current Step: One
  32. from inc_noesis import *
  33.  
  34. import noesis
  35.  
  36. #rapi methods should only be used during handler callbacks
  37. import rapi
  38.  
  39. #registerNoesisTypes is called by Noesis to allow the script to register formats.
  40. #Do not implement this function in script files unless you want them to be dedicated format modules!
  41. def registerNoesisTypes():
  42.     handle = noesis.register("MML2PC Dat(X1C)", ".x01")
  43.     noesis.setHandlerTypeCheck(handle, mml2CheckType)
  44.     noesis.setHandlerLoadModel(handle, mml2LoadModel)
  45.     #noesis.logPopup()
  46.         #print("The log can be useful for catching debug prints from preview loads.\nBut don't leave it on when you release your script, or it will probably annoy people.")
  47.     return 1
  48.    
  49. #check if it's this type based on the data
  50. def mml2CheckType(data):
  51.     #bs = NoeBitStream(data)
  52.     # if len(data) < 16:
  53.         # return 0
  54.     # if bs.readUInt()&0xFF < 1:
  55.         # return 0
  56.     #print("check pass")
  57.     return 1        
  58.  
  59. #load the model
  60. def mml2LoadModel(data, mdlList):
  61.     ctx = rapi.rpgCreateContext()
  62.     bs = NoeBitStream(data)
  63.     x01start = bs.tell()
  64.     #ofs(bs,"Start")
  65.     mdl = checkType0(bs, x01start)
  66.     if(mdl!=None):
  67.         mdlList.append(mdl)
  68.         rapi.rpgReset()
  69.     loadx01(bs, x01start, mdlList)
  70.    
  71.     return 1
  72.    
  73. def checkType0(bs, x01start):
  74.     checkOfs = bs.readUShort()
  75.     checkType = bs.readUShort()
  76.     mdl = None
  77.     x40 = 0
  78.     while(checkType > 0xFF):
  79.         here = bs.tell()-4
  80.         #p("here",hex(here))
  81.         if (checkType == 0x4000):
  82.             tcnt = bs.readUByte()
  83.             qcnt = bs.readUByte()
  84.             vcnt = bs.readUByte()
  85.             scale = bs.readUByte()
  86.             tloc = bs.readUInt()
  87.             qloc = bs.readUInt()
  88.             vloc = bs.readUInt()
  89.             tcol = bs.readUInt()
  90.             qcol = bs.readUInt()
  91.            
  92.             rapi.rpgSetName("Model_40_Mesh_"+str(hex(x40)))
  93.             rapi.rpgSetPosScaleBias(NoeVec3((1.0*(2 ** scale)/256, 1.0*(2 ** scale)/256, 1.0*(2 ** scale)/256)),
  94.                                     NoeVec3((0, 0, 0 )) )
  95.            
  96.             #seek for the vertices
  97.             #p(hex(x01start + hi_hdr[part][VERTOFS]))
  98.             bs.seek(x01start + vloc,NOESEEK_ABS)
  99.             #ofs(bs,"v",part, mdlidx)
  100.             vtx = []
  101.             for v in range(0,vcnt):
  102.                 x,y,z = decode10bit(bs.read("I")[0])
  103.                 vtx.append(x)#+boneposlist[bonecompat[part]][0])
  104.                 vtx.append(y)#+boneposlist[bonecompat[part]][1])
  105.                 vtx.append(z)#+boneposlist[bonecompat[part]][2])
  106.             #p(vtx)
  107.             #p(mdlidx, part, vtx)
  108.             cols = []
  109.             bs.seek(tcol,NOESEEK_ABS)
  110.             for v in range(0,vcnt):
  111.                 cols.append(bs.readUInt())
  112.             colofs = bs.tell()
  113.             #p(len(cols),hi_hdr[part][VERTCNT])
  114.            
  115.            
  116.             newverts = []
  117.             newtris = []
  118.             newuvs = []
  119.             newcols = []
  120.            
  121.             uvs = []
  122.             tris = []
  123.             for uv in range(0,vcnt):
  124.               uvs.append(0)
  125.             #seek to Solo
  126.             #uv2 = []
  127.             bid = []
  128.             bs.seek(x01start + tloc,NOESEEK_ABS)
  129.             #ofs(bs,"t",part, mdlidx)
  130.             for tri in range(0,tcnt):
  131.                 uv2 = []
  132.                 uv2.append(bs.readUShort())
  133.                 uv2.append(bs.readUShort())
  134.                 uv2.append(bs.readUShort())
  135.                 bs.readBytes(2)
  136.                 #the order is 1 3 2, 4 2 3
  137.                 a,b,c,d = decodefaces(bs.readUInt())
  138.                 tris.append(a)
  139.                 tris.append(b)
  140.                 tris.append(c)
  141.                 uvs[a] = uv2[0]
  142.                 uvs[b] = uv2[1]
  143.                 uvs[c] = uv2[2]
  144.                 newverts.append(vtx[a*3+0])
  145.                 newverts.append(vtx[a*3+1])
  146.                 newverts.append(vtx[a*3+2])
  147.                 newverts.append(vtx[b*3+0])
  148.                 newverts.append(vtx[b*3+1])
  149.                 newverts.append(vtx[b*3+2])
  150.                 newverts.append(vtx[c*3+0])
  151.                 newverts.append(vtx[c*3+1])
  152.                 newverts.append(vtx[c*3+2])
  153.                 newcols.append(cols[a])
  154.                 newcols.append(cols[b])
  155.                 newcols.append(cols[c])
  156.                 newtris.append(tri*3+0)
  157.                 newtris.append(tri*3+2)
  158.                 newtris.append(tri*3+1)
  159.                 newuvs.append(uv2[0])
  160.                 newuvs.append(uv2[1])
  161.                 newuvs.append(uv2[2])
  162.            
  163.             #seek to dual
  164.             bs.seek(x01start + qloc,NOESEEK_ABS)
  165.             #ofs(bs,"q",part, mdlidx)
  166.             for quad in range(0,qcnt):
  167.                 uv2 = []
  168.                 uv2.append(bs.readUShort())
  169.                 uv2.append(bs.readUShort())
  170.                 uv2.append(bs.readUShort())
  171.                 uv2.append(bs.readUShort())
  172.                 #the order is 1 3 2, 4 2 3
  173.                 a,b,c,d = decodefaces(bs.readUInt())
  174.                 tris.append(a)
  175.                 tris.append(c)
  176.                 tris.append(b)
  177.                 tris.append(d)
  178.                 tris.append(b)
  179.                 tris.append(c)
  180.                 uvs[a] = uv2[0]
  181.                 uvs[b] = uv2[1]
  182.                 uvs[c] = uv2[2]
  183.                 uvs[d] = uv2[3]
  184.                 newverts.append(vtx[a*3+0])
  185.                 newverts.append(vtx[a*3+1])
  186.                 newverts.append(vtx[a*3+2])
  187.                 newverts.append(vtx[b*3+0])
  188.                 newverts.append(vtx[b*3+1])
  189.                 newverts.append(vtx[b*3+2])
  190.                 newverts.append(vtx[c*3+0])
  191.                 newverts.append(vtx[c*3+1])
  192.                 newverts.append(vtx[c*3+2])
  193.                 newverts.append(vtx[d*3+0])
  194.                 newverts.append(vtx[d*3+1])
  195.                 newverts.append(vtx[d*3+2])
  196.                 newcols.append(cols[a])
  197.                 newcols.append(cols[b])
  198.                 newcols.append(cols[c])
  199.                 newcols.append(cols[d])
  200.                 newtris.append(quad*4+0+(tcnt*3))
  201.                 newtris.append(quad*4+2+(tcnt*3))
  202.                 newtris.append(quad*4+1+(tcnt*3))
  203.                 newtris.append(quad*4+3+(tcnt*3))
  204.                 newtris.append(quad*4+1+(tcnt*3))
  205.                 newtris.append(quad*4+2+(tcnt*3))
  206.                 newuvs.append(uv2[0])
  207.                 newuvs.append(uv2[1])
  208.                 newuvs.append(uv2[2])
  209.                 newuvs.append(uv2[3])
  210.                
  211.             #for u in range(0,len(uvs)):
  212.             #    print(u,(uvs[u]&0xFF)/256,(uvs[u]>>8&0xFF)/256)
  213.             #verts = struct.pack("f"*len(vtx),*vtx)
  214.             #uvbuffer  = struct.pack("H"*len(uvs),*uvs)
  215.             #tribuffer = struct.pack("H"*len(tris),*tris)
  216.             #bwt = []
  217.             #print(len(newcols)==(len(newverts)//3))
  218.             #beginmerge = []
  219.             #endmerge = []
  220.             #for v in range(0,len(newverts)//3):
  221.                
  222.             #    if(newcols[v] >> 30 & 0x01 == 0x01):
  223.                     #endmerge.append([v,newcols[v] >> 24 & 0x03])
  224.             #        bid.append(boneparent[part])
  225.             #    else:
  226.             #        bid.append(part)
  227.             #    bwt.append(1.0)
  228.             #p(len(newcols),len(bid),len(newverts)//3)
  229.             #for v in range(0,len(bid)):
  230.                 #this is complicated. vertices belong to the parent bone when...
  231.                 #their corresponding vertex color's alpha byte is greater than 00...
  232.                 #and there may be some flags at play that determine exactly how...
  233.                 #the game merges these vertices.
  234.                 #so, we have to modify the existing bone weights and potentially positions
  235.                 #so far, best I can tell, 0x80 is a flag for which vert to merge to, and...
  236.                 # flag 0xC0 is for which vertex to merge from.
  237.                 # However, I have seen 0xD0 and 0xE0, meaning there could be a 6 bit
  238.                 # integer after the initial two bit flag
  239.             #    if(newcols[v] > 0xFFFFFF):
  240.             #        bid[v] = boneparent[part]
  241.                
  242.            
  243.             #idxData = struct.pack("i"*len(bid),*bid)
  244.             #valData = struct.pack("f"*len(bwt),*bwt)
  245.            
  246.            
  247.             #rapi.rpgBindBoneIndexBuffer(idxData, noesis.RPGEODATA_INT, 4, 1)
  248.             #rapi.rpgBindBoneWeightBuffer(valData, noesis.RPGEODATA_FLOAT, 4, 1)
  249.            
  250.             colbuffer = struct.pack("I"*len(newcols),*newcols)
  251.             verts = struct.pack("f"*len(newverts),*newverts)
  252.             uvbuffer  = struct.pack("H"*len(newuvs),*newuvs)
  253.             tribuffer = struct.pack("H"*len(newtris),*newtris)
  254.            
  255.            
  256.            
  257.             #p(len(newcols),len(newverts))
  258.            
  259.             #print(len(list(uvbuffer)))
  260.             rapi.rpgBindPositionBuffer(verts, noesis.RPGEODATA_FLOAT, 12)
  261.             rapi.rpgBindUV1Buffer(uvbuffer, noesis.RPGEODATA_UBYTE, 2)
  262.             #rapi.rpgBindColorBuffer(colbuffer, noesis.RPGEODATA_UBYTE, 4, 3)
  263.             rapi.rpgCommitTriangles(tribuffer, noesis.RPGEODATA_USHORT, len(tris), noesis.RPGEO_TRIANGLE, 1)
  264.             #rapi.rpgCommitTriangles(None, noesis.RPGEODATA_USHORT, len(vtx)//3, noesis.RPGEO_POINTS, 1)
  265.             #rapi.rpgOptimize()
  266.         x40 += 1
  267.         mdl = rapi.rpgConstructModel()
  268.         #bonelist = rapi.multiplyBones(bonelist)
  269.         #mdl.setBones(bonelist)
  270.         bs.seek(here,NOESEEK_ABS)
  271.         #ofs(bs)
  272.         bs.seek(checkOfs,NOESEEK_REL)
  273.         #ofs(bs)
  274.         checkOfs = bs.readUShort()
  275.         checkType = bs.readUShort()
  276.         #if(checkOfs % 4 != 0):
  277.         #    p("error in offset reading")
  278.         #    break
  279.    
  280.     bs.seek(-4,NOESEEK_REL)
  281.     return mdl
  282.  
  283. def loadx1c(bs, mdlidx, x01start, offset):
  284.     #info for our x1c headers
  285.     #Sections
  286.     HI      = 0 #High Res
  287.     MED     = 1 #Medium Res
  288.     LO      = 2 #Low Res
  289.     BONEPOS = 3 #Bone positioning list
  290.     BONEPNT = 4 #Bone parent list
  291.     MAT     = 5 #Material texture and palette info
  292.     HILT    = 7 #Vertex color/lighting for High
  293.     MEDLT   = 8 #Vertex color/lighting for Medium
  294.     LOLT    = 9 #Vertex color/lighting for Low
  295.     ############################################
  296.     #info for HI headers
  297.     SOLOCNT = 0 #Tri count
  298.     DUALCNT = 1 #Quad count
  299.     VERTCNT = 2 #this is definitely the vertex count.
  300.     SCALE   = 3 #Scale factor as a power of two
  301.     SOLOOFS = 4 #offset for solo
  302.     DUALOFS = 5 #offset for dual
  303.     VERTOFS = 6 #offset for verts
  304.     ############################################
  305.    
  306.     #go to the beginning of this model
  307.     bs.seek(x01start+offset, NOESEEK_ABS)
  308.     #ofs(bs)
  309.     #Read the parts count for each LOD
  310.     #Note that parts are separate meshes that belong to the same model
  311.     #This can also be understood as a bone count, since there is one part per bone
  312.     hicnt = bs.readUByte()
  313.     medcnt = bs.readUByte()
  314.     lowcnt = bs.readUByte()
  315.     #I THINK this is the texture count.
  316.     texcnt = bs.readUByte()
  317.     x1c_hdr = []
  318.     hi_hdr = []
  319.     bonepart = []
  320.     boneparent = []
  321.     bonecompat = []
  322.     boneflags = []
  323.    
  324.     PARTID = 0 #The bone ID
  325.     PARENT = 1 #The Parent's ID
  326.     COMPAT = 2 #The ID of the bone this is an alternate of, used for held items for turning on and off.
  327.     #and the last one is ?? Some kind of flag I'm sure. Might be scale
  328.     boneposlist = []
  329.     bonelist = []
  330.    
  331.    
  332.     for h in range(0,10):
  333.         x1c_hdr.append(bs.readUInt())
  334.    
  335.     bs.seek(x01start+x1c_hdr[HI])
  336.     for hdr in range(0,hicnt):
  337.         hi_hdr.append([bs.readUByte(),bs.readUByte(),
  338.                         bs.readUByte(),bs.readUByte(),
  339.                         bs.readUInt(),bs.readUInt(),
  340.                         bs.readUInt()])
  341.    
  342.     colofs = x01start+x1c_hdr[HILT]
  343.     #p(colofs)
  344.    
  345.     #read parent infos
  346.     bs.seek(x01start + x1c_hdr[BONEPNT],NOESEEK_ABS)
  347.     for part in range(0,hicnt):
  348.         #p(hex(hi_hdr[part][VERTCNT]))
  349.         bonepart.append(bs.readByte())
  350.         boneparent.append(bs.readByte())
  351.         bonecompat.append(bs.readByte())
  352.         boneflags.append(bs.readByte())
  353.     #p(bonepart)
  354.     #read pos modifiers
  355.     bs.seek(x01start + x1c_hdr[BONEPOS],NOESEEK_ABS)
  356.     for part in range(0,hicnt):    
  357.         boneposlist.append([bs.readShort(),-bs.readShort(),-bs.readShort()])
  358.        
  359.    
  360.     for part in range(0,hicnt):
  361.         #p(hex(hi_hdr[part][VERTCNT]))
  362.         pp = part#bonepart.index(part)
  363.         b = NoeBone(part,"Bone_"+str(part),
  364.                     NoeMat43(( NoeVec3((1.0, 0.0, 0.0)),
  365.                     NoeVec3((0.0, 1.0, 0.0)),
  366.                     NoeVec3((0.0, 0.0, 1.0)),
  367.                     NoeVec3((0.0, 0.0, 0.0 )) ) ))
  368.         if(boneparent[pp] == max(boneparent)):
  369.             b.parentIndex = 0
  370.         else:
  371.             b.parentIndex = boneparent[pp]
  372.         b.setMatrix(NoeMat43(( NoeVec3((1.0, 0.0, 0.0)),
  373.                                 NoeVec3((0.0, 1.0, 0.0)),
  374.                                 NoeVec3((0.0, 0.0, 1.0)),
  375.                                 NoeVec3((boneposlist[bonecompat[part]][0]/256,
  376.                                         boneposlist[bonecompat[part]][1]/256,
  377.                                         boneposlist[bonecompat[part]][2]/256 )) ) ))
  378.         if(bonecompat[part] != part):
  379.             b.parentIndex = bonecompat[part]
  380.             b.setMatrix(NoeMat43(( NoeVec3((1.0, 0.0, 0.0)),
  381.                                 NoeVec3((0.0, 1.0, 0.0)),
  382.                                 NoeVec3((0.0, 0.0, 1.0)),
  383.                                 NoeVec3((0, 0, 0 )) ) ))
  384.         bonelist.append(b)
  385.        
  386.         rapi.rpgSetName("Model_"+str(mdlidx)+"_Mesh_"+str(hex(part)))
  387.        
  388.         xm = boneposlist[bonecompat[part]][0]/256
  389.         ym = boneposlist[bonecompat[part]][1]/256
  390.         zm = boneposlist[bonecompat[part]][2]/256
  391.         while(boneparent[pp] != max(boneparent)):
  392.             pp = boneparent[pp]
  393.             xm += boneposlist[bonecompat[pp]][0]/256
  394.             ym += boneposlist[bonecompat[pp]][1]/256
  395.             zm += boneposlist[bonecompat[pp]][2]/256
  396.         #rapi.rpgSetPosScaleBias(NoeVec3((1.0, 1.0, 1.0)),
  397.         #                        NoeVec3((boneposlist[bonecompat[part]][0],
  398.         #                                boneposlist[bonecompat[part]][1],
  399.         #                                boneposlist[bonecompat[part]][2] )) )
  400.         rapi.rpgSetPosScaleBias(NoeVec3((1.0*(2 ** hi_hdr[part][SCALE])/256, 1.0*(2 ** hi_hdr[part][SCALE])/256, 1.0*(2 ** hi_hdr[part][SCALE])/256)),
  401.                                 NoeVec3((xm, ym, zm )) )
  402.        
  403.         #seek for the vertices
  404.         #p(hex(x01start + hi_hdr[part][VERTOFS]))
  405.         bs.seek(x01start + hi_hdr[part][VERTOFS],NOESEEK_ABS)
  406.         #ofs(bs,"v",part, mdlidx)
  407.         vtx = []
  408.         for v in range(0,hi_hdr[part][VERTCNT]):
  409.             x,y,z = decode10bit(bs.read("I")[0])
  410.             vtx.append(x)#+boneposlist[bonecompat[part]][0])
  411.             vtx.append(y)#+boneposlist[bonecompat[part]][1])
  412.             vtx.append(z)#+boneposlist[bonecompat[part]][2])
  413.         #p(vtx)
  414.         #p(mdlidx, part, vtx)
  415.         cols = []
  416.         bs.seek(colofs,NOESEEK_ABS)
  417.         for v in range(0,hi_hdr[part][VERTCNT]):
  418.             cols.append(bs.readUInt())
  419.         colofs = bs.tell()
  420.         #p(len(cols),hi_hdr[part][VERTCNT])
  421.        
  422.        
  423.         newverts = []
  424.         newtris = []
  425.         newuvs = []
  426.         newcols = []
  427.        
  428.         uvs = []
  429.         tris = []
  430.         for uv in range(0,hi_hdr[part][VERTCNT]):
  431.           uvs.append(0)
  432.         #seek to Solo
  433.         #uv2 = []
  434.         bid = []
  435.         bs.seek(x01start + hi_hdr[part][SOLOOFS],NOESEEK_ABS)
  436.         #ofs(bs,"t",part, mdlidx)
  437.         for tri in range(0,hi_hdr[part][SOLOCNT]):
  438.             uv2 = []
  439.             uv2.append(bs.readUShort())
  440.             uv2.append(bs.readUShort())
  441.             uv2.append(bs.readUShort())
  442.             bs.readBytes(2)
  443.             #the order is 1 3 2, 4 2 3
  444.             a,b,c,d = decodefaces(bs.readUInt())
  445.             tris.append(a)
  446.             tris.append(b)
  447.             tris.append(c)
  448.             uvs[a] = uv2[0]
  449.             uvs[b] = uv2[1]
  450.             uvs[c] = uv2[2]
  451.             # vct1 = hi_hdr[part][VERTCNT] - 1
  452.             # vct2 = vct1 - 1
  453.             # vct3 = vct2 - 1
  454.             # vct4 = vct3 - 1
  455.             # if((a == vct1 or a == vct4 or a == vct2 or a == vct3) and
  456.                 # boneflags[part] & 0x40 == 0x40 and
  457.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  458.                 # bid.append(boneparent[part])
  459.             # else:
  460.                 # bid.append(part)
  461.             # if((b == vct4 or b == vct1 or b == vct2 or b == vct3) and
  462.                 # boneflags[part] & 0x40 == 0x40 and
  463.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  464.                 # bid.append(boneparent[part])
  465.             # else:
  466.                 # bid.append(part)
  467.             # if((c == vct4 or c == vct1 or c == vct2 or c == vct3) and
  468.                 # boneflags[part] & 0x40 == 0x40 and
  469.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  470.                 # bid.append(boneparent[part])
  471.             # else:
  472.                 # bid.append(part)
  473.             #print ((uv2[0]&0xFF)/256,(uv2[0]>>8&0xFF)/256)
  474.             #print ((uv2[1]&0xFF)/256,(uv2[1]>>8&0xFF)/256)
  475.             #print ((uv2[2]&0xFF)/256,(uv2[2]>>8&0xFF)/256)
  476.             newverts.append(vtx[a*3+0])
  477.             newverts.append(vtx[a*3+1])
  478.             newverts.append(vtx[a*3+2])
  479.             newverts.append(vtx[b*3+0])
  480.             newverts.append(vtx[b*3+1])
  481.             newverts.append(vtx[b*3+2])
  482.             newverts.append(vtx[c*3+0])
  483.             newverts.append(vtx[c*3+1])
  484.             newverts.append(vtx[c*3+2])
  485.             newcols.append(cols[a])
  486.             newcols.append(cols[b])
  487.             newcols.append(cols[c])
  488.             # newcols.append(cols[b])
  489.             # newcols.append(cols[b])
  490.             # newcols.append(cols[b])
  491.             # newcols.append(cols[c])
  492.             # newcols.append(cols[c])
  493.             # newcols.append(cols[c])
  494.             newtris.append(tri*3+0)
  495.             newtris.append(tri*3+2)
  496.             newtris.append(tri*3+1)
  497.             newuvs.append(uv2[0])
  498.             newuvs.append(uv2[1])
  499.             newuvs.append(uv2[2])
  500.        
  501.         #seek to dual
  502.         bs.seek(x01start + hi_hdr[part][DUALOFS],NOESEEK_ABS)
  503.         #ofs(bs,"q",part, mdlidx)
  504.         for quad in range(0,hi_hdr[part][DUALCNT]):
  505.             uv2 = []
  506.             uv2.append(bs.readUShort())
  507.             uv2.append(bs.readUShort())
  508.             uv2.append(bs.readUShort())
  509.             uv2.append(bs.readUShort())
  510.             #the order is 1 3 2, 4 2 3
  511.             a,b,c,d = decodefaces(bs.readUInt())
  512.             tris.append(a)
  513.             tris.append(c)
  514.             tris.append(b)
  515.             tris.append(d)
  516.             tris.append(b)
  517.             tris.append(c)
  518.             uvs[a] = uv2[0]
  519.             uvs[b] = uv2[1]
  520.             uvs[c] = uv2[2]
  521.             uvs[d] = uv2[3]
  522.             # vct1 = hi_hdr[part][VERTCNT] - 1
  523.             # vct2 = vct1 - 1
  524.             # vct3 = vct2 - 1
  525.             # vct4 = vct3 - 1
  526.             # if((a == vct1 or a == vct4 or a == vct2 or a == vct3) and
  527.                 # boneflags[part] & 0x40 == 0x40 and
  528.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  529.                 # bid.append(boneparent[part])
  530.             # else:
  531.                 # bid.append(part)
  532.             # if((b == vct4 or b == vct1 or b == vct2 or b == vct3) and
  533.                 # boneflags[part] & 0x40 == 0x40 and
  534.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  535.                 # bid.append(boneparent[part])
  536.             # else:
  537.                 # bid.append(part)
  538.             # if((c == vct4 or c == vct1 or c == vct2 or c == vct3) and
  539.                 # boneflags[part] & 0x40 == 0x40 and
  540.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  541.                 # bid.append(boneparent[part])
  542.             # else:
  543.                 # bid.append(part)
  544.             # if((d == vct4 or d == vct1 or d == vct2 or d == vct3) and
  545.                 # boneflags[part] & 0x40 == 0x40 and
  546.                 # boneflags[boneparent[bonecompat[part]]] & 0x40 == 0x40):
  547.                 # bid.append(boneparent[part])
  548.             # else:
  549.                 # bid.append(part)
  550.             #print ((uv2[0]&0xFF)/256,(uv2[0]>>8&0xFF)/256)
  551.             #print ((uv2[1]&0xFF)/256,(uv2[1]>>8&0xFF)/256)
  552.             #print ((uv2[2]&0xFF)/256,(uv2[2]>>8&0xFF)/256)
  553.             #print ((uv2[3]&0xFF)/256,(uv2[3]>>8&0xFF)/256)
  554.             newverts.append(vtx[a*3+0])
  555.             newverts.append(vtx[a*3+1])
  556.             newverts.append(vtx[a*3+2])
  557.             newverts.append(vtx[b*3+0])
  558.             newverts.append(vtx[b*3+1])
  559.             newverts.append(vtx[b*3+2])
  560.             newverts.append(vtx[c*3+0])
  561.             newverts.append(vtx[c*3+1])
  562.             newverts.append(vtx[c*3+2])
  563.             newverts.append(vtx[d*3+0])
  564.             newverts.append(vtx[d*3+1])
  565.             newverts.append(vtx[d*3+2])
  566.             newcols.append(cols[a])
  567.             newcols.append(cols[b])
  568.             newcols.append(cols[c])
  569.             newcols.append(cols[d])
  570.             # newcols.append(cols[a])
  571.             # newcols.append(cols[b])
  572.             # newcols.append(cols[c])
  573.             # newcols.append(cols[d])
  574.             # newcols.append(cols[a])
  575.             # newcols.append(cols[b])
  576.             # newcols.append(cols[c])
  577.             # newcols.append(cols[d])
  578.             newtris.append(quad*4+0+(hi_hdr[part][SOLOCNT]*3))
  579.             newtris.append(quad*4+2+(hi_hdr[part][SOLOCNT]*3))
  580.             newtris.append(quad*4+1+(hi_hdr[part][SOLOCNT]*3))
  581.             newtris.append(quad*4+3+(hi_hdr[part][SOLOCNT]*3))
  582.             newtris.append(quad*4+1+(hi_hdr[part][SOLOCNT]*3))
  583.             newtris.append(quad*4+2+(hi_hdr[part][SOLOCNT]*3))
  584.             newuvs.append(uv2[0])
  585.             newuvs.append(uv2[1])
  586.             newuvs.append(uv2[2])
  587.             newuvs.append(uv2[3])
  588.            
  589.         #for u in range(0,len(uvs)):
  590.         #    print(u,(uvs[u]&0xFF)/256,(uvs[u]>>8&0xFF)/256)
  591.         #verts = struct.pack("f"*len(vtx),*vtx)
  592.         #uvbuffer  = struct.pack("H"*len(uvs),*uvs)
  593.         #tribuffer = struct.pack("H"*len(tris),*tris)
  594.         bwt = []
  595.         #print(len(newcols)==(len(newverts)//3))
  596.         beginmerge = []
  597.         endmerge = []
  598.         for v in range(0,len(newverts)//3):
  599.            
  600.             if(newcols[v] >> 30 & 0x01 == 0x01):
  601.                 #endmerge.append([v,newcols[v] >> 24 & 0x03])
  602.                 bid.append(boneparent[part])
  603.             else:
  604.                 bid.append(part)
  605.             bwt.append(1.0)
  606.         #p(len(newcols),len(bid),len(newverts)//3)
  607.         #for v in range(0,len(bid)):
  608.             #this is complicated. vertices belong to the parent bone when...
  609.             #their corresponding vertex color's alpha byte is greater than 00...
  610.             #and there may be some flags at play that determine exactly how...
  611.             #the game merges these vertices.
  612.             #so, we have to modify the existing bone weights and potentially positions
  613.             #so far, best I can tell, 0x80 is a flag for which vert to merge to, and...
  614.             # flag 0xC0 is for which vertex to merge from.
  615.             # However, I have seen 0xD0 and 0xE0, meaning there could be a 6 bit
  616.             # integer after the initial two bit flag
  617.         #    if(newcols[v] > 0xFFFFFF):
  618.         #        bid[v] = boneparent[part]
  619.            
  620.        
  621.         idxData = struct.pack("i"*len(bid),*bid)
  622.         valData = struct.pack("f"*len(bwt),*bwt)
  623.        
  624.        
  625.         rapi.rpgBindBoneIndexBuffer(idxData, noesis.RPGEODATA_INT, 4, 1)
  626.         rapi.rpgBindBoneWeightBuffer(valData, noesis.RPGEODATA_FLOAT, 4, 1)
  627.        
  628.         colbuffer = struct.pack("I"*len(newcols),*newcols)
  629.         verts = struct.pack("f"*len(newverts),*newverts)
  630.         uvbuffer  = struct.pack("H"*len(newuvs),*newuvs)
  631.         tribuffer = struct.pack("H"*len(newtris),*newtris)
  632.        
  633.        
  634.        
  635.         #p(len(newcols),len(newverts))
  636.        
  637.         #print(len(list(uvbuffer)))
  638.         rapi.rpgBindPositionBuffer(verts, noesis.RPGEODATA_FLOAT, 12)
  639.         rapi.rpgBindUV1Buffer(uvbuffer, noesis.RPGEODATA_UBYTE, 2)
  640.         rapi.rpgBindColorBuffer(colbuffer, noesis.RPGEODATA_UBYTE, 4, 3)
  641.         rapi.rpgCommitTriangles(tribuffer, noesis.RPGEODATA_USHORT, len(tris), noesis.RPGEO_TRIANGLE, 1)
  642.         #rapi.rpgCommitTriangles(None, noesis.RPGEODATA_USHORT, len(vtx)//3, noesis.RPGEO_POINTS, 1)
  643.         #rapi.rpgOptimize()
  644.     mdl = rapi.rpgConstructModel()
  645.    
  646.     bonelist = rapi.multiplyBones(bonelist)
  647.     mdl.setBones(bonelist)
  648.     return mdl
  649.  
  650. def loadx01(bs, x01start, mdlList):
  651.    
  652.     UNKSHORT1 = 0
  653.     UNKSHORT2 = 1
  654.     OFFSET    = 2 #where in the x01 file
  655.     UNKINT1   = 3
  656.     UNKINT2   = 4
  657.     #I don't know what all of it is yet, but I know which one is the offset.
  658.     x01_hdr = []
  659.    
  660.     #memorize our X01 start position since each file inside references this, rather than the DAT start
  661.     x01start = bs.tell()
  662.     #ofs(bs,"x01hdr")
  663.    
  664.    
  665.    
  666.    
  667.    
  668.     mdlcount = bs.readUInt() #first thing in the x01
  669.     for i in range(0,mdlcount):
  670.         x01_hdr.append([bs.readUShort(),bs.readUShort(),bs.readUInt(),bs.readUInt(),bs.readUInt()])
  671.        
  672.     for i in range(0, mdlcount):
  673.         mdlList.append(loadx1c(bs, i, x01start , x01_hdr[i][OFFSET]))
  674.         rapi.rpgReset()
  675.         #rapi.rpgClearBufferBinds()
  676. #def multiplyparts(part,):
  677.    
  678.  
  679. def decode10bit(_uint):
  680.     w = (_uint >> 30 & 0x0003)
  681.     tx = (_uint >>  0 & 0x03FF)
  682.     tz = (_uint >> 10 & 0x03FF)
  683.     ty = (_uint >> 20 & 0x03FF)
  684.     if (tx & 0x200 > 0):
  685.         x = tx ^ 0x3FF * -1
  686.     else:
  687.         x = tx
  688.     if (ty & 0x200 > 0):
  689.         y = ty ^ 0x3FF * -1
  690.     else:
  691.         y = ty
  692.     if (tz & 0x200 > 0):
  693.         z = tz ^ 0x3FF * -1
  694.     else:
  695.         z = tz
  696.    
  697.     return [x,-z,-y]
  698.    
  699. def decodefaces(_uint):
  700.     a = _uint >>  0 & 0x7F
  701.     b = _uint >>  7 & 0x7F
  702.     c = _uint >> 14 & 0x7F
  703.     d = _uint >> 21 & 0x7F
  704.     tex_info = _uint >> 28 &0x0F
  705.    
  706.     return [a,b,c,d]
  707. #debugging helper functions
  708. def p(*args):
  709.     noesis.logPopup()
  710.     print(args)
  711.    
  712. def ofs(bs,*args):
  713.     p(hex(bs.tell()),*args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement