-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
253 lines (229 loc) · 6.82 KB
/
index.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { F } from "https://esm.sh/[email protected]/index-BM7ZTN7E.d.ts";
import {
Map,
View,
TileLayer,
defaultControls,
FullScreen,
} from "./src/deps.ts"
import { TerrainRenderer } from "./src/TerrainRenderer.ts"
import { TerrainTile } from "./src/TerrainTile.ts"
import { html, render } from "https://esm.sh/[email protected]"
import { styleMap } from "https://esm.sh/[email protected]/directives/style-map"
const errors: Error[] = []
const onError = (e: Error) => {
(document.body.querySelector("#map") as HTMLElement).style.display = "none"
errors.push(e)
render(html`<p>
<b>Info</b>:<br/>
Error occurred.<br/>
Sorry, It seems your browser is not supported.<br/>
You need a browser that supports WebGPU.<br/>
This page was tested only on Chrome 134.<br/>
Firefox (including Nightly(136)) seems not working yet.<br/>
Or.. it's possibly just my mistake. You can write an issue on GitHub.<br/>
---<br/>
<b>navigator.userAgent</b>: ${navigator.userAgent}<br/>
<b>import.meta.url</b>: ${import.meta.url}<br/>
<b>location.href</b>: ${location.href}<br/>
---<br/>
${
errors.map(e => html`
<b>${e.name}</b>: ${e.message}
<br/>
`)
}
</p>`, document.body.querySelector("tweak") as HTMLElement)
throw e
}
try {
const terrain = await TerrainRenderer.from()
terrain.g!.root.device.addEventListener("uncapturederror", e => {
const error = (e as GPUUncapturedErrorEvent).error
onError(new Error(error.message))
})
const view = new View({
center: [0, 0],
zoom: 1,
projection: "EPSG:4326",
})
const frontLayer = new TileLayer({ source: new TerrainTile(terrain) })
const backLayer = new TileLayer({ source: new TerrainTile(terrain) })
const fullScreen = new FullScreen()
const map = new Map({
controls: defaultControls().extend([
fullScreen,
]),
target: "map",
layers: [
frontLayer,
backLayer,
],
view,
})
fullScreen.on("enterfullscreen", () => {
map.getTargetElement().classList.add("full")
})
fullScreen.on("leavefullscreen", () => {
map.getTargetElement().classList.remove("full")
})
const getViewHeight = () => {
const [ x0, y0, x1, y1 ] = view.getViewStateAndExtent().extent
const [w, h] = [ x1-x0, y1-y0 ]
return Math.min(w / 2, h)
}
const setViewHeight =
(h: number, [x, y]: number[]) => {
const w = h * 2
view.fit([
x-w/2,
y-h/2,
x+w/2,
y+h/2,
])
}
const pushState = () => {
history.pushState(
{},
"",
terrain.state + "/" +
[...view.getCenter()!, getViewHeight()]
.map(x => x.toFixed(2))
.join("/")
)
}
const pullState = () => {
terrain.state = location.hash
let [_seed, _mul, _add, _dd1, _dd2, _powa, _pow,
x, y, h] = location.hash.split("/").map(Number)
x ||= 0
y ||= 0
h ||= 180
view.setCenter([x, y])
setViewHeight(h, [x, y])
}
pullState()
const debounce =
(t: number) =>
<T extends unknown[]>
(f: (...args: T) => void) => {
let timer: number | undefined = undefined
return (...args: T) => {
clearTimeout(timer)
timer = setTimeout(() => f(...args), t)
}
}
map.on("moveend", debounce(300)(pushState))
const refresh = () => {
backLayer.setSource(new TerrainTile(terrain))
const src = backLayer.getSource()!
src.on("tileloadend", () => {
frontLayer.setSource(src)
})
}
const slider =
(name: "DD1" | "DD2" | "POWA" | "POW" | "MUL" | "ADD" | "SEED",
min: number, max: number, isInt = false) =>
html`
<v style="height: 27px;">
<h>
<i><b>${name.padEnd(4, "\u00a0")}</b></i>
${isInt
? "\u00a0"
: terrain[name].toFixed(2)
.padStart(5, "\u00a0")
.slice(0, -terrain[name].toFixed(2).length)
}
<input
type="number"
step=${isInt ? 1 : 0.01}
min=${min}
max=${max}
name=${name}
.value=${isInt
? terrain[name].toString()
.padStart(5, "0")
: terrain[name].toFixed(2)
}
@input=${(e: InputEvent) => {
const el = e.target as HTMLInputElement
if (isInt) {
el.value = Number(el.value).toString()
.padStart(5, "0")
}
if (el.value.length > 5) {
el.value = el.value.slice(0, 5)
}
}}
@change=${(e: InputEvent) => {
const el = e.target as HTMLInputElement
u(v => {
if (!(v < min || max < v))
terrain[name] = v
})(e)
el.value = isInt
? terrain[name].toString()
.padStart(5, "0")
: terrain[name].toFixed(2)
console.log(el.value)
pushState()
}}
style=${styleMap({
width: `${isInt
? 7
: terrain[name].toFixed(2).length + 3
}ch`
})}
/>
</h>
<input
type="range"
step=${isInt ? 1 : 0.01}
min=${min}
max=${max}
.value=${terrain[name]}
@input=${u(v => terrain[name] = v)}
@change=${pushState}
/>
<v/>
`
const u =
(f?: (v: number) => void) =>
(e?: InputEvent) => {
f && f(Number((e!.target as HTMLInputElement).value)!)
refresh()
render(html`
<h gap-20>
<v fill gap-10>
<h2 style="height: 27px;"><i>Tweak</i></h2>
${slider("SEED", 0, 99999, true)}
</v>
<v fill gap-10>
${slider("MUL", 0, 10)}
${slider("ADD", -2, 2)}
</v>
</h>
<h gap-20>
<v fill gap-10>
${slider("DD1", -1, 1)}
${slider("POWA", 0, 10)}
</v>
<v fill gap-10>
${slider("DD2", -1, 1)}
${slider("POW", 0, 10)}
</v>
</h>
`, document.body.querySelector("tweak") as HTMLElement)
}
u()()
addEventListener("hashchange", () => {
pullState()
u()()
})
} catch(e) {
if (e instanceof Error) {
onError(e)
} else {
onError(new Error(String(e)))
}
}