Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- readMaterials() {
- const mat_count = this.header.material_count;
- // First we read the number of textures in each material and then get the offset
- // to each of the respective material definitions
- for(let i = 0; i < mat_count; i++) {
- const ofs = this.header.material_ofs;;
- const unj_matlist_size = 8;
- const unj_matlist_t = {
- diffuse_texture_count : this.view.getUint8(mat_ofs + i*unj_mat_size + 0x00),
- effect_texture_count : this.view.getUint8(mat_ofs + i*unj_mat_size + 0x01),
- material_ofs : this.view.getUint32(mat_ofs + i*unj_matlist_size + 0x04)
- }
- this.materials[i] = {
- color : {},
- offset : unj_matlist_t.material_ofs,
- diffuse_textures : new Array(unj_matlist_t.diffuse_texture_count),
- effect_textures : new Array(unj_matlist_t.effect_texture_count)
- }
- }
- // Then we 'seek' to each one of the respective material definitions, read the
- // color values for the material, and then the texture preferences
- this.materials.forEach( mat => {
- let ofs = mat.offset;
- // We skip the first 8 bytes, since we don't know what it does
- ofs += 8;
- // Then we read the mapping type of the respective textures
- for(let i = 0; i < mat.diffuse_textures.length; i++) {
- const byte = this.view.getUint8(ofs + 1);
- ofs += 4;
- mat.diffuse_textures[i] = {};
- switch(byte){
- case 1:
- mat.diffuse_textures[i].mappingType = "env";
- break;
- default:
- mat.diffuse_textures[i].mappingType = "uv";
- break;
- }
- }
- // Then we read all of the color values for the material
- for(let i = 0; i < 12; i++) {
- const a = this.view.getUint8(ofs + 0);
- const b = this.view.getUint8(ofs + 1);
- const c = this.view.getUint8(ofs + 2);
- const d = this.view.getUint8(ofs + 3);
- ofs += 4;
- switch(d) {
- case 0x54:
- mat.color.emissive = {
- r : a/255,
- g : b/255,
- b : c/255
- };
- break;
- case 0x55:
- mat.color.ambient = {
- r : a/255,
- g : b/255,
- b : c/255
- };
- break;
- case 0x56:
- mat.color.diffuse = {
- r : a/255,
- g : b/255,
- b : c/255
- };
- break;
- case 0x57:
- mat.color.specular = {
- r : a/255,
- g : b/255,
- b : c/255
- };
- break;
- case 0x58:
- const alpha = a/255;
- mat.color.emissive = mat.color.emissive || {};
- mat.color.ambient = mat.color.ambient || {};
- mat.color.diffuse = mat.color.diffuse || {};
- mat.color.specular = mat.color.specular || {};
- mat.color.emissive.a = alpha;
- mat.color.ambient.a = alpha;
- mat.color.diffuse.a = alpha;
- mat.color.specular.a = alpha;
- break;
- case 0x5b:
- // convert bytes to float
- mat.color.specular_coefficient = [a, b, c, d];
- break;
- }
- }
- // Then we read all of the texture properties
- for(let i = 0; i < mat.diffuse_textures.length; i++) {
- do {
- const a = this.view.getUint8(ofs + 0);
- const b = this.view.getUint8(ofs + 1);
- const c = this.view.getUint8(ofs + 2);
- const d = this.view.getUint8(ofs + 3);
- ofs += 4;
- switch(d) {
- case 0x21:
- mat.diffuse_textures[i].disableLighting = (a === 1);
- break;
- case 0x22:
- mat.diffuse_textures[i].alphaTestEnabled = (a === 1);
- break;
- case 0x23:
- mat.diffuse_textures[i].zTestEnabled = (a === 1);
- break;
- case 0x24:
- mat.diffuse_textures[i].stencilTestEnabled = (a === 1);
- break;
- case 0x27:
- mat.diffuse_textures[i].colorTestEnabled = (a === 1);
- break;
- case 0x28:
- mat.diffuse_textures[i].logicOpEnabled = (a === 1);
- break;
- case 0x48:
- // convert bytes to float
- mat.diffuse_textures[i].scaleU = [a, b, c, d];
- break;
- case 0x49:
- // convert bytes to float
- mat.diffuse_textures[i].scaleV = [a, b, c, d]
- break;
- case 0x4A:
- mat.diffuse_textures[i].offsetU = [a, b, c, d];
- break;
- case 0x4B:
- mat.diffuse_textures[i].offsetV = [a, b, c, d];
- break;
- case 0x5E:
- mat.diffuse_textures[i].diffuseEnabled = a < 2;
- break;
- case 0xC7:
- mat.diffuse_textures[i].clampU = (a & 1) !== 0;
- mat.diffuse_textures[i].clampV = (b & 1) !== 0;
- break;
- case 0xC9:
- mat.diffuse_textures[i].textureFunction = (a & 7);
- mat.diffuse_textures[i].textureFunctionUsesAlpha = (b === 1);
- break;
- case 0xDB:
- mat.diffuse_textures[i].alphaFunction = a;
- mat.diffuse_textures[i].alphaRef = b;
- break;
- case 0xDE:
- mat.diffuse_textures[i].zTestFunction = a;
- break;
- case 0xDF:
- mat.diffuse_textures[i].blendMode = b;
- mat.diffuse_textures[i].blendFactorA = a;
- mat.diffuse_textures[i].blendFactorB = a;
- break;
- case 0xE0:
- mat.diffuse_textures[i].blendFixedA = (c << 16) | (b << 8) | a;
- break;
- case 0xE1:
- mat.diffuse_textures[i].blendFixedB = (c << 16) | (b << 8) | a;
- break;
- case 0xE6:
- mat.diffuse_textures[i].logicOp = a;
- break;
- case 0xE7:
- mat.diffuse_textures[i].zWriteDisabled = a ? true : false;
- break;
- case 0xE8:
- mat.diffuse_textures[i].maskRGB = (c << 16) | (b << 8) | a;
- break;
- case 0xE9:
- mat.diffuse_textures[i].maskAlpha = (c << 16) | (b << 8) | a;
- break;
- }
- } while(d !== 0x0b);
- }
- // Then we read the texture id
- for(let i = 0; i < mat.diffuse_textures.length; i++) {
- mat.diffuse_textures[i].texture_id = this.view.getUint32(ofs, true);
- ofs += 4;
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement