@celljs/ta-voxel 0.0.0-stage → 4.30.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.
@@ -0,0 +1,244 @@
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 (twice flies), a click locks the mouse to look (Esc frees it), the left
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
+ // a stick where the thumb comes down, the right half looks as it is dragged (a tap there does nothing); the buttons
5
+ // of the game are its own elements over it, which call `press`.
6
+ const LOOK = 0.0024;
7
+ const TOUCH_LOOK = 0.0055;
8
+ /** How far the stick goes, in CSS pixels. */
9
+ const STICK = 52;
10
+ /** Two jumps within this time: flying. */
11
+ const DOUBLE = 300;
12
+ export class VoxelControls {
13
+ constructor(element, hooks = {}) {
14
+ this.element = element;
15
+ this.hooks = hooks;
16
+ this.yaw = 0;
17
+ this.pitch = 0;
18
+ this.held = new Set();
19
+ this.run = false;
20
+ this.keys = new Set();
21
+ this.touches = new Map();
22
+ this.lastJump = 0;
23
+ /** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
24
+ this.lockRefused = false;
25
+ this.off = [];
26
+ const listen = (target, type, run, options) => {
27
+ target.addEventListener(type, run, options);
28
+ this.off.push(() => target.removeEventListener(type, run, options));
29
+ };
30
+ listen(element, 'keydown', event => this.keyDown(event));
31
+ listen(element, 'keyup', event => this.keyUp(event));
32
+ listen(element, 'blur', () => this.release());
33
+ listen(element, 'mousedown', event => this.mouseDown(event));
34
+ listen(window, 'mouseup', event => this.mouseUp(event));
35
+ listen(element, 'mousemove', event => this.mouseMove(event));
36
+ listen(element, 'contextmenu', event => event.preventDefault());
37
+ const refused = () => {
38
+ this.lockRefused = true;
39
+ };
40
+ document.addEventListener('pointerlockerror', refused);
41
+ this.off.push(() => document.removeEventListener('pointerlockerror', refused));
42
+ listen(element, 'wheel', event => {
43
+ var _a, _b;
44
+ event.preventDefault();
45
+ (_b = (_a = this.hooks).pick) === null || _b === void 0 ? void 0 : _b.call(_a, { step: Math.sign(event.deltaY) });
46
+ }, { passive: false });
47
+ listen(element, 'touchstart', event => this.touchStart(event), { passive: false });
48
+ listen(element, 'touchmove', event => this.touchMove(event), { passive: false });
49
+ listen(element, 'touchend', event => this.touchEnd(event), { passive: false });
50
+ listen(element, 'touchcancel', event => this.touchEnd(event), { passive: false });
51
+ }
52
+ get locked() {
53
+ return typeof document !== 'undefined' && document.pointerLockElement === this.element;
54
+ }
55
+ /** Where to walk: forward and to the right, -1 to 1. */
56
+ movement() {
57
+ const k = this.keys;
58
+ let forward = (k.has('KeyW') || k.has('ArrowUp') ? 1 : 0) - (k.has('KeyS') || k.has('ArrowDown') ? 1 : 0);
59
+ let strafe = (k.has('KeyD') || k.has('ArrowRight') ? 1 : 0) - (k.has('KeyA') || k.has('ArrowLeft') ? 1 : 0);
60
+ if (this.stick) {
61
+ forward = -this.stick.y;
62
+ strafe = this.stick.x;
63
+ }
64
+ return { forward, strafe };
65
+ }
66
+ /** Whether running: Shift held (walking; flying it goes down), or the stick pushed all the way. */
67
+ running() {
68
+ return this.run || this.keys.has('ShiftLeft') || this.keys.has('ShiftRight') || (!!this.stick && Math.hypot(this.stick.x, this.stick.y) > 0.95);
69
+ }
70
+ /** A button of the page pressed or let go. */
71
+ press(action, down) {
72
+ var _a, _b;
73
+ if (down) {
74
+ if (action === 'jump') {
75
+ this.jumped();
76
+ }
77
+ this.held.add(action);
78
+ }
79
+ else {
80
+ this.held.delete(action);
81
+ }
82
+ (_b = (_a = this.hooks).action) === null || _b === void 0 ? void 0 : _b.call(_a, action, down);
83
+ }
84
+ /** Lets go of everything: the page was left, a panel opened. */
85
+ release() {
86
+ for (const action of [...this.held]) {
87
+ this.press(action, false);
88
+ }
89
+ this.keys.clear();
90
+ this.touches.clear();
91
+ this.stick = undefined;
92
+ }
93
+ turn(dx, dy, scale = LOOK) {
94
+ this.yaw -= dx * scale;
95
+ this.pitch = Math.max(-1.5, Math.min(1.5, this.pitch - dy * scale));
96
+ }
97
+ /** The way it looks, as a vector (x east, y up, z south). */
98
+ direction() {
99
+ const cos = Math.cos(this.pitch);
100
+ return { x: -Math.sin(this.yaw) * cos, y: Math.sin(this.pitch), z: -Math.cos(this.yaw) * cos };
101
+ }
102
+ dispose() {
103
+ for (const off of this.off) {
104
+ off();
105
+ }
106
+ if (this.locked) {
107
+ document.exitPointerLock();
108
+ }
109
+ }
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
+ keyDown(event) {
122
+ var _a, _b, _c, _d;
123
+ if (event.metaKey || event.ctrlKey || event.altKey) {
124
+ return;
125
+ }
126
+ const code = event.code;
127
+ if (/^Digit[1-9]$/.test(code)) {
128
+ (_b = (_a = this.hooks).pick) === null || _b === void 0 ? void 0 : _b.call(_a, { slot: Number(code.slice(5)) - 1 });
129
+ return;
130
+ }
131
+ const action = code === 'Space' ? 'jump' : code === 'ShiftLeft' || code === 'ShiftRight' ? 'down' : code === 'KeyE' || code === 'KeyF' ? 'interact' : undefined;
132
+ if (['KeyW', 'KeyA', 'KeyS', 'KeyD', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space', 'ShiftLeft', 'ShiftRight', 'KeyE', 'KeyF'].includes(code)) {
133
+ event.preventDefault();
134
+ if (!event.repeat) {
135
+ this.keys.add(code);
136
+ if (action) {
137
+ this.press(action, true);
138
+ }
139
+ }
140
+ return;
141
+ }
142
+ (_d = (_c = this.hooks).key) === null || _d === void 0 ? void 0 : _d.call(_c, event.key, event);
143
+ }
144
+ keyUp(event) {
145
+ this.keys.delete(event.code);
146
+ const action = event.code === 'Space' ? 'jump' : event.code.startsWith('Shift') ? 'down' : event.code === 'KeyE' || event.code === 'KeyF' ? 'interact' : undefined;
147
+ if (action) {
148
+ this.press(action, false);
149
+ }
150
+ }
151
+ mouseDown(event) {
152
+ var _a;
153
+ this.element.focus();
154
+ // The first click locks the mouse; without the lock (refused, or none) a click acts at once.
155
+ if (!this.locked && !this.lockRefused && typeof this.element.requestPointerLock === 'function') {
156
+ try {
157
+ const lock = this.element.requestPointerLock();
158
+ (_a = lock === null || lock === void 0 ? void 0 : lock.catch) === null || _a === void 0 ? void 0 : _a.call(lock, () => {
159
+ this.lockRefused = true;
160
+ });
161
+ }
162
+ catch (_b) {
163
+ this.lockRefused = true;
164
+ }
165
+ return;
166
+ }
167
+ this.press(event.button === 2 ? 'secondary' : 'primary', true);
168
+ }
169
+ mouseUp(event) {
170
+ const action = event.button === 2 ? 'secondary' : 'primary';
171
+ if (this.held.has(action)) {
172
+ this.press(action, false);
173
+ }
174
+ }
175
+ mouseMove(event) {
176
+ if (this.locked) {
177
+ this.turn(event.movementX, event.movementY);
178
+ }
179
+ else if (event.buttons & 1) {
180
+ this.turn(event.movementX, event.movementY);
181
+ }
182
+ }
183
+ touchStart(event) {
184
+ event.preventDefault();
185
+ const box = this.element.getBoundingClientRect();
186
+ for (const touch of Array.from(event.changedTouches)) {
187
+ const left = touch.clientX - box.left < box.width / 2;
188
+ this.touches.set(touch.identifier, { kind: left && !this.stick ? 'stick' : 'look', x: touch.clientX, y: touch.clientY });
189
+ if (left && !this.stick) {
190
+ this.stick = {
191
+ x: 0,
192
+ y: 0,
193
+ ox: touch.clientX - box.left,
194
+ oy: touch.clientY - box.top,
195
+ px: touch.clientX - box.left,
196
+ py: touch.clientY - box.top,
197
+ };
198
+ }
199
+ }
200
+ }
201
+ touchMove(event) {
202
+ event.preventDefault();
203
+ const box = this.element.getBoundingClientRect();
204
+ for (const touch of Array.from(event.changedTouches)) {
205
+ const known = this.touches.get(touch.identifier);
206
+ if (!known) {
207
+ continue;
208
+ }
209
+ if (known.kind === 'look') {
210
+ this.turn(touch.clientX - known.x, touch.clientY - known.y, TOUCH_LOOK);
211
+ known.x = touch.clientX;
212
+ known.y = touch.clientY;
213
+ }
214
+ else if (this.stick) {
215
+ const px = touch.clientX - box.left;
216
+ const py = touch.clientY - box.top;
217
+ let dx = px - this.stick.ox;
218
+ let dy = py - this.stick.oy;
219
+ const length = Math.hypot(dx, dy);
220
+ // The base follows a thumb that goes past it, so that turning back needs no reach.
221
+ if (length > STICK) {
222
+ this.stick.ox += (dx / length) * (length - STICK);
223
+ this.stick.oy += (dy / length) * (length - STICK);
224
+ dx = px - this.stick.ox;
225
+ dy = py - this.stick.oy;
226
+ }
227
+ this.stick.x = dx / STICK;
228
+ this.stick.y = dy / STICK;
229
+ this.stick.px = px;
230
+ this.stick.py = py;
231
+ }
232
+ }
233
+ }
234
+ touchEnd(event) {
235
+ var _a;
236
+ for (const touch of Array.from(event.changedTouches)) {
237
+ if (((_a = this.touches.get(touch.identifier)) === null || _a === void 0 ? void 0 : _a.kind) === 'stick') {
238
+ this.stick = undefined;
239
+ }
240
+ this.touches.delete(touch.identifier);
241
+ }
242
+ }
243
+ }
244
+ //# sourceMappingURL=voxel-controls.js.map
@@ -0,0 +1,20 @@
1
+ import type { VoxelBlocks } from './voxel-blocks.js';
2
+ import { type VoxelChunk, type VoxelStore } from './voxel-chunks.js';
3
+ export interface MeshPart {
4
+ positions: Float32Array;
5
+ colors: Float32Array;
6
+ uvs: Float32Array;
7
+ indices: Uint32Array;
8
+ }
9
+ export interface ChunkMesh {
10
+ solid: MeshPart;
11
+ glow: MeshPart;
12
+ clear: MeshPart;
13
+ /** Water: seen through less than glass. */
14
+ liquid: MeshPart;
15
+ /** The faces drawn, for the budget of a frame. */
16
+ faces: number;
17
+ }
18
+ export declare namespace VoxelMesher {
19
+ function mesh(store: VoxelStore, chunk: VoxelChunk, blocks: VoxelBlocks): ChunkMesh;
20
+ }
@@ -0,0 +1,128 @@
1
+ import { CHUNK } from './voxel-chunks.js';
2
+ /** How light each side is: up, down, east and west, south and north. */
3
+ const SHADE = [1, 0.55, 0.82, 0.82, 0.7, 0.7];
4
+ /** How light a corner is by how many of its three blocks are there (ambient occlusion). */
5
+ const AO = [0.52, 0.68, 0.84, 1];
6
+ /** The six faces: the normal, then the two axes along the face (u, v), in order for the corners to go round. */
7
+ const FACES = [
8
+ { n: [0, 1, 0], u: [0, 0, 1], v: [1, 0, 0] },
9
+ { n: [0, -1, 0], u: [1, 0, 0], v: [0, 0, 1] },
10
+ { n: [1, 0, 0], u: [0, 1, 0], v: [0, 0, 1] },
11
+ { n: [-1, 0, 0], u: [0, 0, 1], v: [0, 1, 0] },
12
+ { n: [0, 0, 1], u: [1, 0, 0], v: [0, 1, 0] },
13
+ { n: [0, 0, -1], u: [0, 1, 0], v: [1, 0, 0] },
14
+ ];
15
+ class Builder {
16
+ constructor() {
17
+ this.positions = [];
18
+ this.colors = [];
19
+ this.uvs = [];
20
+ this.indices = [];
21
+ }
22
+ part() {
23
+ return {
24
+ positions: new Float32Array(this.positions),
25
+ colors: new Float32Array(this.colors),
26
+ uvs: new Float32Array(this.uvs),
27
+ indices: new Uint32Array(this.indices),
28
+ };
29
+ }
30
+ }
31
+ /** A number in [0, 1) the same for the same block, for the little differences of colour between blocks. */
32
+ function hash(x, y, z) {
33
+ let h = (x * 374761393 + y * 668265263 + z * 2147483647) | 0;
34
+ h = Math.imul(h ^ (h >>> 13), 1274126177);
35
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
36
+ }
37
+ export var VoxelMesher;
38
+ (function (VoxelMesher) {
39
+ function mesh(store, chunk, blocks) {
40
+ const solid = new Builder();
41
+ const glow = new Builder();
42
+ const clear = new Builder();
43
+ const liquid = new Builder();
44
+ const { looks, opaque } = blocks;
45
+ const ox = chunk.cx * CHUNK;
46
+ const oz = chunk.cz * CHUNK;
47
+ const { data, height } = chunk;
48
+ /** The block beside, in this chunk or the next; what is not loaded counts as solid (no face). */
49
+ const at = (x, y, z) => {
50
+ var _a;
51
+ if (y < 0) {
52
+ return 1;
53
+ }
54
+ if (x >= 0 && x < CHUNK && z >= 0 && z < CHUNK) {
55
+ return y < height ? data[(y * CHUNK + z) * CHUNK + x] : 0;
56
+ }
57
+ return (_a = store.get(ox + x, y, oz + z)) !== null && _a !== void 0 ? _a : -1;
58
+ };
59
+ const hides = (id) => id === -1 || opaque[id] === 1;
60
+ let faces = 0;
61
+ for (let y = 0; y < height; y++) {
62
+ for (let z = 0; z < CHUNK; z++) {
63
+ for (let x = 0; x < CHUNK; x++) {
64
+ const id = data[(y * CHUNK + z) * CHUNK + x];
65
+ if (id === 0) {
66
+ continue;
67
+ }
68
+ const look = looks[id];
69
+ const out = look.liquid ? liquid : look.glow ? glow : look.clear ? clear : solid;
70
+ const jitter = look.flat ? 1 : 0.95 + hash(ox + x, y, oz + z) * 0.1;
71
+ const r = (((look.color >> 16) & 0xff) / 255) * jitter;
72
+ const g = (((look.color >> 8) & 0xff) / 255) * jitter;
73
+ const b = ((look.color & 0xff) / 255) * jitter;
74
+ for (let f = 0; f < 6; f++) {
75
+ const { n, u, v } = FACES[f];
76
+ const next = at(x + n[0], y + n[1], z + n[2]);
77
+ // Water shows only to the air; a block, to what does not hide it; glass not to glass.
78
+ if (look.liquid ? next !== 0 : hides(next) || (look.clear && next === id)) {
79
+ continue;
80
+ }
81
+ faces++;
82
+ const base = out.positions.length / 3;
83
+ // The corner of the face at (0, 0) of u and v: on the side of the normal.
84
+ const cx = x + (n[0] > 0 ? 1 : 0);
85
+ const cy = y + (n[1] > 0 ? 1 : 0) - (look.liquid && f === 0 && next === 0 ? 0.12 : 0);
86
+ const cz = z + (n[2] > 0 ? 1 : 0);
87
+ const light = [];
88
+ for (let k = 0; k < 4; k++) {
89
+ const su = k === 1 || k === 2 ? 1 : 0;
90
+ const sv = k >= 2 ? 1 : 0;
91
+ 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
+ let shade = SHADE[f];
93
+ if (!look.glow && !look.clear) {
94
+ // The three blocks beside this corner, outside the face.
95
+ const du = su ? 1 : -1;
96
+ const dv = sv ? 1 : -1;
97
+ const px = x + n[0];
98
+ const py = y + n[1];
99
+ const pz = z + n[2];
100
+ const side1 = hides(at(px + u[0] * du, py + u[1] * du, pz + u[2] * du)) ? 1 : 0;
101
+ const side2 = hides(at(px + v[0] * dv, py + v[1] * dv, pz + v[2] * dv)) ? 1 : 0;
102
+ 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
+ const ao = side1 && side2 ? 0 : 3 - side1 - side2 - corner;
104
+ light.push(ao);
105
+ shade *= AO[ao];
106
+ }
107
+ out.colors.push(r * shade, g * shade, b * shade);
108
+ // The grain where the face is in the world, a block a repeat.
109
+ const wu = u[0] * (ox + cx) + u[1] * cy + u[2] * (oz + cz);
110
+ const wv = v[0] * (ox + cx) + v[1] * cy + v[2] * (oz + cz);
111
+ out.uvs.push(wu + su, wv + sv);
112
+ }
113
+ // Split the quad along the diagonal of the lighter corners, so that the shadow does not slant.
114
+ if (light.length === 4 && light[0] + light[2] < light[1] + light[3]) {
115
+ out.indices.push(base + 1, base + 2, base + 3, base + 1, base + 3, base);
116
+ }
117
+ else {
118
+ out.indices.push(base, base + 1, base + 2, base, base + 2, base + 3);
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ return { solid: solid.part(), glow: glow.part(), clear: clear.part(), liquid: liquid.part(), faces };
125
+ }
126
+ VoxelMesher.mesh = mesh;
127
+ })(VoxelMesher || (VoxelMesher = {}));
128
+ //# sourceMappingURL=voxel-mesher.js.map
@@ -0,0 +1,21 @@
1
+ export interface RayHit {
2
+ x: number;
3
+ y: number;
4
+ z: number;
5
+ /** The face it entered by: where a block placed on it goes is (x + nx, y + ny, z + nz). */
6
+ nx: number;
7
+ ny: number;
8
+ nz: number;
9
+ distance: number;
10
+ }
11
+ export declare namespace VoxelRay {
12
+ function cast(from: {
13
+ x: number;
14
+ y: number;
15
+ z: number;
16
+ }, direction: {
17
+ x: number;
18
+ y: number;
19
+ z: number;
20
+ }, reach: number, pick: (x: number, y: number, z: number) => boolean): RayHit | undefined;
21
+ }
@@ -0,0 +1,30 @@
1
+ // The block aimed at (VoxelRay): a ray from the eye through the blocks, block by block (Amanatides and Woo), to the
2
+ // first one `pick` takes, and the face it went in by, where a block put would go.
3
+ export var VoxelRay;
4
+ (function (VoxelRay) {
5
+ function cast(from, direction, reach, pick) {
6
+ const length = Math.hypot(direction.x, direction.y, direction.z) || 1;
7
+ const d = [direction.x / length, direction.y / length, direction.z / length];
8
+ const p = [from.x, from.y, from.z];
9
+ const cell = p.map(Math.floor);
10
+ const step = d.map(v => (v > 0 ? 1 : v < 0 ? -1 : 0));
11
+ const delta = d.map(v => (v === 0 ? Number.POSITIVE_INFINITY : Math.abs(1 / v)));
12
+ const next = d.map((v, i) => (v > 0 ? (cell[i] + 1 - p[i]) / v : v < 0 ? (p[i] - cell[i]) / -v : Number.POSITIVE_INFINITY));
13
+ let normal = [0, 0, 0];
14
+ let travelled = 0;
15
+ for (let i = 0; i < 512 && travelled <= reach; i++) {
16
+ if (pick(cell[0], cell[1], cell[2])) {
17
+ return { x: cell[0], y: cell[1], z: cell[2], nx: normal[0], ny: normal[1], nz: normal[2], distance: travelled };
18
+ }
19
+ const axis = next[0] < next[1] ? (next[0] < next[2] ? 0 : 2) : next[1] < next[2] ? 1 : 2;
20
+ travelled = next[axis];
21
+ next[axis] += delta[axis];
22
+ cell[axis] += step[axis];
23
+ normal = [0, 0, 0];
24
+ normal[axis] = -step[axis];
25
+ }
26
+ return undefined;
27
+ }
28
+ VoxelRay.cast = cast;
29
+ })(VoxelRay || (VoxelRay = {}));
30
+ //# sourceMappingURL=voxel-ray.js.map
@@ -0,0 +1,28 @@
1
+ import type * as T from 'three';
2
+ import type { ChunkMesh } from './voxel-mesher.js';
3
+ type Three = typeof T;
4
+ /** A grain of grey pixels, the same every time: a block of 16 × 16. */
5
+ export declare function voxelGrain(THREE: Three): T.DataTexture;
6
+ export declare class VoxelMeshes {
7
+ protected readonly THREE: Three;
8
+ readonly group: T.Group;
9
+ protected readonly solid: T.MeshBasicMaterial;
10
+ protected readonly glow: T.MeshBasicMaterial;
11
+ protected readonly clear: T.MeshBasicMaterial;
12
+ protected readonly liquid: T.MeshBasicMaterial;
13
+ protected readonly grain: T.DataTexture;
14
+ protected readonly meshes: Map<number, T.Mesh<T.BufferGeometry<T.NormalBufferAttributes, T.BufferGeometryEventMap>, T.Material<T.MaterialEventMap>[] | T.Material<T.MaterialEventMap>, T.Object3DEventMap>[]>;
15
+ constructor(THREE: Three);
16
+ /** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
17
+ set(key: number, x: number, z: number, mesh: ChunkMesh): void;
18
+ remove(key: number): void;
19
+ has(key: number): boolean;
20
+ keys(): IterableIterator<number>;
21
+ /**
22
+ * The light of the world: `sun` the tint of what the sun lights (white at noon, blue and dark at night),
23
+ * `glow` that of what glows (warmer and brighter at night: more than 1 is brighter than its colour).
24
+ */
25
+ light(sun: T.Color, glow: T.Color): void;
26
+ dispose(): void;
27
+ }
28
+ export {};
@@ -0,0 +1,104 @@
1
+ /** A grain of grey pixels, the same every time: a block of 16 × 16. */
2
+ export function voxelGrain(THREE) {
3
+ const size = 16;
4
+ const data = new Uint8Array(size * size * 4);
5
+ let seed = 11;
6
+ const random = () => {
7
+ seed = (seed * 16807) % 2147483647;
8
+ return seed / 2147483647;
9
+ };
10
+ for (let i = 0; i < size * size; i++) {
11
+ const x = i % size;
12
+ const y = Math.floor(i / size);
13
+ const edge = x === 0 || y === 0 ? -14 : 0;
14
+ const value = Math.max(0, Math.min(255, 232 + Math.floor(random() * 22) + edge - (random() < 0.06 ? 22 : 0)));
15
+ data.set([value, value, value, 255], i * 4);
16
+ }
17
+ const texture = new THREE.DataTexture(data, size, size);
18
+ texture.wrapS = THREE.RepeatWrapping;
19
+ texture.wrapT = THREE.RepeatWrapping;
20
+ texture.magFilter = THREE.NearestFilter;
21
+ // Far away the grain blends, so that walls do not shimmer in stripes.
22
+ texture.minFilter = THREE.LinearMipmapLinearFilter;
23
+ texture.anisotropy = 4;
24
+ texture.generateMipmaps = true;
25
+ texture.colorSpace = THREE.SRGBColorSpace;
26
+ texture.needsUpdate = true;
27
+ return texture;
28
+ }
29
+ export class VoxelMeshes {
30
+ constructor(THREE) {
31
+ this.THREE = THREE;
32
+ this.meshes = new Map();
33
+ this.group = new THREE.Group();
34
+ this.grain = voxelGrain(THREE);
35
+ this.solid = new THREE.MeshBasicMaterial({ vertexColors: true, map: this.grain });
36
+ this.glow = new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false });
37
+ this.clear = new THREE.MeshBasicMaterial({ vertexColors: true, map: this.grain, transparent: true, opacity: 0.55, depthWrite: false });
38
+ this.liquid = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.82, depthWrite: false });
39
+ }
40
+ /** The meshes of a chunk, at its corner in the scene (`x`, `z`), instead of what it had. */
41
+ set(key, x, z, mesh) {
42
+ this.remove(key);
43
+ const made = [];
44
+ const add = (part, material, order) => {
45
+ if (part.indices.length === 0) {
46
+ return;
47
+ }
48
+ const { THREE } = this;
49
+ const geometry = new THREE.BufferGeometry();
50
+ geometry.setAttribute('position', new THREE.BufferAttribute(part.positions, 3));
51
+ geometry.setAttribute('color', new THREE.BufferAttribute(part.colors, 3));
52
+ geometry.setAttribute('uv', new THREE.BufferAttribute(part.uvs, 2));
53
+ geometry.setIndex(new THREE.BufferAttribute(part.indices, 1));
54
+ geometry.computeBoundingSphere();
55
+ const object = new THREE.Mesh(geometry, material);
56
+ object.position.set(x, 0, z);
57
+ object.renderOrder = order;
58
+ object.matrixAutoUpdate = false;
59
+ object.updateMatrix();
60
+ this.group.add(object);
61
+ made.push(object);
62
+ };
63
+ add(mesh.solid, this.solid, 0);
64
+ add(mesh.glow, this.glow, 0);
65
+ add(mesh.clear, this.clear, 1);
66
+ add(mesh.liquid, this.liquid, 1);
67
+ this.meshes.set(key, made);
68
+ }
69
+ remove(key) {
70
+ var _a;
71
+ for (const object of (_a = this.meshes.get(key)) !== null && _a !== void 0 ? _a : []) {
72
+ this.group.remove(object);
73
+ object.geometry.dispose();
74
+ }
75
+ this.meshes.delete(key);
76
+ }
77
+ has(key) {
78
+ return this.meshes.has(key);
79
+ }
80
+ keys() {
81
+ return this.meshes.keys();
82
+ }
83
+ /**
84
+ * The light of the world: `sun` the tint of what the sun lights (white at noon, blue and dark at night),
85
+ * `glow` that of what glows (warmer and brighter at night: more than 1 is brighter than its colour).
86
+ */
87
+ light(sun, glow) {
88
+ this.solid.color.copy(sun);
89
+ this.clear.color.copy(sun);
90
+ this.liquid.color.copy(sun);
91
+ this.glow.color.copy(glow);
92
+ }
93
+ dispose() {
94
+ for (const key of [...this.meshes.keys()]) {
95
+ this.remove(key);
96
+ }
97
+ this.solid.dispose();
98
+ this.glow.dispose();
99
+ this.clear.dispose();
100
+ this.liquid.dispose();
101
+ this.grain.dispose();
102
+ }
103
+ }
104
+ //# sourceMappingURL=voxel-three.js.map
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './voxel-blocks';
2
+ export * from './voxel-body';
3
+ export * from './voxel-chunks';
4
+ export * from './voxel-controls';
5
+ export * from './voxel-mesher';
6
+ export * from './voxel-ray';
7
+ export * from './voxel-three';
8
+ //# sourceMappingURL=index.d.ts.map
package/lib/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./voxel-blocks"), exports);
18
+ __exportStar(require("./voxel-body"), exports);
19
+ __exportStar(require("./voxel-chunks"), exports);
20
+ __exportStar(require("./voxel-controls"), exports);
21
+ __exportStar(require("./voxel-mesher"), exports);
22
+ __exportStar(require("./voxel-ray"), exports);
23
+ __exportStar(require("./voxel-three"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,27 @@
1
+ export interface BlockLook {
2
+ /** Its colour, `0xrrggbb`. */
3
+ color: number;
4
+ /** What stands in the way and is stood on. Water is not. */
5
+ solid: boolean;
6
+ /** Seen through: glass, leaves, water; the faces beside it are drawn. */
7
+ clear?: boolean;
8
+ /** Water: one swims in it, its top a little lower. */
9
+ liquid?: boolean;
10
+ /** Lit by itself (a lamp, a window at night): drawn by the light of the world's glow, not the sun. */
11
+ glow?: boolean;
12
+ /** How it is drawn: the grain of a block, or flat. */
13
+ flat?: boolean;
14
+ }
15
+ export declare const AIR = 0;
16
+ export declare class VoxelBlocks {
17
+ readonly looks: BlockLook[];
18
+ protected readonly names: Map<string, number>;
19
+ /** By id, for the mesher: drawn as solid (hides what is behind it). */
20
+ opaque: Uint8Array<ArrayBuffer>;
21
+ solid: Uint8Array<ArrayBuffer>;
22
+ /** A kind of block, its id; the same name again is the same id. */
23
+ define(name: string, look: BlockLook): number;
24
+ id(name: string): number | undefined;
25
+ name(id: number): string | undefined;
26
+ }
27
+ //# sourceMappingURL=voxel-blocks.d.ts.map