hero-saver-threejs/herosaver.min.js

2 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-06-29 14:43:39 +02:00
function init(){!function(){var e,t;RK.STLExporter=function(){},RK.STLExporter.prototype={constructor:THREE.STLExporter,parse:(e=new THREE.Vector3,t=new THREE.Matrix3,function(r){console.log(r);var n="";for(var o in n+="solid exported\n",r)r[o].traverse(function(r){if(r instanceof RK.Mesh){if(0==r.visible)return;var o=r.geometry,a=r.matrixWorld,i=r.skeleton,s=r;if(o instanceof RK.BufferGeometry){var c=o.clone();o=(new RK.Geometry).fromBufferGeometry(o);for(var l=c.getAttribute("skinIndex"),d=c.getAttribute("skinWeight"),p=c.getAttribute("morphTarget0"),f=0;void 0!==p;)f++,p=c.getAttribute("morphTarget"+f);if(void 0!==l){o.skinIndices=[],o.skinWeights=[],o.morphTargets=[];for(var u=0;u<f;u++)o.morphTargets[u]={},o.morphTargets[u].vertices=[];for(var m=0;m<o.vertices.length;m++)for(o.skinIndices.push((new THREE.Vector4).fromBufferAttribute(l,m)),o.skinWeights.push((new THREE.Vector4).fromBufferAttribute(d,m)),u=0;u<f;u++)o.morphTargets[u].vertices.push((new THREE.Vector3).fromBufferAttribute(c.getAttribute("morphTarget"+u)))}}if(o instanceof RK.Geometry){var g=o.vertices,h=o.faces;if(t.getNormalMatrix(a),void 0!==h){m=0;for(var v=h.length;m<v;m++){var y=h[m];e.copy(y.normal).applyMatrix3(t).normalize(),n+="\tfacet normal "+e.x+" "+e.y+" "+e.z+"\n",n+="\t\touter loop\n";var x=[y.a,y.b,y.c];for(u=0;u<3;u++){var b=x[u];if(void 0!==o.skinIndices&&0==o.skinIndices.length)e.copy(g[b]).applyMatrix4(a),n+="\t\t\tvertex "+e.x+" "+e.y+" "+e.z+"\n";else{e.copy(g[b]);var w=[o.skinIndices[b].x,o.skinIndices[b].y,o.skinIndices[b].z,o.skinIndices[b].w],T=[o.skinWeights[b].x,o.skinWeights[b].y,o.skinWeights[b].z,o.skinWeights[b].w],E=[i.boneInverses[w[0]],i.boneInverses[w[1]],i.boneInverses[w[2]],i.boneInverses[w[3]]],R=[i.bones[w[0]].matrixWorld,i.bones[w[1]].matrixWorld,i.bones[w[2]].matrixWorld,i.bones[w[3]].matrixWorld];if("undefined"!==o.morphTargets)for(var j=[],k=[],S=[],A=[],I=0;I<o.morphTargets.length;I++)j[I]=o.morphTargets[I].vertices[b].x,k[I]=o.morphTargets[I].vertices[b].y,S[I]=o.morphTargets[I].vertices[b].z,A[I]=s.morphTargetInfluences[I];var _=new THREE.Vector4;if("undefined"!==s.geometry.morphTargets){var C=new THREE.Vector4(e.x,e.y,e.z);for(I=0;I<o.morphTargets.length;I++)C.lerp(new THREE.Vector4(j[I],k[I],S[I],1),A[I])}for(var B=0;B<4;B++){var L=new THREE.Vector4(e.x,e.y,e.z);L.multiplyScalar(T[B]),L.applyMatrix4(E[B]).applyMatrix4(R[B]),_.add(L)}n+="\t\t\tvertex "+_.x+" "+_.y+" "+_.z+"\n"}}n+="\t\tendloop\n",n+="\tendfacet\n"}}}}});return n+="endsolid exported\n"})},"undefined"!=typeof module&&module.exports?module.exports=RK.STLExporter:"undefined"!=typeof define&&null!==define&&null!==define.amd&&define([],function(){return saveAs});var r,n,o,a,i,s=CK.character,c={"margin-left":"20px","font-size":"1.4em",color:"rgba(255, 255, 255, 0.8)",cursor:"pointer"};jQuery("<a />").css(c).text("Export Figure"),n=jQuery("<a />").css(c).text("Export Model (STL)"),o=jQuery("<a />").css(c).text("Export (JSON)"),a=jQuery("<input/>").attr({type:"file",id:"ljson"}).css({display:"none"}).text("Import (JSON)"),i=jQuery("<label/>").attr({for:"ljson"}).css(c).text("Import (JSON)"),enlarge=jQuery("<a />").css(c).text("Enlarge"),reset_scale=jQuery("<a />").css(c).text("Reset Scale"),(r=jQuery(".characterArea")).append(n),r.append(o),r.append(a),r.append(i),r.append(enlarge),r.append(reset_scale),r.css("right",0),n.click(function(e){e.preventDefault(),download_stl(s)}),enlarge.click(function(e){e.preventDefault(),set_object_props(s,[10,10,10],[Math.PI/2,0,0])}),reset_scale.click(function(e){e.preventDefault(),set_object_props(s,[1,1,1],[0,0,0])}),o.click(function(e){e.preventDefault();var t=JSON.stringify(CK.data),r=get_name();download(t,r+".json","text/plain")}),a.on("change",function(e){e.preventDefault();var t=e.target.files[0],r=new FileReader;r.onload=function(e){e.preventDefault(),CK.change(JSON.parse(e.target.result))},r.readAsText(t)})}()}function inject_script(e,t){var r=document.getElementsByTagName("head")[0],n=document.createElement("script");n.src=e,n.onload=function(e){t()},r.appendChild(n)}function download_stl(e){var t=(new