roamwright 0.0.0-stage → 0.1.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,172 @@
1
+ import * as THREE from 'three';
2
+ import { TAU } from '../math/scalar.js';
3
+ import { createNoise } from '../math/noise.js';
4
+
5
+ // ════════════════════════════════════════════════════════════════════════
6
+ // The unit shapes every builder places (U), the matrix that places them (TRS), a colour as a builder takes it (C), and a rod
7
+ // between two points (stick)
8
+ // ════════════════════════════════════════════════════════════════════════
9
+ const _m4 = new THREE.Matrix4(),
10
+ _q = new THREE.Quaternion(),
11
+ _eu = new THREE.Euler(),
12
+ _sc = new THREE.Vector3(),
13
+ _ps = new THREE.Vector3(),
14
+ _cc = new THREE.Color();
15
+ function TRS(x, y, z, sx = 1, sy = 1, sz = 1, ry = 0, rx = 0, rz = 0) {
16
+ // returns a shared matrix: use it immediately
17
+ _eu.set(rx, ry, rz, 'YXZ');
18
+ _q.setFromEuler(_eu);
19
+ return _m4.compose(_ps.set(x, y, z), _q, _sc.set(sx, sy, sz));
20
+ }
21
+ // A unit triangular prism, the shape of a gable roof.
22
+ function prismGeometry() {
23
+ const a = [-0.5, 0, -0.5],
24
+ b = [0.5, 0, -0.5],
25
+ c = [0.5, 0, 0.5],
26
+ d = [-0.5, 0, 0.5],
27
+ e = [0, 1, -0.5],
28
+ f = [0, 1, 0.5];
29
+ const g = new THREE.BufferGeometry();
30
+ g.setAttribute('position', new THREE.Float32BufferAttribute([a, d, f, a, f, e, b, e, f, b, f, c, d, c, f, b, a, e].flat(), 3));
31
+ g.computeVertexNormals();
32
+ return g;
33
+ }
34
+ // The two triangular ends of that prism alone: a gable's wall, under a roof drawn without them.
35
+ function gableGeometry() {
36
+ const g = new THREE.BufferGeometry();
37
+ g.setAttribute(
38
+ 'position',
39
+ new THREE.Float32BufferAttribute([-0.5, 0, 0.5, 0.5, 0, 0.5, 0, 1, 0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, 0, 1, -0.5], 3),
40
+ );
41
+ g.computeVertexNormals();
42
+ return g;
43
+ }
44
+ // The ways a face can look, as [axis, sign]
45
+ const WAYS = { '+x': [0, 1], '-x': [0, -1], '+y': [1, 1], '-y': [1, -1], '+z': [2, 1], '-z': [2, -1] };
46
+ // A unit shape without its flat faces that look the ways named ('+x', '-y', ...), the rest in their order: for a part whose faces
47
+ // lie against something, where no one can see them (the back of a box fixed to a wall, the foot of a rod set in one)
48
+ function without(shape, ...ways) {
49
+ const g = shape.index ? shape.toNonIndexed() : shape,
50
+ p = g.attributes.position.array,
51
+ n = g.attributes.normal.array,
52
+ away = t => ways.some(w => [0, 3, 6].every(k => n[t + k + WAYS[w][0]] * WAYS[w][1] > 0.99)),
53
+ kept = [];
54
+ for (let t = 0; t < p.length; t += 9) if (!away(t)) kept.push(t);
55
+ const pick = a => {
56
+ const out = new Float32Array(kept.length * 9);
57
+ kept.forEach((t, i) => out.set(a.subarray(t, t + 9), i * 9));
58
+ return new THREE.BufferAttribute(out, 3);
59
+ },
60
+ q = new THREE.BufferGeometry();
61
+ q.setAttribute('position', pick(p));
62
+ q.setAttribute('normal', pick(n));
63
+ return q;
64
+ }
65
+ // Lumps shaped from a sphere, their normals following the shape itself, so that a coarse lump is still shaded round (not in
66
+ // facets): an icosahedron split `fineness` times, each point moved to shape(direction, out) and facing the way the shape faces
67
+ // there. The supplied rock primitive uses the same sampling rule.
68
+ const _dir = new THREE.Vector3(),
69
+ _side = new THREE.Vector3(),
70
+ _up = new THREE.Vector3(),
71
+ _here = new THREE.Vector3(),
72
+ _a = new THREE.Vector3(),
73
+ _b = new THREE.Vector3();
74
+ function lump(fineness, shape) {
75
+ const g = new THREE.IcosahedronGeometry(1, fineness),
76
+ position = g.attributes.position.array,
77
+ normal = g.attributes.normal.array,
78
+ near = 0.03; // how far round the sphere the shape is sampled to find which way it faces
79
+ for (let i = 0; i < position.length; i += 3) {
80
+ _dir.set(position[i], position[i + 1], position[i + 2]).normalize();
81
+ _side.set(-_dir.z, 0, _dir.x);
82
+ if (_side.lengthSq() < 1e-4) _side.set(1, 0, 0);
83
+ _side.normalize();
84
+ _up.crossVectors(_side, _dir);
85
+ shape(_dir, _here);
86
+ shape(_a.copy(_dir).addScaledVector(_side, near).normalize(), _a).sub(_here);
87
+ shape(_b.copy(_dir).addScaledVector(_up, near).normalize(), _b).sub(_here);
88
+ _a.cross(_b).normalize();
89
+ if (_a.dot(_dir) < 0) _a.negate();
90
+ position.set([_here.x, _here.y, _here.z], i);
91
+ normal.set([_a.x, _a.y, _a.z], i);
92
+ }
93
+ return g;
94
+ }
95
+ // A stone broken from the rock, about a metre across and a little lower than it is wide: worn round by the weather, with three
96
+ // faces where it split along its joints, and flat where it lies; shaded round where it is worn and flat on its broken faces
97
+ // (80 triangles). Its colours are white, for the builder's colour to tint.
98
+ const STONE_JOINTS = [
99
+ [0.62, 0.5, 0.6, 0.37],
100
+ [-0.74, 0.18, 0.64, 0.39],
101
+ [0.12, 0.3, -0.95, 0.4],
102
+ ].map(([x, y, z, d]) => [x / Math.hypot(x, y, z), y / Math.hypot(x, y, z), z / Math.hypot(x, y, z), d]); // [the face's normal, its distance from the middle]
103
+ const box = new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0),
104
+ cyl6 = new THREE.CylinderGeometry(0.5, 0.5, 1, 6).translate(0, 0.5, 0);
105
+ export function createUnitShapes(fbm = createNoise().fbm) {
106
+ function stonePoint(dir, out) {
107
+ let r = 0.5 * (0.8 + 0.4 * fbm(dir.x * 1.7 + 3, dir.z * 1.7 + dir.y * 1.3, 2, 190));
108
+ for (const [nx, ny, nz, d] of STONE_JOINTS) {
109
+ const towards = dir.x * nx + dir.y * ny + dir.z * nz;
110
+ if (towards > 0.05) r = Math.min(r, d / towards);
111
+ }
112
+ return out.set(dir.x * r, Math.max(dir.y * r * 0.82, -0.3), dir.z * r);
113
+ }
114
+ function brokenStone() {
115
+ const g = lump(1, stonePoint);
116
+ // white vertex colours: an instanced rock on a material that reads vertex colours would otherwise be drawn black
117
+ g.setAttribute('color', new THREE.Float32BufferAttribute(new Float32Array(g.attributes.position.count * 3).fill(1), 3));
118
+ return g;
119
+ }
120
+ return {
121
+ box,
122
+ cyl: new THREE.CylinderGeometry(0.5, 0.5, 1, 10).translate(0, 0.5, 0),
123
+ cyl6,
124
+ taper: new THREE.CylinderGeometry(0.3, 0.5, 1, 6).translate(0, 0.5, 0),
125
+ cone: new THREE.ConeGeometry(0.5, 1, 10).translate(0, 0.5, 0),
126
+ cone6: new THREE.ConeGeometry(0.5, 1, 6).translate(0, 0.5, 0),
127
+ pyr: new THREE.ConeGeometry(0.7071, 1, 4).rotateY(Math.PI / 4).translate(0, 0.5, 0),
128
+ dome: new THREE.SphereGeometry(0.5, 10, 5, 0, TAU, 0, Math.PI / 2),
129
+ sph: new THREE.IcosahedronGeometry(0.5, 1),
130
+ rock: brokenStone(),
131
+ prism: prismGeometry(),
132
+ eaves: without(prismGeometry(), '+z', '-z'), // a gable roof's two slopes, its ends left open for its gables
133
+ gable: gableGeometry(),
134
+ torso: new THREE.CylinderGeometry(0.5, 0.4, 1, 12).translate(0, 0.5, 0),
135
+ skirt: new THREE.CylinderGeometry(0.32, 0.5, 1, 12).translate(0, 0.5, 0),
136
+ head: new THREE.IcosahedronGeometry(0.5, 2),
137
+ limb: new THREE.CylinderGeometry(0.5, 0.38, 1, 8).translate(0, -0.5, 0),
138
+ cloak: new THREE.CylinderGeometry(0.5, 0.64, 1, 10, 3, true, Math.PI * 0.5, Math.PI).translate(0, -0.5, 0),
139
+ quad: new THREE.PlaneGeometry(1, 1).translate(0, 0.5, 0),
140
+ boxF: without(box, '-z'), // a box without its back face, for parts fixed to a wall
141
+ boxFootless: without(box, '-y'), // a box without its foot, for parts that stand on something or go into a wall at their foot
142
+ cyl16: new THREE.CylinderGeometry(0.5, 0.5, 1, 16).translate(0, 0.5, 0),
143
+ fing: new THREE.CylinderGeometry(0.5, 0.4, 1, 5).translate(0, -0.5, 0),
144
+ cone16: new THREE.ConeGeometry(0.5, 1, 16).translate(0, 0.5, 0),
145
+ dome16: new THREE.SphereGeometry(0.5, 20, 8, 0, TAU, 0, Math.PI / 2),
146
+ };
147
+ }
148
+ function C(hex, jitter = 0, rng) {
149
+ // sRGB hex -> linear [r,g,b], optionally brightness-jittered
150
+ _cc.setHex(hex);
151
+ const k = jitter && rng ? 1 + (rng() - 0.5) * 2 * jitter : 1;
152
+ return [_cc.r * k, _cc.g * k, _cc.b * k];
153
+ }
154
+
155
+ // A rod of radius r from point a to point b ([x, y, z]), added to the builder gb.
156
+ const _rod = {
157
+ m: new THREE.Matrix4(),
158
+ at: new THREE.Vector3(),
159
+ turn: new THREE.Quaternion(),
160
+ size: new THREE.Vector3(),
161
+ way: new THREE.Vector3(),
162
+ },
163
+ UP = new THREE.Vector3(0, 1, 0);
164
+ function stick(gb, a, b, r, col, geo = cyl6) {
165
+ const { m, at, turn, size, way } = _rod;
166
+ way.set(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
167
+ const len = way.length();
168
+ turn.setFromUnitVectors(UP, way.divideScalar(len));
169
+ gb.add(geo, col, m.compose(at.set(a[0], a[1], a[2]), turn, size.set(r, len, r)));
170
+ }
171
+
172
+ export { box, C, TRS, lump, stick, without };
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { createRenderContext } from './rendering/context.js';
2
+ export { createNoise } from './math/noise.js';
3
+ export { mulberry32 } from './math/random.js';
4
+ export { GB as GeometryBuilder, geometryOf } from './geometry/builder.js';
5
+ export { createUnitShapes } from './geometry/primitives.js';
6
+ export { createSpaces } from './motion/spaces.js';
7
+ export { createPool } from './runtime/pool.js';
8
+ export { createPlayer } from './motion/player.js';
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 MJP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright © 2010-2024 three.js authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/math/curve.js ADDED
@@ -0,0 +1,94 @@
1
+ // ════════════════════════════════════════════════════════════════════════
2
+ // A smooth curve through a line of [x, z] points: a centripetal Catmull-Rom spline, walked at even steps of its length.
3
+ //
4
+ // It is Three.js's CatmullRomCurve3 (open, centripetal) and its getSpacedPoints, written out in plain numbers so that the
5
+ // ground's model needs no Three.js and can run in a worker. Every sum is done in the order Three.js does it: roads, rivers
6
+ // and the baked ground come out the same to the last bit, which is also why a two-point line keeps Three.js's quirk of
7
+ // using the far end's extension for both ends.
8
+ // ════════════════════════════════════════════════════════════════════════
9
+ const ARC_STEPS = 200, // the pieces the curve's length is measured in
10
+ REPEATED = 1e-4; // points closer than this (to the power the knots are spaced by) count as one
11
+
12
+ // One coordinate of the cubic between p1 and p2, with p0 before and p3 after, the knots dt0, dt1 and dt2 apart, at t in 0..1.
13
+ function cubic(x0, x1, x2, x3, dt0, dt1, dt2, t) {
14
+ const t1 = ((x1 - x0) / dt0 - (x2 - x0) / (dt0 + dt1) + (x2 - x1) / dt1) * dt1,
15
+ t2 = ((x2 - x1) / dt1 - (x3 - x1) / (dt1 + dt2) + (x3 - x2) / dt2) * dt1,
16
+ c2 = -3 * x1 + 3 * x2 - 2 * t1 - t2,
17
+ c3 = 2 * x1 - 2 * x2 + t1 + t2,
18
+ tt = t * t;
19
+ return x1 + t1 * t + c2 * tt + c3 * (tt * t);
20
+ }
21
+ const knot = (a, b) => Math.pow((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]), 0.25);
22
+ const _before = [0, 0],
23
+ _after = [0, 0];
24
+ // The point of the curve through pts at t in 0..1, where t runs evenly from point to point.
25
+ function curvePoint(pts, t) {
26
+ const count = pts.length,
27
+ at = (count - 1) * t;
28
+ let i = Math.floor(at),
29
+ weight = at - i;
30
+ if (weight === 0 && i === count - 1) {
31
+ i = count - 2;
32
+ weight = 1;
33
+ }
34
+ // past either end the line is carried on by its last step
35
+ const first = pts[0],
36
+ last = pts[count - 1];
37
+ _after[0] = last[0] - pts[count - 2][0] + last[0];
38
+ _after[1] = last[1] - pts[count - 2][1] + last[1];
39
+ _before[0] = first[0] - pts[1][0] + first[0];
40
+ _before[1] = first[1] - pts[1][1] + first[1];
41
+ const atEnd = i + 2 >= count,
42
+ p0 = i > 0 ? pts[i - 1] : atEnd ? _after : _before,
43
+ p1 = pts[i],
44
+ p2 = pts[i + 1],
45
+ p3 = atEnd ? _after : pts[i + 2];
46
+ let dt0 = knot(p0, p1),
47
+ dt1 = knot(p1, p2),
48
+ dt2 = knot(p2, p3);
49
+ if (dt1 < REPEATED) dt1 = 1;
50
+ if (dt0 < REPEATED) dt0 = dt1;
51
+ if (dt2 < REPEATED) dt2 = dt1;
52
+ return [cubic(p0[0], p1[0], p2[0], p3[0], dt0, dt1, dt2, weight), cubic(p0[1], p1[1], p2[1], p3[1], dt0, dt1, dt2, weight)];
53
+ }
54
+ // The curve's length from its start to each of ARC_STEPS + 1 points along it.
55
+ function arcLengths(pts) {
56
+ const lengths = [0];
57
+ let last = curvePoint(pts, 0),
58
+ sum = 0;
59
+ for (let p = 1; p <= ARC_STEPS; p++) {
60
+ const point = curvePoint(pts, p / ARC_STEPS);
61
+ sum += Math.sqrt((point[0] - last[0]) * (point[0] - last[0]) + (point[1] - last[1]) * (point[1] - last[1]));
62
+ lengths.push(sum);
63
+ last = point;
64
+ }
65
+ return lengths;
66
+ }
67
+ // The t at which the curve has run the share u of its length.
68
+ function shareToT(lengths, u) {
69
+ const target = u * lengths[ARC_STEPS];
70
+ let low = 0,
71
+ high = ARC_STEPS;
72
+ while (low <= high) {
73
+ const i = Math.floor(low + (high - low) / 2),
74
+ over = lengths[i] - target;
75
+ if (over < 0) low = i + 1;
76
+ else if (over > 0) high = i - 1;
77
+ else {
78
+ high = i;
79
+ break;
80
+ }
81
+ }
82
+ if (lengths[high] === target) return high / ARC_STEPS;
83
+ return (high + (target - lengths[high]) / (lengths[high + 1] - lengths[high])) / ARC_STEPS;
84
+ }
85
+ // The line through pts as a smooth curve of evenly spaced points, about `spacing` apart.
86
+ function spacedCurve(pts, spacing) {
87
+ const lengths = arcLengths(pts),
88
+ steps = Math.max(2, Math.round(lengths[ARC_STEPS] / spacing)),
89
+ points = [];
90
+ for (let d = 0; d <= steps; d++) points.push(curvePoint(pts, shareToT(lengths, d / steps)));
91
+ return points;
92
+ }
93
+
94
+ export { spacedCurve };
package/math/noise.js ADDED
@@ -0,0 +1,80 @@
1
+ import { createHash } from './random.js';
2
+
3
+ export function createNoise(worldSeed = 0) {
4
+ const hash2 = createHash(worldSeed);
5
+ // Value noise in 0..1, smooth between the integer grid points.
6
+ function vnoise(x, z, seed) {
7
+ const ix = Math.floor(x),
8
+ iz = Math.floor(z),
9
+ fx = x - ix,
10
+ fz = z - iz;
11
+ const ux = fx * fx * (3 - 2 * fx),
12
+ uz = fz * fz * (3 - 2 * fz);
13
+ const a = hash2(ix, iz, seed),
14
+ b = hash2(ix + 1, iz, seed),
15
+ c = hash2(ix, iz + 1, seed),
16
+ d = hash2(ix + 1, iz + 1, seed);
17
+ return a + (b - a) * ux + (c - a) * uz + (a - b - c + d) * ux * uz;
18
+ }
19
+ // Fractal noise: oct octaves of value noise, each twice as fine and half as strong.
20
+ function fbm(x, z, oct, seed) {
21
+ let s = 0,
22
+ a = 0.5,
23
+ n = 0;
24
+ for (let i = 0; i < oct; i++) {
25
+ s += a * vnoise(x, z, seed + i * 17);
26
+ n += a;
27
+ a *= 0.5;
28
+ x = x * 2.03 + 11.7;
29
+ z = z * 2.03 - 5.3;
30
+ }
31
+ return s / n;
32
+ }
33
+ // Ridged fractal noise: sharp crests where the noise crosses its middle, for ridges and veins.
34
+ function ridged(x, z, oct, seed) {
35
+ let s = 0,
36
+ a = 0.5,
37
+ n = 0;
38
+ for (let i = 0; i < oct; i++) {
39
+ const v = 1 - Math.abs(vnoise(x, z, seed + i * 31) * 2 - 1);
40
+ s += a * v * v;
41
+ n += a;
42
+ a *= 0.5;
43
+ x = x * 2.1 + 3.1;
44
+ z = z * 2.1 + 8.9;
45
+ }
46
+ return s / n;
47
+ }
48
+ // Tileable noise for textures: it repeats every `per` cells (every `perY` along y, where the cells are not square).
49
+ function tnoise(x, y, per, seed, perY = per) {
50
+ const ix = Math.floor(x),
51
+ iy = Math.floor(y),
52
+ fx = x - ix,
53
+ fy = y - iy;
54
+ const ux = fx * fx * (3 - 2 * fx),
55
+ uy = fy * fy * (3 - 2 * fy);
56
+ const x0 = ((ix % per) + per) % per,
57
+ x1 = (x0 + 1) % per,
58
+ y0 = ((iy % perY) + perY) % perY,
59
+ y1 = (y0 + 1) % perY;
60
+ const a = hash2(x0, y0, seed),
61
+ b = hash2(x1, y0, seed),
62
+ c = hash2(x0, y1, seed),
63
+ d = hash2(x1, y1, seed);
64
+ return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
65
+ }
66
+ // Fractal noise that tiles with period per, for seamless textures.
67
+ function tfbm(u, v, per, oct, seed) {
68
+ let s = 0,
69
+ a = 0.5,
70
+ n = 0;
71
+ for (let i = 0; i < oct; i++) {
72
+ s += a * tnoise(u * per, v * per, per, seed + i * 13);
73
+ n += a;
74
+ a *= 0.5;
75
+ per *= 2;
76
+ }
77
+ return s / n;
78
+ }
79
+ return { fbm, ridged, tfbm, tnoise, vnoise };
80
+ }
package/math/random.js ADDED
@@ -0,0 +1,27 @@
1
+ // A small seeded random generator: the same seed gives the same world every time. Its `state()` is where the stream stands:
2
+ // mulberry32(state) goes on from there, as a stream handed to a worker of the pool does.
3
+ function mulberry32(a) {
4
+ const next = function () {
5
+ a |= 0;
6
+ a = (a + 0x6d2b79f5) | 0;
7
+ let t = Math.imul(a ^ (a >>> 15), 1 | a);
8
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
9
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
10
+ };
11
+ next.state = () => a;
12
+ return next;
13
+ }
14
+ // A random item of a, drawn with the generator R.
15
+ const pickR = (R, a) => a[(R() * a.length) | 0];
16
+ export { mulberry32, pickR };
17
+
18
+ export function createHash(worldSeed = 0) {
19
+ // A random number in 0..1 fixed by two integers and a seed.
20
+ function hash2(ix, iz, seed) {
21
+ let h = (Math.imul(ix, 374761393) + Math.imul(iz, 668265263) + Math.imul(seed + worldSeed, 1442695041)) | 0;
22
+ h = Math.imul(h ^ (h >>> 13), 1274126177);
23
+ h ^= h >>> 16;
24
+ return (h >>> 0) / 4294967296;
25
+ }
26
+ return hash2;
27
+ }
package/math/scalar.js ADDED
@@ -0,0 +1,12 @@
1
+ const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
2
+ const lerp = (a, b, t) => a + (b - a) * t;
3
+ // Smoothstep: 0 at a, 1 at b, eased between (b may be less than a, to fall instead of rise).
4
+ const sstep = (a, b, x) => {
5
+ const t = clamp((x - a) / (b - a), 0, 1);
6
+ return t * t * (3 - 2 * t);
7
+ };
8
+ const TAU = Math.PI * 2;
9
+ // 1 inside the rectangle f (centre x, z, half sizes hx, hz), falling to 0 over a margin m outside it.
10
+ const rectMask = (x, z, f, m) => sstep(f.hx + m, f.hx, Math.abs(x - f.x)) * sstep(f.hz + m, f.hz, Math.abs(z - f.z));
11
+
12
+ export { clamp, lerp, rectMask, sstep, TAU };