-
Notifications
You must be signed in to change notification settings - Fork 1
/
VST-converter - 001.html
152 lines (116 loc) · 5.65 KB
/
VST-converter - 001.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
<!DOCTYPE html>
<html>
<head>
<title>ViSTa .vst 3d mesh file converter</title>
</head>
<body>
<script src="VST-utils.js"></script>
<script>
// 0.0.01 - 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);
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
LODpatchesCount = getValFromString(fileContentsUInt8, LODoffset + LOD_PATCHES_COUNT_OFFSET, 4, "int")
console.log (" LODpatchesCount: " + LODpatchesCount + " (0x" + LODpatchesCount.toString(16) + ")")
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] = LODmaxVert - LODvertexCount + 1
LOD_vertex_DB_End[LODindex] = 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
}
}
</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>