-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.js
More file actions
542 lines (527 loc) · 23.6 KB
/
World.js
File metadata and controls
542 lines (527 loc) · 23.6 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import Chunk from "./Chunk.js";
import { Block, LongID } from "./Block.js";
import Player from "./Player.js";
import { vec3, radian2degree } from "./math.js";
import { PerlinNoise } from "./noise.js";
import { FluidCalculator } from "./WorldFluidCal.js";
import { ChunksLightCalculation } from "./WorldLight.js";
import { EventDispatcher } from "./EventDispatcher.js";
import { settings } from "./settings.js";
class WorldStorage {
constructor(id) {
this.id = id;
let worlds = this._getWorlds();
if (!(id in worlds)) worlds[id] = {
createAt: Date.now(),
modifyAt: Date.now(),
};
this._setWorlds(worlds);
};
_getWorlds() { return JSON.parse(localStorage.getItem("worlds") || "{}"); };
_setWorlds(data) { localStorage.setItem("worlds", JSON.stringify(data)); };
_updateWorld(fn) {
let worlds = this._getWorlds();
let ans = fn(worlds[this.id]);
worlds[this.id].modifyAt = Date.now();
this._setWorlds(worlds);
return ans;
};
get(key, defaultValue) {
key = key.split(">");
return this._updateWorld(world => {
let p = world;
for (let k of key)
if (!(p = p[k]))
return defaultValue;
return p;
});
};
set(key, value) {
key = key.split(">");
return this._updateWorld(world => {
let lp = null, p = world;
for (let i = 0; i < key.length; ++i) {
let k = key[i];
lp = p; p = p[k];
if (i != key.length - 1) {
if (!p) lp[k] = p = {};
}
else return lp[k] = value;
}
});
};
del(key) {
key = key.split(">");
return this._updateWorld(world => {
let lp = null, p = world;
for (let i = 0; i < key.length; ++i) {
let k = key[i];
lp = p; p = p[k];
if (!p) return;
if (i == key.length - 1)
return delete lp[k];
}
});
};
};
class World extends EventDispatcher {
constructor({
worldName = "My World",
worldType = "pre-classic",
renderer = null,
seed = Date.now(),
storageId = null,
} = {}) {
super();
if (seed === "") seed = Date.now();
if (worldName === "") worldName = "My World";
if (!(["flat", "pre-classic"].includes(worldType))) {
console.warn("Undefined world type: " + worldType);
worldType = "pre-classic";
}
if (storageId != null) {
this.storager = new WorldStorage(storageId);
seed = this.storager.get("seed");
worldName = this.storager.get("name");
worldType = this.storager.get("type");
this.seed = seed;
this.noise = new PerlinNoise(seed);
}
else {
this.seed = seed;
this.noise = new PerlinNoise(seed);
storageId = Date.now() + "-" + this.noise.seed;
this.storager = new WorldStorage(storageId);
this.storager.set("name", worldName);
this.storager.set("type", worldType);
this.storager.set("seed", seed);
}
this.name = worldName;
this.type = worldType;
this.chunkMap = {};
this.callbacks = {};
let entities = this.storager.get("entities", []);
if (entities.length) {
this.entities = entities.map(entity => {
switch (entity.type) {
case "Player": return Player.from(entity).setWorld(this);
// case "Entity": return Entity.from(entity).setWorld(this);
}
});
let mainPlayerUid = this.storager.get("mainPlayer");
this.mainPlayer = this.entities.find(ent => ent.uid == mainPlayerUid);
}
else {
this.mainPlayer = new Player(this);
this.entities = [this.mainPlayer];
this.saveEntities();
this.storager.set("mainPlayer", this.mainPlayer.uid);
}
this.renderer = renderer;
for (let x = -2; x <= 2; ++x)
for (let z = -2; z <= 2; ++z)
for (let y = 2; y >= -2; --y)
this.loadChunk(x, y, z);
this.fluidCalculator = new FluidCalculator(this);
this.lightingCalculator = new ChunksLightCalculation(this);
this.setRenderer(renderer);
this.tickTimerId = null;
this.lastTickTimestamp = performance.now();
this.beforeTick();
};
saveEntities() {
this.storager.set("entities", this.entities.map(ent => ent.toObj()));
};
generator = (chunkX, chunkY, chunkZ, tileMap) => {
switch(this.type) {
case "flat":
let block = Block.getBlockByBlockName(chunkY >= 0? "air":
chunkX%2? chunkZ%2? "grass": "stone"
: chunkZ%2? "stone": "grass");
for (let i = 0; i < tileMap.length; ++i)
tileMap[i] = block.longID;
break;
case "pre-classic": {
const {X_SIZE, Y_SIZE, Z_SIZE} = Chunk;
const noise = this.noise, fn = noise.gen2d.bind(noise);
let air = Block.getBlockByBlockName("air"),
stone = Block.getBlockByBlockName(
chunkY%2
? chunkX%2
? chunkZ%2? "grass": "stone"
: chunkZ%2? "stone": "grass"
: chunkX%2
? chunkZ%2? "stone": "grass"
: chunkZ%2? "grass": "stone"
);
let fn3 = noise.gen3d.bind(noise);
let elevations = [];
for (let x = 0; x < X_SIZE; ++x)
for (let z = 0; z < Z_SIZE; ++z) {
let i = chunkX * X_SIZE + x, k = chunkZ * Z_SIZE + z;
let elevation = (fn(i/200,k/200)+fn(i/50,k/50)/2+fn(i/10,k/10)/64)/2;
elevation = Math.floor(elevation * 128);
elevations[x] = elevations[x] || [];
elevations[x][z] = elevation;
for (let y = 0; y < Y_SIZE; ++y) {
let j = chunkY * Y_SIZE + y;
if (j < elevation) {
let n3 = (fn3(i / 16, j / 16, k / 16));
let n33 = (fn3(i/256,j/256,k/256) +fn3(i/128,j/128,k/128)/2 + fn3(i/64, j/64, k/64) / 4+fn3(i/25,j/25,k/25)/8);
let vein = (fn3(i / 5, j / 5, k / 5) + 1) / 2;
tileMap[Chunk.getLinearBlockIndex(x, y, z)] =
(vein < 0.18? air: n33 > 0 && n3 < -0.1? air: stone).longID;
}
else {
elevations.haveSurface = elevations.haveSurface || j === elevation;
tileMap[Chunk.getLinearBlockIndex(x, y, z)] = air.longID;
}
}
}
let treeNoise = [], R = 5;
for (let x = -R - 2; x < X_SIZE + R + 2; ++x)
for (let z = -R - 2; z < Z_SIZE + R + 2; ++z) {
let i = chunkX * X_SIZE + x, k = chunkZ * Z_SIZE + z;
treeNoise[x] = treeNoise[x] || [];
treeNoise[x][z] = (fn(k/10, i/10)+1)/2 + fn(k/5, i/5)/2;
}
let treePlacement = [], haveTreeAround = [];
for (let x = -2; x < X_SIZE + 2; ++x)
for (let z = -2; z < Z_SIZE + 2; ++z) {
treePlacement[x] = treePlacement[x] || [];
let max = -10;
for (let a = -R; a <= R; ++a)
for (let b = -R; b <= R; ++b) {
let t = treeNoise[x + a][z + b];
if (t > max) max = t;
}
treePlacement[x][z] = treeNoise[x][z] === max;
}
let afterEle = [];
for (let x = 0; x < X_SIZE; ++x)
for (let z = 0, y; z < Z_SIZE; ++z) {
afterEle[x] = afterEle[x] || [];
if (elevations[x][z] >= chunkY * Y_SIZE && elevations[x][z] < (chunkY + 1) * Y_SIZE) {
for (y = Chunk.getRelativeBlockXYZ(0, elevations[x][z], 0)[1]; y > 0; --y)
if (Block.getBlockByBlockLongID(tileMap[Chunk.getLinearBlockIndex(x, y - 1, z)]).name !== "air") {
elevations[x][z] = chunkY * Y_SIZE + y;
break;
}
if (y == -1) elevations[x][z] = chunkY * Y_SIZE;
}
}
for (let x = 0; x < X_SIZE; ++x)
for (let z = 0; z < Z_SIZE; ++z) {
let f = true;
for (let a = -2; a <= 2 && f; ++a)
for (let b = -2; b <= 2 && f; ++b) {
if (treePlacement[x + a][z + b]) f = false;
}
haveTreeAround[x] = haveTreeAround[x] || [];
haveTreeAround[x][z] = !f;
}
for (let x = 0; x < X_SIZE; ++x)
for (let z = 0; z < Z_SIZE; ++z)
for (let y = Y_SIZE - 1; y >= 0; --y) {
if (Block.getBlockByBlockLongID(tileMap[Chunk.getLinearBlockIndex(x, y, z)]).name !== "air")
break;
let elevation = elevations[x][z];
let j = chunkY * Y_SIZE + y;
let flowerPlacement = treeNoise[x][z] < 0.15;
if (treePlacement[x][z] && j - elevation < 5 && elevations.haveSurface)
tileMap[Chunk.getLinearBlockIndex(x, y, z)] = Block.getBlockByBlockName("oak_log").longID;
else if (j - elevation < 7 && j - elevation > 3 && haveTreeAround[x][z] !== false)
tileMap[Chunk.getLinearBlockIndex(x, y, z)] = Block.getBlockByBlockName("oak_leaves").longID;
else if (flowerPlacement && j <= elevation && y > 0 && elevations.haveSurface
&& Block.getBlockByBlockLongID(tileMap[Chunk.getLinearBlockIndex(x, y - 1, z)]).name !== "air")
tileMap[Chunk.getLinearBlockIndex(x, y, z)] = Block.getBlockByBlockName("dandelion").longID;
}
break;}
}
let ck = Chunk.chunkKeyByChunkXYZ(chunkX, chunkY, chunkZ);
let data = this.storager.get("chunks>" + ck, {});
for (let lb in data) {
tileMap[lb] = new LongID(data[lb]);
}
};
setRenderer(renderer = null) {
if (renderer === this.renderer) return this;
const lastRenderer = this.renderer;
this.renderer = renderer;
if (lastRenderer) {
lastRenderer.setWorld();
for (let ck in this.chunkMap) {
this.chunkMap[ck].setRenderer();
}
}
if (!renderer) return this;
for (let ck in this.chunkMap) {
this.chunkMap[ck].setRenderer(renderer);
}
return this;
};
getChunkByChunkKey(chunkKey) {
return this.chunkMap[chunkKey] || null;
};
getChunkByChunkXYZ(chunkX, chunkY, chunkZ) {
return this.getChunkByChunkKey(Chunk.chunkKeyByChunkXYZ(chunkX, chunkY, chunkZ));
};
getChunkByBlockXYZ(blockX, blockY, blockZ) {
return this.getChunkByChunkKey(Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ));
};
loadChunk(chunkX, chunkY, chunkZ) {
let ck = Chunk.chunkKeyByChunkXYZ(chunkX, chunkY, chunkZ),
chunk = this.chunkMap[ck];
if (chunk) return chunk;
chunk = this.chunkMap[ck] = new Chunk(this, chunkX, chunkY, chunkZ);
this.dispatchEvent("onChunkLoad", chunk);
return chunk;
};
getTile(blockX, blockY, blockZ) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) return c.getTile(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ));
return null;
};
setTile(blockX, blockY, blockZ, id, bd) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) {
let t = c.setTile(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ), id, bd);
this.dispatchEvent("onTileChanges", blockX, blockY, blockZ);
return t;
}
return null;
};
getBlock(blockX, blockY, blockZ) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) return c.getBlock(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ));
return null;
};
setBlock(blockX, blockY, blockZ, blockName) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) {
let rxyz = Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ);
let t = c.setBlock(...rxyz, blockName);
let data = this.storager.get("chunks>" + c.chunkKey, {});
data[Chunk.getLinearBlockIndex(...rxyz)] = this.getTile(blockX, blockY, blockZ);
this.storager.set("chunks>" + c.chunkKey, data);
this.saveEntities();
this.dispatchEvent("onTileChanges", blockX, blockY, blockZ);
return t;
}
return null;
};
getLight(blockX, blockY, blockZ) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) return c.getLight(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ));
return null;
};
getSkylight(blockX, blockY, blockZ) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) return c.getSkylight(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ));
return null;
};
getTorchlight(blockX, blockY, blockZ) {
[blockX, blockY, blockZ] = [blockX, blockY, blockZ].map(Math.floor);
let c = this.chunkMap[Chunk.chunkKeyByBlockXYZ(blockX, blockY, blockZ)];
if (c) return c.getTorchlight(...Chunk.getRelativeBlockXYZ(blockX, blockY, blockZ));
return null;
};
// 每一帧触发 由renderer的requestAnimationFrame触发
onRender(timestamp, dt) {
for (let ck in this.chunkMap) {
this.chunkMap[ck].onRender(timestamp, dt);
}
this.fluidCalculator.onRender(timestamp, dt);
this.lightingCalculator.onRender(timestamp, dt);
this.entities.forEach(e => e.onRender(timestamp, dt));
const {mainPlayer} = this;
if (settings.showDebugOutput) {
const hit = mainPlayer.controller.getHitting?.() ?? null;
let block = hit? this.getBlock(...hit.blockPos): null;
let longID = hit? this.getTile(...hit.blockPos): null;
let chunk = this.getChunkByBlockXYZ(...[...mainPlayer.position].map(n => n < 0? n - 1: n));
if (!this.fpss) this.fpss = [];
this.fpss.push(dt);
if (this.fpss.length > 15) this.fpss.shift();
document.getElementsByTagName("mcpage-play")[0].debugOutput.style.display = null;
document.getElementsByTagName("mcpage-play")[0].debugOutput.innerHTML = Object.entries({
"FPS: ": (1000 / (this.fpss.reduce((n, i) => n + i, 0) / this.fpss.length)).toFixed(2),
"Player:": [
"XYZ: " + [...mainPlayer.position].map(n => n.toFixed(1)).join(", "),
`Pitch: ${radian2degree(mainPlayer.pitch).toFixed(2)}°, Yaw: ${Math.abs(radian2degree(mainPlayer.yaw) * 100 % 36000 / 100).toFixed(2)}°`,
`Chunk: ${chunk? Chunk.getRelativeBlockXYZ(...mainPlayer.position).map(n => ~~n).join(" ") + " in " + [chunk.x, chunk.y, chunk.z].join(" "): "null"}`,
`Light: ${this.getLight(...mainPlayer.position)} (${this.getSkylight(...mainPlayer.position)} sky, ${this.getTorchlight(...mainPlayer.position)} block)`,
],
"Crosshairs:": [
`XYZ: ${hit? hit.blockPos.join(" "): "null"} (${
hit? Chunk.getRelativeBlockXYZ(...hit.blockPos).join(" "): "null"
}/${
hit? Chunk.getLinearBlockIndex(...Chunk.getRelativeBlockXYZ(...hit.blockPos)): "null"
} in ${
hit? Chunk.getChunkXYZByBlockXYZ(...hit.blockPos).join(" "): "null"
}, ${ hit? hit.axis? hit.axis: "in block": "null" })`,
`Block: ${block? block.name: "null"} (${longID?.id ?? "null"}, ${longID?.bd ?? "null"}, ${longID? longID: "null"})`,
`Face: ${
block?.renderType === Block.renderType.FLOWER? "flower face": hit?.axis ?? "null"
}, light: ${
hit? this.getLight(...vec3.add(hit.blockPos, {"x+": [1,0,0], "x-": [-1,0,0], "y+": [0,1,0], "y-": [0,-1,0], "z+": [0,0,1], "z-": [0,0,-1]}[hit.axis] || [0,0,0], [])): "null"
} (${
hit? this.getSkylight(...vec3.add(hit.blockPos, {"x+": [1,0,0], "x-": [-1,0,0], "y+": [0,1,0], "y-": [0,-1,0], "z+": [0,0,1], "z-": [0,0,-1]}[hit.axis] || [0,0,0], [])): "null"
} sky, ${
hit? this.getTorchlight(...vec3.add(hit.blockPos, {"x+": [1,0,0], "x-": [-1,0,0], "y+": [0,1,0], "y-": [0,-1,0], "z+": [0,0,1], "z-": [0,0,-1]}[hit.axis] || [0,0,0], [])): "null"
} block)`,
],
}).map(([k, v]) => `<p>${k}${
Array.isArray(v)
? v.map(str => `<p>\t${str}</p>`).join("")
: v
}</p>`).join("");
}
else {
document.getElementsByTagName("mcpage-play")[0].debugOutput.style.display = "none";
}
let cxyz = Chunk.getChunkXYZByBlockXYZ(...mainPlayer.position),
[cx, cy, cz] = cxyz;
if (vec3.exactEquals(cxyz, mainPlayer.lastChunk || []))
for (let dx = -1; dx <= 1; ++dx)
for (let dz = -1; dz <= 1; ++dz)
for (let dy = 1; dy >= -1; --dy)
this.loadChunk(cx + dx, cy + dy, cz + dz);
mainPlayer.lastChunk = cxyz;
};
// 游戏刻 每秒触发20次 目的是分离渲染事件和游戏事件 为未来的多线程做准备
onTick() {
for (let ck in this.chunkMap) {
this.chunkMap[ck].onTick();
}
this.fluidCalculator.onTick();
this.lightingCalculator.onTick();
this.entities.forEach(e => e.onTick());
// 每 20s 存一次实体信息
if (Date.now() - (this.lastEntitiesSaveTime || 0) > 20_000) {
this.lastEntitiesSaveTime = Date.now();
this.saveEntities();
}
};
// 记录每一次的时间戳 调整下一次游戏刻的时间间隔 并执行回调
// 原始的mc实现中是通过渲染帧来调用游戏刻 但由于未来多线程中这两个回调是在不同线程中 而线程间通讯是有时间成本的
// 调用上和渲染帧无关,可以看作是乱序执行的,因此事件有可能会有一帧的延迟显示
beforeTick = () => {
let now = performance.now(), dt = now - this.lastTickTimestamp;
if (dt <= 50) {
this.lastTickTimestamp = now;
this.tickTimerId = setTimeout(this.beforeTick, 50);
}
else {
this.lastTickTimestamp += 50;
this.tickTimerId = setTimeout(this.beforeTick, 0);
}
if (this.renderer?.isPlaying()) {
this.onTick();
this.dispatchEvent("tick");
}
};
// return null->uncollision else -> { axis->("x+-y+-z+-": collision face, "": in block, b)lockPos}
rayTraceBlock(start, end, chunkFn) {
if (start.some(Number.isNaN) || end.some(Number.isNaN) || vec3.equals(start, end))
return null;
if (chunkFn(...start.map(Math.floor))) return {
axis: "", blockPos: start.map(Math.floor)
};
let vec = vec3.subtract(end, start, end),
len = vec3.length(vec),
delta = vec3.create(),
axisStepDir = vec3.create(),
blockPos = vec3.create(),
nextWay = vec3.create();
vec.forEach((dir, axis) => {
let d = delta[axis] = len / Math.abs(dir);
axisStepDir[axis] = dir < 0? -1: 1;
blockPos[axis] = dir > 0? Math.ceil(start[axis]) - 1: Math.floor(start[axis]);
nextWay[axis] = d === Infinity? Infinity :d * (dir > 0
? Math.ceil(start[axis]) - start[axis]
: start[axis] - Math.floor(start[axis]));
});
for (let way = 0, axis; way <= len;) {
axis = nextWay[0] < nextWay[1] && nextWay[0] < nextWay[2]
? 0 : nextWay[1] < nextWay[2]? 1: 2;
way = nextWay[axis];
if (way > len) break;
blockPos[axis] += axisStepDir[axis];
if (chunkFn(...blockPos))
return {
axis: "xyz"[axis] + (axisStepDir[axis] > 0? '-': '+'),
blockPos
};
nextWay[axis] += delta[axis];
}
return null;
};
// 向大佬低头
// from: https://github.com/guckstift/BlockWeb/blob/master/src/boxcast.js
hitboxesCollision(box, vec, chunkFn) {
let len = vec3.length(vec);
if(len === 0) return null;
let boxmin = box.min, boxmax = box.max,
lead = vec3.create(),
leadvox = vec3.create(),
trailvox = vec3.create(),
step = vec.map(n => n > 0? 1: -1),
waydelta = vec.map(n => len / Math.abs(n)),
waynext = vec3.create();
for(let k = 0, distnext, trail; k < 3; k ++) {
if(vec[k] > 0) {
lead[k] = boxmax[k];
trail = boxmin[k];
trailvox[k] = Math.floor(trail);
leadvox[k] = Math.ceil(lead[k]) - 1;
distnext = Math.ceil(lead[k]) - lead[k];
}
else {
lead[k] = boxmin[k];
trail = boxmax[k];
trailvox[k] = Math.ceil(trail) - 1;
leadvox[k] = Math.floor(lead[k]);
distnext = lead[k] - Math.floor(lead[k]);
}
waynext[k] = waydelta[k] === Infinity? Infinity: waydelta[k] * distnext;
}
for (let way = 0, axis; way <= len; ) {
axis = waynext[1] < waynext[0] && waynext[1] < waynext[2]
? 1: waynext[0] < waynext[2]? 0: 2;
// axis = waynext[0] < waynext[1] && waynext[0] < waynext[2]
// ? 0: waynext[1] < waynext[2]? 1: 2;
way = waynext[axis];
if (way > len) break;
waynext[axis] += waydelta[axis];
leadvox[axis] += step[axis];
trailvox[axis] += step[axis];
let [stepx, stepy, stepz] = step,
xs = axis === 0? leadvox[0]: trailvox[0],
ys = axis === 1? leadvox[1]: trailvox[1],
zs = axis === 2? leadvox[2]: trailvox[2],
[xe, ye, ze] = vec3.add(leadvox, step),
x, y, z;
for(x = xs; x !== xe; x += stepx)
for(y = ys; y !== ye; y += stepy)
for(z = zs; z !== ze; z += stepz)
if(chunkFn(x, y, z)) return {
axis: axis,
step: step[axis],
pos: lead[axis] + way * (vec[axis] / len),
};
}
return null;
};
dispose() {};
};
export {
World as default,
World,
};