home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 9384 / w3u5.7z / w3u5.ms
Encoding:
Text File  |  2015-07-04  |  23.3 KB  |  791 lines

  1. tex_path = "K:\\W3tup"    
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. fn Half2Float u=
  10. (
  11.  
  12.    sign = bit.and ( bit.shift u -15 ) 0x00000001
  13.    exponent = bit.and ( bit.shift u -10 ) 0x0000001F
  14.    mantis = bit.and u 0x000003FF
  15.  
  16.    if (exponent == 0) then
  17.    (
  18.       if(mantis == 0) then
  19.       (
  20.          return bit.shift sign 31
  21.       )
  22.       else
  23.       (
  24.                         tmp = bit.and mantis 0x00000400
  25.          while ( tmp !=0  ) do
  26.          (
  27.             mantis = bit.shift mantis 1
  28.             exponent -=1
  29.          )
  30.          exponent = exponent +1   
  31.          mantis = bit.and mantis 0x00000400 --> should be ~0x00000400
  32.       )
  33.    )
  34.    if(exponent == 31) then
  35.    (
  36.       if( mantis ==0) then 
  37.          return bit.or (bit.shift sign 31) 0x7F800000
  38.       else
  39.          return bit.or (bit.or (bit.shift sign 31) 0x7F800000 ) (bit.shift mantis 13)
  40.    )   
  41.    
  42.    exponent = exponent + (127-15)
  43.    mantis = bit.shift mantis 13
  44.    
  45.    val = bit.or ( bit.or (bit.shift sign 31) ( bit.shift exponent 23) ) (mantis)   
  46.    
  47.    return (bit.intAsFloat val) 
  48. )
  49.  
  50.  
  51. fn ReadUnknowProperty file = (
  52.     back = ftell file
  53.     sizeToGoNext = readlong file #unsigned
  54.     print ("ReadUnknowProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string))
  55.     fseek file (back + sizeToGoNext) #seek_set
  56.     )
  57.  
  58. fn ReadUInt8Property file = (
  59.     back = ftell file
  60.     sizeToGoNext = readlong file #unsigned
  61.     value = readByte file #unsigned
  62.     fseek file (back + sizeToGoNext) #seek_set
  63.     print ("ReadUInt8Property: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string)  + "    value: " + (value as string))
  64.     return value
  65.     )
  66.     
  67. fn ReadUInt32Property file = (
  68.     back = ftell file
  69.     sizeToGoNext = readlong file #unsigned
  70.     value = readLong file #unsigned
  71.     fseek file (back + sizeToGoNext) #seek_set
  72.     print ("ReadUInt32Property: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string)  + "    value: " + (value as string))
  73.     return value
  74.     )
  75.  
  76. fn ReadFloatProperty file = (
  77.     back = ftell file
  78.     sizeToGoNext = readlong file #unsigned
  79.     value = readFloat file
  80.     fseek file (back + sizeToGoNext) #seek_set
  81.     print ("ReadFloatPropertyfile: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string)  + "    value: " + (value as string))
  82.     return value
  83.     )
  84.     
  85. fn ReadVector3Property file = (
  86.     back = ftell file
  87.     sizeToGoNext = readlong file #unsigned
  88.     fseek file 1 #seek_cur
  89.     fseek file 4 #seek_cur
  90.     x = ReadFloatProperty file
  91.     fseek file 4 #seek_cur
  92.     y = ReadFloatProperty file
  93.     fseek file 4 #seek_cur
  94.     z = ReadFloatProperty file
  95.  
  96.     fseek file (back + sizeToGoNext) #seek_set    
  97.     value = [x, y, z]    
  98.     print ("ReadVector3Property: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string)  + "    value: " + (value as string))
  99.     return value
  100. )
  101.  
  102. fn ReadEMVTProperty file str_array= (
  103.     back = ftell file
  104.     sizeToGoNext = readlong file #unsigned
  105.     enumStringId = readShort file #unsigned
  106.     enumString = str_array[enumStringId + 1]
  107.     vertexType = ""    
  108.     if (enumString == "MVT_SkinnedMesh") then vertexType = "EMVT_SKINNED"
  109.     else vertexType = "EMVT_STATIC"
  110.     print ("ReadEMVTProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string)  + "    enumStringId: " + (enumStringId as string) + "    enumString: " + enumString +"    vertexType:" + vertexType)
  111.     return vertexType
  112.     )
  113.  
  114.  
  115.  
  116.     
  117.     
  118.     
  119.     
  120.     
  121. struct SMaterial (
  122.     public
  123.     diffuse_map = "",
  124.     normal_map = "",
  125.     tex_path = "",
  126.     ambient_map = "",
  127.     
  128.     fn GenerateMaterial = (
  129.         smat = StandardMaterial()
  130.         print("GenerateMaterial:")
  131.         matname = getFilenameFile diffuse_map
  132.         --print("GenerateMaterial: matname:  " + matname)
  133.         
  134.         it = matname.count
  135.         while ((it>0) AND (matname[it] != "_") ) do it = it - 1
  136.         if ( it > 0) do  matname = substring  matname 1 (it - 1) 
  137.         
  138.         print("GenerateMaterial: matname:  " + matname)
  139.         smat.name = matname
  140.         smat.showInViewport = true
  141.         
  142.         --diffuse 
  143.         search_path = tex_path + "\\" + (getFilenamePath diffuse_map) 
  144.         search_name = filenameFromPath  diffuse_map
  145.         print("GenerateMaterial: DiffuseMap: search_path: " + search_path)
  146.         print("GenerateMaterial: DiffuseMap: search_name: " + search_name)
  147.         print("GenerateMaterial: DiffuseMap: exist files: ")
  148.         ex_files = getFiles (search_path + search_name + "*")
  149.         print( ex_files )
  150.         dif_name = ""
  151.         if (ex_files.count > 0) then dif_name = ex_files[1]
  152.         else dif_name = search_path + search_name + "*" + ".DDS"
  153.         smat.diffuseMap = bitmaptexture filename: dif_name name: (filenameFromPath diffuse_map)
  154.         
  155.         
  156.         --normal
  157.         search_path = tex_path + "\\" + (getFilenamePath normal_map) 
  158.         search_name = filenameFromPath  normal_map
  159.         print("GenerateMaterial: NormalMap: search_path: " + search_path)
  160.         print("GenerateMaterial: NormalMap: search_name: " + search_name)
  161.         print("GenerateMaterial: NormalMap: exist files: ")
  162.         ex_files = getFiles (search_path + search_name + "*")
  163.         print( ex_files )
  164.         norm_name = ""
  165.         if (ex_files.count > 0) then norm_name = ex_files[1]
  166.         else norm_name = search_path + search_name + "*" + ".DDS"
  167.         
  168.         smat.bumpMap = Normal_Bump normal_map: (bitmaptexture filename: norm_name  name: (filenameFromPath normal_map) ) 
  169.         smat.bumpMapAmount = 100
  170.         
  171.         
  172.         --smat.diffuseMap = bitmaptexture filename: dif_name name: (filenameFromPath diffuse_map) 
  173.         
  174.         
  175.         return smat
  176.         )
  177. )
  178.  
  179.  
  180.  
  181. struct Meshdata (
  182.     public
  183.     back = 0,
  184.     data2 = #(),
  185.     nModel = 0,
  186.     nMat = #()
  187. )
  188.  
  189.  
  190.  
  191.  
  192. struct Submesh_data (
  193.     public
  194.     vertype = "EMVT_STATIC",
  195.     dataI = #(),
  196.     dataH  = #()
  197. )
  198.  
  199.  
  200.  
  201.  
  202. struct SVertexBufferInfos (
  203.     public
  204.     verticesCoordsOffset = 0,
  205.     uvOffset = 0,
  206.     normalsOffset = 0,
  207.     nbVertices = 0
  208. )
  209.  
  210.  
  211.  
  212.  
  213. struct SMeshInfos (
  214.     public
  215.     firstIndice = 0,
  216.     firstVertex = 0,
  217.     numBonesPerVertex = 0,
  218.     vertexType = "EMVT_STATIC",
  219.     numIndices = 0,
  220.     numVertices = 0,
  221.     materialID = 0    
  222.     )
  223.  
  224.     
  225.     
  226.     
  227. struct SBufferInfos (
  228.     public
  229.     indicesBufferOffset = 0,
  230.     indicesBufferSize = 0,
  231.     quantizationOffset = [0, 0, 0],
  232.     quantizationScale = [1, 1, 1],
  233.     verticesBufferOffset = 0,
  234.     verticesBufferSize = 0 ,
  235.     verticesBuffer = #()
  236.     )
  237.     
  238.     
  239.     
  240.     
  241. struct W3_DataInfos (
  242.     public
  243.     dataBlockType = 0,
  244.     dataTypeName = "",
  245.     size = 0,
  246.     adress = 0
  247.     )
  248.     
  249.     
  250.     
  251.     
  252. fn W3_CMaterialInstance file infos str_array = (
  253.     print ("W3_CMaterialInstance:")
  254.     mat = SMaterial()    
  255.     fseek file (infos.adress + 1)  #seek_set
  256.     back = ftell file
  257.     print ("W3_CMaterialInstance: back: " + (bit.intAsHex(back) as string) )
  258.     
  259.     while true do (
  260.         print ("W3_CMaterialInstance: offset: " + (bit.intAsHex(ftell file) as string))
  261.         propertyID = readShort file #unsigned
  262.         propertyTypeID = readShort file #unsigned
  263.         print ("W3_CMaterialInstance: propertyID: " + (propertyID as string) + " propertyTypeID: " + (propertyTypeID as string))
  264.         if ((propertyID == 0) OR (propertyTypeID == 0) OR (propertyID + 1> str_array.count) OR (propertyTypeID + 1> str_array.count)) do (
  265.             exit
  266.             )
  267.         property = str_array[propertyID + 1]
  268.         propertyType = str_array[propertyTypeID + 1]
  269.         print ("W3_CMaterialInstance: property: " + property + "     propertyType: " + propertyType)
  270.         if (property == "baseMaterial") then (
  271.             mat = ReadIMaterialProperty file str_array
  272.             exit
  273.             )
  274.         else   ReadUnknowProperty file
  275.         )    
  276.     return mat    
  277.     )
  278.     
  279.     
  280.     
  281.     
  282. fn ReadIMaterialProperty file  str_array = (
  283.     print ("ReadIMaterialProperty:")
  284.     mat = SMaterial()    
  285.     back = ftell file
  286.     print ("ReadIMaterialProperty: back: " + (bit.intAsHex(back) as string) )
  287.     fseek file 10 #seek_cur
  288.     nbProperty = readLong file    
  289.     
  290.     
  291.     textures = #()
  292.     
  293.     for tmp_str in str_array do (
  294.         if ((findString tmp_str ".xbm") != undefined ) do append textures tmp_str
  295.         )
  296.     
  297.  
  298.     print ("ReadIMaterialProperty: nbProperty: " + (nbProperty as string) )
  299.     
  300.     for i = 1 to nbProperty do (
  301.         back = ftell file
  302.         sizeToGoToNext = readlong file #unsigned
  303.         print ("ReadIMaterialProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoToNext: " + (bit.intAsHex(sizeToGoToNext) as string))
  304.         propId = readshort file #unsigned
  305.         propTypeId = readshort file #unsigned
  306.         print ("ReadIMaterialProperty: propId: " + (propId as string) + "    propTypeId: " + (propTypeId as string))
  307.         if (propId < 0) OR ( propId + 1 > str_array.count) do (
  308.             exit
  309.             )
  310.         print("ReadIMaterialProperty: ID:  " + str_array[propId + 1])
  311.         
  312.         if (str_array[propId + 1] == "Diffuse") do (
  313.             texId = readByte file #unsigned
  314.             texId = 255 - texId            
  315.             print("ReadIMaterialProperty: texId:  " + (texId as string))
  316.             if (texId <= textures.count) do (
  317.                 print("ReadIMaterialProperty: Diffuse map:  " + textures[texId ])
  318.                 mat.diffuse_map = textures[texId ]
  319.                 )
  320.             )
  321.             
  322.         if (str_array[propId + 1] == "Normal") do (
  323.             texId = readByte file #unsigned
  324.             texId = 255 - texId            
  325.             print("ReadIMaterialProperty: texId:  " + (texId as string))
  326.             if (texId <= textures.count) do (
  327.                 print("ReadIMaterialProperty: Normal map:  " + textures[texId ])
  328.                 mat.normal_map = textures[texId ]
  329.                 )
  330.             )
  331.             
  332.         if (str_array[propId + 1] == "Ambient") do (
  333.             texId = readByte file #unsigned
  334.             texId = 255 - texId            
  335.             print("ReadIMaterialProperty: texId:  " + (texId as string))
  336.             if (texId <= textures.count) do (
  337.                 print("ReadIMaterialProperty: Ambient map:  " + textures[texId ])
  338.                 mat.ambient_map = textures[texId ]
  339.                 )
  340.             )
  341.         
  342.         fseek file (back + sizeToGoToNext) #seek_set
  343.         )
  344.     
  345.     
  346.     return mat    
  347.     )    
  348.     
  349.     
  350.     
  351.     
  352.     
  353.     
  354.     
  355.     
  356.     
  357.     
  358.     
  359. --  ╨╜╤â╨╢╨╜╨░ ╨╛╤é╨╗╨░╨┤╨║╨░    
  360. fn ReadRenderChunksProperty file bufferInfos =(
  361.     print("ReadRenderChunksProperty:")
  362.     temp_buffer = bufferInfos
  363.     back = ftell file
  364.     sizeToGoNext = readlong file #unsigned
  365.     print ("ReadRenderChunksProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string) )
  366.         
  367.     fseek file 5 #seek_cur
  368.     nbBuffers = readByte file #unsigned
  369.     print ("ReadRenderChunksProperty: nbBuffers: " +  (nbBuffers as string) )
  370.     
  371.     while sizeToGoNext > (( ftell file ) - back ) do (        
  372.         buffInfos = SVertexBufferInfos()
  373.         buffInfos.verticesCoordsOffset = readLong file #unsigned
  374.         buffInfos.uvOffset  = readLong file #unsigned
  375.         buffInfos.normalsOffset =  readLong file #unsigned
  376.         fseek file 14 #seek_cur
  377.         buffInfos.nbVertices = readShort file #unsigned        
  378.         print ("ReadRenderChunksProperty verticesCoordsOffset: " + ((bit.intAsHex(buffInfos.verticesCoordsOffset)) as string) + "    uvOffset: " + ((bit.intAsHex(buffInfos.uvOffset)) as string) + "    normalsOffset: " + ((bit.intAsHex(buffInfos.normalsOffset)) as string) + "    nbVertices: " + ((bit.intAsHex(buffInfos.nbVertices)) as string) )
  379.         fseek file 9 #seek_cur
  380.         append temp_buffer.verticesBuffer buffInfos
  381.         )
  382.     
  383.     
  384.     print("    ReadRenderChunksPropertyEND:")
  385.     fseek file (back + sizeToGoNext) #seek_set
  386.     return temp_buffer
  387.     )    
  388.     
  389.  
  390. fn ReadSMeshChunkPackedProperty file str_array = (
  391.     print("ReadSMeshChunkPackedProperty:")
  392.     meshes = #()
  393.     meshInfos = SMeshInfos()
  394.     
  395.     back = ftell file
  396.     sizeToGoNext = readlong file #unsigned
  397.     nbChunks = readlong file #unsigned
  398.     print ("ReadSMeshChunkPackedProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string) + "    nbChunks: " + (nbChunks as string))
  399.     fseek file 1 #seek_cur
  400.     chunkId = 0
  401.     
  402.     while true do (
  403.         print ("ReadSMeshChunkPackedProperty: offset: " + (bit.intAsHex(ftell file) as string))
  404.         propertyID = readShort file #unsigned
  405.         propertyTypeID = readShort file #unsigned
  406.         print ("ReadSMeshChunkPackedProperty: propertyID: " + (propertyID as string) + " propertyTypeID: " + (propertyTypeID as string))
  407.         if ((propertyID == 0) OR (propertyTypeID == 0) OR (propertyID + 1> str_array.count) OR (propertyTypeID + 1> str_array.count)) do (
  408.             append meshes meshInfos
  409.             chunkId = chunkId + 1
  410.             if (chunkId >= nbChunks) then exit
  411.             else (
  412.             
  413.                 newMeshInfos = SMeshInfos()
  414.                 newMeshInfos.vertexType = meshInfos.vertexType
  415.                 newMeshInfos.numBonesPerVertex = meshInfos.numBonesPerVertex
  416.                 meshInfos = newMeshInfos
  417.  
  418.                 fseek file -1 #seek_cur
  419.  
  420.                 propertyID = readShort file #unsigned
  421.                 propertyTypeID = readShort file #unsigned
  422.                 )
  423.             )    
  424.             
  425.         property = str_array[propertyID + 1]
  426.         propertyType = str_array[propertyTypeID + 1]
  427.         print ("ReadSMeshChunkPackedProperty: property: " + property + "     propertyType: " + propertyType)
  428.  
  429.         if (property == "numIndices") then meshInfos.numIndices = ReadUInt32Property file
  430.         else if (property == "numVertices") then meshInfos.numVertices = ReadUInt32Property file
  431.         else if (property == "firstVertex") then meshInfos.firstVertex = ReadUInt32Property file
  432.         else if (property == "firstIndex") then meshInfos.firstIndice = ReadUInt32Property file 
  433.         else if (property == "vertexType") then meshInfos.vertexType = ReadEMVTProperty file str_array
  434.         else if (property == "numBonesPerVertex") then meshInfos.numBonesPerVertex = ReadUInt8Property file
  435.         else if (property == "materialID") then meshInfos.materialID = ReadUInt32Property file
  436.         else ReadUnknowProperty file        
  437.         )    
  438.     print ("    ReadSMeshChunkPackedPropertyEND")
  439.     fseek file (back + sizeToGoNext) #seek_set
  440.     return meshes
  441.     )    
  442.  
  443.     
  444.     
  445.     
  446. fn ReadSMeshCookedDataProperty file str_array = (
  447.     print("ReadSMeshCookedDataProperty:")    
  448.     back = ftell file
  449.     sizeToGoNext = readlong file #unsigned
  450.     print ("ReadSMeshCookedDataProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string))
  451.     fseek file 1 #seek_cur
  452.     bufferInfos = SBufferInfos()
  453.  
  454.     
  455.     while true do (
  456.         print ("ReadSMeshCookedDataProperty: offset: " + (bit.intAsHex(ftell file) as string))
  457.         propertyID = readShort file #unsigned
  458.         propertyTypeID = readShort file #unsigned
  459.         print ("ReadSMeshCookedDataProperty: propertyID: " + (propertyID as string) + " propertyTypeID: " + (propertyTypeID as string))        
  460.         if ((propertyID == 0) OR (propertyTypeID == 0) OR ((propertyID + 1)> str_array.count) OR ((propertyTypeID + 1)> str_array.count)) do (
  461.             exit
  462.             )    
  463.     
  464.         property = str_array[propertyID + 1]
  465.         print ("ReadSMeshCookedDataProperty: property: " + property  )
  466.  
  467.         if (property == "indexBufferSize") then bufferInfos.indicesBufferSize = ReadUInt32Property file 
  468.         else if (property == "indexBufferOffset") then bufferInfos.indicesBufferOffset = ReadUInt32Property file 
  469.         else if (property == "vertexBufferSize") then bufferInfos.verticesBufferSize = ReadUInt32Property file 
  470.         else if (property == "quantizationScale") then bufferInfos.quantizationScale = ReadVector3Property file
  471.         else if (property == "quantizationOffset") then bufferInfos.quantizationOffset = ReadVector3Property file
  472.         else if (property == "renderChunks") then bufferInfos = ReadRenderChunksProperty file bufferInfos
  473.         else ReadUnknowProperty file
  474.         )
  475.     print(bufferInfos)
  476.     print ("    ReadSMeshCookedDataPropertyEND")
  477.     
  478.     fseek file (back + sizeToGoNext) #seek_set
  479.     return bufferInfos
  480.     )
  481.     
  482.     
  483.     
  484.     
  485. fn ReadMaterialsProperty file = (    
  486.     print("ReadMaterialsProperty:")
  487.     back = ftell file
  488.     sizeToGoNext = readlong file #unsigned
  489.     nbChunks = readlong file #unsigned
  490.     print ("ReadMaterialsProperty: back: " + (bit.intAsHex(back) as string) + "    sizeToGoNext: " + (sizeToGoNext as string) + "    nbChunks: " + (nbChunks as string))
  491.     
  492.     for  i = 1  to  nbChunks do (
  493.         value = readlong file #unsigned
  494.         )
  495.     fseek file (back + sizeToGoNext) #seek_set
  496.     print("    ReadMaterialsPropertyEND")    
  497.     )
  498.  
  499.     
  500.     
  501.     
  502.     
  503.     
  504.     
  505.     
  506.     
  507.     
  508. fn W3_ReadBuffer file file_buffer bufferInfos meshInfos mesh_name mat = (
  509.     print ("W3_ReadBuffer:")
  510.     vert_array = #()
  511.     UV_array = #()
  512.     face_array = #()
  513.     print ("W3_ReadBuffer: bufferInfos")
  514.     print (bufferInfos)
  515.     print ("W3_ReadBuffer: meshInfos")
  516.     print (meshInfos)
  517.     
  518.     
  519.     num_vertices = meshInfos.numVertices
  520.     
  521.     vertexSize = 20;
  522.     if (meshInfos.vertexType == "EMVT_SKINNED") do ( vertexSize = vertexSize + meshInfos.numBonesPerVertex * 2 )
  523.     
  524.     first_vertex = meshInfos.firstVertex
  525.     
  526.     inf = SVertexBufferInfos()
  527.     sum = 0
  528.     
  529.     for i = 1 to bufferInfos.verticesBuffer.count do (
  530.         sum = sum + bufferInfos.verticesBuffer[i].nbVertices;
  531.         if ( sum > meshInfos.firstVertex) do (
  532.             inf = bufferInfos.verticesBuffer[i]
  533.             exit
  534.             )
  535.         )
  536.     print ("W3_ReadBuffer: inf:")
  537.     print (inf)
  538.     
  539.     fseek file_buffer ( inf.verticesCoordsOffset + (meshInfos.firstVertex - (sum - inf.nbVertices)) * vertexSize) #seek_set
  540.     print ("W3_ReadBuffer: vertex offset: 0x" + ((bit.intAsHex(ftell file_buffer)) as string ))
  541.     
  542.     for i = 1 to meshInfos.numVertices do (
  543.         x = readshort file_buffer #unsigned
  544.         y = readshort file_buffer #unsigned
  545.         z = readshort file_buffer #unsigned
  546.         qq = readshort file_buffer #unsigned
  547.         if (meshInfos.vertexType == "EMVT_SKINNED") do ( fseek file_buffer (meshInfos.numBonesPerVertex * 2) #seek_cur)
  548.         --print (((bit.intAsHex(ftell file_buffer)) as string ) + "   " + ((bit.intAsHex(x)) as string ) + "   " + ((bit.intAsHex(y)) as string ) + "   " + ((bit.intAsHex(z)) as string ) )
  549.         append vert_array [ (x * bufferInfos.quantizationScale.X + 65536*bufferInfos.quantizationOffset.X), (y * bufferInfos.quantizationScale.Y + 65536*bufferInfos.quantizationOffset.Y), (z * bufferInfos.quantizationScale.Z + 65536*bufferInfos.quantizationOffset.Z) ]
  550.         --append vert_array [x, y, z]
  551.         )
  552.  
  553.     fseek file_buffer ( inf.uvOffset + (meshInfos.firstVertex - (sum - inf.nbVertices)) * 4 ) #seek_set
  554.     
  555.     
  556.  
  557.     print ("W3_ReadBuffer: UV offset: 0x" + ((bit.intAsHex(ftell file_buffer)) as string ))
  558.     
  559.     for i = 1 to meshInfos.numVertices do (
  560.         x = Half2Float( readshort file_buffer)
  561.         y = 1 - Half2Float( readshort file_buffer)        
  562.         append UV_array [ x, y, 0 ]
  563.         )
  564.     
  565.     fseek file_buffer ( bufferInfos.indicesBufferOffset + meshInfos.firstIndice * 2 ) #seek_set
  566.     print ("W3_ReadBuffer: indicies offset: 0x" + ((bit.intAsHex(ftell file_buffer)) as string ))
  567.     
  568.     for i = 1 to (meshInfos.numIndices / 3) do (
  569.         i3 = (readshort f #unsigned ) + 1 
  570.         i2 = (readshort f #unsigned ) + 1
  571.         i1 = (readshort f #unsigned ) + 1        
  572.         append face_array[i1,i2,i3]
  573.         )    
  574.     try(
  575.     msh = mesh name:mesh_name vertices:vert_array faces: face_array
  576.     msh.numTVerts = UV_array.count
  577.     buildTVFaces msh
  578.     for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  579.     for j = 1 to face_array.count do setTVFace msh j face_array[j]
  580.     --print(mat)
  581.     if ( mat == undefined ) then (
  582.         newmat = StandardMaterial()
  583.         msh.material = newmat    
  584.         )
  585.     else (
  586.         
  587.         msh.material = mat.GenerateMaterial()
  588.         )
  589.     )
  590.     catch
  591.     (
  592.     print("##############################################################")
  593.     print("##############################################################")
  594.     print ("        ERROR CREATING MESH")
  595.     print("##############################################################")
  596.     print("##############################################################")
  597.     )
  598.     )    
  599.     
  600.     
  601.     
  602.     
  603.     
  604.     
  605.     
  606.     
  607.     
  608.     
  609.     
  610.     
  611.     
  612.     
  613. fn W3_CMesh file binary_file infos str_array mesh_name Materials  = (
  614.     print("W3_CMesh:")
  615.     bufferInfos = SBufferInfos()
  616.     meshes = #()
  617.     fseek file (infos.adress + 1) #seek_set
  618.     print ("W3_CMesh: base offset: " + (bit.intAsHex(ftell file) as string))
  619.     
  620.     while true do (
  621.         print ("W3_CMesh: offset: " + (bit.intAsHex(ftell file) as string))
  622.         propertyID = readShort file #unsigned
  623.         propertyTypeID = readShort file #unsigned
  624.         print ("W3_CMesh: propertyID: " + (propertyID as string) + " propertyTypeID: " + (propertyTypeID as string))
  625.         if ((propertyID == 0) OR (propertyTypeID == 0) OR ((propertyID + 1)> str_array.count) OR ((propertyTypeID + 1)> str_array.count)) do (
  626.             exit
  627.             )            
  628.         property = str_array[propertyID + 1]
  629.         propertyType = str_array[propertyTypeID + 1]    
  630.         print ("W3_CMesh: property: " + property + "     propertyType: " + propertyType)
  631.         
  632.         if (property == "materials") then    (
  633.             ReadMaterialsProperty file
  634.             )
  635.         else if (property == "chunks") then (
  636.             meshes = ReadSMeshChunkPackedProperty file str_array
  637.             )
  638.         else if (propertyType == "SMeshCookedData") then    (
  639.             bufferInfos = ReadSMeshCookedDataProperty file str_array
  640.             )
  641.         else (
  642.             ReadUnknowProperty file 
  643.             )
  644.         )
  645.     
  646.     
  647.     
  648.     
  649.     print ("W3_CMesh: Generation Mesh")
  650.     print ("W3_CMesh: Meshes")
  651.     print (meshes)
  652.     print ("W3_CMesh: bufferInfos")
  653.     print (bufferInfos)
  654.     
  655.     for i = 1 to meshes.count do (
  656.         --if ((meshes[i].materialID >= 0) AND (meshes[i].materialID <= Materials.count)) then    W3_CMesh fm f meshes[i] str_array mesh_name Materials[meshes[i].materialID]
  657.         --else W3_CMesh fm f meshes[i] str_array mesh_name undefined 
  658.         if ((meshes[i].materialID + 1 > 0) AND (meshes[i].materialID + 1 <= Materials.count)) then W3_ReadBuffer file binary_file bufferInfos meshes[i] mesh_name Materials[meshes[i].materialID + 1]
  659.         else W3_ReadBuffer file binary_file bufferInfos meshes[i] mesh_name undefined
  660.         )
  661.         
  662.     )
  663.     
  664.     
  665.     
  666.     
  667.     
  668.     
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.     
  686.     
  687.  
  688. fname = getOpenFileName \
  689. caption:"Open .w2mesh.1.buffer from Mesh folder" \
  690. types:"Witcher 3 Wild Hunt (*.w2mesh.1.buffer)|*.w2mesh.1.buffer" \
  691. historyCategory:"W#_WHObjectPresets"    
  692.  
  693. mesh_name = getFilenameFile fname
  694. mesh_name = getFilenameFile mesh_name
  695. mesh_name = getFilenameFile mesh_name
  696. print(mesh_name)
  697.  
  698.  
  699.  
  700. --fnamem = "K:\\w3u\\characters\\models\\geralt\\body\\model\\h_00_mg__geralt.w2mesh"
  701. fnamem = substring fname 1 (fname.count - 9)
  702. f = fopen fname "rb"
  703. fm = fopen fnamem "rb"
  704.  
  705.  
  706. print("fname :" + fname)
  707. print("fname :" + fnamem )
  708.  
  709.  
  710.  
  711. fseek fm 0x0000 #seek_set
  712.  
  713. header = ""
  714. for i = 1 to 4 do (
  715.     header = header + bit.intAsChar (readbyte fm #unsigned)
  716.     )
  717. print ("Header: " + header)
  718.  
  719. fseek fm 0x0008 #seek_cur
  720.  
  721.  
  722. header_data = #()
  723. for i = 1 to 38 do (
  724.     append header_data (readlong fm #unsigned)
  725.     )
  726. print ("Header: " + header)
  727.  
  728.  
  729. stringChunkStart = header_data[8];
  730. stringChunkSize = header_data[9];
  731. contentChunkStart = header_data[20];
  732. contentChunkSize = header_data[21];
  733.  
  734. print ("stringChunkStart: 0x" + ((bit.intAsHex(stringChunkStart)) as string))
  735. print ("stringChunkSize: " + (stringChunkSize as String))
  736. print ("contentChunkStart: 0x" + ((bit.intAsHex(contentChunkStart)) as string))
  737. print ("contentChunkSize: " + (contentChunkSize as String))
  738.  
  739. Materials = #()
  740. str_array = #()
  741. fseek fm stringChunkStart #seek_set
  742.  
  743. print("astr")
  744.  
  745. while ((ftell fm) - stringChunkStart) < stringChunkSize do
  746.     (
  747.     tmp_string = ""
  748.     tmp_char = bit.intAsChar (readbyte fm #unsigned)
  749.     while tmp_char != "" do (
  750.         tmp_string = tmp_string + tmp_char
  751.         tmp_char = bit.intAsChar (readbyte fm #unsigned)
  752.         )    
  753.     append str_array tmp_string
  754.     print (tmp_string)
  755.     )
  756. print("astrEND")
  757. --for i = 1 to contentChunkSize do (
  758. --    )
  759.  
  760. infos_array = #()
  761. meshes = #()
  762. fseek fm contentChunkStart #seek_set
  763.  
  764. for i = 1 to contentChunkSize do (
  765.     infos = W3_DataInfos()
  766.  
  767.     infos.dataBlockType =  readshort fm #unsigned
  768.     infos.dataTypeName = str_array[infos.dataBlockType + 1]
  769.     fseek fm 6 #seek_cur
  770.     infos.size = readlong fm #unsigned
  771.     infos.adress = readlong fm #unsigned
  772.     fseek fm 8 #seek_cur
  773.     if ( infos.dataTypeName == "CMesh" ) do ( append meshes infos)
  774.     if ( infos.dataTypeName == "CMaterialInstance") do
  775.         (
  776.             mat = W3_CMaterialInstance fm infos str_array
  777.             mat.tex_path = tex_path
  778.             append Materials mat
  779.         )
  780.     print (infos)
  781.     )
  782.     
  783. print("GLOBAL: meshes")
  784. print(meshes)
  785. print("GLOBAL: Materials")
  786. print(Materials)
  787.  
  788. for i = 1 to meshes.count do (
  789.  
  790.     W3_CMesh fm f meshes[i] str_array mesh_name Materials  
  791.     )