SaiDAV-StlSaver/herosaver.min.js

1 line
7.5 KiB
JavaScript

if(function(r,l){"function"==typeof define&&define.amd?define([],l):"object"==typeof exports?module.exports=l():r.download=l()}(this,function(){return function l(a,e,k){function q(a){var h=a.split(/[:;,]/);a=h[1];var d=(h=("base64"==h[2]?atob:decodeURIComponent)(h.pop())).length,b=0,c=new Uint8Array(d);for(b;b<d;++b)c[b]=h.charCodeAt(b);return new f([c],{type:a})}function m(a,b){if("download"in d)return d.href=a,d.setAttribute("download",n),d.className="download-js-link",d.innerHTML="downloading...",d.style.display="none",document.body.appendChild(d),setTimeout(function(){d.click(),document.body.removeChild(d),!0===b&&setTimeout(function(){g.URL.revokeObjectURL(d.href)},250)},66),!0;if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent))return/^data:/.test(a)&&(a="data:"+a.replace(/^data:([\w\/\-\+]+)/,"application/octet-stream")),!window.open(a)&&confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")&&(location.href=a),!0;var c=document.createElement("iframe");document.body.appendChild(c),!b&&/^data:/.test(a)&&(a="data:"+a.replace(/^data:([\w\/\-\+]+)/,"application/octet-stream")),c.src=a,setTimeout(function(){document.body.removeChild(c)},333)}var g=window,b=k||"application/octet-stream",c=!e&&!k&&a,d=document.createElement("a");k=function(a){return String(a)};var f=g.Blob||g.MozBlob||g.WebKitBlob||k,n=e||"download",f=f.call?f.bind(g):Blob;if("true"===String(this)&&(a=[a,b],b=a[0],a=a[1]),c&&2048>c.length&&(n=c.split("/").pop().split("?")[0],d.href=c,-1!==d.href.indexOf(c))){var p=new XMLHttpRequest;return p.open("GET",c,!0),p.responseType="blob",p.onload=function(a){l(a.target.response,n,"application/octet-stream")},setTimeout(function(){p.send()},0),p}if(/^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(a)){if(!(2096103.424<a.length&&f!==k))return navigator.msSaveBlob?navigator.msSaveBlob(q(a),n):m(a);a=q(a),b=a.type||"application/octet-stream"}else if(/([\x80-\xff])/.test(a)){e=0;var t=(c=new Uint8Array(a.length)).length;for(e;e<t;++e)c[e]=a.charCodeAt(e);a=new f([c],{type:b})}if(a=a instanceof f?a:new f([a],{type:b}),navigator.msSaveBlob)return navigator.msSaveBlob(a,n);if(g.URL)m(g.URL.createObjectURL(a),!0);else{if("string"==typeof a||a.constructor===k)try{return m("data:"+b+";base64,"+g.btoa(a))}catch(h){return m("data:"+b+","+encodeURIComponent(a))}(b=new FileReader).onload=function(a){m(this.result)},b.readAsDataURL(a)}return!0}}),void 0===THREE&&"undefined"!=typeof require)var THREE=require("three");THREE.STLExporter=function(){},THREE.STLExporter.prototype={constructor:THREE.STLExporter,parse:function(){var vector=new THREE.Vector3,normalMatrixWorld=new THREE.Matrix3;return function(scene){var output="";return output+="solid exported\n",scene.traverse(function(object){if(object instanceof THREE.Mesh){if(0==object.visible)return;var geometry=object.geometry,matrixWorld=object.matrixWorld,skeleton=object.skeleton,mesh=object;if(geometry instanceof THREE.BufferGeometry){var oldgeometry=geometry.clone();geometry=(new THREE.Geometry).fromBufferGeometry(geometry);for(var skinIndex=oldgeometry.getAttribute("skinIndex"),skinWeight=oldgeometry.getAttribute("skinWeight"),morphTarget=oldgeometry.getAttribute("morphTarget0"),mtcount=0;void 0!==morphTarget;)mtcount++,morphTarget=oldgeometry.getAttribute("morphTarget"+mtcount);if(void 0!==skinIndex){geometry.skinIndices=[],geometry.skinWeights=[],geometry.morphTargets=[];for(j=0;j<mtcount;j++)geometry.morphTargets[j]={},geometry.morphTargets[j].vertices=[];for(i=0;i<geometry.vertices.length;i++){geometry.skinIndices.push((new THREE.Vector4).fromAttribute(skinIndex,i)),geometry.skinWeights.push((new THREE.Vector4).fromAttribute(skinWeight,i));for(j=0;j<mtcount;j++)geometry.morphTargets[j].vertices.push((new THREE.Vector3).fromAttribute(oldgeometry.getAttribute("morphTarget"+j)))}}}if(geometry instanceof THREE.Geometry){var vertices=geometry.vertices,faces=geometry.faces;if(normalMatrixWorld.getNormalMatrix(matrixWorld),void 0!==faces)for(var i=0,l=faces.length;i<l;i++){var face=faces[i];vector.copy(face.normal).applyMatrix3(normalMatrixWorld).normalize(),output+="\tfacet normal "+vector.x+" "+vector.y+" "+vector.z+"\n",output+="\t\touter loop\n";for(var indices=[face.a,face.b,face.c],j=0;j<3;j++){var vertexIndex=indices[j];if(void 0!==geometry.skinIndices&&0==geometry.skinIndices.length)vector.copy(vertices[vertexIndex]).applyMatrix4(matrixWorld),output+="\t\t\tvertex "+vector.x+" "+vector.y+" "+vector.z+"\n";else{vector.copy(vertices[vertexIndex]);var boneIndices=[geometry.skinIndices[vertexIndex].x,geometry.skinIndices[vertexIndex].y,geometry.skinIndices[vertexIndex].z,geometry.skinIndices[vertexIndex].w],weights=[geometry.skinWeights[vertexIndex].x,geometry.skinWeights[vertexIndex].y,geometry.skinWeights[vertexIndex].z,geometry.skinWeights[vertexIndex].w],inverses=[skeleton.boneInverses[boneIndices[0]],skeleton.boneInverses[boneIndices[1]],skeleton.boneInverses[boneIndices[2]],skeleton.boneInverses[boneIndices[3]]],skinMatrices=[skeleton.bones[boneIndices[0]].matrixWorld,skeleton.bones[boneIndices[1]].matrixWorld,skeleton.bones[boneIndices[2]].matrixWorld,skeleton.bones[boneIndices[3]].matrixWorld];if("undefined"!==geometry.morphTargets)for(var morphMatricesX=[],morphMatricesY=[],morphMatricesZ=[],morphMatricesInfluence=[],mt=0;mt<geometry.morphTargets.length;mt++)morphMatricesX[mt]=geometry.morphTargets[mt].vertices[vertexIndex].x,morphMatricesY[mt]=geometry.morphTargets[mt].vertices[vertexIndex].y,morphMatricesZ[mt]=geometry.morphTargets[mt].vertices[vertexIndex].z,morphMatricesInfluence[mt]=mesh.morphTargetInfluences[mt];var finalVector=new THREE.Vector4;if("undefined"!==mesh.geometry.morphTargets)for(var morphVector=new THREE.Vector4(vector.x,vector.y,vector.z),mt=0;mt<geometry.morphTargets.length;mt++)morphVector.lerp(new THREE.Vector4(morphMatricesX[mt],morphMatricesY[mt],morphMatricesZ[mt],1),morphMatricesInfluence[mt]);for(var k=0;k<4;k++){var tempVector=new THREE.Vector4(vector.x,vector.y,vector.z);tempVector.multiplyScalar(weights[k]),tempVector.applyMatrix4(inverses[k]).applyMatrix4(skinMatrices[k]),finalVector.add(tempVector)}output+="\t\t\tvertex "+finalVector.x+" "+finalVector.y+" "+finalVector.z+"\n"}}output+="\t\tendloop\n",output+="\tendfacet\n"}}}}),output+="endsolid exported\n"}}()},"undefined"!=typeof module&&module.exports?module.exports=THREE.STLExporter:"undefined"!=typeof define&&null!==define&&null!==define.amd&&define([],function(){return saveAs}),$("#add").css({display:"none"});var stl=$("<a/>").addClass("shop-button").css({margin:"5px","pointer-events":"auto"}).text("Download STL"),sjson=$("<a/>").addClass("shop-button").css({margin:"5px","pointer-events":"auto"}).text("Save JSON"),ljson=$("<input/>").attr({type:"file",id:"ljson"}).css({display:"none"}).text("Load JSON"),labelljson=$("<label/>").attr({for:"ljson"}).addClass("shop-button").css({margin:"5px","pointer-events":"auto"}).text("Load JSON");stl.click(function(e){e.preventDefault();var stlString=(new THREE.STLExporter).parse(CK.activeCharacter.threeObj.children[1]);download(stlString,"char.stl","text/plain")}),sjson.click(function(e){e.preventDefault();var char_json=JSON.stringify(CK.activeCharacter.characterData);download(char_json,"char.json","text/plain")}),ljson.on("change",function(e){e.preventDefault();var file=e.target.files[0],reader=new FileReader;reader.onload=function(e){e.preventDefault(),CK.change(JSON.parse(e.target.result))},reader.readAsText(file)}),$("#print-my-mini").append(stl),$("#print-my-mini").append(sjson),$("#print-my-mini").append(ljson),$("#print-my-mini").append(labelljson);