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
|
/// <reference lib="dom" />
import { aabb_overlap } from "./helper.ts";
import { AABB, get_graphics_part, get_prefab, get_respackentry, get_spatialindex, GraphicsPart, Prefab, Resource, SpatialIndex } from "./resources.ts";
const canvas = document.createElement("canvas")
canvas.style.position = "absolute"
canvas.style.top = "0px"
canvas.style.left = "0px"
canvas.style.width = "100vw"
canvas.style.height = "100vh"
document.body.append(canvas)
const ctx = canvas.getContext("2d")!
let loader: Loader | undefined;
function draw() {
ctx.fillStyle = "black"
ctx.fillRect(0, 0, canvas.width, canvas.height)
loader?.root.draw()
requestAnimationFrame(draw)
}
function resize() {
canvas.width = globalThis.innerWidth
canvas.height = globalThis.innerHeight
}
canvas.addEventListener("resize", () => resize())
resize()
draw()
async function init() {
const resp = await fetch("http://127.0.0.1:28556/entry")
if (!resp.ok) throw new Error("aaaa");
const entry_res = new Resource(await resp.bytes())
const entry = await get_respackentry(entry_res)
const root = await get_spatialindex(entry.c_spatial_index!)
loader = new Loader(root)
console.log("begin load");
await loader.update()
console.log("end load");
}
init()
let limit = 10000;
class Loader {
view: AABB
root: SNode
constructor(root: SpatialIndex) {
this.view = { min: { x: 0, y: 0, z: 0 }, max: { x: 1, y: 1, z: 1 } }
this.root = new SNode(root)
}
async update() {
await this.root.load(this.view)
}
}
class SNode {
children: (SNode | undefined)[]
prefab?: Prefab
graphics?: GraphicsPart
aabb?: AABB
constructor(public data: SpatialIndex) {
this.children = new Array(data.child.length).fill(undefined)
}
async load(view: AABB) {
if (this.data.prefab && !this.prefab)
this.prefab = await get_prefab(this.data.prefab)
if (this.prefab?.graphics[0] && !this.graphics)
this.graphics = await get_graphics_part(this.prefab.graphics[0][1])
for (let i = 0; i < this.data.child.length; i++) {
const [aabb, child_data_hash] = this.data.child[i];
if (!aabb_overlap(aabb, view)) continue
if (this.children[i] == undefined) {
this.children[i] = new SNode(await get_spatialindex(child_data_hash))
limit -= 1
if (limit <= 0) return
}
await this.children[i]!.load(view)
}
}
draw() {
for (const c of this.children) {
if (c) c.draw()
}
if (!this.graphics) return
ctx.fillStyle = "red"
ctx.save()
ctx.scale(100000, 100000)
ctx.translate(-0.155, -0.63)
ctx.translate(this.prefab!.transform![10], this.prefab!.transform![11])
for (const c of this.graphics.read()) {
if (c.point) ctx.fillRect(c.point.x, c.point.y, 0.00003, 0.00003)
}
ctx.restore()
}
}
|