-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVST-converter - 002.html
236 lines (192 loc) · 10.7 KB
/
VST-converter - 002.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
s<!DOCTYPE html>
<html>
<head>
<title>ViSTa .vst 3d mesh file converter</title>
</head>
<body>
<script src="VST-utils.js"></script>
<script>
// 0.0.2 - Patch and triangle strip ok
// 0.0.1 - LODs ok
var rawFileContents;
var fileContentsUInt8;
fields = [];
fieldNames = [];
LODfaces = [];
LODvertex = [];
LODlenghts = [];
LOD_vertex_DB_start= [];
LOD_vertex_DB_End = [];
const VALUE_LEN = 4 // 32 bit integer
const VST_HEADER_LEN = 40
const LOD_HEADER_LEN = 28
const BOUNDING_BOX_LEN = 24
const TEXTURE_REF_LEN = 2048
const COORD_SYS_LEN = 4096
const PATCH_HEADER_LEN = 24
// For VST header:
const TEXTURES_COUNT_OFFSET = 28
const VERTEX_COUNT_OFFSET = 32
const LODS_COUNT_OFFSET = 36
// For LOD header:
const LOD_VERTEX_COUNT_OFFSET = 12
const LOD_PATCHES_COUNT_OFFSET = 20
const LOD_LAST_VERTEX_POINTER_OFFSET = 24
// For PATCH:
const PATCH_TYPE_OFFSET = 8
const PATCH_FACES_COUNT_OFFSET = 16 // Number of "arrays of vertex pointers" ("=index arrays") in patch
const PATCH_VERTEX_COUNT_OFFSET = 20 // Number of "vertex pointers" (="index") in patch
// for Vertex:
const VALUES_PER_VERTEX = 5 // 2 for texture coordinates + 3 for space coordinates
const SINGLE_VERTEX_LEN = VALUES_PER_VERTEX * VALUE_LEN
function showHeader(uint8slice, fieldsCount, indent) {
// Show hex values of header, grouped by field length (for debugging purposes)
prevOffset = 0
prevLength = 0
// s = sliceToString(uint8slice,0,uint8slice.length)
//console.log(s);
for (fieldIndex = 0; fieldIndex < fieldsCount; fieldIndex++) {
fieldOffset = prevOffset + prevLength;
fieldLength = fields[fieldIndex];
fieldStr = uint8slice.slice(fieldOffset, fieldOffset + fieldLength);
console.log( indent,"Field ",(fieldIndex) , " (" , fieldNames[fieldIndex] , "): " , stringToHex(fieldStr) , " ---> " , stringToHexInv(fieldStr) , " ---> " , stringToDecInv(fieldStr));
prevOffset = fieldOffset;
prevLength = fieldLength;
}
}
function processFile() {
console.log(fileContentsUInt8,length);
LODtoConvert = 0;
texturesCount = getValFromString(fileContentsUInt8, TEXTURES_COUNT_OFFSET, VALUE_LEN)
vertexCount = getValFromString(fileContentsUInt8, VERTEX_COUNT_OFFSET, VALUE_LEN)
LODcount = getValFromString(fileContentsUInt8, LODS_COUNT_OFFSET, VALUE_LEN)
vertexListOffset = VST_HEADER_LEN + BOUNDING_BOX_LEN + texturesCount * TEXTURE_REF_LEN + COORD_SYS_LEN
vertexListLen = vertexCount * VALUES_PER_VERTEX * VALUE_LEN
vertexListEnd = vertexListOffset + vertexListLen - 1
firstLODoffset = vertexListEnd + 1
firstLODlen = getValFromString(fileContentsUInt8, firstLODoffset, VALUE_LEN)
prevLODoffset = firstLODoffset
prevLODlen = 0
for (LODindex = 0; LODindex < LODcount; LODindex++) {
console.log (" === LOD n." ,LODindex)
LODoffset = prevLODoffset + prevLODlen
LOD_Header_Contents = fileContentsUInt8.slice(LODoffset, LODoffset + LOD_HEADER_LEN);
console.log (" LODoffset: " +LODoffset+ " ($" + LODoffset.toString(16) + ")")
LODlen = getValFromString(fileContentsUInt8, LODoffset, 4)
LODlenghts[LODindex] = LODlen;
console.log (" LODlen: " + LODlen + " (0x" + LODlen.toString(16) + ")")
LOD_end = LODoffset + LODlen - 1
console.log (" LOD_end: " + LOD_end + "(0x" + LOD_end.toString(16) + ")")
nextLODoffset = LODoffset + LODlen
console.log (" Next LOD starts at: " + nextLODoffset + " (0x" + nextLODoffset.toString(16) + ")")
console.log (" Header: " + stringToHex(LOD_Header_Contents)); // Whole header preview
fields = [4,8,4,4,4,4];
fieldNames =["Total Size","Reserved", "N vertex" , "LOD threshold" ,"N patches" , "Last vertex index" ];
showHeader(LOD_Header_Contents, 6, " ") //Detailed header contents
LODvertexCount = getValFromString(fileContentsUInt8, LODoffset + LOD_VERTEX_COUNT_OFFSET, 4, "int")
console.log (" LODvertexCount: " + LODvertexCount + " (0x" + LODvertexCount.toString(16) + ")")
LODpatchesCount = getValFromString(fileContentsUInt8, LODoffset + LOD_PATCHES_COUNT_OFFSET, 4, "int")
console.log (" LODpatchesCount: " + LODpatchesCount + " (0x" + LODpatchesCount.toString(16) + ")")
LODmaxVert = getValFromString(fileContentsUInt8, LODoffset + LOD_LAST_VERTEX_POINTER_OFFSET, 4, "int")
console.log (" LODmaxVert: " + LODmaxVert + " (0x" + LODmaxVert.toString(16) + ")")
LOD_vertex_DB_start[LODindex] = LODoffset + LOD_HEADER_LEN + LODmaxVert - LODvertexCount + 1
LOD_vertex_DB_End[LODindex] = LODoffset + LOD_HEADER_LEN + LODmaxVert + 1
console.log (" Vertex database for this LOD: from 0x" + LOD_vertex_DB_start[LODindex].toString(16) + " to 0x " + LOD_vertex_DB_End[LODindex].toString(16))
prevLODoffset = LODoffset
prevLODlen = LODlen
prevPatchesBytes = 0
////////////////// ================ PATCH ===========
for (patchIndex = 0; patchIndex< LODpatchesCount; patchIndex++) {
console.log (" === PATCH n." + patchIndex)
patchOffset = LODoffset + LOD_HEADER_LEN + BOUNDING_BOX_LEN + prevPatchesBytes
console.log (" patchOffset: " + patchOffset + " ($" + patchOffset.toString(16) + ")")
fields= [8,4,4,4,4];
fieldNames = ["Reserved", "Triangles or PointClouds", "Pointer to texture", "N. of groups in patch", "Number of vertex pointers in patch"];
fieldsTypes = ["int","int","int","int","int"];
patchHeaderContents = fileContentsUInt8.slice(patchOffset, patchOffset + PATCH_HEADER_LEN);
showHeader(patchHeaderContents, 5, " "); // Detailed header contents
patchArraysCount = getValFromString(patchHeaderContents, 16, 4, "int"); // Number of index arrays in patch
patchIndexesCount = getValFromString(patchHeaderContents, 20, 4, "int"); // Number of indexes in the patch
patchLength = PATCH_HEADER_LEN + patchArraysCount*4 + patchIndexesCount*4;
console.log (" patchLength: " + patchLength);
//patchesCount = patchesCount + 1
prevPatchesBytes = prevPatchesBytes + patchLength;// debug
//// ============== INDEX ARRAY ==============
if ( LODindex === LODtoConvert ) {
arrayLenghtsDatabaseOffset = patchOffset + PATCH_HEADER_LEN;
arraysDatabaseOffset = arrayLenghtsDatabaseOffset + patchArraysCount;
lengthsDatabase = fileContentsUInt8.slice(arrayLenghtsDatabaseOffset, (arrayLenghtsDatabaseOffset + patchArraysCount) * 4);
arraysDatabase = fileContentsUInt8.slice(arraysDatabaseOffset, (arraysDatabaseOffset + patchIndexesCount) * 4);
totalIndexesCount = 0;
arrayLocalOffset=0;
arraysDatabaseInt = [];
for (arrayIndex = 0 ; arrayIndex < patchArraysCount; arrayIndex++) { // parse all arrays
arrayLength = getValFromString(lengthsDatabase, arrayIndex*4, 4, "int");
totalIndexesCount += arrayLength; // for debugging
arrayRawContents = arraysDatabase.slice(arrayLocalOffset, arrayLocalOffset + arrayLength * 4);
/*console.log(" Array n. " + arrayIndex +
"; length offset = " + (arrayLenghtsDatabaseOffset + arrayIndex*4).toString(16) +
", length = " + arrayLength +
", contents offset = " + (arraysDatabaseOffset + arrayLocalOffset).toString(16));*/
//console.log(" Raw contents = ", stringToHex(arrayRawContents));
arrayIntContents = [];
for (byteIndex = 0; byteIndex < arrayLength; byteIndex++) {
rawVal = getValFromString(arrayRawContents, byteIndex * 4, 4, "int");
//console.log(" rawVal=",rawVal);
arrayIntContents.push(rawVal);
}
arrayLocalOffset += arrayLength * 4;
arraysDatabaseInt.push(arrayIntContents);
//console.log(" Int Contents = ", arrayIntContents);
//console.log( createPLYline(vertexOffset));
}
console.log(" Arrays database (=triangles strip): ", arraysDatabaseInt);
console.log(" PATCH CHECK:");
console.log(" patchArraysCount=",patchArraysCount);
console.log(" patchIndexesCount=",patchIndexesCount);
console.log(" patchArraysCount + patchIndexesCount =",(patchArraysCount + patchIndexesCount));
console.log(" patchLength = PATCH_HEADER_LEN + patchArraysCount*4 + patchIndexesCount*4 =",(PATCH_HEADER_LEN + patchArraysCount*4 + patchIndexesCount*4), patchLength);
console.log(" totalIndexesCount=",totalIndexesCount + " should be = patchIndexesCount = " + patchIndexesCount);
console.log(" Next patch offset (or next LOD offset if last patch) = 0x" + (patchOffset + patchLength).toString(16));
}
}
LODfaces[LODindex] = patchArraysCount;
LODvertex[LODindex] = patchIndexesCount;
console.log("================================");
}
console.log ("Final report:")
totalLODSlength = 0
console.log ("Number of Levels Of Depth (LODs): " + LODcount)
console.log ("");
console.log ("LOD,", "Faces,", "Vertex used")
for (LODindex = 0; LODindex < LODcount; LODindex++) {
console.log (LODindex, LODfaces[LODindex], LODvertex[LODindex])
totalLODSlength = totalLODSlength + LODlenghts[LODindex]
}
fileHeaderLength = VST_HEADER_LEN + BOUNDING_BOX_LEN + texturesCount * TEXTURE_REF_LEN + COORD_SYS_LEN
console.log ("")
console.log ("N. of vertex: " + vertexCount)
console.log ("")
console.log ( "CHECK:")
console.log ( "File header length = " + fileHeaderLength)
console.log ( "total vertex length = "+ vertexListLen)
console.log ( "total LODS length = " + totalLODSlength)
console.log ( "file length = " , fileContentsUInt8.length, fileHeaderLength + vertexListLen + totalLODSlength)
}
</script>
Load and Save text file locally.<br>
<br>
<input type="file" id="inpFile" width="100" accept=".vst"><br>
Status: <span id="status" name="status">-</span>
<button id="btnSave" name="btnSave" onclick="saveFile(rawFileContents, myName)")>Save...</button><br>
<button id="btnProcess" name="btnProcess" onclick="processFile()">Process file</button><br>
<br>
See console for output.
</body>
<script>
const fileSelector = document.getElementById('inpFile');
fileSelector.addEventListener('change', (event) => loadFile(event.target.files[0]));
console.log("Ready.");
document.getElementById("status").innerHTML = "READY.";
</script>
</html>