Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- readVertexGroups() {
- // First we 'seek' to the vertex group definitions and read the
- // uv count and the offset to the vertex list definition
- let ofs = this.header.vertex_group_ofs;
- for(let i = 0; i < this.header.vertex_group_count; i++) {
- const unj_vertex_group_t = {
- uv_count : this.view.getUint32(ofs + 0, true),
- group_ofs : this.view.getUint32(ofs + 4, true)
- }
- this.vertex_groups[i] = unj_vertex_group_t;
- ofs += 8;
- }
- // Then we seek to each one of the vertex list definitions and
- // read the number of bone influences, which bones, how many vertices
- // and the format of the stored vertices in the vertex list
- for(let i = 0; i < this.header.vertex_group_count; i++) {
- ofs = this.vertex_groups[i].group_ofs;
- const unj_vertex_list_t = {
- unkown_1 : this.view.getUint32(ofs + 0x00, true),
- vertex_format : this.view.getUint32(ofs + 0x04, true),
- unknown_2 : this.view.getUint32(ofs + 0x08, true),
- unknown_3 : this.view.getUint8(ofs + 0x09),
- vertex_length : this.view.getUint8(ofs + 0x0a),
- nop : this.view.getUint12(ofs + 0x0e, true),
- vertex_count_ofs : this.view.getUint32(ofs + 0x10, true),
- vertex_list_ofs : this.view.getUint32(ofs + 0x14, true),
- bone_binding_ofs : this.view.getUint32(ofs + 0x18, true),
- bone_binding_count : this.view.getUint32(ofs + 0x1c, true),
- total_vertex_count : this.view.getUint32(ofs + 0x20, true),
- unknown_4 : this.view.getUint32(ofs + 0x24, true),
- unknown_5 : this.view.getUint32(ofs + 0x28, true),
- vertex_scale : this.view.getFloat32(ofs + 0x2c, true)
- };
- // Read the number of vertices in the vertex list
- ofs = unj_vertex_list_t.vertex_count_ofs;
- unj_vertex_list_t.vertex_count = this.view.getUint32(ofs, true);
- // Read each of the bone influences for the group
- unj_vertex_list_t.bones = new Array();
- ofs = unj_vertex_list_t.bone_binding_ofs;
- for(let k = 0; k < unj_vertex_list_t.bone_binding_count; k++) {
- unj_vertex_list_t.bones[k] = this.view.getUint32(ofs, true);
- ofs += 4;
- }
- // Then we save all of the values from the struct into instance memory
- for(let key in unj_vertex_list_t) {
- this.vertex_groups[i][key] = unj_vertex_list_t[key];
- }
- }
- // Then we seek to the vertex list and read the values for the vertices
- for(let i = 0; i < this.header.vertex_group_count; i++) {
- const UINT8 = 1;
- const INT8 = 1;
- const INT16 = 2;
- const FLOAT = 3;
- const bones = this.vertex_groups[i].bones;
- const vertices = new Array(this.vertex_groups[i].vertex_count);
- const scale = this.vertex_groups[i].scale;
- const format = this.vertex_groups[i].vertex_format;
- const stride = this.vertex_groups[i].vertex_length;
- const uv_count = this.vertex_groups[i].uv_count;
- const uvFormat = (format & 0x3);
- const colorFormat = (format >> 2) & 0x7;
- const normalFormat = (format >> 5) & 0x3;
- const positionFormat = (format >> 7) & 0x3;
- const weightFormat = (format >> 9) & 0x3;
- // Then we seek to the vertex list and read the values
- let ofs = this.vertex_groups[i].vertex_list_ofs;
- for(let k = 0; k < vertices.length; k++) {
- const vertex = {};
- const start = ofs;
- // Read bone weight
- vertex.weight = [];
- switch(weightFormat) {
- case UINT8:
- for(let j = 0; j < bones.length; j++) {
- vertex.weight[j] = {
- bone_id : bones[j],
- weight : this.view.getUint8(ofs) / 0x7f
- }
- ofs++;
- }
- break;
- case INT16:
- for(let j = 0; j < bones.length; j++) {
- vertex.weight[j] = {
- bone_id : bones[j],
- weight : this.view.getInt16(ofs, true) / 0x7fff
- }
- ofs+=2;
- }
- break;
- case FLOAT:
- for(let j = 0; j < bones.length; j++) {
- vertex.weight[j] = {
- bone_id : bones[j],
- weight : this.view.getFloat32(ofs, true);
- }
- ofs+=4;
- }
- break;
- }
- // Read uv values
- vertex.uvs = [];
- switch(uvFormat) {
- case INT8:
- for(let j = 0; j < uv_count; j++) {
- vertex.uvs[j] = {
- u : this.view.getInt8(ofs + 0) / 0x7f,
- v : this.view.getInt8(ofs + 1) / 0x7f
- }
- ofs+=2;
- }
- break;
- case INT16:
- if(ofs % 2) {
- ofs += 2 - (ofs % 2);
- }
- for(let j = 0; j < uv_count; j++) {
- vertex.uvs[j] = {
- u : this.view.getInt16(ofs + 0) / 0x7fff,
- v : this.view.getInt16(ofs + 2) / 0x7fff
- }
- ofs+=4;
- }
- break;
- case FLOAT:
- if(ofs % 4) {
- ofs += 4 - (ofs % 4);
- }
- for(let j = 0; j < bones.length; j++) {
- vertex.uvs[j] = {
- u : this.view.getFloat32(ofs + 0),
- v : this.view.getFloat32(ofs + 4)
- }
- ofs+=8;
- }
- break;
- }
- // Read colors (we assume ARGB4444)
- if(colorFormat) {
- if(ofs % 2) {
- ofs += 2 - (ofs % 2);
- }
- let byte1 = this.view.getUint8(ofs + 0);
- let byte2 = this.view.getUint8(ofs + 1);
- ofs += 2;
- vertex.color = {
- r : ((byte1 >> 0) & 0xf) / 0x0f,
- g : ((color1 >> 4) & 0xf) / 0x0f,
- b : ((color2 >> 0) & 0xf) / 0x0f,
- a : ((color2 >> 4) & 0xf) / 0x0f
- };
- }
- // Read normals
- switch(normalFormat) {
- case UINT8:
- vertex.normal = {
- x : this.view.getUint8(ofs + 0) / 0x7f,
- y : this.view.getUint8(ofs + 1) / 0x7f,
- z : this.view.getUint8(ofs + 2) / 0x7f
- }
- ofs+=3;
- break;
- case INT16:
- if(ofs % 2) {
- ofs += 2 - (ofs % 2);
- }
- vertex.normal = {
- x : this.view.getInt16(ofs + 0) / 0x7fff,
- y : this.view.getInt16(ofs + 2) / 0x7fff,
- z : this.view.getInt16(ofs + 6) / 0x7fff
- }
- ofs+=6;
- break;
- case FLOAT:
- if(ofs % 4) {
- ofs += 4 - (ofs % 4);
- }
- vertex.normal = {
- x : this.view.getFloat32(ofs + 0),
- y : this.view.getFloat32(ofs + 4),
- z : this.view.getFloat32(ofs + 8)
- }
- ofs+=12;
- break;
- }
- // Read Position
- switch(positionFormat) {
- case UINT8:
- vertex.position = {
- x : this.view.getUint8(ofs + 0) / 0x7f * scale,
- y : this.view.getUint8(ofs + 1) / 0x7f * scale,
- z : this.view.getUint8(ofs + 2) / 0x7f * scale
- }
- ofs+=3;
- break;
- case INT16:
- if(ofs % 2) {
- ofs += 2 - (ofs % 2);
- }
- vertex.position = {
- x : this.view.getInt16(ofs + 0) / 0x7fff * scale,
- y : this.view.getInt16(ofs + 2) / 0x7fff * scale,
- z : this.view.getInt16(ofs + 6) / 0x7fff * scale
- }
- ofs+=6;
- break;
- case FLOAT:
- if(ofs % 4) {
- ofs += 4 - (ofs % 4);
- }
- vertex.position = {
- x : this.view.getFloat32(ofs + 0) * scale,
- y : this.view.getFloat32(ofs + 4) * scale,
- z : this.view.getFloat32(ofs + 8) * scale
- }
- ofs+=12;
- break;
- }
- const end = ofs;
- if(end - start !== vertex_length) {
- throw new Error("Phission Mailed, better luck next time");
- }
- vertices[k] = vertex;
- }
- // Last we replace the vertex group with the finalized vertex list
- this.vertex_groups[i] = vertices;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement