Advertisement
Guest User

readVertexGroups

a guest
Dec 24th, 2019
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. readVertexGroups() {
  2.  
  3.     // First we 'seek' to the vertex group definitions and read the
  4.     // uv count and the offset to the vertex list definition
  5.    
  6.     let ofs = this.header.vertex_group_ofs;
  7.    
  8.     for(let i = 0; i < this.header.vertex_group_count; i++) {
  9.         const unj_vertex_group_t = {
  10.             uv_count : this.view.getUint32(ofs + 0, true),
  11.             group_ofs : this.view.getUint32(ofs + 4, true)
  12.         }
  13.         this.vertex_groups[i] = unj_vertex_group_t;
  14.         ofs += 8;
  15.     }
  16.    
  17.     // Then we seek to each one of the vertex list definitions and
  18.     // read the number of bone influences, which bones, how many vertices
  19.     // and the format of the stored vertices in the vertex list
  20.    
  21.     for(let i = 0; i < this.header.vertex_group_count; i++) {
  22.        
  23.         ofs = this.vertex_groups[i].group_ofs;
  24.        
  25.         const unj_vertex_list_t = {
  26.             unkown_1 : this.view.getUint32(ofs + 0x00, true),
  27.             vertex_format : this.view.getUint32(ofs + 0x04, true),
  28.             unknown_2 : this.view.getUint32(ofs + 0x08, true),
  29.             unknown_3 : this.view.getUint8(ofs + 0x09),
  30.             vertex_length : this.view.getUint8(ofs + 0x0a),
  31.             nop : this.view.getUint12(ofs + 0x0e, true),
  32.             vertex_count_ofs : this.view.getUint32(ofs + 0x10, true),
  33.             vertex_list_ofs : this.view.getUint32(ofs + 0x14, true),
  34.             bone_binding_ofs : this.view.getUint32(ofs + 0x18, true),
  35.             bone_binding_count : this.view.getUint32(ofs + 0x1c, true),
  36.             total_vertex_count : this.view.getUint32(ofs + 0x20, true),
  37.             unknown_4 : this.view.getUint32(ofs + 0x24, true),
  38.             unknown_5 : this.view.getUint32(ofs + 0x28, true),
  39.             vertex_scale : this.view.getFloat32(ofs + 0x2c, true)
  40.         };
  41.        
  42.         // Read the number of vertices in the vertex list
  43.        
  44.         ofs = unj_vertex_list_t.vertex_count_ofs;
  45.         unj_vertex_list_t.vertex_count = this.view.getUint32(ofs, true);
  46.        
  47.         // Read each of the bone influences for the group
  48.        
  49.         unj_vertex_list_t.bones = new Array();
  50.         ofs = unj_vertex_list_t.bone_binding_ofs;
  51.         for(let k = 0; k < unj_vertex_list_t.bone_binding_count; k++) {
  52.             unj_vertex_list_t.bones[k] = this.view.getUint32(ofs, true);
  53.             ofs += 4;
  54.         }
  55.        
  56.         // Then we save all of the values from the struct into instance memory
  57.        
  58.         for(let key in unj_vertex_list_t) {
  59.             this.vertex_groups[i][key] = unj_vertex_list_t[key];
  60.         }
  61.     }
  62.    
  63.     // Then we seek to the vertex list and read the values for the vertices
  64.    
  65.     for(let i = 0; i < this.header.vertex_group_count; i++) {
  66.        
  67.         const UINT8 = 1;
  68.         const INT8 = 1;
  69.         const INT16 = 2;
  70.         const FLOAT = 3;
  71.                
  72.         const bones = this.vertex_groups[i].bones;
  73.         const vertices = new Array(this.vertex_groups[i].vertex_count);
  74.         const scale = this.vertex_groups[i].scale;
  75.         const format = this.vertex_groups[i].vertex_format;
  76.         const stride = this.vertex_groups[i].vertex_length;
  77.         const uv_count = this.vertex_groups[i].uv_count;
  78.        
  79.         const uvFormat = (format & 0x3);
  80.         const colorFormat = (format >> 2) & 0x7;
  81.         const normalFormat = (format >> 5) & 0x3;
  82.         const positionFormat = (format >> 7) & 0x3;
  83.         const weightFormat = (format >> 9) & 0x3;
  84.        
  85.         // Then we seek to the vertex list and read the values
  86.        
  87.         let ofs = this.vertex_groups[i].vertex_list_ofs;       
  88.         for(let k = 0; k < vertices.length; k++) {
  89.             const vertex = {};
  90.             const start = ofs;
  91.            
  92.             // Read bone weight
  93.            
  94.             vertex.weight = [];
  95.             switch(weightFormat) {
  96.             case UINT8:
  97.                 for(let j = 0; j < bones.length; j++) {
  98.                     vertex.weight[j] = {
  99.                         bone_id : bones[j],
  100.                         weight : this.view.getUint8(ofs) / 0x7f
  101.                     }
  102.                     ofs++;
  103.                 }
  104.                 break;
  105.             case INT16:
  106.                 for(let j = 0; j < bones.length; j++) {
  107.                     vertex.weight[j] = {
  108.                         bone_id : bones[j],
  109.                         weight : this.view.getInt16(ofs, true) / 0x7fff
  110.                     }
  111.                     ofs+=2;
  112.                 }
  113.                 break;
  114.             case FLOAT:
  115.                 for(let j = 0; j < bones.length; j++) {
  116.                     vertex.weight[j] = {
  117.                         bone_id : bones[j],
  118.                         weight : this.view.getFloat32(ofs, true);
  119.                     }
  120.                     ofs+=4;
  121.                 }
  122.                 break;
  123.             }
  124.            
  125.             // Read uv values
  126.            
  127.             vertex.uvs = [];
  128.             switch(uvFormat) {
  129.             case INT8:
  130.                 for(let j = 0; j < uv_count; j++) {
  131.                     vertex.uvs[j] = {
  132.                         u : this.view.getInt8(ofs + 0) / 0x7f,
  133.                         v : this.view.getInt8(ofs + 1) / 0x7f
  134.                     }
  135.                     ofs+=2;
  136.                 }
  137.                 break;
  138.             case INT16:
  139.                 if(ofs % 2) {
  140.                     ofs += 2 - (ofs % 2);
  141.                 }
  142.                
  143.                 for(let j = 0; j < uv_count; j++) {
  144.                     vertex.uvs[j] = {
  145.                         u : this.view.getInt16(ofs + 0) / 0x7fff,
  146.                         v : this.view.getInt16(ofs + 2) / 0x7fff
  147.                     }
  148.                     ofs+=4;
  149.                 }
  150.                 break;
  151.             case FLOAT:
  152.                 if(ofs % 4) {
  153.                     ofs += 4 - (ofs % 4);
  154.                 }
  155.                
  156.                 for(let j = 0; j < bones.length; j++) {
  157.                     vertex.uvs[j] = {
  158.                         u : this.view.getFloat32(ofs + 0),
  159.                         v : this.view.getFloat32(ofs + 4)
  160.                     }
  161.                     ofs+=8;
  162.                 }
  163.                 break;
  164.             }
  165.            
  166.             // Read colors (we assume ARGB4444)
  167.            
  168.             if(colorFormat) {
  169.                 if(ofs % 2) {
  170.                     ofs += 2 - (ofs % 2);
  171.                 }
  172.                 let byte1 = this.view.getUint8(ofs + 0);
  173.                 let byte2 = this.view.getUint8(ofs + 1);
  174.                 ofs += 2;
  175.                 vertex.color = {
  176.                     r : ((byte1 >> 0) & 0xf) / 0x0f,
  177.                     g : ((color1 >> 4) & 0xf) / 0x0f,
  178.                     b : ((color2 >> 0) & 0xf) / 0x0f,
  179.                     a : ((color2 >> 4) & 0xf) / 0x0f
  180.                 };
  181.             }
  182.            
  183.             // Read normals
  184.            
  185.             switch(normalFormat) {
  186.             case UINT8:
  187.                 vertex.normal = {
  188.                     x : this.view.getUint8(ofs + 0) / 0x7f,
  189.                     y : this.view.getUint8(ofs + 1) / 0x7f,
  190.                     z : this.view.getUint8(ofs + 2) / 0x7f
  191.                 }
  192.                 ofs+=3;
  193.                 break;
  194.             case INT16:
  195.                 if(ofs % 2) {
  196.                     ofs += 2 - (ofs % 2);
  197.                 }
  198.                 vertex.normal = {
  199.                     x : this.view.getInt16(ofs + 0) / 0x7fff,
  200.                     y : this.view.getInt16(ofs + 2) / 0x7fff,
  201.                     z : this.view.getInt16(ofs + 6) / 0x7fff
  202.                 }
  203.                 ofs+=6;
  204.                 break;
  205.             case FLOAT:
  206.                 if(ofs % 4) {
  207.                     ofs += 4 - (ofs % 4);
  208.                 }
  209.                
  210.                 vertex.normal = {
  211.                     x : this.view.getFloat32(ofs + 0),
  212.                     y : this.view.getFloat32(ofs + 4),
  213.                     z : this.view.getFloat32(ofs + 8)
  214.                 }
  215.                 ofs+=12;
  216.                 break;
  217.             }
  218.            
  219.             // Read Position
  220.            
  221.             switch(positionFormat) {
  222.             case UINT8:
  223.                 vertex.position = {
  224.                     x : this.view.getUint8(ofs + 0) / 0x7f * scale,
  225.                     y : this.view.getUint8(ofs + 1) / 0x7f * scale,
  226.                     z : this.view.getUint8(ofs + 2) / 0x7f * scale
  227.                 }
  228.                 ofs+=3;
  229.                 break;
  230.             case INT16:
  231.                 if(ofs % 2) {
  232.                     ofs += 2 - (ofs % 2);
  233.                 }
  234.                 vertex.position = {
  235.                     x : this.view.getInt16(ofs + 0) / 0x7fff * scale,
  236.                     y : this.view.getInt16(ofs + 2) / 0x7fff * scale,
  237.                     z : this.view.getInt16(ofs + 6) / 0x7fff * scale
  238.                 }
  239.                 ofs+=6;
  240.                 break;
  241.             case FLOAT:
  242.                 if(ofs % 4) {
  243.                     ofs += 4 - (ofs % 4);
  244.                 }
  245.                
  246.                 vertex.position = {
  247.                     x : this.view.getFloat32(ofs + 0) * scale,
  248.                     y : this.view.getFloat32(ofs + 4) * scale,
  249.                     z : this.view.getFloat32(ofs + 8) * scale
  250.                 }
  251.                 ofs+=12;
  252.                 break;
  253.             }
  254.            
  255.             const end = ofs;
  256.             if(end - start !== vertex_length) {
  257.                 throw new Error("Phission Mailed, better luck next time");
  258.             }
  259.            
  260.             vertices[k] = vertex;
  261.         }
  262.        
  263.         // Last we replace the vertex group with the finalized vertex list
  264.        
  265.         this.vertex_groups[i] = vertices;
  266.        
  267.     }
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement