forked from dserhienko/StlSaver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathherosaver_gltf11.js
435 lines (358 loc) · 18.5 KB
/
herosaver_gltf11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//cycle.js, by douglascrockford; 2018-05-15 [Public Domain] see https://github.com/douglascrockford/JSON-js for usage
if (typeof JSON.decycle !== "function") {JSON.decycle = function decycle(object, replacer) {"use strict"; var objects = new WeakMap(); return (function derez(value, path) {var old_path; var nu; if (replacer !== undefined) {value = replacer(value)} if (typeof value === "object" && value !== null && !(value instanceof Boolean) && !(value instanceof Date) && !(value instanceof Number) && !(value instanceof RegExp) && !(value instanceof String)) {old_path = objects.get(value); if (old_path !== undefined) {return {$ref: old_path}} objects.set(value, path); if (Array.isArray(value)) {nu = []; value.forEach(function (element, i) {nu[i] = derez(element, path + "[" + i + "]")})} else {nu = {}; Object.keys(value).forEach(function (name) {nu[name] = derez(value[name], path + "[" + JSON.stringify(name) + "]")})} return nu} return value}(object, "$"))}} if (typeof JSON.retrocycle !== "function") {JSON.retrocycle = function retrocycle($) {"use strict"; var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\(?:[\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/; (function rez(value) {if (value && typeof value === "object") {if (Array.isArray(value)) {value.forEach(function (element, i) {if (typeof element === "object" && element !== null) {var path = element.$ref; if (typeof path === "string" && px.test(path)) {value[i] = eval(path)} else {rez(element)} } })} else {Object.keys(value).forEach(function (name) {var item = value[name]; if (typeof item === "object" && item !== null) {var path = item.$ref; if (typeof path === "string" && px.test(path)) {value[name] = eval(path)} else {rez(item)} } })} } }($)); return $}}
import {OBJExporter} from 'https://cdn.jsdelivr.net/gh/carlalldis/StlSaver@latest/OBJexporter5.js';
import {ColladaExporter} from 'https://cdn.jsdelivr.net/gh/burndaflame/three.js@dev/examples/jsm/exporters/ColladaExporter.js';
import {GLTFExporter} from 'https://cdn.jsdelivr.net/gh/burndaflame/three.js@dev/examples/jsm/exporters/GLTFExporter.js';
import * as THREE from 'https://cdn.jsdelivr.net/gh/burndaflame/three.js@dev/build/three.module.js';
console.log("HeroSaver - Starting Loading");
function init() {
//download.js v4.2, by dandavis; 2008-2017. [MIT] see http://danml.com/download.html for tests/usage
function download(data, strFileName, strMimeType) {
var self = window, // this script is only for browsers anyway...
defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads
mimeType = strMimeType || defaultMime,
payload = data,
url = !strFileName && !strMimeType && payload,
anchor = document.createElement("a"),
toString = function(a){return String(a);},
myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString),
fileName = strFileName || "download",
blob,
reader;
myBlob= myBlob.call ? myBlob.bind(self) : Blob ;
if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
payload=[payload, mimeType];
mimeType=payload[0];
payload=payload[1];
}
if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument
fileName = url.split("/").pop().split("?")[0];
anchor.href = url; // assign href prop to temp anchor
if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path:
var ajax=new XMLHttpRequest();
ajax.open( "GET", url, true);
ajax.responseType = 'blob';
ajax.onload= function(e){
download(e.target.response, fileName, defaultMime);
};
setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return:
return ajax;
} // end if valid url?
} // end if url?
//go ahead and download dataURLs right away
if(/^data\:[\w+\-]+\/[\w+\-]+[,;]/.test(payload)){
if(payload.length > (1024*1024*1.999) && myBlob !== toString ){
payload=dataUrlToBlob(payload);
mimeType=payload.type || defaultMime;
}else{
return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
navigator.msSaveBlob(dataUrlToBlob(payload), fileName) :
saver(payload) ; // everyone else can save dataURLs un-processed
}
}//end if dataURL passed?
blob = payload instanceof myBlob ?
payload :
new myBlob([payload], {type: mimeType}) ;
function dataUrlToBlob(strUrl) {
var parts= strUrl.split(/[:;,]/),
type= parts[1],
decoder= parts[2] == "base64" ? atob : decodeURIComponent,
binData= decoder( parts.pop() ),
mx= binData.length,
i= 0,
uiArr= new Uint8Array(mx);
for(i;i<mx;++i) uiArr[i]= binData.charCodeAt(i);
return new myBlob([uiArr], {type: type});
}
function saver(url, winMode){
if ('download' in anchor) { //html5 A[download]
anchor.href = url;
anchor.setAttribute("download", fileName);
anchor.className = "download-js-link";
anchor.innerHTML = "downloading...";
anchor.style.display = "none";
document.body.appendChild(anchor);
setTimeout(function() {
anchor.click();
document.body.removeChild(anchor);
if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(anchor.href);}, 250 );}
}, 66);
return true;
}
// handle non-a[download] safari as best we can:
if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent)) {
url=url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
if(!window.open(url)){ // popup blocked, offer direct download:
if(confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")){ location.href=url; }
}
return true;
}
//do iframe dataURL download (old ch+FF):
var f = document.createElement("iframe");
document.body.appendChild(f);
if(!winMode){ // force a mime that will download:
url="data:"+url.replace(/^data:([\w\/\-\+]+)/, defaultMime);
}
f.src=url;
setTimeout(function(){ document.body.removeChild(f); }, 333);
}//end saver
if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
return navigator.msSaveBlob(blob, fileName);
}
if(self.URL){ // simple fast and modern way using Blob and URL:
saver(self.URL.createObjectURL(blob), true);
}else{
// handle non-Blob()+non-URL browsers:
if(typeof blob === "string" || blob.constructor===toString ){
try{
return saver( "data:" + mimeType + ";base64," + self.btoa(blob) );
}catch(y){
return saver( "data:" + mimeType + "," + encodeURIComponent(blob) );
}
}
// Blob but not URL support:
reader=new FileReader();
reader.onload=function(e){
saver(this.result);
};
reader.readAsDataURL(blob);
}
return true;
}; /* end download() */
(function () {
RK.STLExporter = function () {};
RK.STLExporter.prototype = {
constructor: THREE.STLExporter,
parse: (function () {
var vector = new THREE.Vector3();
var normalMatrixWorld = new THREE.Matrix3();
return function (scenes) {
console.log(scenes);
var output = '';
output += 'solid exported\n';
for (var scene_nr in scenes) {
scenes[scene_nr].traverse(function (object) {
if (object instanceof RK.Mesh) {
// if object is hidden - exit
if (object.visible == false) return;
var geometry = object.geometry;
var matrixWorld = object.matrixWorld;
var skeleton = object.skeleton;
var mesh = object;
console.log('Found Mesh' + mesh.name);
if (typeof onlyExportThisMesh !== 'undefined' && mesh.name != onlyExportThisMesh) {
console.log('Looking for :' + onlyExportThisMesh + ' therefore ignoring ' + mesh.name);
return;
}
console.log('exporting: ' + mesh.name);
if (geometry instanceof RK.BufferGeometry) {
var oldgeometry = geometry.clone();
geometry = new RK.Geometry().fromBufferGeometry(geometry);
var skinIndex0 = oldgeometry.getAttribute('skinIndex0');
var skinWeight0 = oldgeometry.getAttribute('skinWeight0');
var morphTarget = oldgeometry.getAttribute('morphTarget0');
var mtcount = 0;
// TODO: find out what skinIndex1 and skinWeight1 are supposed to acomplish
// var skinIndex1 = oldgeometry.getAttribute('skinIndex0');
// var skinWeight1 = oldgeometry.getAttribute('skinWeight0');
while (typeof morphTarget !== 'undefined') {
mtcount++;
morphTarget = oldgeometry.getAttribute('morphTarget' + mtcount);
}
if (typeof skinIndex0 !== '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(skinIndex0, i));
geometry.skinWeights.push((new THREE.Vector4()).fromBufferAttribute(skinWeight0, i));
// if( typeof skinIndex1 !== 'undefined') {
// geometry.skinIndices.push((new THREE.Vector4()).fromBufferAttribute(skinIndex1, i));
// geometry.skinWeights.push((new THREE.Vector4()).fromBufferAttribute(skinWeight1, 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]); //.applyMatrix4( matrixWorld );
// see https://github.com/mrdoob/three.js/issues/3187
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
];
//this checks to see if the mesh has any morphTargets - jc
if (geometry.morphTargets !== 'undefined') {
var morphMatricesX = [];
var morphMatricesY = [];
var morphMatricesZ = [];
var morphMatricesInfluence = [];
for (var mt = 0; mt < geometry.morphTargets.length; mt++) {
//collect the needed vertex info - jc
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++) {
//not pretty, but it gets the job done - jc
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]);
//the inverse takes the vector into local bone space
tempVector.applyMatrix4(inverses[k])
//which is then transformed to the appropriate world space
.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;
});
}
var menu_style = { "width": "150px", "background-color": "crimson", "cursor": "pointer", "pointer-events": "auto", "text-align": "center", "padding": "6px", "font-size": "large" };
var character_area, stl_base, gltf_base, collada_base, obj_base;
stl_base = jQuery("<a/>").css(menu_style).text("Export STL");
gltf_base = jQuery("<a/>").css(menu_style).text("Export GLTF");
collada_base = jQuery("<a/>").css(menu_style).text("Export Collada");
obj_base = jQuery("<a/>").css(menu_style).text("Export OBJ");
character_area = jQuery(".dropdowns-0-1-31").first();
//character_area.css({"display": "flex", "justify-content": "center", "align-content": "center"});
character_area.prepend(stl_base);
character_area.prepend(gltf_base);
character_area.prepend(collada_base);
character_area.prepend(obj_base);
stl_base.click(function (e) {
e.preventDefault();
var exporter = new RK.STLExporter();
var stlString = exporter.parse([CK.character])
var name = get_name();
download(stlString, name + '.stl', 'application/sla');
});
gltf_base.click(function (e) {
e.preventDefault();
// Instantiate a exporter
var exporter = new GLTFExporter();
// Parse the input and generate the glTF output
exporter.parse(CK.scene, function (gltf) {
console.log(gltf);
var name = get_name();
download(JSON.stringify(gltf), name + '.gltf', "text/plain");
}/*, options*/);
});
collada_base.click(function (e) {
e.preventDefault();
// Instantiate a exporter
var exporter = new ColladaExporter();
// Parse the input and generate the glTF output
exporter.parse(CK.scene, function (data) {
console.log(data);
var name = get_name();
download(JSON.stringify(data), name + '.collada', "text/plain");
}/*, options*/);
});
obj_base.click(function (e) {
e.preventDefault();
var exporter = new OBJExporter();
var objString = exporter.parse(CK.character)
var name = get_name();
download(objString, name + '.obj', 'application/sla');
});
})()
};
function inject_script(url, callback) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
script.onload = function (e) {
callback()
};
head.appendChild(script);
}
inject_script("//code.jquery.com/jquery-3.3.1.min.js", function () {init()});
function get_name() {
var timestamp = new Date().getUTCMilliseconds();
var uqID = timestamp.toString(36);
var name = "Character " + uqID;
try {
var getName = CK.character.data.meta.character_name
name = getName === "" ? name : getName;
} catch (e) {
if (e instanceof ReferenceError) {
console.log("Name of character data location has changed");
console.log(e);
} else {
console.log("Other Error");
console.log(e);
}
}
return name;
}