-
Notifications
You must be signed in to change notification settings - Fork 1
/
VST-converter-kaitai-020.html
324 lines (280 loc) · 12 KB
/
VST-converter-kaitai-020.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
323
324
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Get texture reference from VST file: <input type="file" id="inpHeaderFile" name = "inpHeaderFile" width="100" accept=".vst" ><br>
Texture reference: <span id = "spnTextureRef" name = "spnTextureRef">-</span><br>
Load VST+texture : <input type="file" id="inpVST_TXTfile" name = "inpVST_TXTfile" width="100" accept= ".vst,.rgb" multiple><br>
Load VST file only: <input type="file" id="inpVSTfile" name = "inpVSTfile" width="100" accept=".vst"><br>
<span id="file_status" name="file_status" >waiting</span><br>
<span id="file_length" name="file_length" >-</span><br>
<button id="PLY" name="PLY" onclick = "savePLY()">Save as .PLY</button><br>
<br>
<button id="XYZ" name="XYZ" onclick = "saveXYZ()">Save as .XYZ</button><br>
<script src="KaitaiStream.js"></script>
<script src="Vst-header.js"></script>
<script src="Vst.js"></script>
<script src="img_gray16.js"></script>
<script>
// 0.2.0: Prepared for imnplementation of texture; still only supporting bare .vst; to include kaitai library for IMG textures img_gray16.js
//LODS_TO_PROCESS = 1; // debug
myVst = null;
myImg = null;
PLY_HEADER_BASE = "" +
"ply\n" +
"format ascii 1.0\n" +
"comment Created by VST JS convereter\n" +
"element vertex VVVV\n" +
"property float x\n" +
"property float y\n" +
"property float z\n" +
"element face FFFF\n" +
"property list uchar int vertex_index\n" +
"end_header\n"
PLYfiles = [];
XYZfiles = [];
VST_HEADER_LENGTH = 1+1+1+1 + 1+1+1+1 + 4 + 4 + 1+1+1+1 + 1+1+1+1+1+1+1+1 + 4 + 4 + 4; // 40
BOUNDING_BOX_LENGTH = 6*4; // 24
TEXTURE_REF_LENGTH = 2048;
COORDINATE_SYSTEM_LENGTH = 4096;
LOD_HEADER_LENGTH = 4 + 1+1+1+1+1+1+1+1 + 4 + 4 + 4 + 4; //28
PATCH_HEADER_LENGTH = 8 + 4 + 4 + 4 + 4; // 24
const headerSelector = document.getElementById('inpHeaderFile');
headerSelector.addEventListener('change', (event) => {
loadVSTheader(event.target.files[0])
});
const VST_TXT_Selector = document.getElementById('inpVST_TXTfile');
VST_TXT_Selector.addEventListener('change', (event) => {
loadVST_TXT(event.target.files[0], event.target.files[1])
});
const VST_Selector = document.getElementById('inpVSTfile');
VST_Selector.addEventListener('change', (event) => {
loadVST(event.target.files[0])
});
function saveFile(fileContent, filename,status_id) {
myBlob = new Blob([fileContent], {type: "application/octet-stream"});
var url = window.URL.createObjectURL(myBlob);
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
// a(anchor);
anchor.addEventListener('load', (event) => {
console.log("LOAD");
});
anchor.addEventListener('ended', (event) => {
console.log("ENDED");
});
anchor.addEventListener('loadeddata', (event) => {
console.log("loadeddata");
});
anchor.addEventListener('loadstart', (event) => {
console.log("loadstart");
});
anchor.addEventListener('mousedown', (event) => {
console.log("mousedown");
});
anchor.click();
window.URL.revokeObjectURL(url);
document.getElementById(status_id).innerHTML = "Saved to " + filename;
}
function loadVSTheader(fileHandler) {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
rawFileContents = event.target.result;
fileContentsUInt8 = new Uint8Array(rawFileContents);
console.log("Loaded: ", event, rawFileContents.length);
document.getElementById("file_status").innerHTML = "Loaded";
document.getElementById("file_length").innerHTML = fileContentsUInt8.length + " bytes";
st = new KaitaiStream(fileContentsUInt8);
myVstHeader = new Vsth(st);
console.log("This file contains:");
console.log(myVstHeader.vistaHeader.lodsCount, " LODs");
sliceUInt8 = myVstHeader.textureRef;
textureRefRaw = String.fromCharCode(...sliceUInt8)
textureRef = textureRefRaw.split("\x00");
textureName = textureRef[2];
spnTextureRef.innerHTML = textureName;
console.log(textureName);
});
reader.readAsArrayBuffer(fileHandler);
console.log("Header reading process initiated...");
}
function loadVST(fileHandler) {
myName = fileHandler.name;
const reader = new FileReader();
reader.addEventListener('load', (event) => {
rawFileContents = event.target.result;
fileContentsUInt8 = new Uint8Array(rawFileContents); // Extract from the generic ArrayBuffer an array of Unsigned Integers (0..255)
console.log("Loaded: ", event, rawFileContents.length);
document.getElementById("file_status").innerHTML = "Loaded";
document.getElementById("file_length").innerHTML = fileContentsUInt8.length + " bytes";
st = new KaitaiStream(fileContentsUInt8);
myVst = new Vst(st);
totalFileLength = VST_HEADER_LENGTH +
BOUNDING_BOX_LENGTH +
TEXTURE_REF_LENGTH * myVst.vistaHeader.texturesCount +
COORDINATE_SYSTEM_LENGTH +
myVst.vistaHeader.vertexCount*20;
LODsLength = 0;
console.log("This file contains:");
console.log(myVst.lods.length, " LODs");
sliceUInt8 = myVst.textureRef;
textureRefRaw = String.fromCharCode(...sliceUInt8)
textureRef = textureRefRaw.split("\x00");
textureName = textureRef[2];
console.log(textureName);
LODS_TO_PROCESS = myVst.lods.length; //debug
prevLODvertexCount = 0;
for (var lodIndex = 0; lodIndex < LODS_TO_PROCESS; lodIndex++) {
trianglesInLOD = 0;
myLOD = myVst.lods[lodIndex];
LODsLength += LOD_HEADER_LENGTH + BOUNDING_BOX_LENGTH;
lastVertex = myLOD.header.highestVertex;
LODvertexCount = myLOD.header.vertexCount;
firstVertex = lastVertex - LODvertexCount + 1;
console.log(" LOD n. " + lodIndex);
console.log(" Contains n. " + myLOD.header.patchesCount + " patches.");
console.log(" Uses vertex from " + firstVertex + " to " + lastVertex);
LODtrianglesCount = 0; // For PLY output
PLYvertex = "";
PLYfaces = "";
PLYfiles.push("");
for (var patchIndex = 0; patchIndex < myLOD.header.patchesCount; patchIndex++) {
myPatch = myLOD.patches[patchIndex];
totalPatchLength = PATCH_HEADER_LENGTH + myPatch.header.stripsCount*4 + myPatch.header.indexCount*4;
LODsLength += totalPatchLength;
console.log(" Patch n. " + patchIndex);
console.log(" Arrays/Strips num: ", myPatch.header.stripsCount);
console.log(" Arrays lengths:", myPatch.trianglesStripsLengths);
console.log(" Total length: ", totalPatchLength);
pointers = myPatch.pointersToVertexOfFaces; // Pointers to vertex database at beginning of file
XYZfile = "";
for (p=0; p < pointers.length; p++) {
vertex = myVst.vertices[pointers[p]];
XYZline = vertex.x + " " + vertex.y + " " + vertex.z + "\n";
XYZfile += XYZline;
}
XYZfiles.push(XYZfile);
triangleStrips = [];
arrayOffset = 0;
arrayLen = 0; // dummy value, to properly calculate first arrayOffset (=0)
processedTriangles = 0;
// The "arrays" are actually just a sequence of values; how many values form
// an array depends on arrayLen values; such values are stored in sequence, just before the arrays.
totalArraysTrianglesCount = 0;
for (arrayIndex = 0; arrayIndex < myPatch.header.stripsCount; arrayIndex++) {
// An = length of array n
// Sn = An - 2 = lenght of strip n = number of triangles
// Triangle n.1: vertex 0, 1, 2 of array
// Following triangles: Using the remaining (Sn -1) vertex after vertex 2
//
// Example:
// Array: [10, 15, 14, 18, 17, 11]
// ^^^^^^^^^^^ ^^^^^^^^^
// | | | |
// | | | |
// \ | / |
// Triangle n.1 |
// \- Further triangles
//
// [10, 15, 14, 18, 17, 11]
// | | |
// | | |
// \ | /
// n.2
//
// [10, 15, 14, 18, 17, 11]
// | | |
// | | |
// \ | /
// n.3
//
// [10, 15, 14, 18, 17, 11]
// | | |
// | | |
// \ | /
// n.4
//
// Array lenght : 6
// Strip lenght (= number of triangles): Sn = 6-2 = 4
// First triangle: [10, 15, 14]
// Other triangles are described by 3 vertex (Sn-1 = 3) after vertex of first triangle: 18, 17, 11
arrayOffset += arrayLen;
arrayLen = myPatch.trianglesStripsLengths[arrayIndex];
trianglesCount = arrayLen - 2; // Sn = An-2 0 number of triangles/faces in this strip
totalArraysTrianglesCount += trianglesCount;
//console.log("Processing array n.", arrayIndex , "(from pos. " , arrayOffset , " to pos " , (arrayOffset + arrayLen-1) );
triangleStrip = [];
triangles = [];
for (triangleIndex = 0 + 2; triangleIndex < arrayLen; triangleIndex++) {
//console.log("Processing triangle n." + (triangleIndex-1) + " of " + (arrayLen-3));
triangle = [];
PLYface = "3 "; // Always triangles
for (pointIndex = 0; pointIndex < 3; pointIndex++) {
if (triangleIndex % 2 == 0) { // Even triangle => CCW, Odd triangle => CW
vertexPointer = pointers[arrayOffset + triangleIndex - pointIndex];
} else {
vertexPointer = pointers[arrayOffset + triangleIndex - (2-pointIndex)];
}
//console.log("Adding point n." + (pointIndex+1) + " of 3");
// [ [1, 3, 5, 7], [11, 10, 8, 7, 9, 14 , 15], 6[, ...] Arrays database (=sequence of integers)
// ^^^^^^^^^^^ ^^^^^^^^^^
// | ^ | ^
// | | | |
// | triangleIndex | triangleIndex
// | (2..3) | (2..6)
// | |
// | |
// \- Array offset \- Array offset
// (constant) (constant)
x = myVst.vertices[vertexPointer].x;
y = myVst.vertices[vertexPointer].y;
z = myVst.vertices[vertexPointer].z;
point = {x, y, z};
PLYface += (vertexPointer - firstVertex) + " "; // Each ply file has its own independent index list starting from 0
triangle.push(point)
} // point
triangles.push(triangle);
PLYfaces += PLYface + "\n";
LODtrianglesCount++;
} // triangle
triangleStrips.push(triangles);
processedTriangles += triangles.length;
} // arrayIndex
trianglesInLOD += totalArraysTrianglesCount;
} // patches
PLYheader = PLY_HEADER_BASE.replace("VVVV", LODvertexCount);
PLYheader = PLYheader.replace("FFFF", LODtrianglesCount);
PLYfiles[lodIndex] += PLYheader;
console.log("FIRST,LAST=",firstVertex,lastVertex);
for (v=firstVertex; v <= lastVertex; v++) {
PLYvertex += myVst.vertices[v].x + " " + myVst.vertices[v].y + " " + myVst.vertices[v].z + "\n";
}
PLYfiles[lodIndex] += PLYvertex;
PLYfiles[lodIndex] += PLYfaces;
//console.log("File ply for LOD n. :" , lodIndex, PLYfiles[lodIndex]);
prevLODvertexCount = LODvertexCount;
console.log("triangles expected :",trianglesInLOD)
console.log("triangles processed :",processedTriangles)
} // LODs
totalFileLength += LODsLength;
console.log("totalFileLength=",totalFileLength);
alert("Done");
});
reader.readAsArrayBuffer(fileHandler);
//console.log("Reading process initiated...");
}
function savePLY() {
for (lodIndex=0; lodIndex < LODS_TO_PROCESS; lodIndex++) {
saveFile(PLYfiles[lodIndex], "LOD"+lodIndex + ".ply","file_status");
}
}
function saveXYZ() {
for (lodIndex=0; lodIndex < LODS_TO_PROCESS; lodIndex++) {
saveFile(XYZfiles[lodIndex], "LOD"+lodIndex + ".txt","file_status");
}
}
function loadTexture() {
}
</script>
</body>
</html>