-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxd.ts
57 lines (48 loc) · 1.5 KB
/
txd.ts
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
import * as rw from "./rwbind";
async function main() {
await rw.init({
loadTextures: false,
locateFile: function (path, prefix) {
console.log("locateFile", path, prefix);
return prefix + "/wasm/" + path;
}
});
let response = await fetch("/data/models/generic.txd");
let stream = new rw.StreamMemory(await response.arrayBuffer());
let header = new rw.ChunkHeaderInfo(stream);
console.assert(header.type === rw.PluginID.ID_TEXDICTIONARY);
header.delete();
let txd = new rw.TexDictionary(stream);
txd.setCurrent();
stream.delete();
for (let lnk = txd.textures.begin; !lnk.is(txd.textures.end); lnk = lnk.next) {
let tex = rw.Texture.fromDict(lnk);
let img = tex.raster.toImage();
img.unindex();
if (img.depth < 24) {
console.warn("ignoring 16-bit texture", tex.name);
} else {
let c = document.createElement("canvas");
c.width = img.width;
c.height = img.height;
c.title = tex.name;
let ctx = c.getContext("2d");
let buf = ctx.createImageData(img.width, img.height);
let pixels = img.pixels;
if (img.hasAlpha()) {
buf.data.set(pixels);
} else {
for (let i = 0, j = 0; i < buf.data.length;) {
buf.data[i++] = pixels[j++];
buf.data[i++] = pixels[j++];
buf.data[i++] = pixels[j++];
buf.data[i++] = 0xff;
}
}
ctx.putImageData(buf, 0, 0);
document.body.appendChild(c);
}
img.delete();
}
}
main().catch(console.error);