Advertisement
Guest User

Unj Read Material Function

a guest
Dec 23rd, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. readMaterials() {
  2.    
  3.     const mat_count = this.header.material_count;
  4.    
  5.     // First we read the number of textures in each material and then get the offset
  6.     // to each of the respective material definitions
  7.    
  8.     for(let i = 0; i < mat_count; i++) {
  9.         const ofs = this.header.material_ofs;;
  10.         const unj_matlist_size = 8;
  11.  
  12.         const unj_matlist_t = {
  13.             diffuse_texture_count : this.view.getUint8(mat_ofs + i*unj_mat_size + 0x00),
  14.             effect_texture_count : this.view.getUint8(mat_ofs + i*unj_mat_size + 0x01),
  15.             material_ofs : this.view.getUint32(mat_ofs + i*unj_matlist_size + 0x04)
  16.         }
  17.  
  18.         this.materials[i] = {
  19.             color : {},
  20.             offset : unj_matlist_t.material_ofs,
  21.             diffuse_textures : new Array(unj_matlist_t.diffuse_texture_count),
  22.             effect_textures : new Array(unj_matlist_t.effect_texture_count)
  23.         }
  24.  
  25.     }
  26.    
  27.     // Then we 'seek' to each one of the respective material definitions, read the
  28.     // color values for the material, and then the texture preferences
  29.    
  30.     this.materials.forEach( mat => {
  31.         let ofs = mat.offset;
  32.        
  33.         // We skip the first 8 bytes, since we don't know what it does
  34.         ofs += 8;
  35.  
  36.         // Then we read the mapping type of the respective textures
  37.         for(let i = 0; i < mat.diffuse_textures.length; i++) {
  38.             const byte = this.view.getUint8(ofs + 1);
  39.             ofs += 4;
  40.            
  41.             mat.diffuse_textures[i] = {};
  42.  
  43.             switch(byte){
  44.             case 1:
  45.                 mat.diffuse_textures[i].mappingType = "env";
  46.                 break;
  47.             default:
  48.                 mat.diffuse_textures[i].mappingType = "uv";
  49.                 break;
  50.             }
  51.         }
  52.  
  53.         // Then we read all of the color values for the material
  54.  
  55.         for(let i = 0; i < 12; i++) {
  56.            
  57.             const a = this.view.getUint8(ofs + 0);
  58.             const b = this.view.getUint8(ofs + 1);
  59.             const c = this.view.getUint8(ofs + 2);
  60.             const d = this.view.getUint8(ofs + 3);
  61.             ofs += 4;
  62.  
  63.             switch(d) {
  64.             case 0x54:
  65.                 mat.color.emissive = {
  66.                     r : a/255,
  67.                     g : b/255,
  68.                     b : c/255
  69.                 };
  70.                 break;
  71.             case 0x55:
  72.                 mat.color.ambient = {
  73.                     r : a/255,
  74.                     g : b/255,
  75.                     b : c/255
  76.                 };
  77.                 break;
  78.             case 0x56:
  79.                 mat.color.diffuse = {
  80.                     r : a/255,
  81.                     g : b/255,
  82.                     b : c/255
  83.                 };
  84.                 break;
  85.             case 0x57:
  86.                 mat.color.specular = {
  87.                     r : a/255,
  88.                     g : b/255,
  89.                     b : c/255
  90.                 };
  91.                 break;
  92.             case 0x58:
  93.                 const alpha = a/255;
  94.                 mat.color.emissive = mat.color.emissive || {};
  95.                 mat.color.ambient = mat.color.ambient || {};
  96.                 mat.color.diffuse = mat.color.diffuse || {};
  97.                 mat.color.specular = mat.color.specular || {};
  98.  
  99.                 mat.color.emissive.a = alpha;
  100.                 mat.color.ambient.a = alpha;
  101.                 mat.color.diffuse.a = alpha;
  102.                 mat.color.specular.a = alpha;
  103.                 break;
  104.             case 0x5b:
  105.                 // convert bytes to float
  106.                 mat.color.specular_coefficient = [a, b, c, d];
  107.                 break;
  108.             }
  109.  
  110.         }
  111.  
  112.         // Then we read all of the texture properties
  113.         for(let i = 0; i < mat.diffuse_textures.length; i++) {
  114.            
  115.             do {
  116.            
  117.                 const a = this.view.getUint8(ofs + 0);
  118.                 const b = this.view.getUint8(ofs + 1);
  119.                 const c = this.view.getUint8(ofs + 2);
  120.                 const d = this.view.getUint8(ofs + 3);
  121.                 ofs += 4;
  122.  
  123.                 switch(d) {
  124.                 case 0x21:
  125.                     mat.diffuse_textures[i].disableLighting = (a === 1);
  126.                     break;
  127.                 case 0x22:
  128.                     mat.diffuse_textures[i].alphaTestEnabled = (a === 1);
  129.                     break;
  130.                 case 0x23:
  131.                     mat.diffuse_textures[i].zTestEnabled = (a === 1);
  132.                     break;
  133.                 case 0x24:
  134.                     mat.diffuse_textures[i].stencilTestEnabled = (a === 1);
  135.                     break;
  136.                 case 0x27:
  137.                     mat.diffuse_textures[i].colorTestEnabled = (a === 1);
  138.                     break;
  139.                 case 0x28:
  140.                     mat.diffuse_textures[i].logicOpEnabled = (a === 1);
  141.                     break;
  142.                 case 0x48:
  143.                     // convert bytes to float
  144.                     mat.diffuse_textures[i].scaleU = [a, b, c, d];
  145.                     break;
  146.                 case 0x49:
  147.                     // convert bytes to float
  148.                     mat.diffuse_textures[i].scaleV = [a, b, c, d]
  149.                     break;
  150.                 case 0x4A:
  151.                     mat.diffuse_textures[i].offsetU = [a, b, c, d];
  152.                     break;
  153.                 case 0x4B:
  154.                     mat.diffuse_textures[i].offsetV = [a, b, c, d];
  155.                     break;
  156.                 case 0x5E:
  157.                     mat.diffuse_textures[i].diffuseEnabled = a < 2;
  158.                     break;
  159.                 case 0xC7:
  160.                     mat.diffuse_textures[i].clampU = (a & 1) !== 0;
  161.                     mat.diffuse_textures[i].clampV = (b & 1) !== 0;
  162.                     break;
  163.                 case 0xC9:
  164.                     mat.diffuse_textures[i].textureFunction = (a & 7);
  165.                     mat.diffuse_textures[i].textureFunctionUsesAlpha = (b === 1);
  166.                     break;
  167.                 case 0xDB:
  168.                     mat.diffuse_textures[i].alphaFunction = a;
  169.                     mat.diffuse_textures[i].alphaRef = b;
  170.                     break;
  171.                 case 0xDE:
  172.                     mat.diffuse_textures[i].zTestFunction = a;
  173.                     break;
  174.                 case 0xDF:
  175.                     mat.diffuse_textures[i].blendMode = b;
  176.                     mat.diffuse_textures[i].blendFactorA = a;
  177.                     mat.diffuse_textures[i].blendFactorB = a;
  178.                     break;
  179.                 case 0xE0:
  180.                     mat.diffuse_textures[i].blendFixedA = (c << 16) | (b << 8) | a;
  181.                     break;
  182.                 case 0xE1:
  183.                     mat.diffuse_textures[i].blendFixedB = (c << 16) | (b << 8) | a;
  184.                     break;
  185.                 case 0xE6:
  186.                     mat.diffuse_textures[i].logicOp = a;
  187.                     break;
  188.                 case 0xE7:
  189.                     mat.diffuse_textures[i].zWriteDisabled = a ? true : false;
  190.                     break;
  191.                 case 0xE8:
  192.                     mat.diffuse_textures[i].maskRGB = (c << 16) | (b << 8) | a;
  193.                     break;
  194.                 case 0xE9:
  195.                     mat.diffuse_textures[i].maskAlpha = (c << 16) | (b << 8) | a;
  196.                     break;
  197.                 }
  198.  
  199.             } while(d !== 0x0b);
  200.  
  201.         }
  202.  
  203.         // Then we read the texture id
  204.         for(let i = 0; i < mat.diffuse_textures.length; i++) {
  205.            
  206.             mat.diffuse_textures[i].texture_id = this.view.getUint32(ofs, true);
  207.             ofs += 4;
  208.  
  209.         }
  210.  
  211.     });
  212.    
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement