Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************************
  2.  *
  3.  * MIT License
  4.  *
  5.  * Copyright (c) 2018 Benjamin Collins (kion @ dashgl.com)
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  *****************************************************************************/
  26.  
  27.  
  28. THREE.DashExporter = function() {
  29.  
  30.     this.influence = false;
  31.     this.groupflags = {
  32.         uv0 : false,
  33.         uv1 : false,
  34.         normal : false,
  35.         vcolor : false
  36.     };
  37.  
  38.     this.bones = [];
  39.     this.vertices = [];
  40.     this.textures = [];
  41.     this.materials = [];
  42.     this.groups = [];
  43.     this.anims = [];
  44.  
  45.     this.blocks = [];
  46.  
  47. }
  48.  
  49. THREE.DashExporter.prototype = {
  50.  
  51.     constructor : THREE.DashExporter,
  52.  
  53.     parse : function(mesh) {
  54.  
  55.         // Get Bones
  56.  
  57.         this.getBones(mesh.skeleton);
  58.  
  59.         // Get Vertices
  60.  
  61.         this.getVertices(mesh.geometry);
  62.  
  63.         // Get Textures
  64.  
  65.         this.getTextures(mesh.material);   
  66.  
  67.         // Get Materials
  68.        
  69.         this.getMaterials(mesh.material);  
  70.        
  71.         // Get Face groups
  72.  
  73.         this.getGroups(mesh.geometry);
  74.  
  75.         // Get Animations
  76.    
  77.         this.getAnims(mesh.geometry);
  78.  
  79.         // write blocks
  80.  
  81.         this.writeBones();
  82.        
  83.         // Write vertices
  84.  
  85.         this.writeVertices();
  86.  
  87.         // Write Textures
  88.  
  89.         this.writeTextures();
  90.  
  91.         // Write Materials
  92.  
  93.         this.writeMaterials();
  94.  
  95.         // Write Face Groups
  96.  
  97.         this.writeFaceGroups();
  98.  
  99.         // Write Header
  100.        
  101.         this.writeHeader();
  102.  
  103.         // Write blob to zip (for testing)
  104.  
  105.         return this.writeBlob();
  106.  
  107.     },
  108.  
  109.     getBones : function(skeleton) {
  110.  
  111.         if(!skeleton) {
  112.             return;
  113.         }
  114.  
  115.         // Populate bones
  116.  
  117.         for(let i = 0; i < skeleton.bones.length; i++) {
  118.            
  119.             let bone = {
  120.                 id : i,
  121.                 elements : skeleton.bones[i].matrix.elements
  122.             };
  123.  
  124.             if(i === 0) {
  125.                 bone.parent = -1;
  126.             } else {
  127.                 bone.parent = skeleton.bones[i].parent.name
  128.             }
  129.  
  130.             this.bones.push(bone);
  131.  
  132.         }
  133.  
  134.     },
  135.  
  136.     getVertices : function(geometry) {
  137.  
  138.         // If bones exist, check for number of influence
  139.  
  140.         if(this.bones.length) {
  141.            
  142.             this.influence = true;
  143.  
  144.         }
  145.  
  146.         // Read all of the vertices
  147.        
  148.         for(let i = 0; i < geometry.vertices.length; i++) {
  149.  
  150.             let vertex = {
  151.                 pos : {
  152.                     x : geometry.vertices[i].x,
  153.                     y : geometry.vertices[i].y,
  154.                     z : geometry.vertices[i].z
  155.                 }
  156.             };
  157.  
  158.             if(this.influence) {
  159.                
  160.                 vertex.indice = {
  161.                     x : geometry.skinIndices[i].x,
  162.                     y : geometry.skinIndices[i].y,
  163.                     z : geometry.skinIndices[i].z,
  164.                     w : geometry.skinIndices[i].w
  165.                 }
  166.                
  167.                 vertex.weight = {
  168.                     x : geometry.skinWeights[i].x,
  169.                     y : geometry.skinWeights[i].y,
  170.                     z : geometry.skinWeights[i].z,
  171.                     w : geometry.skinWeights[i].w
  172.                 }
  173.  
  174.             }
  175.  
  176.             this.vertices.push(vertex);
  177.  
  178.         }
  179.  
  180.     },
  181.  
  182.     getTextures : function(material) {
  183.  
  184.         // Check if single material is used
  185.  
  186.         if(!Array.isArray(material)) {
  187.             material = [material];
  188.         }
  189.  
  190.         // Get texture for each material
  191.  
  192.         for(let i = 0; i < material.length; i++) {
  193.            
  194.             if(!material[i].map) {
  195.                 continue;
  196.             }
  197.  
  198.             let uuid = material[i].map.uuid;
  199.             let found = false;
  200.            
  201.             for(let k = 0; k < this.textures.length; k++) {
  202.                 if(this.textures[k].uuid !== uuid) {
  203.                     continue;
  204.                 }
  205.                 found = true;
  206.                 break;
  207.             }
  208.  
  209.             if(found) {
  210.                 continue;
  211.             }
  212.            
  213.             this.textures.push({
  214.                 uuid : uuid,
  215.                 img : material[i].map.image
  216.             });
  217.  
  218.         }
  219.  
  220.     },
  221.  
  222.     getMaterials : function(material) {
  223.  
  224.         // Check if single material is used
  225.  
  226.         if(!Array.isArray(material)) {
  227.             material = [material];
  228.         }
  229.  
  230.         // Get texture for each material
  231.  
  232.         for(let i = 0; i < material.length; i++) {
  233.            
  234.             // Default Diffuse
  235.  
  236.             let mat = {
  237.                 diffuse : {
  238.                     r : material[i].color.r,
  239.                     g : material[i].color.g,
  240.                     b : material[i].color.b
  241.                 }
  242.             };
  243.            
  244.             // If texture, set index reference
  245.  
  246.             if(material[i].map) {
  247.                
  248.                 let uuid = material[i].map.uuid;
  249.  
  250.                 for(let k = 0; k < this.textures.length; k++) {
  251.                     if(this.textures[k].uuid !== uuid) {
  252.                         continue;
  253.                     }
  254.                     mat.map0 = k;
  255.                     break;
  256.                 }
  257.  
  258.             }
  259.  
  260.             this.materials.push(mat);
  261.  
  262.         }
  263.  
  264.     },
  265.  
  266.     getGroups : function(geometry) {
  267.  
  268.         // Check for uv0
  269.  
  270.         if(geometry.faceVertexUvs && geometry.faceVertexUvs[0]) {
  271.             this.groupflags.uv0 = true;
  272.         }
  273.  
  274.         // Check for uv1
  275.  
  276.         if(geometry.faceVertexUvs && geometry.faceVertexUvs[1]) {
  277.             this.groupflags.uv1 = true;
  278.         }
  279.  
  280.         // Check for vertex normal
  281.  
  282.         if(geometry.faces[0].vertexNormals.length) {
  283.             this.groupflags.normal = true;
  284.         }
  285.  
  286.         // Check for vertex color
  287.  
  288.         if(geometry.faces[0].vertexColors.length) {
  289.             this.groupflags.vcolor = true;
  290.         }
  291.  
  292.         // Loop over the faces
  293.  
  294.         let group;
  295.         let matId = -1;
  296.  
  297.         for(let i = 0; i < geometry.faces.length; i++) {
  298.  
  299.             let face = geometry.faces[i];
  300.             let matIndex = face.materialIndex;
  301.  
  302.             if(matId !== matIndex) {
  303.                 if(group) {
  304.                     this.groups.push(group);
  305.                 }
  306.                
  307.                 matId = matIndex;
  308.                 group = {
  309.                     matIndex : matIndex,
  310.                     tri : []
  311.                 };
  312.             }
  313.  
  314.             let a = {
  315.                 index : face.a
  316.             }
  317.  
  318.             let b = {
  319.                 index : face.b
  320.             }
  321.  
  322.             let c = {
  323.                 index : face.c
  324.             }
  325.  
  326.             if(this.groupflags.uv0) {
  327.                
  328.                 a.uv0 = {
  329.                     u : geometry.faceVertexUvs[0][i][0].x,
  330.                     v : geometry.faceVertexUvs[0][i][0].y
  331.                 }
  332.                
  333.                 b.uv0 = {
  334.                     u : geometry.faceVertexUvs[0][i][1].x,
  335.                     v : geometry.faceVertexUvs[0][i][1].y
  336.                 }
  337.                
  338.                 c.uv0 = {
  339.                     u : geometry.faceVertexUvs[0][i][2].x,
  340.                     v : geometry.faceVertexUvs[0][i][2].y
  341.                 }
  342.  
  343.             }
  344.  
  345.             if(this.groupflags.vcolor) {
  346.  
  347.                 a.vcolor = {
  348.                     r : face.vertexColors[0].r,
  349.                     g : face.vertexColors[0].g,
  350.                     b : face.vertexColors[0].b
  351.                 }
  352.  
  353.                 b.vcolor = {
  354.                     r : face.vertexColors[1].r,
  355.                     g : face.vertexColors[1].g,
  356.                     b : face.vertexColors[1].b
  357.                 }
  358.  
  359.                 b.vcolor = {
  360.                     r : face.vertexColors[2].r,
  361.                     g : face.vertexColors[2].g,
  362.                     b : face.vertexColors[2].b
  363.                 }
  364.  
  365.             }
  366.  
  367.             group.tri.push(a, b, c);
  368.  
  369.         }
  370.  
  371.         this.groups.push(group);
  372.  
  373.     },
  374.  
  375.     getAnims : function(geometry) {
  376.  
  377.         if(!geometry.animations || !geometry.animations.length) {
  378.             return;
  379.         }
  380.        
  381.         for(let i = 0; i < geometry.animations.length; i++) {
  382.  
  383.  
  384.         }
  385.  
  386.     },
  387.  
  388.     calcBufferLen : function(byteLen) {
  389.        
  390.         let bufferLen;
  391.         let remainder = byteLen % 16;
  392.  
  393.         if(remainder === 8) {
  394.             bufferLen = byteLen + 8;
  395.         } else if(remainder < 8) {
  396.             bufferLen = byteLen + (16 - remainder);
  397.         } else {
  398.             bufferLen = byteLen + (16 - remainder) + 16;
  399.         }
  400.        
  401.         return bufferLen;
  402.  
  403.     },
  404.  
  405.     writeBones : function() {
  406.        
  407.         if(!this.bones.length) {
  408.             return;
  409.         }
  410.  
  411.         // First read the bones list
  412.  
  413.         let structSize = (16 * 4 + 4);
  414.         let byteLen = this.bones.length * structSize;
  415.         let bufferLen = this.calcBufferLen(byteLen);
  416.            
  417.         let buffer = new ArrayBuffer(bufferLen);
  418.         let view = new DataView(buffer);
  419.    
  420.         view.setUint8(0, 0x42); // 'B'
  421.         view.setUint8(1, 0x4F); // '0'
  422.         view.setUint8(2, 0x4E); // 'N'
  423.         view.setUint8(3, 0x45); // 'E'
  424.         view.setUint32(4, byteLen, true);
  425.        
  426.         let ofs = 8;
  427.         for(let i = 0; i < this.bones.length; i++) {
  428.  
  429.             view.setInt16(ofs, this.bones[i].id, true);
  430.             view.setInt16(ofs + 2, this.bones[i].parent, true);
  431.             ofs += 4;
  432.            
  433.             this.bones[i].elements.forEach(e => {
  434.                 view.setFloat32(ofs, e, true);
  435.                 ofs += 4;
  436.             });
  437.  
  438.         }
  439.        
  440.         this.blocks.push({
  441.             type : "BONE",
  442.             num : this.bones.length,
  443.             buffer : buffer
  444.         });
  445.  
  446.     },
  447.  
  448.     writeVertices : function() {
  449.        
  450.         let structSize = 12;
  451.        
  452.         if(this.influence) {
  453.             structSize += 2*4 + 4*4;
  454.         }
  455.  
  456.         let byteLen = this.vertices.length * structSize;
  457.         let bufferLen = this.calcBufferLen(byteLen);
  458.  
  459.         let buffer = new ArrayBuffer(bufferLen);
  460.         let view = new DataView(buffer);
  461.    
  462.         view.setUint8(0, 0x56); // 'V'
  463.         view.setUint8(1, 0x45); // 'E'
  464.         view.setUint8(2, 0x52); // 'R'
  465.         view.setUint8(3, 0x54); // 'T'
  466.         view.setUint32(4, byteLen, true);
  467.        
  468.         let ofs = 8;
  469.         for(let i = 0; i < this.vertices.length; i++) {
  470.  
  471.             view.setFloat32(ofs + 0, this.vertices[i].x, true);
  472.             view.setFloat32(ofs + 4, this.vertices[i].y, true);
  473.             view.setFloat32(ofs + 8, this.vertices[i].z, true);
  474.             ofs += 12;
  475.  
  476.             if(!this.influence) {
  477.                 continue;
  478.             }
  479.            
  480.             view.setUint16(ofs + 0, this.vertices[i].indice.x, true);
  481.             view.setUint16(ofs + 2, this.vertices[i].indice.y, true);
  482.             view.setUint16(ofs + 4, this.vertices[i].indice.z, true);
  483.             view.setUint16(ofs + 6, this.vertices[i].indice.w, true);
  484.             ofs += 8;
  485.            
  486.             view.setFloat32(ofs + 0, this.vertices[i].weight.x, true);
  487.             view.setFloat32(ofs + 4, this.vertices[i].weight.y, true);
  488.             view.setFloat32(ofs + 8, this.vertices[i].weight.z, true);
  489.             view.setFloat32(ofs + 12, this.vertices[i].weight.w, true);
  490.             ofs += 16;
  491.  
  492.         }
  493.  
  494.         this.blocks.push({
  495.             type : "VERT",
  496.             buffer : buffer,
  497.             num : this.vertices.length,
  498.             flag : this.influence
  499.         });
  500.  
  501.     },
  502.  
  503.     writeTextures : function() {
  504.  
  505.         for(let i = 0; i < this.textures.length; i++) {
  506.            
  507.             switch(this.textures[i].img.tagName) {
  508.             case "CANVAS":
  509.                 let canvas = this.textures[i].img;
  510.                 let data = canvas.toDataURL("image/png");
  511.  
  512.                 let index = data.indexOf(',')+1
  513.                 data = data.substr(index);
  514.                 data = window.atob(data);
  515.  
  516.                 let byteLen = data.length;
  517.                 let bufferLen = this.calcBufferLen(byteLen);
  518.                
  519.                 let buffer = new ArrayBuffer(bufferLen);
  520.                 let view = new DataView(buffer);
  521.    
  522.                 view.setUint8(0, 0x54); // 'T'
  523.                 view.setUint8(1, 0x45); // 'E'
  524.                 view.setUint8(2, 0x58); // 'X'
  525.                 view.setUint8(3, 0);    // '\0'
  526.                 view.setUint32(4, byteLen, true);
  527.  
  528.                 let ofs = 8;
  529.                 for(let i = 0; i < byteLen; i++) {
  530.                     let byte = data.charCodeAt(i);
  531.                     view.setUint8(ofs, byte);
  532.                     ofs++;
  533.                 }
  534.  
  535.                 this.blocks.push({
  536.                     type : "TEX",
  537.                     id : i,
  538.                     flag : "PNG",
  539.                     buffer : buffer
  540.                 });
  541.  
  542.                 break;
  543.             case "IMG":
  544.                 break;
  545.             }
  546.  
  547.         }
  548.  
  549.     },
  550.  
  551.     writeMaterials : function() {
  552.  
  553.         for(let i = 0; i < this.materials.length; i++) {
  554.  
  555.             let byteLen = 0;
  556.             let properties = Object.keys(this.materials[i]);
  557.  
  558.             for(let key in this.materials[i]) {
  559.                
  560.                 switch(key) {
  561.                 case "diffuse":
  562.                     byteLen += 16;
  563.                     break;
  564.                 case "map0":
  565.                     byteLen += 8;
  566.                     break;
  567.                 }
  568.  
  569.             }
  570.  
  571.             let bufferLen = this.calcBufferLen(byteLen);
  572.             let buffer = new ArrayBuffer(bufferLen);
  573.             let view = new DataView(buffer);
  574.  
  575.             view.setUint8(0, "M".charCodeAt(0));
  576.             view.setUint8(1, "A".charCodeAt(0));
  577.             view.setUint8(2, "T".charCodeAt(0));
  578.             view.setUint8(3, 0);
  579.             view.setUint32(4, byteLen, true);
  580.            
  581.             let ofs = 8;
  582.             for(let key in this.materials[i]) {
  583.                
  584.                 let val = this.materials[i][key];
  585.                 console.log(val);
  586.  
  587.                 switch(key) {
  588.                 case "diffuse":
  589.  
  590.                     view.setUint8(ofs + 0, "D".charCodeAt(0));
  591.                     view.setUint8(ofs + 1, "I".charCodeAt(0));
  592.                     view.setUint8(ofs + 2, "F".charCodeAt(0));
  593.                     view.setUint8(ofs + 3, "F".charCodeAt(0));
  594.                     view.setFloat32(ofs + 4, val.r);
  595.                     view.setFloat32(ofs + 8, val.r);
  596.                     view.setFloat32(ofs + 12, val.r);
  597.                     ofs += 16;
  598.  
  599.                     break;
  600.                 case "map0":
  601.  
  602.                     view.setUint8(ofs + 0, "M".charCodeAt(0));
  603.                     view.setUint8(ofs + 1, "A".charCodeAt(0));
  604.                     view.setUint8(ofs + 2, "P".charCodeAt(0));
  605.                     view.setUint8(ofs + 3, "0".charCodeAt(0));
  606.                     view.setUint32(ofs + 4, val);
  607.                     ofs += 8;
  608.                     break;
  609.                 }
  610.             }
  611.  
  612.             this.blocks.push({
  613.                 type : "MAT",
  614.                 id : i,
  615.                 num : properties.length,
  616.                 buffer: buffer
  617.             });
  618.            
  619.             console.log("okay");
  620.         }
  621.  
  622.     },
  623.  
  624.     writeFaceGroups : function() {
  625.  
  626.         let structSize = 2;
  627.         let flags = 0;
  628.  
  629.         if(this.groupflags.uv0) {
  630.             structSize += 8;
  631.             flags |= 1;
  632.         }
  633.  
  634.         if(this.groupflags.uv1) {
  635.             structSize += 8;
  636.             flags |= 2;
  637.         }
  638.  
  639.         if(this.groupflags.normal) {
  640.             structSize += 12;
  641.             flags |= 4;
  642.         }  
  643.  
  644.         if(this.groupflags.vcolor) {
  645.             structSize += 12;
  646.             flags |= 8;
  647.         }  
  648.  
  649.         for(let i = 0; i < this.groups.length; i++) {
  650.            
  651.             let group = this.groups[i];
  652.  
  653.             let byteLen = group.tri.length * structSize;
  654.             let bufferLen = this.calcBufferLen(byteLen);
  655.  
  656.             let buffer = new ArrayBuffer(bufferLen);
  657.             let view = new DataView(buffer);
  658.  
  659.             view.setUint8(0, 0x46); // 'F'
  660.             view.setUint8(1, 0x41); // 'A'
  661.             view.setUint8(2, 0x43); // 'C'
  662.             view.setUint8(3, 0x45); // 'E'
  663.             view.setUint32(4, byteLen, true);
  664.            
  665.             let ofs = 8;
  666.  
  667.             group.tri.forEach(tri => {
  668.                
  669.                 view.setUint16(ofs, tri.index, true);
  670.                 ofs += 2;
  671.  
  672.                 if(this.groupflags.uv0) {
  673.                     view.setFloat32(ofs + 0, tri.uv0.u, true);
  674.                     view.setFloat32(ofs + 4, tri.uv0.v, true);
  675.                     ofs += 8;
  676.                 }
  677.  
  678.                 if(this.groupflags.uv1) {
  679.                     view.setFloat32(ofs + 0, tri.uv1.u, true);
  680.                     view.setFloat32(ofs + 4, tri.uv1.v, true);
  681.                     ofs += 8;
  682.                 }
  683.  
  684.                 if(this.groupflags.normal) {
  685.                     view.setFloat32(ofs + 0, tri.normal.x, true);
  686.                     view.setFloat32(ofs + 4, tri.normal.y, true);
  687.                     view.setFloat32(ofs + 8, tri.normal.z, true);
  688.                     ofs += 12;
  689.                 }  
  690.  
  691.                 if(this.groupflags.vcolor) {
  692.                    
  693.                     tri.vcolor = tri.vcolor || { r : 1, g : 1, b : 1 };
  694.                     view.setFloat32(ofs + 0, tri.vcolor.r, true);
  695.                     view.setFloat32(ofs + 4, tri.vcolor.g, true);
  696.                     view.setFloat32(ofs + 8, tri.vcolor.b, true);
  697.                     ofs += 12;
  698.                 }  
  699.  
  700.             });
  701.  
  702.             this.blocks.push({
  703.                 type : "FACE",
  704.                 id : i,
  705.                 mat_id : group.matIndex,
  706.                 flags: flags,
  707.                 num : group.tri.length,
  708.                 buffer : buffer
  709.             });
  710.  
  711.         }
  712.  
  713.     },
  714.  
  715.     writeHeader : function() {
  716.  
  717.         let byteLen = (this.blocks.length+2) * 16;
  718.         let buffer = new ArrayBuffer(byteLen);
  719.         let view = new DataView(buffer);
  720.  
  721.         view.setUint8(0, 0x44); // 'D'
  722.         view.setUint8(1, 0x41); // 'A'
  723.         view.setUint8(2, 0x53); // 'S'
  724.         view.setUint8(3, 0x48); // 'H'
  725.         view.setUint32(4, 1, true);
  726.         view.setUint32(8, 16, true);
  727.         view.setUint32(12, this.blocks.length, true);
  728.  
  729.         let ofs = 16;
  730.         let startOfs = byteLen;
  731.  
  732.         for(let i = 0; i < this.blocks.length; i++) {
  733.            
  734.             let block = this.blocks[i];
  735.             switch(block.type) {
  736.             case "BONE":
  737.                 view.setUint8(ofs + 0, 'B'.charCodeAt(0));
  738.                 view.setUint8(ofs + 1, 'O'.charCodeAt(0));
  739.                 view.setUint8(ofs + 2, 'N'.charCodeAt(0));
  740.                 view.setUint8(ofs + 3, 'E'.charCodeAt(0));
  741.                 view.setUint32(ofs + 4, startOfs, true);
  742.                 view.setUint32(ofs + 12, block.num, true);
  743.                 break;
  744.             case "VERT":
  745.                 view.setUint8(ofs + 0, 'V'.charCodeAt(0));
  746.                 view.setUint8(ofs + 1, 'E'.charCodeAt(0));
  747.                 view.setUint8(ofs + 2, 'R'.charCodeAt(0));
  748.                 view.setUint8(ofs + 3, 'T'.charCodeAt(0));
  749.                 view.setUint32(ofs + 4, startOfs, true);
  750.                 if(block.flag) {
  751.                     view.setUint32(ofs + 8, 1, true);
  752.                 }
  753.                 view.setUint32(ofs + 12, block.num, true);
  754.                 break;
  755.             case "TEX":
  756.                 view.setUint8(ofs + 0, 'T'.charCodeAt(0));
  757.                 view.setUint8(ofs + 1, 'E'.charCodeAt(0));
  758.                 view.setUint8(ofs + 2, 'X'.charCodeAt(0));
  759.                 view.setUint32(ofs + 4, startOfs, true);
  760.                 view.setUint32(ofs + 8, block.id, true);
  761.                 for(let k = 0; k < block.flag.length; k++) {
  762.                     view.setUint8(ofs + 12+k, block.flag.charCodeAt(k));
  763.                 }
  764.                 break;
  765.             case "MAT":
  766.                 view.setUint8(ofs + 0, 'M'.charCodeAt(0));
  767.                 view.setUint8(ofs + 1, 'A'.charCodeAt(0));
  768.                 view.setUint8(ofs + 2, 'T'.charCodeAt(0));
  769.                 view.setUint32(ofs + 4, startOfs, true);
  770.                 view.setUint32(ofs + 8, block.id, true);
  771.                 view.setUint32(ofs + 12, block.num, true);
  772.                 break;
  773.             case "FACE":
  774.                 view.setUint8(ofs + 0, 'F'.charCodeAt(0));
  775.                 view.setUint8(ofs + 1, 'A'.charCodeAt(0));
  776.                 view.setUint8(ofs + 2, 'C'.charCodeAt(0));
  777.                 view.setUint8(ofs + 3, 'E'.charCodeAt(0));
  778.                 view.setUint32(ofs + 4, startOfs, true);
  779.                 view.setUint16(ofs + 8, block.mat_id, true);
  780.                 view.setUint16(ofs + 10, block.flags, true);
  781.                 view.setUint32(ofs + 12, block.num, true);
  782.                 break;
  783.             case "ANIM":
  784.                
  785.                 break;
  786.             }
  787.  
  788.             ofs += 16;
  789.             startOfs += block.buffer.byteLength;
  790.         }
  791.  
  792.         this.blocks.unshift({
  793.             type : "HEAD",
  794.             buffer : buffer
  795.         });
  796.  
  797.     },
  798.  
  799.     writeBlob : function() {
  800.        
  801.         let array = new Array(this.blocks.length);
  802.  
  803.         for(let i = 0; i < this.blocks.length; i++) {
  804.             array[i] = this.blocks[i].buffer;
  805.         }
  806.        
  807.         return = new Blob(array);
  808.  
  809.     }
  810.  
  811. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement