SaiDAV-StlSaver/herosaver.min.js

18 lines
8.0 KiB
JavaScript

;(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 h=("base64"==h[2]?atob:decodeURIComponent)(h.pop()),d=h.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;"true"===String(this)&&(a=[a,b],b=a[0],a=a[1]);if(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 c=new Uint8Array(a.length),t=c.length;for(e;e<t;++e)c[e]=a.charCodeAt(e);a=new f([c],{type:b})}a=a instanceof f?a:new f([a],{type:b});if(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,b.onload=function(a){m(this.result)},b.readAsDataURL(a)}return!0}});if(typeof THREE=='undefined'&&typeof require!='undefined')
var THREE=require('three')
RK.STLExporter=function(){};RK.STLExporter.prototype={constructor:THREE.STLExporter,parse:(function(){var vector=new THREE.Vector3();var normalMatrixWorld=new THREE.Matrix3();return function(scene){var output='';output+='solid exported\n';scene.traverse(function(object){if(object instanceof RK.Mesh){if(object.visible==false)return;var geometry=object.geometry;var matrixWorld=object.matrixWorld;var skeleton=object.skeleton;var mesh=object;if(geometry instanceof RK.BufferGeometry){var oldgeometry=geometry.clone();geometry=new RK.Geometry().fromBufferGeometry(geometry);var skinIndex=oldgeometry.getAttribute('skinIndex');var skinWeight=oldgeometry.getAttribute('skinWeight');var morphTarget=oldgeometry.getAttribute('morphTarget0');var mtcount=0;while(typeof morphTarget!=='undefined'){mtcount++;morphTarget=oldgeometry.getAttribute('morphTarget'+mtcount);}
if(typeof skinIndex!=='undefined'){geometry.skinIndices=[];geometry.skinWeights=[];geometry.morphTargets=[];for(var j=0;j<mtcount;j++){geometry.morphTargets[j]={};geometry.morphTargets[j].vertices=[];}
for(var i=0;i<geometry.vertices.length;i++){geometry.skinIndices.push((new THREE.Vector4()).fromBufferAttribute(skinIndex,i));geometry.skinWeights.push((new THREE.Vector4()).fromBufferAttribute(skinWeight,i));for(var j=0;j<mtcount;j++){geometry.morphTargets[j].vertices.push((new THREE.Vector3()).fromBufferAttribute(oldgeometry.getAttribute('morphTarget'+j)));}}}}
if(geometry instanceof RK.Geometry){var vertices=geometry.vertices;var faces=geometry.faces;normalMatrixWorld.getNormalMatrix(matrixWorld);if(typeof faces!='undefined'){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';var indices=[face.a,face.b,face.c];for(var j=0;j<3;j++){var vertexIndex=indices[j];if(typeof geometry.skinIndices!=='undefined'&&geometry.skinIndices.length==0){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];var weights=[geometry.skinWeights[vertexIndex].x,geometry.skinWeights[vertexIndex].y,geometry.skinWeights[vertexIndex].z,geometry.skinWeights[vertexIndex].w];var inverses=[skeleton.boneInverses[boneIndices[0]],skeleton.boneInverses[boneIndices[1]],skeleton.boneInverses[boneIndices[2]],skeleton.boneInverses[boneIndices[3]]];var skinMatrices=[skeleton.bones[boneIndices[0]].matrixWorld,skeleton.bones[boneIndices[1]].matrixWorld,skeleton.bones[boneIndices[2]].matrixWorld,skeleton.bones[boneIndices[3]].matrixWorld];if(geometry.morphTargets!=='undefined'){var morphMatricesX=[];var morphMatricesY=[];var morphMatricesZ=[];var morphMatricesInfluence=[];for(var 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(mesh.geometry.morphTargets!=='undefined'){var morphVector=new THREE.Vector4(vector.x,vector.y,vector.z);for(var 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';return output;};}())};if(typeof module!=="undefined"&&module.exports){module.exports=RK.STLExporter}
else if((typeof define!=="undefined"&&define!==null)&&(define.amd!==null)){define([],function(){return saveAs;});}
$("#add").css({"display":"none"});var stl=$("<a/>").addClass("shop-button").css({"margin":"5px","pointer-events":"auto"}).text("Download STL");var sjson=$("<a/>").addClass("shop-button").css({"margin":"5px","pointer-events":"auto"}).text("Save JSON");var ljson=$("<input/>").attr({"type":"file","id":"ljson"}).css({"display":"none"}).text("Load JSON");var labelljson=$("<label/>").attr({"for":"ljson"}).addClass("shop-button").css({"margin":"5px","pointer-events":"auto"}).text("Load JSON");stl.click(function(e){e.preventDefault();var exporter=new RK.STLExporter();var objs=CK.activeCharacter.threeObj.children;var max_obj=0;var i;for(i in objs){if(objs[i].children.length>objs[max_obj].children.length){max_obj=i;}}
var stlString=exporter.parse(CK.activeCharacter.threeObj.children[max_obj])
var name=CK.activeCharacter.name
name=name===""?"unnamed":name
download(stlString,name+'.stl','text/plain');});sjson.click(function(e){e.preventDefault();var char_json=JSON.stringify(CK.activeCharacter.characterData);var name=CK.activeCharacter.name
name=name===""?"unnamed":name
download(char_json,name+".json","text/plain");});ljson.on('change',function(e){e.preventDefault();var file=e.target.files[0];var reader=new FileReader();reader.onload=(function(theFile){return function(e){e.preventDefault();CK.change(JSON.parse(e.target.result));};})(file);reader.readAsText(file);});$("#print-my-mini").append(stl);$("#print-my-mini").append(sjson);$("#print-my-mini").append(ljson);$("#print-my-mini").append(labelljson);