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