-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.html
322 lines (294 loc) · 9.27 KB
/
index.html
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
<html>
<head>
<Title>Make glb</Title>
<style>
.area {
width: 45em;
padding: 15px;
border: 1px solid #333;
background: rgba(0,0,0,0.7);
}
#drop_zone {
border: 2px dashed #bbb;
-webkit-border-radius: 5px;
border-radius: 5px;
padding: 50px;
text-align: center;
font: 21pt bold arial;
color: #bbb;
}
</style>
</head>
<body>
<div class="area">
<div id="drop_zone">Drop all files and folders for your glTF asset here</div>
</div>
<div id="download">
<a href="#" id="downloadLink"></a>
</div>
<div>
<ul id=list></ul></div>
<script>
var files=[];
var fileblobs=[];
var gltf;
var remainingfilestoprocess=0;
var glbfilename;
var outputBuffers;
var bufferMap;
var bufferOffset;
function handleDragOver(event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
}
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
function addDownloadButton() {
var btn = document.createElement("button");
btn.id="downloadBtn";
btn.disabled=true;
btn.onclick= startDownload;
btn.appendChild(document.createTextNode("Processing..."));
document.getElementById("download").appendChild(btn);
}
function startDownload(){
document.getElementById("downloadLink").click();
}
function handleFileSelect(event) {
event.stopPropagation();
event.preventDefault();
document.getElementById('list').innerHTML="";
addDownloadButton();
var items = event.dataTransfer.items;
remainingfilestoprocess=items.length;
for (var i=0; i<items.length; i++) {
if (items[i].getAsEntry) {
var entry = items[i].getAsEntry();
} else if (items[i].webkitGetAsEntry) {
var entry = items[i].webkitGetAsEntry();
}
if (entry) {
traverseFileTree(entry);
}
}
}
function traverseFileTree(item, path) {
path = path || "";
if (item.isFile) {
item.file(function(file) {
files.push(file);
var fileitem = '<li><strong>'+ escape(file.name)+ '</strong> ('+ file.type + ') - '+
file.size+ ' bytes, last modified: '+ file.lastModifiedDate +
'</li>';
document.getElementById('list').innerHTML += fileitem;
var extension = file.name.split('.').pop();
if ( extension === "gltf")
{
glbfilename=file.name.substr(file.name.lastIndexOf('/')+1,file.name.lastIndexOf('.'));
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event) {
gltf = JSON.parse(event.target.result);
checkRemaining();
};
}
else{
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
fileblobs[theFile.name.toLowerCase()]=(e.target.result);
checkRemaining();
};
})(file);
reader.readAsArrayBuffer(file);
}
},function(error){
console.log(error);
});
} else if (item.isDirectory) {
var dirReader = item.createReader();
dirReader.readEntries(function(entries) {
remainingfilestoprocess+=entries.length;
checkRemaining();
for (var i=0; i<entries.length; i++) {
traverseFileTree(entries[i], path + item.name + "/");
}
});
}
}
function checkRemaining(){
remainingfilestoprocess--;
if(remainingfilestoprocess===0){
outputBuffers = [];
bufferMap = new Map();
bufferOffset = 0;
processBuffers().then(fileSave);
}
}
function processBuffers(){
var pendingBuffers = gltf.buffers.map(function (buffer, bufferIndex) {
return dataFromUri(buffer)
.then(function(data) {
if (data !== undefined) {
outputBuffers.push(data);
}
delete buffer.uri;
buffer.byteLength = data.byteLength;
bufferMap.set(bufferIndex, bufferOffset);
bufferOffset += alignedLength(data.byteLength);
});
});
return Promise.all(pendingBuffers)
.then(function() {
var bufferIndex = gltf.buffers.length;
var images = gltf.images || [];
var pendingImages = images.map(function (image) {
return dataFromUri(image).then(function(data) {
if (data === undefined) {
delete image['uri'];
return;
}
var bufferView = {
buffer: 0,
byteOffset: bufferOffset,
byteLength: data.byteLength,
};
bufferMap.set(bufferIndex, bufferOffset);
bufferIndex++;
bufferOffset += alignedLength(data.byteLength);
var bufferViewIndex = gltf.bufferViews.length;
gltf.bufferViews.push(bufferView);
outputBuffers.push(data);
image['bufferView'] = bufferViewIndex;
image['mimeType'] = getMimeType(image.uri);
delete image['uri'];
});
});
return Promise.all(pendingImages);
});
}
function fileSave(){
var Binary = {
Magic: 0x46546C67
};
for (var _i = 0, _a = gltf.bufferViews; _i < _a.length; _i++) {
var bufferView = _a[_i];
if(bufferView.byteOffset=== undefined){
bufferView.byteOffset=0;
}
else{
bufferView.byteOffset = bufferView.byteOffset + bufferMap.get(bufferView.buffer);
}
bufferView.buffer = 0;
}
var binBufferSize = bufferOffset;
gltf.buffers = [{
byteLength: binBufferSize
}];
var enc = new TextEncoder();
var jsonBuffer = enc.encode(JSON.stringify(gltf));
var jsonAlignedLength = alignedLength(jsonBuffer.length);
var padding;
if (jsonAlignedLength !== jsonBuffer.length) {
padding = jsonAlignedLength- jsonBuffer.length;
}
var totalSize = 12 + // file header: magic + version + length
8 + // json chunk header: json length + type
jsonAlignedLength +
8 + // bin chunk header: chunk length + type
binBufferSize;
var finalBuffer = new ArrayBuffer(totalSize);
var dataView = new DataView(finalBuffer);
var bufIndex = 0;
dataView.setUint32(bufIndex, Binary.Magic, true);
bufIndex += 4;
dataView.setUint32(bufIndex, 2, true);
bufIndex += 4;
dataView.setUint32(bufIndex, totalSize, true);
bufIndex += 4;
// JSON
dataView.setUint32(bufIndex, jsonAlignedLength, true);
bufIndex += 4;
dataView.setUint32(bufIndex, 0x4E4F534A, true);
bufIndex += 4;
for (var j=0;j<jsonBuffer.length;j++){
dataView.setUint8(bufIndex, jsonBuffer[j]);
bufIndex++;
}
if(padding!==undefined){
for (var j=0;j<padding;j++){
dataView.setUint8(bufIndex, 0x20);
bufIndex++;
}
}
// BIN
dataView.setUint32(bufIndex, binBufferSize, true);
bufIndex += 4;
dataView.setUint32(bufIndex, 0x004E4942, true);
bufIndex += 4;
for (var i = 0; i < outputBuffers.length; i++) {
var bufoffset = bufIndex + bufferMap.get(i);
var buf = new Uint8Array(outputBuffers[i]);
var thisbufindex=bufoffset;
for (var j=0;j<buf.byteLength;j++){
dataView.setUint8(thisbufindex, buf[j]);
thisbufindex++;
}
}
var a = document.getElementById("downloadLink");
var file = new Blob([finalBuffer],{type: 'model/json-binary'})
a.href = URL.createObjectURL(file);
a.download = glbfilename+".glb";
document.getElementById("downloadBtn").disabled=false;
document.getElementById("downloadBtn").textContent="Download .glb";
a.click();
}
function isBase64(uri) {
return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
}
function decodeBase64(uri) {
return fetch(uri).then(function(response) { return response.arrayBuffer(); });
}
function dataFromUri(buffer) {
if (buffer.uri === undefined) {
return Promise.resolve(undefined);
} else if (isBase64(buffer.uri)) {
return decodeBase64(buffer.uri);
} else {
var filename=buffer.uri.substr(buffer.uri.lastIndexOf('/')+1);
return Promise.resolve(fileblobs[filename.toLowerCase()]);
}
}
function alignedLength(value) {
var alignValue = 4;
if (value == 0) {
return value;
}
var multiple = value % alignValue;
if (multiple === 0) {
return value;
}
return value + (alignValue - multiple);
}
function getMimeType(filename) {
for (var mimeType in gltfMimeTypes) {
for (var extensionIndex in gltfMimeTypes[mimeType]) {
var extension = gltfMimeTypes[mimeType][extensionIndex];
if (filename.toLowerCase().endsWith('.' + extension)) {
return mimeType;
}
}
}
return 'application/octet-stream';
}
var gltfMimeTypes = {
'image/png': ['png'],
'image/jpeg': ['jpg', 'jpeg'],
'text/plain': ['glsl', 'vert', 'vs', 'frag', 'fs', 'txt'],
'image/vnd-ms.dds': ['dds']
};
</script>
</body>
</html>