Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
107
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. THREE.DashExporter = function() {
  27.  
  28.     this.groupflags = {
  29.         uv0: false,
  30.         uv1: false,
  31.         normal: false,
  32.         vcolor: false
  33.     };
  34.  
  35.     this.bones = [];
  36.     this.vertices = [];
  37.     this.textures = [];
  38.     this.materials = [];
  39.     this.groups = [];
  40.     this.anims = [];
  41.  
  42.     this.blocks = [];
  43.  
  44. }
  45.  
  46. THREE.DashExporter.prototype = {
  47.  
  48.     constructor: THREE.DashExporter,
  49.  
  50.     parse: function(mesh) {
  51.  
  52.         // Get Bones
  53.  
  54.         this.getBones(mesh.skeleton);
  55.  
  56.         // Get Vertices
  57.  
  58.         this.getVertices(mesh.geometry);
  59.  
  60.         // Get Textures
  61.  
  62.         this.getTextures(mesh.material);
  63.  
  64.         // Get Materials
  65.  
  66.         this.getMaterials(mesh.material);
  67.  
  68.         // Get Face groups
  69.  
  70.         this.getFaceGroups(mesh.geometry);
  71.  
  72.         // Get Animations
  73.  
  74.         this.getAnims(mesh.geometry);
  75.  
  76.         // write blocks
  77.  
  78.         this.writeBones();
  79.  
  80.         // Write vertices
  81.  
  82.         this.writeVertices();
  83.  
  84.         // Write Textures
  85.  
  86.         this.writeTextures();
  87.  
  88.         // Write Materials
  89.  
  90.         this.writeMaterials();
  91.  
  92.         // Write Face Groups
  93.  
  94.         this.writeFaceGroups();
  95.  
  96.         // Write Header
  97.  
  98.         this.writeHeader();
  99.  
  100.         // Write blob to zip (for testing)
  101.  
  102.         return this.writeBlob();
  103.  
  104.     },
  105.  
  106.     getBones: function(skeleton) {
  107.  
  108.         if (!skeleton) {
  109.             return;
  110.         }
  111.  
  112.         // Populate bones
  113.  
  114.         for (let i = 0; i < skeleton.bones.length; i++) {
  115.  
  116.             let bone = {
  117.                 id: i,
  118.                 elements: skeleton.bones[i].matrix.elements
  119.             };
  120.  
  121.             if (i === 0) {
  122.                 bone.parent = -1;
  123.             } else {
  124.                 bone.parent = skeleton.bones[i].parent.name
  125.             }
  126.  
  127.             this.bones.push(bone);
  128.  
  129.         }
  130.  
  131.     },
  132.  
  133.     getVertices: function(geometry) {
  134.  
  135.         // Read all of the vertices
  136.  
  137.         for (let i = 0; i < geometry.vertices.length; i++) {
  138.  
  139.             let vertex = {
  140.                 pos: {
  141.                     x: geometry.vertices[i].x,
  142.                     y: geometry.vertices[i].y,
  143.                     z: geometry.vertices[i].z
  144.                 }
  145.             };
  146.  
  147.             if (this.bones.length) {
  148.  
  149.                 vertex.indice = {
  150.                     x: geometry.skinIndices[i].x,
  151.                     y: geometry.skinIndices[i].y,
  152.                     z: geometry.skinIndices[i].z,
  153.                     w: geometry.skinIndices[i].w
  154.                 }
  155.  
  156.                 vertex.weight = {
  157.                     x: geometry.skinWeights[i].x,
  158.                     y: geometry.skinWeights[i].y,
  159.                     z: geometry.skinWeights[i].z,
  160.                     w: geometry.skinWeights[i].w
  161.                 }
  162.  
  163.             }
  164.  
  165.             this.vertices.push(vertex);
  166.  
  167.         }
  168.  
  169.     },
  170.  
  171.     getTextures: function(material) {
  172.  
  173.         // Check if single material is used
  174.  
  175.         if (!Array.isArray(material)) {
  176.             material = [material];
  177.         }
  178.  
  179.         // Get texture for each material
  180.  
  181.         for (let i = 0; i < material.length; i++) {
  182.  
  183.             if (!material[i].map) {
  184.                 continue;
  185.             }
  186.  
  187.             let uuid = material[i].map.uuid;
  188.             let found = false;
  189.  
  190.             for (let k = 0; k < this.textures.length; k++) {
  191.                 if (this.textures[k].uuid !== uuid) {
  192.                     continue;
  193.                 }
  194.                 found = true;
  195.                 break;
  196.             }
  197.  
  198.             if (found) {
  199.                 continue;
  200.             }
  201.  
  202.             let canvas, ctx;
  203.  
  204.             let tex = material[i].map;
  205.             switch (tex.image.tagName) {
  206.                 case "IMG":
  207.                     canvas = document.createElement("canvas");
  208.                     canvas.width = tex.image.offsetWidth;
  209.                     canvas.height = tex.image.offsetHeight;
  210.                     ctx = canvas.getContext("2d");
  211.                     ctx.drawImage(tex.image, 0, 0);
  212.                     break;
  213.                 case "CANVAS":
  214.                     canvas = tex.image;
  215.                     break;
  216.             }
  217.  
  218.  
  219.             this.textures.push({
  220.                 uuid: uuid,
  221.                 img: canvas,
  222.                 flipY: tex.flipY,
  223.                 width: canvas.width,
  224.                 height: canvas.height,
  225.                 wrapS: tex.wrapS,
  226.                 wrapT: tex.wrapT
  227.             });
  228.  
  229.         }
  230.  
  231.     },
  232.  
  233.     getMaterials: function(material) {
  234.  
  235.         // Check if single material is used
  236.  
  237.         if (!Array.isArray(material)) {
  238.             material = [material];
  239.         }
  240.  
  241.         // Get texture for each material
  242.  
  243.         for (let i = 0; i < material.length; i++) {
  244.  
  245.             // Default Diffuse
  246.  
  247.             let mat = {};
  248.  
  249.             if (material[i].color) {
  250.                 mat.diffuse = {
  251.                     r: material[i].color.r,
  252.                     g: material[i].color.g,
  253.                     b: material[i].color.b
  254.                 };
  255.             }
  256.  
  257.             // If texture, set index reference
  258.  
  259.             if (material[i].map) {
  260.  
  261.                 let uuid = material[i].map.uuid;
  262.  
  263.                 for (let k = 0; k < this.textures.length; k++) {
  264.                     if (this.textures[k].uuid !== uuid) {
  265.                         continue;
  266.                     }
  267.                     mat.map0 = k;
  268.                     break;
  269.                 }
  270.  
  271.             }
  272.  
  273.             this.materials.push(mat);
  274.  
  275.         }
  276.  
  277.     },
  278.  
  279.     getFaceGroups: function(geometry) {
  280.  
  281.         // Check for uv0
  282.  
  283.         if (geometry.faceVertexUvs && geometry.faceVertexUvs[0]) {
  284.             this.groupflags.uv0 = true;
  285.         }
  286.  
  287.         // Check for uv1
  288.  
  289.         if (geometry.faceVertexUvs && geometry.faceVertexUvs[1]) {
  290.             this.groupflags.uv1 = true;
  291.         }
  292.  
  293.         // Check for vertex normal
  294.  
  295.         if (geometry.faces[0].vertexNormals.length) {
  296.             this.groupflags.normal = true;
  297.         }
  298.  
  299.         // Check for vertex color
  300.  
  301.         if (geometry.faces[0].vertexColors.length) {
  302.             this.groupflags.vcolor = true;
  303.         }
  304.  
  305.         // Loop over the faces
  306.  
  307.         let group;
  308.         let matId = -1;
  309.  
  310.         for (let i = 0; i < geometry.faces.length; i++) {
  311.  
  312.             let face = geometry.faces[i];
  313.             let matIndex = face.materialIndex;
  314.  
  315.             if (matId !== matIndex) {
  316.                 if (group) {
  317.                     this.groups.push(group);
  318.                 }
  319.  
  320.                 matId = matIndex;
  321.                 group = {
  322.                     matIndex: matIndex,
  323.                     tri: []
  324.                 };
  325.             }
  326.  
  327.             let a = {
  328.                 index: face.a
  329.             }
  330.  
  331.             let b = {
  332.                 index: face.b
  333.             }
  334.  
  335.             let c = {
  336.                 index: face.c
  337.             }
  338.  
  339.             if (this.groupflags.uv0) {
  340.  
  341.                 a.uv0 = {
  342.                     u: geometry.faceVertexUvs[0][i][0].x,
  343.                     v: geometry.faceVertexUvs[0][i][0].y
  344.                 }
  345.  
  346.                 b.uv0 = {
  347.                     u: geometry.faceVertexUvs[0][i][1].x,
  348.                     v: geometry.faceVertexUvs[0][i][1].y
  349.                 }
  350.  
  351.                 c.uv0 = {
  352.                     u: geometry.faceVertexUvs[0][i][2].x,
  353.                     v: geometry.faceVertexUvs[0][i][2].y
  354.                 }
  355.  
  356.             }
  357.  
  358.             if (this.groupflags.uv1) {
  359.  
  360.                 a.uv0 = {
  361.                     u: geometry.faceVertexUvs[1][i][0].x,
  362.                     v: geometry.faceVertexUvs[1][i][0].y
  363.                 }
  364.  
  365.                 b.uv0 = {
  366.                     u: geometry.faceVertexUvs[1][i][1].x,
  367.                     v: geometry.faceVertexUvs[1][i][1].y
  368.                 }
  369.  
  370.                 c.uv0 = {
  371.                     u: geometry.faceVertexUvs[1][i][2].x,
  372.                     v: geometry.faceVertexUvs[1][i][2].y
  373.                 }
  374.  
  375.             }
  376.  
  377.             if (this.groupflags.normal) {
  378.  
  379.                 a.normal = {
  380.                     x: face.vertexNormals[0].x,
  381.                     y: face.vertexNormals[0].y,
  382.                     z: face.vertexNormals[0].z
  383.                 }
  384.  
  385.                 b.normal = {
  386.                     x: face.vertexNormals[1].x,
  387.                     y: face.vertexNormals[1].y,
  388.                     z: face.vertexNormals[1].z
  389.                 }
  390.  
  391.                 c.normal = {
  392.                     x: face.vertexNormals[2].x,
  393.                     y: face.vertexNormals[2].y,
  394.                     z: face.vertexNormals[2].z
  395.                 }
  396.  
  397.             }
  398.  
  399.             if (this.groupflags.vcolor) {
  400.  
  401.                 a.vcolor = {
  402.                     r: face.vertexColors[0].r,
  403.                     g: face.vertexColors[0].g,
  404.                     b: face.vertexColors[0].b
  405.                 }
  406.  
  407.                 b.vcolor = {
  408.                     r: face.vertexColors[1].r,
  409.                     g: face.vertexColors[1].g,
  410.                     b: face.vertexColors[1].b
  411.                 }
  412.  
  413.                 b.vcolor = {
  414.                     r: face.vertexColors[2].r,
  415.                     g: face.vertexColors[2].g,
  416.                     b: face.vertexColors[2].b
  417.                 }
  418.  
  419.             }
  420.  
  421.             group.tri.push(a, b, c);
  422.  
  423.         }
  424.  
  425.         this.groups.push(group);
  426.  
  427.     },
  428.  
  429.     getAnims: function(geometry) {
  430.  
  431.         if (!geometry.animations || !geometry.animations.length) {
  432.             return;
  433.         }
  434.  
  435.         for (let i = 0; i < geometry.animations.length; i++) {
  436.  
  437.  
  438.         }
  439.  
  440.     },
  441.  
  442.     calcBufferLen: function(byteLen) {
  443.  
  444.         let bufferLen;
  445.         let remainder = byteLen % 16;
  446.  
  447.         if (remainder === 8) {
  448.             bufferLen = byteLen + 8;
  449.         } else if (remainder < 8) {
  450.             bufferLen = byteLen + (16 - remainder);
  451.         } else {
  452.             bufferLen = byteLen + (16 - remainder) + 16;
  453.         }
  454.  
  455.         return bufferLen;
  456.  
  457.     },
  458.  
  459.     writeBones: function() {
  460.  
  461.         if (!this.bones.length) {
  462.             return;
  463.         }
  464.  
  465.         // First read the bones list
  466.  
  467.         let structSize = (16 * 4 + 4);
  468.         let byteLen = this.bones.length * structSize;
  469.         let bufferLen = this.calcBufferLen(byteLen);
  470.  
  471.         let buffer = new ArrayBuffer(bufferLen);
  472.         let view = new DataView(buffer);
  473.  
  474.         view.setUint8(0, 0x42); // 'B'
  475.         view.setUint8(1, 0x4F); // '0'
  476.         view.setUint8(2, 0x4E); // 'N'
  477.         view.setUint8(3, 0x45); // 'E'
  478.         view.setUint32(4, byteLen, true);
  479.  
  480.         let ofs = 8;
  481.         for (let i = 0; i < this.bones.length; i++) {
  482.  
  483.             view.setInt16(ofs, this.bones[i].id, true);
  484.             view.setInt16(ofs + 2, this.bones[i].parent, true);
  485.             ofs += 4;
  486.  
  487.             this.bones[i].elements.forEach(e => {
  488.                 view.setFloat32(ofs, e, true);
  489.                 ofs += 4;
  490.             });
  491.  
  492.         }
  493.  
  494.         this.blocks.push({
  495.             type: "BONE",
  496.             num: this.bones.length,
  497.             buffer: buffer
  498.         });
  499.  
  500.     },
  501.  
  502.     writeVertices: function() {
  503.  
  504.         let structSize = 12;
  505.  
  506.         if (this.bones.length) {
  507.             structSize += 2 * 4 + 4 * 4;
  508.         }
  509.  
  510.         let byteLen = this.vertices.length * structSize;
  511.         let bufferLen = this.calcBufferLen(byteLen);
  512.  
  513.         let buffer = new ArrayBuffer(bufferLen);
  514.         let view = new DataView(buffer);
  515.  
  516.         view.setUint8(0, 0x56); // 'V'
  517.         view.setUint8(1, 0x45); // 'E'
  518.         view.setUint8(2, 0x52); // 'R'
  519.         view.setUint8(3, 0x54); // 'T'
  520.         view.setUint32(4, byteLen, true);
  521.  
  522.         let ofs = 8;
  523.         for (let i = 0; i < this.vertices.length; i++) {
  524.  
  525.             view.setFloat32(ofs + 0, this.vertices[i].pos.x, true);
  526.             view.setFloat32(ofs + 4, this.vertices[i].pos.y, true);
  527.             view.setFloat32(ofs + 8, this.vertices[i].pos.z, true);
  528.             ofs += 12;
  529.  
  530.             if (!this.bones.length) {
  531.                 continue;
  532.             }
  533.  
  534.             view.setUint16(ofs + 0, this.vertices[i].indice.x, true);
  535.             view.setUint16(ofs + 2, this.vertices[i].indice.y, true);
  536.             view.setUint16(ofs + 4, this.vertices[i].indice.z, true);
  537.             view.setUint16(ofs + 6, this.vertices[i].indice.w, true);
  538.             ofs += 8;
  539.  
  540.             view.setFloat32(ofs + 0, this.vertices[i].weight.x, true);
  541.             view.setFloat32(ofs + 4, this.vertices[i].weight.y, true);
  542.             view.setFloat32(ofs + 8, this.vertices[i].weight.z, true);
  543.             view.setFloat32(ofs + 12, this.vertices[i].weight.w, true);
  544.             ofs += 16;
  545.  
  546.         }
  547.  
  548.         this.blocks.push({
  549.             type: "VERT",
  550.             buffer: buffer,
  551.             num: this.vertices.length
  552.         });
  553.  
  554.     },
  555.  
  556.     writeTextures: function() {
  557.  
  558.         for (let i = 0; i < this.textures.length; i++) {
  559.  
  560.             let tex = this.textures[i];
  561.             let canvas = this.textures[i].img;
  562.             let data = canvas.toDataURL("image/png");
  563.  
  564.             let index = data.indexOf(',') + 1
  565.             data = data.substr(index);
  566.             data = window.atob(data);
  567.  
  568.             let byteLen = data.length;
  569.             let structSize = byteLen + 4;
  570.  
  571.             tex.type = "PNG";
  572.             tex.len = byteLen;
  573.             let props = 1;
  574.             let prop = {};
  575.  
  576.             for (let key in tex) {
  577.                 switch (key) {
  578.                     case "type":
  579.                     case "len":
  580.                     case "flipY":
  581.                     case "width":
  582.                     case "height":
  583.                     case "wrapS":
  584.                     case "wrapT":
  585.                         prop[key] = true;
  586.                         props++;
  587.                         structSize += 8;
  588.                         break;
  589.                 }
  590.             }
  591.  
  592.             let bufferLen = this.calcBufferLen(structSize);
  593.             let buffer = new ArrayBuffer(bufferLen);
  594.             let view = new DataView(buffer);
  595.  
  596.             view.setUint8(0, 0x54); // 'T'
  597.             view.setUint8(1, 0x45); // 'E'
  598.             view.setUint8(2, 0x58); // 'X'
  599.             view.setUint8(3, 0); // '\0'
  600.             view.setUint32(4, structSize, true);
  601.  
  602.             let ofs = 8;
  603.  
  604.             if (prop.type) {
  605.                 view.setUint8(ofs + 0, 'T'.charCodeAt(0));
  606.                 view.setUint8(ofs + 1, 'Y'.charCodeAt(0));
  607.                 view.setUint8(ofs + 2, 'P'.charCodeAt(0));
  608.                 view.setUint8(ofs + 3, 'E'.charCodeAt(0));
  609.                 ofs += 4;
  610.                 for (let k = 0; k < tex.type.length; k++) {
  611.                     view.setUint8(ofs + k, tex.type.charCodeAt(k));
  612.                 }
  613.                 ofs += 4;
  614.             }
  615.  
  616.             if (prop.len) {
  617.                 view.setUint8(ofs + 0, 'L'.charCodeAt(0));
  618.                 view.setUint8(ofs + 1, 'E'.charCodeAt(0));
  619.                 view.setUint8(ofs + 2, 'N'.charCodeAt(0));
  620.                 view.setUint8(ofs + 3, '\0'.charCodeAt(0));
  621.                 view.setUint32(ofs + 4, byteLen, true);
  622.                 ofs += 8;
  623.             }
  624.  
  625.  
  626.             if (prop.flipY) {
  627.                 let flipY = tex.flipY ? 1 : 0;
  628.                 view.setUint8(ofs + 0, 'F'.charCodeAt(0));
  629.                 view.setUint8(ofs + 1, 'L'.charCodeAt(0));
  630.                 view.setUint8(ofs + 2, 'P'.charCodeAt(0));
  631.                 view.setUint8(ofs + 3, 'Y'.charCodeAt(0));
  632.                 view.setUint32(ofs + 4, flipY, true);
  633.                 ofs += 8;
  634.             }
  635.  
  636.             if (prop.width) {
  637.                 view.setUint8(ofs + 0, 'W'.charCodeAt(0));
  638.                 view.setUint8(ofs + 1, 'I'.charCodeAt(0));
  639.                 view.setUint8(ofs + 2, 'D'.charCodeAt(0));
  640.                 view.setUint8(ofs + 3, '\0'.charCodeAt(0));
  641.                 view.setUint32(ofs + 4, tex.width, true);
  642.                 ofs += 8;
  643.             }
  644.  
  645.             if (prop.width) {
  646.                 view.setUint8(ofs + 0, 'H'.charCodeAt(0));
  647.                 view.setUint8(ofs + 1, 'G'.charCodeAt(0));
  648.                 view.setUint8(ofs + 2, 'T'.charCodeAt(0));
  649.                 view.setUint8(ofs + 3, '\0'.charCodeAt(0));
  650.                 view.setUint32(ofs + 4, tex.width, true);
  651.                 ofs += 8;
  652.             }
  653.  
  654.             if (prop.wrapS) {
  655.                 view.setUint8(ofs + 0, 'W'.charCodeAt(0));
  656.                 view.setUint8(ofs + 1, 'R'.charCodeAt(0));
  657.                 view.setUint8(ofs + 2, 'P'.charCodeAt(0));
  658.                 view.setUint8(ofs + 3, 'S'.charCodeAt(0));
  659.                 view.setUint32(ofs + 4, tex.wrapS, true);
  660.                 ofs += 8;
  661.             }
  662.  
  663.             if (prop.wrapT) {
  664.                 view.setUint8(ofs + 0, 'W'.charCodeAt(0));
  665.                 view.setUint8(ofs + 1, 'R'.charCodeAt(0));
  666.                 view.setUint8(ofs + 2, 'P'.charCodeAt(0));
  667.                 view.setUint8(ofs + 3, 'T'.charCodeAt(0));
  668.                 view.setUint32(ofs + 4, tex.wrapT, true);
  669.                 ofs += 8;
  670.             }
  671.  
  672.             view.setUint8(ofs + 0, 'I'.charCodeAt(0));
  673.             view.setUint8(ofs + 1, 'M'.charCodeAt(0));
  674.             view.setUint8(ofs + 2, 'G'.charCodeAt(0));
  675.             view.setUint8(ofs + 3, '\0'.charCodeAt(0));
  676.             ofs += 4;
  677.  
  678.             for (let i = 0; i < byteLen; i++) {
  679.                 view.setUint8(ofs, data.charCodeAt(i));
  680.                 ofs++;
  681.             }
  682.  
  683.             this.blocks.push({
  684.                 type: "TEX",
  685.                 id: i,
  686.                 num: props,
  687.                 buffer: buffer
  688.             });
  689.  
  690.         }
  691.  
  692.     },
  693.  
  694.     writeMaterials: function() {
  695.  
  696.         for (let i = 0; i < this.materials.length; i++) {
  697.  
  698.             let byteLen = 0;
  699.             let properties = Object.keys(this.materials[i]);
  700.  
  701.             for (let key in this.materials[i]) {
  702.  
  703.                 switch (key) {
  704.                     case "diffuse":
  705.                         byteLen += 16;
  706.                         break;
  707.                     case "map0":
  708.                         byteLen += 8;
  709.                         break;
  710.                 }
  711.  
  712.             }
  713.  
  714.             let bufferLen = this.calcBufferLen(byteLen);
  715.             let buffer = new ArrayBuffer(bufferLen);
  716.             let view = new DataView(buffer);
  717.  
  718.             view.setUint8(0, "M".charCodeAt(0));
  719.             view.setUint8(1, "A".charCodeAt(0));
  720.             view.setUint8(2, "T".charCodeAt(0));
  721.             view.setUint8(3, 0);
  722.             view.setUint32(4, byteLen, true);
  723.  
  724.             let ofs = 8;
  725.             for (let key in this.materials[i]) {
  726.  
  727.                 let val = this.materials[i][key];
  728.                 console.log(val);
  729.  
  730.                 switch (key) {
  731.                     case "diffuse":
  732.  
  733.                         view.setUint8(ofs + 0, "D".charCodeAt(0));
  734.                         view.setUint8(ofs + 1, "I".charCodeAt(0));
  735.                         view.setUint8(ofs + 2, "F".charCodeAt(0));
  736.                         view.setUint8(ofs + 3, "F".charCodeAt(0));
  737.                         view.setFloat32(ofs + 4, val.r);
  738.                         view.setFloat32(ofs + 8, val.r);
  739.                         view.setFloat32(ofs + 12, val.r);
  740.                         ofs += 16;
  741.  
  742.                         break;
  743.                     case "map0":
  744.  
  745.                         view.setUint8(ofs + 0, "M".charCodeAt(0));
  746.                         view.setUint8(ofs + 1, "A".charCodeAt(0));
  747.                         view.setUint8(ofs + 2, "P".charCodeAt(0));
  748.                         view.setUint8(ofs + 3, "0".charCodeAt(0));
  749.                         view.setUint32(ofs + 4, val);
  750.                         ofs += 8;
  751.                         break;
  752.                 }
  753.             }
  754.  
  755.             this.blocks.push({
  756.                 type: "MAT",
  757.                 id: i,
  758.                 num: properties.length,
  759.                 buffer: buffer
  760.             });
  761.  
  762.             console.log("okay");
  763.         }
  764.  
  765.     },
  766.  
  767.     writeFaceGroups: function() {
  768.  
  769.         let structSize = 2;
  770.         let flags = 0;
  771.  
  772.         if (this.groupflags.uv0) {
  773.             structSize += 8;
  774.         }
  775.  
  776.         if (this.groupflags.uv1) {
  777.             structSize += 8;
  778.         }
  779.  
  780.         if (this.groupflags.normal) {
  781.             structSize += 12;
  782.         }
  783.  
  784.         if (this.groupflags.vcolor) {
  785.             structSize += 12;
  786.         }
  787.  
  788.         for (let i = 0; i < this.groups.length; i++) {
  789.  
  790.             let group = this.groups[i];
  791.  
  792.             let byteLen = group.tri.length * structSize;
  793.             let bufferLen = this.calcBufferLen(byteLen);
  794.  
  795.             let buffer = new ArrayBuffer(bufferLen);
  796.             let view = new DataView(buffer);
  797.  
  798.             view.setUint8(0, 0x46); // 'F'
  799.             view.setUint8(1, 0x41); // 'A'
  800.             view.setUint8(2, 0x43); // 'C'
  801.             view.setUint8(3, 0x45); // 'E'
  802.             view.setUint32(4, byteLen, true);
  803.  
  804.             let ofs = 8;
  805.  
  806.             group.tri.forEach(tri => {
  807.  
  808.                 view.setUint16(ofs, tri.index, true);
  809.                 ofs += 2;
  810.  
  811.                 if (this.groupflags.uv0) {
  812.                     view.setFloat32(ofs + 0, tri.uv0.u, true);
  813.                     view.setFloat32(ofs + 4, tri.uv0.v, true);
  814.                     ofs += 8;
  815.                 }
  816.  
  817.                 if (this.groupflags.uv1) {
  818.                     view.setFloat32(ofs + 0, tri.uv1.u, true);
  819.                     view.setFloat32(ofs + 4, tri.uv1.v, true);
  820.                     ofs += 8;
  821.                 }
  822.  
  823.                 if (this.groupflags.normal) {
  824.                     view.setFloat32(ofs + 0, tri.normal.x, true);
  825.                     view.setFloat32(ofs + 4, tri.normal.y, true);
  826.                     view.setFloat32(ofs + 8, tri.normal.z, true);
  827.                     ofs += 12;
  828.                 }
  829.  
  830.                 if (this.groupflags.vcolor) {
  831.                     tri.vcolor = tri.vcolor || {
  832.                         r: 1,
  833.                         g: 1,
  834.                         b: 1
  835.                     };
  836.                     view.setFloat32(ofs + 0, tri.vcolor.r, true);
  837.                     view.setFloat32(ofs + 4, tri.vcolor.g, true);
  838.                     view.setFloat32(ofs + 8, tri.vcolor.b, true);
  839.                     ofs += 12;
  840.                 }
  841.  
  842.             });
  843.  
  844.             this.blocks.push({
  845.                 type: "FACE",
  846.                 mat_id: group.matIndex,
  847.                 num: group.tri.length,
  848.                 buffer: buffer
  849.             });
  850.  
  851.         }
  852.  
  853.     },
  854.  
  855.     writeHeader: function() {
  856.  
  857.         let flags = 0;
  858.  
  859.         if (this.groupflags.uv0) {
  860.             flags |= 1;
  861.         }
  862.  
  863.         if (this.groupflags.uv1) {
  864.             flags |= 2;
  865.         }
  866.  
  867.         if (this.groupflags.normal) {
  868.             flags |= 4;
  869.         }
  870.  
  871.         if (this.groupflags.vcolor) {
  872.             flags |= 8;
  873.         }
  874.  
  875.         let byteLen = (this.blocks.length + 3) * 16;
  876.         let gen_text = "Generated by THREE.DashExporter";
  877.  
  878.         let bufferLen = this.calcBufferLen(byteLen + gen_text.length);
  879.         let buffer = new ArrayBuffer(bufferLen);
  880.         let view = new DataView(buffer);
  881.  
  882.         view.setUint8(0, 'D'.charCodeAt(0));
  883.         view.setUint8(1, 'M'.charCodeAt(0));
  884.         view.setUint8(2, 'F'.charCodeAt(0));
  885.         view.setUint8(3, '\0'.charCodeAt(0));
  886.         view.setUint32(4, 1, true);
  887.         view.setUint32(8, flags, true);
  888.         view.setUint32(12, this.blocks.length, true);
  889.  
  890.         let ofs = 32;
  891.         let startOfs = bufferLen;
  892.         console.log("Header length: 0x%s", bufferLen.toString(16));
  893.  
  894.         for (let i = 0; i < this.blocks.length; i++) {
  895.  
  896.             let block = this.blocks[i];
  897.             switch (block.type) {
  898.                 case "BONE":
  899.                     view.setUint8(ofs + 0, 'B'.charCodeAt(0));
  900.                     view.setUint8(ofs + 1, 'O'.charCodeAt(0));
  901.                     view.setUint8(ofs + 2, 'N'.charCodeAt(0));
  902.                     view.setUint8(ofs + 3, 'E'.charCodeAt(0));
  903.                     view.setUint32(ofs + 4, startOfs, true);
  904.                     view.setUint32(ofs + 12, block.num, true);
  905.                     break;
  906.                 case "VERT":
  907.                     view.setUint8(ofs + 0, 'V'.charCodeAt(0));
  908.                     view.setUint8(ofs + 1, 'E'.charCodeAt(0));
  909.                     view.setUint8(ofs + 2, 'R'.charCodeAt(0));
  910.                     view.setUint8(ofs + 3, 'T'.charCodeAt(0));
  911.                     view.setUint32(ofs + 4, startOfs, true);
  912.                     view.setUint32(ofs + 12, block.num, true);
  913.                     break;
  914.                 case "TEX":
  915.                     console.log(block);
  916.                     view.setUint8(ofs + 0, 'T'.charCodeAt(0));
  917.                     view.setUint8(ofs + 1, 'E'.charCodeAt(0));
  918.                     view.setUint8(ofs + 2, 'X'.charCodeAt(0));
  919.                     view.setUint32(ofs + 4, startOfs, true);
  920.                     view.setUint32(ofs + 8, block.id, true);
  921.                     view.setUint32(ofs + 12, block.num, true);
  922.                     break;
  923.                 case "MAT":
  924.                     view.setUint8(ofs + 0, 'M'.charCodeAt(0));
  925.                     view.setUint8(ofs + 1, 'A'.charCodeAt(0));
  926.                     view.setUint8(ofs + 2, 'T'.charCodeAt(0));
  927.                     view.setUint32(ofs + 4, startOfs, true);
  928.                     view.setUint32(ofs + 8, block.id, true);
  929.                     view.setUint32(ofs + 12, block.num, true);
  930.                     break;
  931.                 case "FACE":
  932.                     view.setUint8(ofs + 0, 'F'.charCodeAt(0));
  933.                     view.setUint8(ofs + 1, 'A'.charCodeAt(0));
  934.                     view.setUint8(ofs + 2, 'C'.charCodeAt(0));
  935.                     view.setUint8(ofs + 3, 'E'.charCodeAt(0));
  936.                     view.setUint32(ofs + 4, startOfs, true);
  937.                     view.setUint16(ofs + 8, block.mat_id, true);
  938.                     view.setUint16(ofs + 10, block.flags, true);
  939.                     view.setUint32(ofs + 12, block.num, true);
  940.                     break;
  941.                 case "ANIM":
  942.  
  943.                     break;
  944.             }
  945.  
  946.             ofs += 16;
  947.             startOfs += block.buffer.byteLength;
  948.         }
  949.  
  950.         ofs += 16;
  951.  
  952.         for (let i = 0; i < gen_text.length; i++) {
  953.             view.setUint8(ofs + i, gen_text.charCodeAt(i));
  954.         }
  955.  
  956.         this.blocks.unshift({
  957.             type: "HEAD",
  958.             buffer: buffer
  959.         });
  960.  
  961.     },
  962.  
  963.     writeBlob: function() {
  964.  
  965.         let array = new Array(this.blocks.length);
  966.  
  967.         for (let i = 0; i < this.blocks.length; i++) {
  968.             array[i] = this.blocks[i].buffer;
  969.         }
  970.  
  971.         return new Blob(array);
  972.  
  973.     }
  974.  
  975. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement