@celljs/ta-voxel 4.30.0 → 4.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/esm/voxel-blocks.d.ts +18 -0
- package/esm/voxel-blocks.js +45 -1
- package/esm/voxel-body.d.ts +7 -0
- package/esm/voxel-body.js +33 -8
- package/esm/voxel-chunks.d.ts +1 -1
- package/esm/voxel-chunks.js +6 -4
- package/esm/voxel-controls.d.ts +0 -4
- package/esm/voxel-controls.js +1 -18
- package/esm/voxel-mesher.d.ts +5 -2
- package/esm/voxel-mesher.js +33 -15
- package/esm/voxel-three.d.ts +16 -6
- package/esm/voxel-three.js +43 -28
- package/lib/voxel-blocks.d.ts +18 -0
- package/lib/voxel-blocks.js +46 -2
- package/lib/voxel-body.d.ts +7 -0
- package/lib/voxel-body.js +33 -8
- package/lib/voxel-chunks.d.ts +1 -1
- package/lib/voxel-chunks.js +6 -4
- package/lib/voxel-controls.d.ts +0 -4
- package/lib/voxel-controls.js +1 -18
- package/lib/voxel-mesher.d.ts +5 -2
- package/lib/voxel-mesher.js +33 -15
- package/lib/voxel-three.d.ts +16 -6
- package/lib/voxel-three.js +44 -29
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
# Cell - Ta Voxel
|
|
2
2
|
|
|
3
|
-
A small engine of blocks for Ta's extensions (a plain library, not a pi package), on the Three.js of `pi.ui.Game3D`: kinds of blocks (`VoxelBlocks`), chunks of 16 × 16 columns as high as their blocks (`VoxelStore`), meshes of the faces that show with ambient occlusion (`VoxelMesher`), a body that walks, runs, jumps, flies, swims and goes up a step by itself (`VoxelBody`), the block aimed at (`VoxelRay`), the meshes of Three.js (`VoxelMeshes`) and the controls of the first person for keys, a mouse and a touch screen (`VoxelControls`). Block Earth (`ta-earth`) is its first user.
|
|
3
|
+
A small engine of blocks for Ta's extensions (a plain library, not a pi package), on the Three.js of `pi.ui.Game3D`: kinds of blocks (`VoxelBlocks`), chunks of 16 × 16 columns as high as their blocks (`VoxelStore`), meshes of the faces that show with ambient occlusion (`VoxelMesher`), a body that walks, runs, jumps, flies, swims, climbs a ladder (forward against its wall up, back down) and goes up a step by itself (`VoxelBody`), the block aimed at (`VoxelRay`), the meshes of Three.js (`VoxelMeshes`) and the controls of the first person for keys, a mouse and a touch screen (`VoxelControls`). Block Earth (`ta-earth`) is its first user.
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
- **方块登记**(`VoxelBlocks`):每种方块的颜色、实心、透明、液体、自发光。
|
|
6
6
|
- **分块存储**(`VoxelStore`):16 × 16 列一块,高度按用到的取 8 的倍数,平地一块约 2 KB。
|
|
7
7
|
- **网格**(`VoxelMesher`):只画露出来的面,角落环境光遮蔽,分实心、发光、透明、水四部分;改一块只重做它所在的块。
|
|
8
|
-
- **身体**(`VoxelBody
|
|
8
|
+
- **身体**(`VoxelBody`):走、跑、跳、飞、游泳、爬梯子(顶着墙往前就往上,往后退就下去),按轴碰撞,一格台阶自动走上去。
|
|
9
9
|
- **射线**(`VoxelRay`):准星对着的方块和面。
|
|
10
10
|
- **Three.js 网格**(`VoxelMeshes`)与**第一人称操作**(`VoxelControls`):键鼠(WASD、指针锁定,锁定被拒时拖动看)和手机(左半屏摇杆、右半屏看,按钮由页面自己放)。
|
|
11
11
|
|
package/esm/voxel-blocks.d.ts
CHANGED
|
@@ -11,7 +11,19 @@ export interface BlockLook {
|
|
|
11
11
|
glow?: boolean;
|
|
12
12
|
/** How it is drawn: the grain of a block, or flat. */
|
|
13
13
|
flat?: boolean;
|
|
14
|
+
/** The tile of its faces (VoxelBlocks.tile), and of its top, bottom and sides if they differ; none, the grain. */
|
|
15
|
+
tile?: string;
|
|
16
|
+
top?: string;
|
|
17
|
+
bottom?: string;
|
|
18
|
+
side?: string;
|
|
19
|
+
/** The colour of its sides, if not its own (grass: the tile of its sides is coloured itself, so white). */
|
|
20
|
+
sideColor?: number;
|
|
14
21
|
}
|
|
22
|
+
/** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
|
|
23
|
+
export declare const TILE = 16;
|
|
24
|
+
export declare const TILE_PAD = 2;
|
|
25
|
+
export declare const TILE_CELL: number;
|
|
26
|
+
export declare const TILE_COLUMNS = 16;
|
|
15
27
|
export declare const AIR = 0;
|
|
16
28
|
export declare class VoxelBlocks {
|
|
17
29
|
readonly looks: BlockLook[];
|
|
@@ -19,6 +31,12 @@ export declare class VoxelBlocks {
|
|
|
19
31
|
/** By id, for the mesher: drawn as solid (hides what is behind it). */
|
|
20
32
|
opaque: Uint8Array<ArrayBuffer>;
|
|
21
33
|
solid: Uint8Array<ArrayBuffer>;
|
|
34
|
+
/** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
|
|
35
|
+
readonly tiles: Uint8Array[];
|
|
36
|
+
readonly faceTile: Uint8Array<ArrayBuffer>;
|
|
37
|
+
protected readonly tileNames: Map<string, number>;
|
|
38
|
+
/** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
|
|
39
|
+
tile(name: string, pixels: Uint8Array): number;
|
|
22
40
|
/** A kind of block, its id; the same name again is the same id. */
|
|
23
41
|
define(name: string, look: BlockLook): number;
|
|
24
42
|
id(name: string): number | undefined;
|
package/esm/voxel-blocks.js
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
// The kinds of blocks of a world (VoxelBlocks): what each looks like and how it is met. A world defines its own;
|
|
2
|
-
// the id of a kind is a byte of the chunks, 0 being air. The engine reads only these looks.
|
|
2
|
+
// the id of a kind is a byte of the chunks, 0 being air. The engine reads only these looks. Each face is drawn with a
|
|
3
|
+
// tile of 16 × 16 pixels the world paints (bark, bricks, blades of grass), mostly grey, tinted by the block's colour;
|
|
4
|
+
// one without, the grain.
|
|
5
|
+
/** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
|
|
6
|
+
export const TILE = 16;
|
|
7
|
+
export const TILE_PAD = 2;
|
|
8
|
+
export const TILE_CELL = TILE + TILE_PAD * 2;
|
|
9
|
+
export const TILE_COLUMNS = 16;
|
|
10
|
+
/** A grain of grey pixels, the same every time: a block of 16 × 16, RGBA. */
|
|
11
|
+
function grain() {
|
|
12
|
+
const data = new Uint8Array(TILE * TILE * 4);
|
|
13
|
+
let seed = 11;
|
|
14
|
+
const random = () => {
|
|
15
|
+
seed = (seed * 16807) % 2147483647;
|
|
16
|
+
return seed / 2147483647;
|
|
17
|
+
};
|
|
18
|
+
for (let i = 0; i < TILE * TILE; i++) {
|
|
19
|
+
const x = i % TILE;
|
|
20
|
+
const y = Math.floor(i / TILE);
|
|
21
|
+
const edge = x === 0 || y === 0 ? -14 : 0;
|
|
22
|
+
const value = Math.max(0, Math.min(255, 232 + Math.floor(random() * 22) + edge - (random() < 0.06 ? 22 : 0)));
|
|
23
|
+
data.set([value, value, value, 255], i * 4);
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
3
27
|
export const AIR = 0;
|
|
4
28
|
export class VoxelBlocks {
|
|
5
29
|
constructor() {
|
|
@@ -8,9 +32,24 @@ export class VoxelBlocks {
|
|
|
8
32
|
/** By id, for the mesher: drawn as solid (hides what is behind it). */
|
|
9
33
|
this.opaque = new Uint8Array(256);
|
|
10
34
|
this.solid = new Uint8Array(256);
|
|
35
|
+
/** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
|
|
36
|
+
this.tiles = [grain()];
|
|
37
|
+
this.faceTile = new Uint8Array(256 * 6);
|
|
38
|
+
this.tileNames = new Map([['grain', 0]]);
|
|
39
|
+
}
|
|
40
|
+
/** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
|
|
41
|
+
tile(name, pixels) {
|
|
42
|
+
const known = this.tileNames.get(name);
|
|
43
|
+
if (known !== undefined) {
|
|
44
|
+
return known;
|
|
45
|
+
}
|
|
46
|
+
this.tiles.push(pixels);
|
|
47
|
+
this.tileNames.set(name, this.tiles.length - 1);
|
|
48
|
+
return this.tiles.length - 1;
|
|
11
49
|
}
|
|
12
50
|
/** A kind of block, its id; the same name again is the same id. */
|
|
13
51
|
define(name, look) {
|
|
52
|
+
var _a, _b, _c, _d;
|
|
14
53
|
const known = this.names.get(name);
|
|
15
54
|
if (known !== undefined) {
|
|
16
55
|
return known;
|
|
@@ -23,6 +62,11 @@ export class VoxelBlocks {
|
|
|
23
62
|
this.names.set(name, id);
|
|
24
63
|
this.opaque[id] = look.solid && !look.clear ? 1 : 0;
|
|
25
64
|
this.solid[id] = look.solid ? 1 : 0;
|
|
65
|
+
// Faces in the mesher's order: up, down, then the four sides.
|
|
66
|
+
for (let f = 0; f < 6; f++) {
|
|
67
|
+
const name = f === 0 ? ((_a = look.top) !== null && _a !== void 0 ? _a : look.tile) : f === 1 ? ((_b = look.bottom) !== null && _b !== void 0 ? _b : look.tile) : ((_c = look.side) !== null && _c !== void 0 ? _c : look.tile);
|
|
68
|
+
this.faceTile[id * 6 + f] = name ? ((_d = this.tileNames.get(name)) !== null && _d !== void 0 ? _d : 0) : 0;
|
|
69
|
+
}
|
|
26
70
|
return id;
|
|
27
71
|
}
|
|
28
72
|
id(name) {
|
package/esm/voxel-body.d.ts
CHANGED
|
@@ -8,10 +8,14 @@ export interface BodyInput {
|
|
|
8
8
|
/** Down while flying (Shift). */
|
|
9
9
|
down: boolean;
|
|
10
10
|
run: boolean;
|
|
11
|
+
/** Flying, how many times as fast as it flies by itself (1 if none). */
|
|
12
|
+
boost?: number;
|
|
11
13
|
}
|
|
12
14
|
export interface BodyWorld {
|
|
13
15
|
solid(x: number, y: number, z: number): boolean;
|
|
14
16
|
liquid(x: number, y: number, z: number): boolean;
|
|
17
|
+
/** A ladder: what one climbs, not solid. */
|
|
18
|
+
climb?(x: number, y: number, z: number): boolean;
|
|
15
19
|
}
|
|
16
20
|
export declare class VoxelBody {
|
|
17
21
|
x: number;
|
|
@@ -22,6 +26,9 @@ export declare class VoxelBody {
|
|
|
22
26
|
vz: number;
|
|
23
27
|
onGround: boolean;
|
|
24
28
|
inWater: boolean;
|
|
29
|
+
climbing: boolean;
|
|
30
|
+
/** Walking into something last time: on a ladder, up it. */
|
|
31
|
+
protected pushing: boolean;
|
|
25
32
|
flying: boolean;
|
|
26
33
|
/** How far it just went up a step: the camera eases it. */
|
|
27
34
|
stepped: number;
|
package/esm/voxel-body.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// A body in a world of blocks (VoxelBody): a box of 0.6 × 1.8 that walks, runs, jumps, flies and swims, stopped by
|
|
2
2
|
// solid blocks one axis at a time; walking into a step of one block it goes up it by itself (on a phone one cannot
|
|
3
|
-
// jump and walk with one thumb); it
|
|
3
|
+
// jump and walk with one thumb); on a ladder it holds on, forward against the wall it is on (or jump) goes up and
|
|
4
|
+
// back goes down, walking past it does not; it speeds up and slows down in about a sixth of a second.
|
|
4
5
|
const HALF = 0.3;
|
|
5
6
|
const HEIGHT = 1.8;
|
|
6
7
|
const GRAVITY = 30;
|
|
@@ -9,6 +10,7 @@ const WALK = 4.4;
|
|
|
9
10
|
const RUN = 6.8;
|
|
10
11
|
const FLY = 12;
|
|
11
12
|
const SWIM = 2.6;
|
|
13
|
+
const CLIMB = 3.4;
|
|
12
14
|
/** How quickly it gets to the speed asked: a sixth of a second. */
|
|
13
15
|
const EASE = 6;
|
|
14
16
|
const STEP = 1 / 60;
|
|
@@ -22,6 +24,9 @@ export class VoxelBody {
|
|
|
22
24
|
this.vz = 0;
|
|
23
25
|
this.onGround = false;
|
|
24
26
|
this.inWater = false;
|
|
27
|
+
this.climbing = false;
|
|
28
|
+
/** Walking into something last time: on a ladder, up it. */
|
|
29
|
+
this.pushing = false;
|
|
25
30
|
this.flying = false;
|
|
26
31
|
/** How far it just went up a step: the camera eases it. */
|
|
27
32
|
this.stepped = 0;
|
|
@@ -36,21 +41,30 @@ export class VoxelBody {
|
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
43
|
tick(dt, input, world) {
|
|
39
|
-
|
|
40
|
-
const
|
|
44
|
+
var _a;
|
|
45
|
+
const bx = Math.floor(this.x);
|
|
46
|
+
const bz = Math.floor(this.z);
|
|
47
|
+
this.inWater = world.liquid(bx, Math.floor(this.y + 0.4), bz);
|
|
48
|
+
this.climbing = !this.flying && !!world.climb && (world.climb(bx, Math.floor(this.y + 0.2), bz) || world.climb(bx, Math.floor(this.y + 1.2), bz));
|
|
49
|
+
const speed = this.flying ? FLY * (input.run ? 1.6 : 1) * ((_a = input.boost) !== null && _a !== void 0 ? _a : 1) : this.inWater ? SWIM : input.run ? RUN : WALK;
|
|
41
50
|
const sin = Math.sin(input.yaw);
|
|
42
51
|
const cos = Math.cos(input.yaw);
|
|
43
52
|
const length = Math.min(1, Math.hypot(input.forward, input.strafe));
|
|
44
53
|
const scale = length > 0 ? length / Math.hypot(input.forward, input.strafe) : 0;
|
|
45
|
-
//
|
|
54
|
+
// As the camera turns by yaw: forward is (-sin, -cos), the right (cos, -sin).
|
|
46
55
|
const wantX = (input.strafe * cos - input.forward * sin) * scale * speed;
|
|
47
|
-
const wantZ = (input.strafe * sin
|
|
48
|
-
const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater ? 3 : 1) * dt);
|
|
56
|
+
const wantZ = (-input.strafe * sin - input.forward * cos) * scale * speed;
|
|
57
|
+
const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater || this.climbing ? 3 : 1) * dt);
|
|
49
58
|
this.vx += (wantX - this.vx) * ease;
|
|
50
59
|
this.vz += (wantZ - this.vz) * ease;
|
|
51
60
|
if (this.flying) {
|
|
52
61
|
const wantY = (input.jump ? 1 : 0) - (input.down ? 1 : 0);
|
|
53
|
-
this.vy += (wantY * speed
|
|
62
|
+
this.vy += (wantY * speed - this.vy) * ease;
|
|
63
|
+
}
|
|
64
|
+
else if (this.climbing) {
|
|
65
|
+
const up = input.jump || (input.forward > 0.3 && this.pushing);
|
|
66
|
+
const down = input.down || input.forward < -0.3;
|
|
67
|
+
this.vy = up ? CLIMB : down ? -CLIMB : 0;
|
|
54
68
|
}
|
|
55
69
|
else if (this.inWater) {
|
|
56
70
|
this.vy -= GRAVITY * 0.25 * dt;
|
|
@@ -68,7 +82,8 @@ export class VoxelBody {
|
|
|
68
82
|
const blockedX = this.move(0, this.vx * dt, world);
|
|
69
83
|
const blockedZ = this.move(2, this.vz * dt, world);
|
|
70
84
|
// A step of one block in the way: up it, if there is room above.
|
|
71
|
-
|
|
85
|
+
this.pushing = blockedX || blockedZ;
|
|
86
|
+
if ((blockedX || blockedZ) && (wasOnGround || this.inWater || this.climbing) && !this.flying) {
|
|
72
87
|
const up = this.y + 1.01;
|
|
73
88
|
if (!this.hits(this.x + Math.sign(this.vx) * 0.05, up, this.z + Math.sign(this.vz) * 0.05, world) && !this.hits(this.x, up, this.z, world)) {
|
|
74
89
|
const before = this.y;
|
|
@@ -118,6 +133,16 @@ export class VoxelBody {
|
|
|
118
133
|
if (distance === 0) {
|
|
119
134
|
return false;
|
|
120
135
|
}
|
|
136
|
+
// Fast (flying boosted), in pieces of less than a block, so as not to go through a wall.
|
|
137
|
+
if (Math.abs(distance) > 0.9) {
|
|
138
|
+
const pieces = Math.ceil(Math.abs(distance) / 0.9);
|
|
139
|
+
for (let i = 0; i < pieces; i++) {
|
|
140
|
+
if (this.move(axis, distance / pieces, world)) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
121
146
|
const now = [this.x, this.y, this.z];
|
|
122
147
|
const wanted = [...now];
|
|
123
148
|
wanted[axis] += distance;
|
package/esm/voxel-chunks.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class VoxelChunk {
|
|
|
16
16
|
protected grow(height: number): void;
|
|
17
17
|
}
|
|
18
18
|
export declare namespace VoxelKey {
|
|
19
|
-
/** A chunk by its coordinates, as one number (cz below 2^
|
|
19
|
+
/** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
|
|
20
20
|
function of(cx: number, cz: number): number;
|
|
21
21
|
function parts(key: number): {
|
|
22
22
|
cx: number;
|
package/esm/voxel-chunks.js
CHANGED
|
@@ -48,16 +48,18 @@ export class VoxelChunk {
|
|
|
48
48
|
this.height = next;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
+
/** Chunks of the earth across: its 2^25 columns, 16 a chunk. */
|
|
52
|
+
const SPAN = 2097152;
|
|
51
53
|
export var VoxelKey;
|
|
52
54
|
(function (VoxelKey) {
|
|
53
|
-
/** A chunk by its coordinates, as one number (cz below 2^
|
|
55
|
+
/** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
|
|
54
56
|
function of(cx, cz) {
|
|
55
|
-
return cx *
|
|
57
|
+
return cx * SPAN + cz;
|
|
56
58
|
}
|
|
57
59
|
VoxelKey.of = of;
|
|
58
60
|
function parts(key) {
|
|
59
|
-
const cz = key %
|
|
60
|
-
return { cx: (key - cz) /
|
|
61
|
+
const cz = key % SPAN;
|
|
62
|
+
return { cx: (key - cz) / SPAN, cz };
|
|
61
63
|
}
|
|
62
64
|
VoxelKey.parts = parts;
|
|
63
65
|
})(VoxelKey || (VoxelKey = {}));
|
package/esm/voxel-controls.d.ts
CHANGED
|
@@ -32,11 +32,8 @@ export declare class VoxelControls {
|
|
|
32
32
|
x: number;
|
|
33
33
|
y: number;
|
|
34
34
|
}>;
|
|
35
|
-
protected lastJump: number;
|
|
36
35
|
/** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
|
|
37
36
|
protected lockRefused: boolean;
|
|
38
|
-
/** Twice the jump: the game flies, or lands. */
|
|
39
|
-
onDoubleJump?: () => void;
|
|
40
37
|
protected readonly off: (() => void)[];
|
|
41
38
|
constructor(element: HTMLElement, hooks?: ControlHooks);
|
|
42
39
|
get locked(): boolean;
|
|
@@ -59,7 +56,6 @@ export declare class VoxelControls {
|
|
|
59
56
|
z: number;
|
|
60
57
|
};
|
|
61
58
|
dispose(): void;
|
|
62
|
-
protected jumped(): void;
|
|
63
59
|
protected keyDown(event: KeyboardEvent): void;
|
|
64
60
|
protected keyUp(event: KeyboardEvent): void;
|
|
65
61
|
protected mouseDown(event: MouseEvent): void;
|
package/esm/voxel-controls.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The controls of the first person (VoxelControls), on the element of the game: with keys WASD or the arrows walk,
|
|
2
|
-
// Shift runs (down while flying), Space jumps (
|
|
2
|
+
// Shift runs (down while flying), Space jumps (up while flying), a click locks the mouse to look (Esc frees it), the left
|
|
3
3
|
// button and the right are the game's (break and place), the wheel and 1–9 its bar. On a touch screen the left half is
|
|
4
4
|
// a stick where the thumb comes down, the right half looks as it is dragged (a tap there does nothing); the buttons
|
|
5
5
|
// of the game are its own elements over it, which call `press`.
|
|
@@ -7,8 +7,6 @@ const LOOK = 0.0024;
|
|
|
7
7
|
const TOUCH_LOOK = 0.0055;
|
|
8
8
|
/** How far the stick goes, in CSS pixels. */
|
|
9
9
|
const STICK = 52;
|
|
10
|
-
/** Two jumps within this time: flying. */
|
|
11
|
-
const DOUBLE = 300;
|
|
12
10
|
export class VoxelControls {
|
|
13
11
|
constructor(element, hooks = {}) {
|
|
14
12
|
this.element = element;
|
|
@@ -19,7 +17,6 @@ export class VoxelControls {
|
|
|
19
17
|
this.run = false;
|
|
20
18
|
this.keys = new Set();
|
|
21
19
|
this.touches = new Map();
|
|
22
|
-
this.lastJump = 0;
|
|
23
20
|
/** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
|
|
24
21
|
this.lockRefused = false;
|
|
25
22
|
this.off = [];
|
|
@@ -71,9 +68,6 @@ export class VoxelControls {
|
|
|
71
68
|
press(action, down) {
|
|
72
69
|
var _a, _b;
|
|
73
70
|
if (down) {
|
|
74
|
-
if (action === 'jump') {
|
|
75
|
-
this.jumped();
|
|
76
|
-
}
|
|
77
71
|
this.held.add(action);
|
|
78
72
|
}
|
|
79
73
|
else {
|
|
@@ -107,17 +101,6 @@ export class VoxelControls {
|
|
|
107
101
|
document.exitPointerLock();
|
|
108
102
|
}
|
|
109
103
|
}
|
|
110
|
-
jumped() {
|
|
111
|
-
var _a;
|
|
112
|
-
const now = Date.now();
|
|
113
|
-
if (now - this.lastJump < DOUBLE) {
|
|
114
|
-
(_a = this.onDoubleJump) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
115
|
-
this.lastJump = 0;
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
this.lastJump = now;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
104
|
keyDown(event) {
|
|
122
105
|
var _a, _b, _c, _d;
|
|
123
106
|
if (event.metaKey || event.ctrlKey || event.altKey) {
|
package/esm/voxel-mesher.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type VoxelBlocks } from './voxel-blocks.js';
|
|
2
2
|
import { type VoxelChunk, type VoxelStore } from './voxel-chunks.js';
|
|
3
3
|
export interface MeshPart {
|
|
4
4
|
positions: Float32Array;
|
|
5
|
+
/** Which way each corner's face looks, for a world lit by lights (VoxelMeshes `lit`). */
|
|
6
|
+
normals: Float32Array;
|
|
5
7
|
colors: Float32Array;
|
|
6
8
|
uvs: Float32Array;
|
|
7
9
|
indices: Uint32Array;
|
|
@@ -16,5 +18,6 @@ export interface ChunkMesh {
|
|
|
16
18
|
faces: number;
|
|
17
19
|
}
|
|
18
20
|
export declare namespace VoxelMesher {
|
|
19
|
-
|
|
21
|
+
/** `shade`: darken each side by which way it faces, for a world without lights (by default). */
|
|
22
|
+
function mesh(store: VoxelStore, chunk: VoxelChunk, blocks: VoxelBlocks, shade?: boolean): ChunkMesh;
|
|
20
23
|
}
|
package/esm/voxel-mesher.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TILE, TILE_CELL, TILE_COLUMNS, TILE_PAD } from './voxel-blocks.js';
|
|
1
2
|
import { CHUNK } from './voxel-chunks.js';
|
|
2
3
|
/** How light each side is: up, down, east and west, south and north. */
|
|
3
4
|
const SHADE = [1, 0.55, 0.82, 0.82, 0.7, 0.7];
|
|
@@ -15,6 +16,7 @@ const FACES = [
|
|
|
15
16
|
class Builder {
|
|
16
17
|
constructor() {
|
|
17
18
|
this.positions = [];
|
|
19
|
+
this.normals = [];
|
|
18
20
|
this.colors = [];
|
|
19
21
|
this.uvs = [];
|
|
20
22
|
this.indices = [];
|
|
@@ -22,6 +24,7 @@ class Builder {
|
|
|
22
24
|
part() {
|
|
23
25
|
return {
|
|
24
26
|
positions: new Float32Array(this.positions),
|
|
27
|
+
normals: new Float32Array(this.normals),
|
|
25
28
|
colors: new Float32Array(this.colors),
|
|
26
29
|
uvs: new Float32Array(this.uvs),
|
|
27
30
|
indices: new Uint32Array(this.indices),
|
|
@@ -36,15 +39,19 @@ function hash(x, y, z) {
|
|
|
36
39
|
}
|
|
37
40
|
export var VoxelMesher;
|
|
38
41
|
(function (VoxelMesher) {
|
|
39
|
-
|
|
42
|
+
/** `shade`: darken each side by which way it faces, for a world without lights (by default). */
|
|
43
|
+
function mesh(store, chunk, blocks, shade = true) {
|
|
40
44
|
const solid = new Builder();
|
|
41
45
|
const glow = new Builder();
|
|
42
46
|
const clear = new Builder();
|
|
43
47
|
const liquid = new Builder();
|
|
44
|
-
const { looks, opaque } = blocks;
|
|
48
|
+
const { looks, opaque, faceTile } = blocks;
|
|
49
|
+
const rows = Math.ceil(blocks.tiles.length / TILE_COLUMNS);
|
|
50
|
+
const width = TILE_COLUMNS * TILE_CELL;
|
|
51
|
+
const height = rows * TILE_CELL;
|
|
45
52
|
const ox = chunk.cx * CHUNK;
|
|
46
53
|
const oz = chunk.cz * CHUNK;
|
|
47
|
-
const { data, height } = chunk;
|
|
54
|
+
const { data, height: tall } = chunk;
|
|
48
55
|
/** The block beside, in this chunk or the next; what is not loaded counts as solid (no face). */
|
|
49
56
|
const at = (x, y, z) => {
|
|
50
57
|
var _a;
|
|
@@ -52,13 +59,13 @@ export var VoxelMesher;
|
|
|
52
59
|
return 1;
|
|
53
60
|
}
|
|
54
61
|
if (x >= 0 && x < CHUNK && z >= 0 && z < CHUNK) {
|
|
55
|
-
return y <
|
|
62
|
+
return y < tall ? data[(y * CHUNK + z) * CHUNK + x] : 0;
|
|
56
63
|
}
|
|
57
64
|
return (_a = store.get(ox + x, y, oz + z)) !== null && _a !== void 0 ? _a : -1;
|
|
58
65
|
};
|
|
59
66
|
const hides = (id) => id === -1 || opaque[id] === 1;
|
|
60
67
|
let faces = 0;
|
|
61
|
-
for (let y = 0; y <
|
|
68
|
+
for (let y = 0; y < tall; y++) {
|
|
62
69
|
for (let z = 0; z < CHUNK; z++) {
|
|
63
70
|
for (let x = 0; x < CHUNK; x++) {
|
|
64
71
|
const id = data[(y * CHUNK + z) * CHUNK + x];
|
|
@@ -79,17 +86,25 @@ export var VoxelMesher;
|
|
|
79
86
|
continue;
|
|
80
87
|
}
|
|
81
88
|
faces++;
|
|
89
|
+
const side = f > 1 && look.sideColor !== undefined;
|
|
90
|
+
const fr = side ? ((look.sideColor >> 16) & 0xff) / 255 : r;
|
|
91
|
+
const fg = side ? ((look.sideColor >> 8) & 0xff) / 255 : g;
|
|
92
|
+
const fb = side ? (look.sideColor & 0xff) / 255 : b;
|
|
93
|
+
const tile = faceTile[id * 6 + f];
|
|
94
|
+
const left = (tile % TILE_COLUMNS) * TILE_CELL + TILE_PAD;
|
|
95
|
+
const top = Math.floor(tile / TILE_COLUMNS) * TILE_CELL + TILE_PAD;
|
|
82
96
|
const base = out.positions.length / 3;
|
|
83
97
|
// The corner of the face at (0, 0) of u and v: on the side of the normal.
|
|
84
98
|
const cx = x + (n[0] > 0 ? 1 : 0);
|
|
85
99
|
const cy = y + (n[1] > 0 ? 1 : 0) - (look.liquid && f === 0 && next === 0 ? 0.12 : 0);
|
|
86
100
|
const cz = z + (n[2] > 0 ? 1 : 0);
|
|
87
|
-
const
|
|
101
|
+
const corners = [];
|
|
88
102
|
for (let k = 0; k < 4; k++) {
|
|
89
103
|
const su = k === 1 || k === 2 ? 1 : 0;
|
|
90
104
|
const sv = k >= 2 ? 1 : 0;
|
|
91
105
|
out.positions.push(cx + u[0] * su + v[0] * sv, cy + u[1] * su + v[1] * sv, cz + u[2] * su + v[2] * sv);
|
|
92
|
-
|
|
106
|
+
out.normals.push(n[0], n[1], n[2]);
|
|
107
|
+
let light = shade ? SHADE[f] : 1;
|
|
93
108
|
if (!look.glow && !look.clear) {
|
|
94
109
|
// The three blocks beside this corner, outside the face.
|
|
95
110
|
const du = su ? 1 : -1;
|
|
@@ -101,17 +116,20 @@ export var VoxelMesher;
|
|
|
101
116
|
const side2 = hides(at(px + v[0] * dv, py + v[1] * dv, pz + v[2] * dv)) ? 1 : 0;
|
|
102
117
|
const corner = hides(at(px + u[0] * du + v[0] * dv, py + u[1] * du + v[1] * dv, pz + u[2] * du + v[2] * dv)) ? 1 : 0;
|
|
103
118
|
const ao = side1 && side2 ? 0 : 3 - side1 - side2 - corner;
|
|
104
|
-
|
|
105
|
-
|
|
119
|
+
corners.push(ao);
|
|
120
|
+
light *= AO[ao];
|
|
106
121
|
}
|
|
107
|
-
out.colors.push(
|
|
108
|
-
//
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
|
|
122
|
+
out.colors.push(fr * light, fg * light, fb * light);
|
|
123
|
+
// Across the tile: on the top and bottom along x and z; on a side along it, and up.
|
|
124
|
+
const dx = u[0] * su + v[0] * sv;
|
|
125
|
+
const dy = u[1] * su + v[1] * sv;
|
|
126
|
+
const dz = u[2] * su + v[2] * sv;
|
|
127
|
+
const across = f < 2 ? dx : n[0] !== 0 ? dz : dx;
|
|
128
|
+
const down = f < 2 ? dz : 1 - dy;
|
|
129
|
+
out.uvs.push((left + across * TILE) / width, (top + down * TILE) / height);
|
|
112
130
|
}
|
|
113
131
|
// Split the quad along the diagonal of the lighter corners, so that the shadow does not slant.
|
|
114
|
-
if (
|
|
132
|
+
if (corners.length === 4 && corners[0] + corners[2] < corners[1] + corners[3]) {
|
|
115
133
|
out.indices.push(base + 1, base + 2, base + 3, base + 1, base + 3, base);
|
|
116
134
|
}
|
|
117
135
|
else {
|
package/esm/voxel-three.d.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import type * as T from 'three';
|
|
2
|
+
import { type VoxelBlocks } from './voxel-blocks.js';
|
|
2
3
|
import type { ChunkMesh } from './voxel-mesher.js';
|
|
3
4
|
type Three = typeof T;
|
|
4
|
-
/**
|
|
5
|
-
export declare function
|
|
5
|
+
/** The tiles of the blocks in one texture, each with its edges repeated round it (VoxelMesher maps the faces). */
|
|
6
|
+
export declare function voxelAtlas(THREE: Three, blocks: VoxelBlocks): T.DataTexture;
|
|
6
7
|
export declare class VoxelMeshes {
|
|
7
8
|
protected readonly THREE: Three;
|
|
8
9
|
readonly group: T.Group;
|
|
9
|
-
protected readonly solid: T.MeshBasicMaterial;
|
|
10
|
+
protected readonly solid: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
10
11
|
protected readonly glow: T.MeshBasicMaterial;
|
|
11
|
-
protected readonly clear: T.MeshBasicMaterial;
|
|
12
|
-
protected readonly liquid: T.MeshBasicMaterial;
|
|
12
|
+
protected readonly clear: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
13
|
+
protected readonly liquid: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
14
|
+
protected readonly lit: boolean;
|
|
15
|
+
protected readonly shadows: boolean;
|
|
13
16
|
protected readonly grain: T.DataTexture;
|
|
14
17
|
protected readonly meshes: Map<number, T.Mesh<T.BufferGeometry<T.NormalBufferAttributes, T.BufferGeometryEventMap>, T.Material<T.MaterialEventMap>[] | T.Material<T.MaterialEventMap>, T.Object3DEventMap>[]>;
|
|
15
|
-
|
|
18
|
+
/**
|
|
19
|
+
* `lit`: drawn by the lights of the scene (a sun and the sky) instead of by the colours of the corners alone, the
|
|
20
|
+
* meshes made without the shade of each side (VoxelMesher `shade` false); `shadows`, casting and receiving them.
|
|
21
|
+
*/
|
|
22
|
+
constructor(THREE: Three, blocks: VoxelBlocks, options?: {
|
|
23
|
+
lit?: boolean;
|
|
24
|
+
shadows?: boolean;
|
|
25
|
+
});
|
|
16
26
|
/** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
|
|
17
27
|
set(key: number, x: number, z: number, mesh: ChunkMesh): void;
|
|
18
28
|
remove(key: number): void;
|
package/esm/voxel-three.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
texture
|
|
1
|
+
import { TILE, TILE_CELL, TILE_COLUMNS, TILE_PAD } from './voxel-blocks.js';
|
|
2
|
+
/** The tiles of the blocks in one texture, each with its edges repeated round it (VoxelMesher maps the faces). */
|
|
3
|
+
export function voxelAtlas(THREE, blocks) {
|
|
4
|
+
const rows = Math.ceil(blocks.tiles.length / TILE_COLUMNS);
|
|
5
|
+
const width = TILE_COLUMNS * TILE_CELL;
|
|
6
|
+
const height = rows * TILE_CELL;
|
|
7
|
+
const data = new Uint8Array(width * height * 4);
|
|
8
|
+
blocks.tiles.forEach((pixels, index) => {
|
|
9
|
+
const ox = (index % TILE_COLUMNS) * TILE_CELL;
|
|
10
|
+
const oy = Math.floor(index / TILE_COLUMNS) * TILE_CELL;
|
|
11
|
+
for (let y = 0; y < TILE_CELL; y++) {
|
|
12
|
+
for (let x = 0; x < TILE_CELL; x++) {
|
|
13
|
+
const sx = Math.max(0, Math.min(TILE - 1, x - TILE_PAD));
|
|
14
|
+
const sy = Math.max(0, Math.min(TILE - 1, y - TILE_PAD));
|
|
15
|
+
data.set(pixels.subarray((sy * TILE + sx) * 4, (sy * TILE + sx) * 4 + 4), ((oy + y) * width + ox + x) * 4);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const texture = new THREE.DataTexture(data, width, height);
|
|
20
20
|
texture.magFilter = THREE.NearestFilter;
|
|
21
|
-
// Far away the
|
|
21
|
+
// Far away the tiles blend, so that walls do not shimmer in stripes.
|
|
22
22
|
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
23
23
|
texture.anisotropy = 4;
|
|
24
24
|
texture.generateMipmaps = true;
|
|
@@ -27,15 +27,22 @@ export function voxelGrain(THREE) {
|
|
|
27
27
|
return texture;
|
|
28
28
|
}
|
|
29
29
|
export class VoxelMeshes {
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* `lit`: drawn by the lights of the scene (a sun and the sky) instead of by the colours of the corners alone, the
|
|
32
|
+
* meshes made without the shade of each side (VoxelMesher `shade` false); `shadows`, casting and receiving them.
|
|
33
|
+
*/
|
|
34
|
+
constructor(THREE, blocks, options = {}) {
|
|
31
35
|
this.THREE = THREE;
|
|
32
36
|
this.meshes = new Map();
|
|
33
37
|
this.group = new THREE.Group();
|
|
34
|
-
this.grain =
|
|
35
|
-
this.
|
|
38
|
+
this.grain = voxelAtlas(THREE, blocks);
|
|
39
|
+
this.lit = !!options.lit;
|
|
40
|
+
this.shadows = !!options.shadows;
|
|
41
|
+
const Material = this.lit ? THREE.MeshLambertMaterial : THREE.MeshBasicMaterial;
|
|
42
|
+
this.solid = new Material({ vertexColors: true, map: this.grain });
|
|
36
43
|
this.glow = new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false });
|
|
37
|
-
this.clear = new
|
|
38
|
-
this.liquid = new
|
|
44
|
+
this.clear = new Material({ vertexColors: true, map: this.grain, transparent: true, opacity: 0.55, depthWrite: false });
|
|
45
|
+
this.liquid = new Material({ vertexColors: true, transparent: true, opacity: 0.82, depthWrite: false });
|
|
39
46
|
}
|
|
40
47
|
/** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
|
|
41
48
|
set(key, x, z, mesh) {
|
|
@@ -49,12 +56,17 @@ export class VoxelMeshes {
|
|
|
49
56
|
const geometry = new THREE.BufferGeometry();
|
|
50
57
|
geometry.setAttribute('position', new THREE.BufferAttribute(part.positions, 3));
|
|
51
58
|
geometry.setAttribute('color', new THREE.BufferAttribute(part.colors, 3));
|
|
59
|
+
if (this.lit) {
|
|
60
|
+
geometry.setAttribute('normal', new THREE.BufferAttribute(part.normals, 3));
|
|
61
|
+
}
|
|
52
62
|
geometry.setAttribute('uv', new THREE.BufferAttribute(part.uvs, 2));
|
|
53
63
|
geometry.setIndex(new THREE.BufferAttribute(part.indices, 1));
|
|
54
64
|
geometry.computeBoundingSphere();
|
|
55
65
|
const object = new THREE.Mesh(geometry, material);
|
|
56
66
|
object.position.set(x, 0, z);
|
|
57
67
|
object.renderOrder = order;
|
|
68
|
+
object.castShadow = this.shadows && material === this.solid;
|
|
69
|
+
object.receiveShadow = this.shadows && material !== this.glow;
|
|
58
70
|
object.matrixAutoUpdate = false;
|
|
59
71
|
object.updateMatrix();
|
|
60
72
|
this.group.add(object);
|
|
@@ -85,9 +97,12 @@ export class VoxelMeshes {
|
|
|
85
97
|
* `glow` that of what glows (warmer and brighter at night: more than 1 is brighter than its colour).
|
|
86
98
|
*/
|
|
87
99
|
light(sun, glow) {
|
|
88
|
-
|
|
89
|
-
this.
|
|
90
|
-
|
|
100
|
+
// Lit by the lights of the scene, it is not tinted besides.
|
|
101
|
+
if (!this.lit) {
|
|
102
|
+
this.solid.color.copy(sun);
|
|
103
|
+
this.clear.color.copy(sun);
|
|
104
|
+
this.liquid.color.copy(sun);
|
|
105
|
+
}
|
|
91
106
|
this.glow.color.copy(glow);
|
|
92
107
|
}
|
|
93
108
|
dispose() {
|
package/lib/voxel-blocks.d.ts
CHANGED
|
@@ -11,7 +11,19 @@ export interface BlockLook {
|
|
|
11
11
|
glow?: boolean;
|
|
12
12
|
/** How it is drawn: the grain of a block, or flat. */
|
|
13
13
|
flat?: boolean;
|
|
14
|
+
/** The tile of its faces (VoxelBlocks.tile), and of its top, bottom and sides if they differ; none, the grain. */
|
|
15
|
+
tile?: string;
|
|
16
|
+
top?: string;
|
|
17
|
+
bottom?: string;
|
|
18
|
+
side?: string;
|
|
19
|
+
/** The colour of its sides, if not its own (grass: the tile of its sides is coloured itself, so white). */
|
|
20
|
+
sideColor?: number;
|
|
14
21
|
}
|
|
22
|
+
/** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
|
|
23
|
+
export declare const TILE = 16;
|
|
24
|
+
export declare const TILE_PAD = 2;
|
|
25
|
+
export declare const TILE_CELL: number;
|
|
26
|
+
export declare const TILE_COLUMNS = 16;
|
|
15
27
|
export declare const AIR = 0;
|
|
16
28
|
export declare class VoxelBlocks {
|
|
17
29
|
readonly looks: BlockLook[];
|
|
@@ -19,6 +31,12 @@ export declare class VoxelBlocks {
|
|
|
19
31
|
/** By id, for the mesher: drawn as solid (hides what is behind it). */
|
|
20
32
|
opaque: Uint8Array<ArrayBuffer>;
|
|
21
33
|
solid: Uint8Array<ArrayBuffer>;
|
|
34
|
+
/** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
|
|
35
|
+
readonly tiles: Uint8Array[];
|
|
36
|
+
readonly faceTile: Uint8Array<ArrayBuffer>;
|
|
37
|
+
protected readonly tileNames: Map<string, number>;
|
|
38
|
+
/** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
|
|
39
|
+
tile(name: string, pixels: Uint8Array): number;
|
|
22
40
|
/** A kind of block, its id; the same name again is the same id. */
|
|
23
41
|
define(name: string, look: BlockLook): number;
|
|
24
42
|
id(name: string): number | undefined;
|
package/lib/voxel-blocks.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// The kinds of blocks of a world (VoxelBlocks): what each looks like and how it is met. A world defines its own;
|
|
3
|
-
// the id of a kind is a byte of the chunks, 0 being air. The engine reads only these looks.
|
|
3
|
+
// the id of a kind is a byte of the chunks, 0 being air. The engine reads only these looks. Each face is drawn with a
|
|
4
|
+
// tile of 16 × 16 pixels the world paints (bark, bricks, blades of grass), mostly grey, tinted by the block's colour;
|
|
5
|
+
// one without, the grain.
|
|
4
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.VoxelBlocks = exports.AIR = void 0;
|
|
7
|
+
exports.VoxelBlocks = exports.AIR = exports.TILE_COLUMNS = exports.TILE_CELL = exports.TILE_PAD = exports.TILE = void 0;
|
|
8
|
+
/** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
|
|
9
|
+
exports.TILE = 16;
|
|
10
|
+
exports.TILE_PAD = 2;
|
|
11
|
+
exports.TILE_CELL = exports.TILE + exports.TILE_PAD * 2;
|
|
12
|
+
exports.TILE_COLUMNS = 16;
|
|
13
|
+
/** A grain of grey pixels, the same every time: a block of 16 × 16, RGBA. */
|
|
14
|
+
function grain() {
|
|
15
|
+
const data = new Uint8Array(exports.TILE * exports.TILE * 4);
|
|
16
|
+
let seed = 11;
|
|
17
|
+
const random = () => {
|
|
18
|
+
seed = (seed * 16807) % 2147483647;
|
|
19
|
+
return seed / 2147483647;
|
|
20
|
+
};
|
|
21
|
+
for (let i = 0; i < exports.TILE * exports.TILE; i++) {
|
|
22
|
+
const x = i % exports.TILE;
|
|
23
|
+
const y = Math.floor(i / exports.TILE);
|
|
24
|
+
const edge = x === 0 || y === 0 ? -14 : 0;
|
|
25
|
+
const value = Math.max(0, Math.min(255, 232 + Math.floor(random() * 22) + edge - (random() < 0.06 ? 22 : 0)));
|
|
26
|
+
data.set([value, value, value, 255], i * 4);
|
|
27
|
+
}
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
6
30
|
exports.AIR = 0;
|
|
7
31
|
class VoxelBlocks {
|
|
8
32
|
constructor() {
|
|
@@ -11,9 +35,24 @@ class VoxelBlocks {
|
|
|
11
35
|
/** By id, for the mesher: drawn as solid (hides what is behind it). */
|
|
12
36
|
this.opaque = new Uint8Array(256);
|
|
13
37
|
this.solid = new Uint8Array(256);
|
|
38
|
+
/** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
|
|
39
|
+
this.tiles = [grain()];
|
|
40
|
+
this.faceTile = new Uint8Array(256 * 6);
|
|
41
|
+
this.tileNames = new Map([['grain', 0]]);
|
|
42
|
+
}
|
|
43
|
+
/** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
|
|
44
|
+
tile(name, pixels) {
|
|
45
|
+
const known = this.tileNames.get(name);
|
|
46
|
+
if (known !== undefined) {
|
|
47
|
+
return known;
|
|
48
|
+
}
|
|
49
|
+
this.tiles.push(pixels);
|
|
50
|
+
this.tileNames.set(name, this.tiles.length - 1);
|
|
51
|
+
return this.tiles.length - 1;
|
|
14
52
|
}
|
|
15
53
|
/** A kind of block, its id; the same name again is the same id. */
|
|
16
54
|
define(name, look) {
|
|
55
|
+
var _a, _b, _c, _d;
|
|
17
56
|
const known = this.names.get(name);
|
|
18
57
|
if (known !== undefined) {
|
|
19
58
|
return known;
|
|
@@ -26,6 +65,11 @@ class VoxelBlocks {
|
|
|
26
65
|
this.names.set(name, id);
|
|
27
66
|
this.opaque[id] = look.solid && !look.clear ? 1 : 0;
|
|
28
67
|
this.solid[id] = look.solid ? 1 : 0;
|
|
68
|
+
// Faces in the mesher's order: up, down, then the four sides.
|
|
69
|
+
for (let f = 0; f < 6; f++) {
|
|
70
|
+
const name = f === 0 ? ((_a = look.top) !== null && _a !== void 0 ? _a : look.tile) : f === 1 ? ((_b = look.bottom) !== null && _b !== void 0 ? _b : look.tile) : ((_c = look.side) !== null && _c !== void 0 ? _c : look.tile);
|
|
71
|
+
this.faceTile[id * 6 + f] = name ? ((_d = this.tileNames.get(name)) !== null && _d !== void 0 ? _d : 0) : 0;
|
|
72
|
+
}
|
|
29
73
|
return id;
|
|
30
74
|
}
|
|
31
75
|
id(name) {
|
package/lib/voxel-body.d.ts
CHANGED
|
@@ -8,10 +8,14 @@ export interface BodyInput {
|
|
|
8
8
|
/** Down while flying (Shift). */
|
|
9
9
|
down: boolean;
|
|
10
10
|
run: boolean;
|
|
11
|
+
/** Flying, how many times as fast as it flies by itself (1 if none). */
|
|
12
|
+
boost?: number;
|
|
11
13
|
}
|
|
12
14
|
export interface BodyWorld {
|
|
13
15
|
solid(x: number, y: number, z: number): boolean;
|
|
14
16
|
liquid(x: number, y: number, z: number): boolean;
|
|
17
|
+
/** A ladder: what one climbs, not solid. */
|
|
18
|
+
climb?(x: number, y: number, z: number): boolean;
|
|
15
19
|
}
|
|
16
20
|
export declare class VoxelBody {
|
|
17
21
|
x: number;
|
|
@@ -22,6 +26,9 @@ export declare class VoxelBody {
|
|
|
22
26
|
vz: number;
|
|
23
27
|
onGround: boolean;
|
|
24
28
|
inWater: boolean;
|
|
29
|
+
climbing: boolean;
|
|
30
|
+
/** Walking into something last time: on a ladder, up it. */
|
|
31
|
+
protected pushing: boolean;
|
|
25
32
|
flying: boolean;
|
|
26
33
|
/** How far it just went up a step: the camera eases it. */
|
|
27
34
|
stepped: number;
|
package/lib/voxel-body.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// A body in a world of blocks (VoxelBody): a box of 0.6 × 1.8 that walks, runs, jumps, flies and swims, stopped by
|
|
3
3
|
// solid blocks one axis at a time; walking into a step of one block it goes up it by itself (on a phone one cannot
|
|
4
|
-
// jump and walk with one thumb); it
|
|
4
|
+
// jump and walk with one thumb); on a ladder it holds on, forward against the wall it is on (or jump) goes up and
|
|
5
|
+
// back goes down, walking past it does not; it speeds up and slows down in about a sixth of a second.
|
|
5
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
7
|
exports.VoxelBody = void 0;
|
|
7
8
|
const HALF = 0.3;
|
|
@@ -12,6 +13,7 @@ const WALK = 4.4;
|
|
|
12
13
|
const RUN = 6.8;
|
|
13
14
|
const FLY = 12;
|
|
14
15
|
const SWIM = 2.6;
|
|
16
|
+
const CLIMB = 3.4;
|
|
15
17
|
/** How quickly it gets to the speed asked: a sixth of a second. */
|
|
16
18
|
const EASE = 6;
|
|
17
19
|
const STEP = 1 / 60;
|
|
@@ -25,6 +27,9 @@ class VoxelBody {
|
|
|
25
27
|
this.vz = 0;
|
|
26
28
|
this.onGround = false;
|
|
27
29
|
this.inWater = false;
|
|
30
|
+
this.climbing = false;
|
|
31
|
+
/** Walking into something last time: on a ladder, up it. */
|
|
32
|
+
this.pushing = false;
|
|
28
33
|
this.flying = false;
|
|
29
34
|
/** How far it just went up a step: the camera eases it. */
|
|
30
35
|
this.stepped = 0;
|
|
@@ -39,21 +44,30 @@ class VoxelBody {
|
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
46
|
tick(dt, input, world) {
|
|
42
|
-
|
|
43
|
-
const
|
|
47
|
+
var _a;
|
|
48
|
+
const bx = Math.floor(this.x);
|
|
49
|
+
const bz = Math.floor(this.z);
|
|
50
|
+
this.inWater = world.liquid(bx, Math.floor(this.y + 0.4), bz);
|
|
51
|
+
this.climbing = !this.flying && !!world.climb && (world.climb(bx, Math.floor(this.y + 0.2), bz) || world.climb(bx, Math.floor(this.y + 1.2), bz));
|
|
52
|
+
const speed = this.flying ? FLY * (input.run ? 1.6 : 1) * ((_a = input.boost) !== null && _a !== void 0 ? _a : 1) : this.inWater ? SWIM : input.run ? RUN : WALK;
|
|
44
53
|
const sin = Math.sin(input.yaw);
|
|
45
54
|
const cos = Math.cos(input.yaw);
|
|
46
55
|
const length = Math.min(1, Math.hypot(input.forward, input.strafe));
|
|
47
56
|
const scale = length > 0 ? length / Math.hypot(input.forward, input.strafe) : 0;
|
|
48
|
-
//
|
|
57
|
+
// As the camera turns by yaw: forward is (-sin, -cos), the right (cos, -sin).
|
|
49
58
|
const wantX = (input.strafe * cos - input.forward * sin) * scale * speed;
|
|
50
|
-
const wantZ = (input.strafe * sin
|
|
51
|
-
const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater ? 3 : 1) * dt);
|
|
59
|
+
const wantZ = (-input.strafe * sin - input.forward * cos) * scale * speed;
|
|
60
|
+
const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater || this.climbing ? 3 : 1) * dt);
|
|
52
61
|
this.vx += (wantX - this.vx) * ease;
|
|
53
62
|
this.vz += (wantZ - this.vz) * ease;
|
|
54
63
|
if (this.flying) {
|
|
55
64
|
const wantY = (input.jump ? 1 : 0) - (input.down ? 1 : 0);
|
|
56
|
-
this.vy += (wantY * speed
|
|
65
|
+
this.vy += (wantY * speed - this.vy) * ease;
|
|
66
|
+
}
|
|
67
|
+
else if (this.climbing) {
|
|
68
|
+
const up = input.jump || (input.forward > 0.3 && this.pushing);
|
|
69
|
+
const down = input.down || input.forward < -0.3;
|
|
70
|
+
this.vy = up ? CLIMB : down ? -CLIMB : 0;
|
|
57
71
|
}
|
|
58
72
|
else if (this.inWater) {
|
|
59
73
|
this.vy -= GRAVITY * 0.25 * dt;
|
|
@@ -71,7 +85,8 @@ class VoxelBody {
|
|
|
71
85
|
const blockedX = this.move(0, this.vx * dt, world);
|
|
72
86
|
const blockedZ = this.move(2, this.vz * dt, world);
|
|
73
87
|
// A step of one block in the way: up it, if there is room above.
|
|
74
|
-
|
|
88
|
+
this.pushing = blockedX || blockedZ;
|
|
89
|
+
if ((blockedX || blockedZ) && (wasOnGround || this.inWater || this.climbing) && !this.flying) {
|
|
75
90
|
const up = this.y + 1.01;
|
|
76
91
|
if (!this.hits(this.x + Math.sign(this.vx) * 0.05, up, this.z + Math.sign(this.vz) * 0.05, world) && !this.hits(this.x, up, this.z, world)) {
|
|
77
92
|
const before = this.y;
|
|
@@ -121,6 +136,16 @@ class VoxelBody {
|
|
|
121
136
|
if (distance === 0) {
|
|
122
137
|
return false;
|
|
123
138
|
}
|
|
139
|
+
// Fast (flying boosted), in pieces of less than a block, so as not to go through a wall.
|
|
140
|
+
if (Math.abs(distance) > 0.9) {
|
|
141
|
+
const pieces = Math.ceil(Math.abs(distance) / 0.9);
|
|
142
|
+
for (let i = 0; i < pieces; i++) {
|
|
143
|
+
if (this.move(axis, distance / pieces, world)) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
124
149
|
const now = [this.x, this.y, this.z];
|
|
125
150
|
const wanted = [...now];
|
|
126
151
|
wanted[axis] += distance;
|
package/lib/voxel-chunks.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class VoxelChunk {
|
|
|
16
16
|
protected grow(height: number): void;
|
|
17
17
|
}
|
|
18
18
|
export declare namespace VoxelKey {
|
|
19
|
-
/** A chunk by its coordinates, as one number (cz below 2^
|
|
19
|
+
/** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
|
|
20
20
|
function of(cx: number, cz: number): number;
|
|
21
21
|
function parts(key: number): {
|
|
22
22
|
cx: number;
|
package/lib/voxel-chunks.js
CHANGED
|
@@ -52,16 +52,18 @@ class VoxelChunk {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
exports.VoxelChunk = VoxelChunk;
|
|
55
|
+
/** Chunks of the earth across: its 2^25 columns, 16 a chunk. */
|
|
56
|
+
const SPAN = 2097152;
|
|
55
57
|
var VoxelKey;
|
|
56
58
|
(function (VoxelKey) {
|
|
57
|
-
/** A chunk by its coordinates, as one number (cz below 2^
|
|
59
|
+
/** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
|
|
58
60
|
function of(cx, cz) {
|
|
59
|
-
return cx *
|
|
61
|
+
return cx * SPAN + cz;
|
|
60
62
|
}
|
|
61
63
|
VoxelKey.of = of;
|
|
62
64
|
function parts(key) {
|
|
63
|
-
const cz = key %
|
|
64
|
-
return { cx: (key - cz) /
|
|
65
|
+
const cz = key % SPAN;
|
|
66
|
+
return { cx: (key - cz) / SPAN, cz };
|
|
65
67
|
}
|
|
66
68
|
VoxelKey.parts = parts;
|
|
67
69
|
})(VoxelKey || (exports.VoxelKey = VoxelKey = {}));
|
package/lib/voxel-controls.d.ts
CHANGED
|
@@ -32,11 +32,8 @@ export declare class VoxelControls {
|
|
|
32
32
|
x: number;
|
|
33
33
|
y: number;
|
|
34
34
|
}>;
|
|
35
|
-
protected lastJump: number;
|
|
36
35
|
/** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
|
|
37
36
|
protected lockRefused: boolean;
|
|
38
|
-
/** Twice the jump: the game flies, or lands. */
|
|
39
|
-
onDoubleJump?: () => void;
|
|
40
37
|
protected readonly off: (() => void)[];
|
|
41
38
|
constructor(element: HTMLElement, hooks?: ControlHooks);
|
|
42
39
|
get locked(): boolean;
|
|
@@ -59,7 +56,6 @@ export declare class VoxelControls {
|
|
|
59
56
|
z: number;
|
|
60
57
|
};
|
|
61
58
|
dispose(): void;
|
|
62
|
-
protected jumped(): void;
|
|
63
59
|
protected keyDown(event: KeyboardEvent): void;
|
|
64
60
|
protected keyUp(event: KeyboardEvent): void;
|
|
65
61
|
protected mouseDown(event: MouseEvent): void;
|
package/lib/voxel-controls.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// The controls of the first person (VoxelControls), on the element of the game: with keys WASD or the arrows walk,
|
|
3
|
-
// Shift runs (down while flying), Space jumps (
|
|
3
|
+
// Shift runs (down while flying), Space jumps (up while flying), a click locks the mouse to look (Esc frees it), the left
|
|
4
4
|
// button and the right are the game's (break and place), the wheel and 1–9 its bar. On a touch screen the left half is
|
|
5
5
|
// a stick where the thumb comes down, the right half looks as it is dragged (a tap there does nothing); the buttons
|
|
6
6
|
// of the game are its own elements over it, which call `press`.
|
|
@@ -10,8 +10,6 @@ const LOOK = 0.0024;
|
|
|
10
10
|
const TOUCH_LOOK = 0.0055;
|
|
11
11
|
/** How far the stick goes, in CSS pixels. */
|
|
12
12
|
const STICK = 52;
|
|
13
|
-
/** Two jumps within this time: flying. */
|
|
14
|
-
const DOUBLE = 300;
|
|
15
13
|
class VoxelControls {
|
|
16
14
|
constructor(element, hooks = {}) {
|
|
17
15
|
this.element = element;
|
|
@@ -22,7 +20,6 @@ class VoxelControls {
|
|
|
22
20
|
this.run = false;
|
|
23
21
|
this.keys = new Set();
|
|
24
22
|
this.touches = new Map();
|
|
25
|
-
this.lastJump = 0;
|
|
26
23
|
/** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
|
|
27
24
|
this.lockRefused = false;
|
|
28
25
|
this.off = [];
|
|
@@ -74,9 +71,6 @@ class VoxelControls {
|
|
|
74
71
|
press(action, down) {
|
|
75
72
|
var _a, _b;
|
|
76
73
|
if (down) {
|
|
77
|
-
if (action === 'jump') {
|
|
78
|
-
this.jumped();
|
|
79
|
-
}
|
|
80
74
|
this.held.add(action);
|
|
81
75
|
}
|
|
82
76
|
else {
|
|
@@ -110,17 +104,6 @@ class VoxelControls {
|
|
|
110
104
|
document.exitPointerLock();
|
|
111
105
|
}
|
|
112
106
|
}
|
|
113
|
-
jumped() {
|
|
114
|
-
var _a;
|
|
115
|
-
const now = Date.now();
|
|
116
|
-
if (now - this.lastJump < DOUBLE) {
|
|
117
|
-
(_a = this.onDoubleJump) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
118
|
-
this.lastJump = 0;
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
this.lastJump = now;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
107
|
keyDown(event) {
|
|
125
108
|
var _a, _b, _c, _d;
|
|
126
109
|
if (event.metaKey || event.ctrlKey || event.altKey) {
|
package/lib/voxel-mesher.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type VoxelBlocks } from './voxel-blocks';
|
|
2
2
|
import { type VoxelChunk, type VoxelStore } from './voxel-chunks';
|
|
3
3
|
export interface MeshPart {
|
|
4
4
|
positions: Float32Array;
|
|
5
|
+
/** Which way each corner's face looks, for a world lit by lights (VoxelMeshes `lit`). */
|
|
6
|
+
normals: Float32Array;
|
|
5
7
|
colors: Float32Array;
|
|
6
8
|
uvs: Float32Array;
|
|
7
9
|
indices: Uint32Array;
|
|
@@ -16,6 +18,7 @@ export interface ChunkMesh {
|
|
|
16
18
|
faces: number;
|
|
17
19
|
}
|
|
18
20
|
export declare namespace VoxelMesher {
|
|
19
|
-
|
|
21
|
+
/** `shade`: darken each side by which way it faces, for a world without lights (by default). */
|
|
22
|
+
function mesh(store: VoxelStore, chunk: VoxelChunk, blocks: VoxelBlocks, shade?: boolean): ChunkMesh;
|
|
20
23
|
}
|
|
21
24
|
//# sourceMappingURL=voxel-mesher.d.ts.map
|
package/lib/voxel-mesher.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.VoxelMesher = void 0;
|
|
4
|
+
const voxel_blocks_1 = require("./voxel-blocks");
|
|
4
5
|
const voxel_chunks_1 = require("./voxel-chunks");
|
|
5
6
|
/** How light each side is: up, down, east and west, south and north. */
|
|
6
7
|
const SHADE = [1, 0.55, 0.82, 0.82, 0.7, 0.7];
|
|
@@ -18,6 +19,7 @@ const FACES = [
|
|
|
18
19
|
class Builder {
|
|
19
20
|
constructor() {
|
|
20
21
|
this.positions = [];
|
|
22
|
+
this.normals = [];
|
|
21
23
|
this.colors = [];
|
|
22
24
|
this.uvs = [];
|
|
23
25
|
this.indices = [];
|
|
@@ -25,6 +27,7 @@ class Builder {
|
|
|
25
27
|
part() {
|
|
26
28
|
return {
|
|
27
29
|
positions: new Float32Array(this.positions),
|
|
30
|
+
normals: new Float32Array(this.normals),
|
|
28
31
|
colors: new Float32Array(this.colors),
|
|
29
32
|
uvs: new Float32Array(this.uvs),
|
|
30
33
|
indices: new Uint32Array(this.indices),
|
|
@@ -39,15 +42,19 @@ function hash(x, y, z) {
|
|
|
39
42
|
}
|
|
40
43
|
var VoxelMesher;
|
|
41
44
|
(function (VoxelMesher) {
|
|
42
|
-
|
|
45
|
+
/** `shade`: darken each side by which way it faces, for a world without lights (by default). */
|
|
46
|
+
function mesh(store, chunk, blocks, shade = true) {
|
|
43
47
|
const solid = new Builder();
|
|
44
48
|
const glow = new Builder();
|
|
45
49
|
const clear = new Builder();
|
|
46
50
|
const liquid = new Builder();
|
|
47
|
-
const { looks, opaque } = blocks;
|
|
51
|
+
const { looks, opaque, faceTile } = blocks;
|
|
52
|
+
const rows = Math.ceil(blocks.tiles.length / voxel_blocks_1.TILE_COLUMNS);
|
|
53
|
+
const width = voxel_blocks_1.TILE_COLUMNS * voxel_blocks_1.TILE_CELL;
|
|
54
|
+
const height = rows * voxel_blocks_1.TILE_CELL;
|
|
48
55
|
const ox = chunk.cx * voxel_chunks_1.CHUNK;
|
|
49
56
|
const oz = chunk.cz * voxel_chunks_1.CHUNK;
|
|
50
|
-
const { data, height } = chunk;
|
|
57
|
+
const { data, height: tall } = chunk;
|
|
51
58
|
/** The block beside, in this chunk or the next; what is not loaded counts as solid (no face). */
|
|
52
59
|
const at = (x, y, z) => {
|
|
53
60
|
var _a;
|
|
@@ -55,13 +62,13 @@ var VoxelMesher;
|
|
|
55
62
|
return 1;
|
|
56
63
|
}
|
|
57
64
|
if (x >= 0 && x < voxel_chunks_1.CHUNK && z >= 0 && z < voxel_chunks_1.CHUNK) {
|
|
58
|
-
return y <
|
|
65
|
+
return y < tall ? data[(y * voxel_chunks_1.CHUNK + z) * voxel_chunks_1.CHUNK + x] : 0;
|
|
59
66
|
}
|
|
60
67
|
return (_a = store.get(ox + x, y, oz + z)) !== null && _a !== void 0 ? _a : -1;
|
|
61
68
|
};
|
|
62
69
|
const hides = (id) => id === -1 || opaque[id] === 1;
|
|
63
70
|
let faces = 0;
|
|
64
|
-
for (let y = 0; y <
|
|
71
|
+
for (let y = 0; y < tall; y++) {
|
|
65
72
|
for (let z = 0; z < voxel_chunks_1.CHUNK; z++) {
|
|
66
73
|
for (let x = 0; x < voxel_chunks_1.CHUNK; x++) {
|
|
67
74
|
const id = data[(y * voxel_chunks_1.CHUNK + z) * voxel_chunks_1.CHUNK + x];
|
|
@@ -82,17 +89,25 @@ var VoxelMesher;
|
|
|
82
89
|
continue;
|
|
83
90
|
}
|
|
84
91
|
faces++;
|
|
92
|
+
const side = f > 1 && look.sideColor !== undefined;
|
|
93
|
+
const fr = side ? ((look.sideColor >> 16) & 0xff) / 255 : r;
|
|
94
|
+
const fg = side ? ((look.sideColor >> 8) & 0xff) / 255 : g;
|
|
95
|
+
const fb = side ? (look.sideColor & 0xff) / 255 : b;
|
|
96
|
+
const tile = faceTile[id * 6 + f];
|
|
97
|
+
const left = (tile % voxel_blocks_1.TILE_COLUMNS) * voxel_blocks_1.TILE_CELL + voxel_blocks_1.TILE_PAD;
|
|
98
|
+
const top = Math.floor(tile / voxel_blocks_1.TILE_COLUMNS) * voxel_blocks_1.TILE_CELL + voxel_blocks_1.TILE_PAD;
|
|
85
99
|
const base = out.positions.length / 3;
|
|
86
100
|
// The corner of the face at (0, 0) of u and v: on the side of the normal.
|
|
87
101
|
const cx = x + (n[0] > 0 ? 1 : 0);
|
|
88
102
|
const cy = y + (n[1] > 0 ? 1 : 0) - (look.liquid && f === 0 && next === 0 ? 0.12 : 0);
|
|
89
103
|
const cz = z + (n[2] > 0 ? 1 : 0);
|
|
90
|
-
const
|
|
104
|
+
const corners = [];
|
|
91
105
|
for (let k = 0; k < 4; k++) {
|
|
92
106
|
const su = k === 1 || k === 2 ? 1 : 0;
|
|
93
107
|
const sv = k >= 2 ? 1 : 0;
|
|
94
108
|
out.positions.push(cx + u[0] * su + v[0] * sv, cy + u[1] * su + v[1] * sv, cz + u[2] * su + v[2] * sv);
|
|
95
|
-
|
|
109
|
+
out.normals.push(n[0], n[1], n[2]);
|
|
110
|
+
let light = shade ? SHADE[f] : 1;
|
|
96
111
|
if (!look.glow && !look.clear) {
|
|
97
112
|
// The three blocks beside this corner, outside the face.
|
|
98
113
|
const du = su ? 1 : -1;
|
|
@@ -104,17 +119,20 @@ var VoxelMesher;
|
|
|
104
119
|
const side2 = hides(at(px + v[0] * dv, py + v[1] * dv, pz + v[2] * dv)) ? 1 : 0;
|
|
105
120
|
const corner = hides(at(px + u[0] * du + v[0] * dv, py + u[1] * du + v[1] * dv, pz + u[2] * du + v[2] * dv)) ? 1 : 0;
|
|
106
121
|
const ao = side1 && side2 ? 0 : 3 - side1 - side2 - corner;
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
corners.push(ao);
|
|
123
|
+
light *= AO[ao];
|
|
109
124
|
}
|
|
110
|
-
out.colors.push(
|
|
111
|
-
//
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
125
|
+
out.colors.push(fr * light, fg * light, fb * light);
|
|
126
|
+
// Across the tile: on the top and bottom along x and z; on a side along it, and up.
|
|
127
|
+
const dx = u[0] * su + v[0] * sv;
|
|
128
|
+
const dy = u[1] * su + v[1] * sv;
|
|
129
|
+
const dz = u[2] * su + v[2] * sv;
|
|
130
|
+
const across = f < 2 ? dx : n[0] !== 0 ? dz : dx;
|
|
131
|
+
const down = f < 2 ? dz : 1 - dy;
|
|
132
|
+
out.uvs.push((left + across * voxel_blocks_1.TILE) / width, (top + down * voxel_blocks_1.TILE) / height);
|
|
115
133
|
}
|
|
116
134
|
// Split the quad along the diagonal of the lighter corners, so that the shadow does not slant.
|
|
117
|
-
if (
|
|
135
|
+
if (corners.length === 4 && corners[0] + corners[2] < corners[1] + corners[3]) {
|
|
118
136
|
out.indices.push(base + 1, base + 2, base + 3, base + 1, base + 3, base);
|
|
119
137
|
}
|
|
120
138
|
else {
|
package/lib/voxel-three.d.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import type * as T from 'three';
|
|
2
|
+
import { type VoxelBlocks } from './voxel-blocks';
|
|
2
3
|
import type { ChunkMesh } from './voxel-mesher';
|
|
3
4
|
type Three = typeof T;
|
|
4
|
-
/**
|
|
5
|
-
export declare function
|
|
5
|
+
/** The tiles of the blocks in one texture, each with its edges repeated round it (VoxelMesher maps the faces). */
|
|
6
|
+
export declare function voxelAtlas(THREE: Three, blocks: VoxelBlocks): T.DataTexture;
|
|
6
7
|
export declare class VoxelMeshes {
|
|
7
8
|
protected readonly THREE: Three;
|
|
8
9
|
readonly group: T.Group;
|
|
9
|
-
protected readonly solid: T.MeshBasicMaterial;
|
|
10
|
+
protected readonly solid: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
10
11
|
protected readonly glow: T.MeshBasicMaterial;
|
|
11
|
-
protected readonly clear: T.MeshBasicMaterial;
|
|
12
|
-
protected readonly liquid: T.MeshBasicMaterial;
|
|
12
|
+
protected readonly clear: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
13
|
+
protected readonly liquid: T.MeshBasicMaterial | T.MeshLambertMaterial;
|
|
14
|
+
protected readonly lit: boolean;
|
|
15
|
+
protected readonly shadows: boolean;
|
|
13
16
|
protected readonly grain: T.DataTexture;
|
|
14
17
|
protected readonly meshes: Map<number, T.Mesh<T.BufferGeometry<T.NormalBufferAttributes, T.BufferGeometryEventMap>, T.Material<T.MaterialEventMap>[] | T.Material<T.MaterialEventMap>, T.Object3DEventMap>[]>;
|
|
15
|
-
|
|
18
|
+
/**
|
|
19
|
+
* `lit`: drawn by the lights of the scene (a sun and the sky) instead of by the colours of the corners alone, the
|
|
20
|
+
* meshes made without the shade of each side (VoxelMesher `shade` false); `shadows`, casting and receiving them.
|
|
21
|
+
*/
|
|
22
|
+
constructor(THREE: Three, blocks: VoxelBlocks, options?: {
|
|
23
|
+
lit?: boolean;
|
|
24
|
+
shadows?: boolean;
|
|
25
|
+
});
|
|
16
26
|
/** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
|
|
17
27
|
set(key: number, x: number, z: number, mesh: ChunkMesh): void;
|
|
18
28
|
remove(key: number): void;
|
package/lib/voxel-three.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.VoxelMeshes = void 0;
|
|
4
|
-
exports.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
texture
|
|
4
|
+
exports.voxelAtlas = voxelAtlas;
|
|
5
|
+
const voxel_blocks_1 = require("./voxel-blocks");
|
|
6
|
+
/** The tiles of the blocks in one texture, each with its edges repeated round it (VoxelMesher maps the faces). */
|
|
7
|
+
function voxelAtlas(THREE, blocks) {
|
|
8
|
+
const rows = Math.ceil(blocks.tiles.length / voxel_blocks_1.TILE_COLUMNS);
|
|
9
|
+
const width = voxel_blocks_1.TILE_COLUMNS * voxel_blocks_1.TILE_CELL;
|
|
10
|
+
const height = rows * voxel_blocks_1.TILE_CELL;
|
|
11
|
+
const data = new Uint8Array(width * height * 4);
|
|
12
|
+
blocks.tiles.forEach((pixels, index) => {
|
|
13
|
+
const ox = (index % voxel_blocks_1.TILE_COLUMNS) * voxel_blocks_1.TILE_CELL;
|
|
14
|
+
const oy = Math.floor(index / voxel_blocks_1.TILE_COLUMNS) * voxel_blocks_1.TILE_CELL;
|
|
15
|
+
for (let y = 0; y < voxel_blocks_1.TILE_CELL; y++) {
|
|
16
|
+
for (let x = 0; x < voxel_blocks_1.TILE_CELL; x++) {
|
|
17
|
+
const sx = Math.max(0, Math.min(voxel_blocks_1.TILE - 1, x - voxel_blocks_1.TILE_PAD));
|
|
18
|
+
const sy = Math.max(0, Math.min(voxel_blocks_1.TILE - 1, y - voxel_blocks_1.TILE_PAD));
|
|
19
|
+
data.set(pixels.subarray((sy * voxel_blocks_1.TILE + sx) * 4, (sy * voxel_blocks_1.TILE + sx) * 4 + 4), ((oy + y) * width + ox + x) * 4);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const texture = new THREE.DataTexture(data, width, height);
|
|
24
24
|
texture.magFilter = THREE.NearestFilter;
|
|
25
|
-
// Far away the
|
|
25
|
+
// Far away the tiles blend, so that walls do not shimmer in stripes.
|
|
26
26
|
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
27
27
|
texture.anisotropy = 4;
|
|
28
28
|
texture.generateMipmaps = true;
|
|
@@ -31,15 +31,22 @@ function voxelGrain(THREE) {
|
|
|
31
31
|
return texture;
|
|
32
32
|
}
|
|
33
33
|
class VoxelMeshes {
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* `lit`: drawn by the lights of the scene (a sun and the sky) instead of by the colours of the corners alone, the
|
|
36
|
+
* meshes made without the shade of each side (VoxelMesher `shade` false); `shadows`, casting and receiving them.
|
|
37
|
+
*/
|
|
38
|
+
constructor(THREE, blocks, options = {}) {
|
|
35
39
|
this.THREE = THREE;
|
|
36
40
|
this.meshes = new Map();
|
|
37
41
|
this.group = new THREE.Group();
|
|
38
|
-
this.grain =
|
|
39
|
-
this.
|
|
42
|
+
this.grain = voxelAtlas(THREE, blocks);
|
|
43
|
+
this.lit = !!options.lit;
|
|
44
|
+
this.shadows = !!options.shadows;
|
|
45
|
+
const Material = this.lit ? THREE.MeshLambertMaterial : THREE.MeshBasicMaterial;
|
|
46
|
+
this.solid = new Material({ vertexColors: true, map: this.grain });
|
|
40
47
|
this.glow = new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false });
|
|
41
|
-
this.clear = new
|
|
42
|
-
this.liquid = new
|
|
48
|
+
this.clear = new Material({ vertexColors: true, map: this.grain, transparent: true, opacity: 0.55, depthWrite: false });
|
|
49
|
+
this.liquid = new Material({ vertexColors: true, transparent: true, opacity: 0.82, depthWrite: false });
|
|
43
50
|
}
|
|
44
51
|
/** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
|
|
45
52
|
set(key, x, z, mesh) {
|
|
@@ -53,12 +60,17 @@ class VoxelMeshes {
|
|
|
53
60
|
const geometry = new THREE.BufferGeometry();
|
|
54
61
|
geometry.setAttribute('position', new THREE.BufferAttribute(part.positions, 3));
|
|
55
62
|
geometry.setAttribute('color', new THREE.BufferAttribute(part.colors, 3));
|
|
63
|
+
if (this.lit) {
|
|
64
|
+
geometry.setAttribute('normal', new THREE.BufferAttribute(part.normals, 3));
|
|
65
|
+
}
|
|
56
66
|
geometry.setAttribute('uv', new THREE.BufferAttribute(part.uvs, 2));
|
|
57
67
|
geometry.setIndex(new THREE.BufferAttribute(part.indices, 1));
|
|
58
68
|
geometry.computeBoundingSphere();
|
|
59
69
|
const object = new THREE.Mesh(geometry, material);
|
|
60
70
|
object.position.set(x, 0, z);
|
|
61
71
|
object.renderOrder = order;
|
|
72
|
+
object.castShadow = this.shadows && material === this.solid;
|
|
73
|
+
object.receiveShadow = this.shadows && material !== this.glow;
|
|
62
74
|
object.matrixAutoUpdate = false;
|
|
63
75
|
object.updateMatrix();
|
|
64
76
|
this.group.add(object);
|
|
@@ -89,9 +101,12 @@ class VoxelMeshes {
|
|
|
89
101
|
* `glow` that of what glows (warmer and brighter at night: more than 1 is brighter than its colour).
|
|
90
102
|
*/
|
|
91
103
|
light(sun, glow) {
|
|
92
|
-
|
|
93
|
-
this.
|
|
94
|
-
|
|
104
|
+
// Lit by the lights of the scene, it is not tinted besides.
|
|
105
|
+
if (!this.lit) {
|
|
106
|
+
this.solid.color.copy(sun);
|
|
107
|
+
this.clear.color.copy(sun);
|
|
108
|
+
this.liquid.color.copy(sun);
|
|
109
|
+
}
|
|
95
110
|
this.glow.color.copy(glow);
|
|
96
111
|
}
|
|
97
112
|
dispose() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-voxel",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.31.0",
|
|
4
4
|
"description": "A small engine of blocks for Ta's games: blocks, chunks, meshes with ambient occlusion, a body that walks, jumps, flies and swims, a ray to the block aimed at, the meshes of Three.js and the controls of the first person",
|
|
5
5
|
"homepage": "https://github.com/cellbang/ta",
|
|
6
6
|
"bugs": {
|