summaryrefslogtreecommitdiff
path: root/viewer/resources.ts
blob: c0eaba39ab1e76b1f103bfea77969a2ed69e0763 (plain)
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
import { Loader } from "./loader.ts";

export class Resource<T> {
    constructor(public hash: Uint8Array) { }
    toString() {
        return Array.from(this.hash)
            .map(e => e.toString(16).padStart(2, "0"))
            .join("")
    }

}

export function read_res_table(buffer: Uint8Array, cb: (key: string, value: Uint8Array) => void) {
    const view = new DataView(buffer.buffer)
    let p = 0
    while (p < view.byteLength) {
        const key_len = view.getInt16(p, true)
        p += 2
        const value_len = view.getInt16(p, true)
        p += 2
        const key = String.fromCharCode(...buffer.slice(p, p + key_len))
        p += key_len
        const value = buffer.slice(p, p + value_len)
        p += value_len
        cb(key, value)
    }
}

function get_vec3(b: Uint8Array): Vec3 {
    const k = new DataView(b.buffer)
    return {
        x: k.getFloat32(0, true),
        y: k.getFloat32(4, true),
        z: k.getFloat32(8, true),
    }
}
function get_aabb(b: Uint8Array): AABB {
    return {
        min: get_vec3(b.slice(0, 12)),
        max: get_vec3(b.slice(12, 24))
    }
}

const object_cache = new Map()
async function ob_cached<T>(r: Resource<T>, make: () => Promise<T>): Promise<T> {
    const k = r.toString()
    const l = object_cache.get(k)
    if (l) return l
    const m = await make()
    object_cache.set(k, m)
    return m
}

export async function get_respackentry(l: Loader, r: Resource<RespackEntry>): Promise<RespackEntry> {
    return await ob_cached(r, async () => {
        const buf = await l.download(r);
        const o: RespackEntry = {}
        read_res_table(buf, (key, value) => {
            switch (key) {
                case "c_spatial_index":
                    o.c_spatial_index = new Resource(value)
                    break
                default:
            }
        });
        return o
    })
}
export async function get_spatialindex(l: Loader, r: Resource<SpatialIndex>): Promise<SpatialIndex> {
    return await ob_cached(r, async () => {
        const buf = await l.download(r);
        const o: SpatialIndex = { child: [] }
        read_res_table(buf, (key, value) => {
            switch (key) {
                case "prefab":
                    o.prefab = new Resource(value)
                    break
                case "child":
                    o.child.push([
                        get_aabb(value.slice(0, 24)),
                        new Resource(value.slice(24, 56))
                    ])
                    break
                default:
            }
        });
        return o
    })
}
export async function get_prefab(l: Loader, r: Resource<Prefab>): Promise<Prefab> {
    return await ob_cached(r, async () => {
        const buf = await l.download(r);
        const o: Prefab = { graphics: [] }
        read_res_table(buf, (key, value) => {
            let v, x = 0
            switch (key) {
                case "graphics":
                    o.graphics.push([undefined as unknown as Affine3, new Resource(value.slice(48, 80))])
                    break
                case "transform":
                    v = new DataView(value.buffer.slice(0, 96))
                    o.transform = [
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                        v.getFloat64((x++) * 8, true),
                    ]
                    break
                default:
            }
        });
        return o
    })
}
export async function get_graphics_part(l: Loader, r: Resource<GraphicsPart>): Promise<GraphicsPart> {
    return await ob_cached(r, async () => {
        return new GraphicsPart(await l.download(r))
    })
}

export interface Vec3 { x: number, y: number, z: number }
export interface AABB { min: Vec3, max: Vec3 }
export interface Mat3 { x: Vec3, y: Vec3, z: Vec3 }
export type Affine3 = [number, number, number, number, number, number, number, number, number, number, number, number]

export interface SpatialIndex {
    level?: number
    prefab?: Resource<undefined>
    child: [AABB, Resource<undefined>][]
}
export interface RespackEntry {
    c_spatial_index?: Resource<SpatialIndex>
}
export interface Prefab {
    transform?: Affine3,
    graphics: [Affine3, Resource<GraphicsPart>][]
}

export class GraphicsPart {
    commands: GraphicsCommand[] | undefined
    constructor(public buffer: Uint8Array) { }

    read(): GraphicsCommand[] {
        if (this.commands) return this.commands

        const out: GraphicsCommand[] = []
        let p = 0
        while (p < this.buffer.length) {
            const step = this.buffer[p + 0]
            const kind = this.buffer[p + 1]
            p += 2
            if (kind == 0) {
                if (step == 0) out.push({ no_fill: true })
                else if (step == 1) out.push({ fill: this.buffer[p + 0] })
                else throw new Error("bbb");
            } else if (kind == 1) {
                if (step == 0) out.push({ no_stroke: true })
                else if (step == 1) out.push({ stroke: this.buffer[p + 0] })
                else throw new Error("ccc");
            } else if (kind == 2) {
                if (step == 4) out.push({ stroke_width: new DataView(this.buffer.buffer.slice(p, p + 4)).getFloat32(0) })
                else throw new Error("ddd");
            } else if (kind == 6) {
                if (step == 12) out.push({ point: get_vec3(this.buffer.slice(p, p + 12)) })
                else throw new Error("eee");
            }
            else throw new Error(`unknown gcmd ${kind}`);

            p += step
        }

        this.commands = out
        return out
    }
}

export interface GraphicsCommand {
    no_fill?: true,
    fill?: number,
    no_stroke?: true
    stroke?: number
    stroke_width?: number
    point?: Vec3
}